From b315325f618691b9175c6df023f446d95f24ef1e Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Sun, 6 Oct 2024 10:37:44 -0400 Subject: [PATCH] setup tutorial code --- src/main.cpp | 75 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c2b6ac3..9724dab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,7 +49,7 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, fmt::print("Key pressed {}\n", key_name); } -int main() { +int main(int argc, char *argv[]) { glfwSetErrorCallback(error_callback); @@ -74,32 +74,75 @@ int main() { 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); glfwMakeContextCurrent(window); 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)) { - /* 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); - /* Swap front and back buffers */ - glfwSwapBuffers(window); + mat4x4 m, p, mvp; + 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(); } - glfwTerminate(); + glfwDestroyWindow(window); - return 0; + glfwTerminate(); + exit(EXIT_SUCCESS); }