Lightweight OpenGL 3D Renderer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Euclid/src/main.cpp

132 lines
3.1 KiB

2 years ago
#include <glad/glad.h>
2 years ago
#include <GLFW/glfw3.h>
2 years ago
#include <stdio.h>
2 years ago
#include <vector>
2 years ago
#include <math.h>
2 years ago
#include "../headers/shaders.hpp"
2 years ago
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
2 years ago
#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
2 years ago
2 years ago
float verts[] = {
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
2 years ago
};
unsigned int indices[] = {
0, 1, 3,
1, 2, 3
2 years ago
};
void framebuffer_size_callback(GLFWwindow* win, int w, int h) {
glViewport(0, 0, w, h);
}
void processInput(GLFWwindow *win) {
int action = glfwGetKey(win, GLFW_KEY_ESCAPE);
if (action == GLFW_PRESS) {
glfwSetWindowShouldClose(win, true);
}
}
2 years ago
struct RenderObj {
2 years ago
unsigned int EBO;
Shaders::Shader shader;
2 years ago
};
2 years ago
2 years ago
RenderObj preRenderCallback() {
Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE);
2 years ago
2 years ago
// Vertex buffer object
unsigned int VBO;
glGenBuffers(1, &VBO);
// Vertex Array Object
unsigned int VAO;
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
2 years ago
// Copy verts into buffer
2 years ago
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); // for moving stuff
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
2 years ago
// Set attrib pointers
// Pos
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
2 years ago
glEnableVertexAttribArray(0);
// Color
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
2 years ago
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
return RenderObj {EBO, shader};
2 years ago
}
2 years ago
void renderCallback(RenderObj ro) {
2 years ago
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
2 years ago
2 years ago
float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f;
int vertColLocation = glGetUniformLocation(ro.shader.id, "inputColor");
2 years ago
ro.shader.use();
2 years ago
glUniform4f(vertColLocation, gVal, gVal, gVal, 1.0f);
2 years ago
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
2 years ago
}
int main() {
2 years ago
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hohmann", NULL, NULL);
if (win == NULL) {
2 years ago
printf("Failed to create a window.\n");
2 years ago
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
2 years ago
printf("Failed to init GLAD.\n");
2 years ago
return 1;
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
2 years ago
RenderObj ro = preRenderCallback();
2 years ago
2 years ago
while (!glfwWindowShouldClose(win)) {
// Handle input
processInput(win);
// rendering
2 years ago
renderCallback(ro);
2 years ago
// glfw
glfwSwapBuffers(win);
glfwPollEvents();
}
glfwTerminate();
return 0;
}