setup tutorial code

This commit is contained in:
Benjamin Palko 2024-10-06 10:37:44 -04:00
parent 0eb6b005d6
commit b315325f61

View file

@ -49,7 +49,7 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action,
fmt::print("Key pressed {}\n", key_name); fmt::print("Key pressed {}\n", key_name);
} }
int main() { int main(int argc, char *argv[]) {
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
@ -74,32 +74,75 @@ int main() {
return -1; return -1;
} }
/* GLEW */
GLenum code = glewInit();
if (code != GLEW_OK) {
fmt::print("GLEW failed to init! {} - {}\n", code,
glewGetErrorString(code));
return -1;
}
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSwapInterval(1); glfwSwapInterval(1);
/* Loop until the user closes the window */ /* GLEW */
GLenum err = glewInit();
if (err != GLEW_OK) {
fmt::print("GLEW failed to init! {}\n", err);
return -1;
}
GLuint vertex_buffer;
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
const GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
const GLint mvp_location = glGetUniformLocation(program, "MVP");
const GLint vpos_location = glGetAttribLocation(program, "vPos");
const GLint vcol_location = glGetAttribLocation(program, "vCol");
GLuint vertex_array;
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(void *)offsetof(Vertex, pos));
glEnableVertexAttribArray(vcol_location);
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(void *)offsetof(Vertex, col));
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
/* Render here */ int width, height;
glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */ mat4x4 m, p, mvp;
glfwSwapBuffers(window); mat4x4_identity(m);
mat4x4_rotate_Z(m, m, (float)glfwGetTime());
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
mat4x4_mul(mvp, p, m);
/* Poll for and process events */ glUseProgram(program);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
glfwTerminate(); glfwDestroyWindow(window);
return 0; glfwTerminate();
exit(EXIT_SUCCESS);
} }