From 6b94ae24f09a48f7d20942d48741251ada2bda43 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Wed, 7 Aug 2024 21:50:16 +0300 Subject: upd --- src/main.c | 58 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index a333712..aa042b0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -14,10 +15,9 @@ int main() { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac // Create a windowed mode window and its OpenGL context - GLFWwindow *window = glfwCreateWindow(800, 600, "Blue screen", NULL, NULL); + GLFWwindow *window = glfwCreateWindow(320, 240, "Blue screen", NULL, NULL); if (!window) { glfwTerminate(); return -1; @@ -25,7 +25,18 @@ int main() { glfwMakeContextCurrent(window); - float vertices[6] = {-0.5f, -0.5f, 0.0f, 0.5f, 0.5f, -0.5f}; + // Initialize GLEW + GLenum glewStatus = glewInit(); + if (glewStatus != GLEW_OK) { + printf("Failed to initialize GLEW: %s\n", glewGetErrorString(glewStatus)); + return -1; + } + + float vertices[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; unsigned int VBO; glGenBuffers(1, &VBO); @@ -40,8 +51,7 @@ int main() { " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" "}\0"; - unsigned int vertex_shader; - vertex_shader = glCreateShader(GL_VERTEX_SHADER); + unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_src, NULL); glCompileShader(vertex_shader); @@ -51,7 +61,7 @@ int main() { if (!success) { glGetShaderInfoLog(vertex_shader, 512, NULL, info_log); - printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED %s\n", info_log); + printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", info_log); } const char *fragment_shader_src = @@ -62,26 +72,48 @@ int main() { " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "}\0"; - unsigned int fragment_shader; - fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_src, NULL); glCompileShader(fragment_shader); glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragment_shader, 512, NULL, info_log); - printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED %s\n", info_log); + printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", info_log); + } + + unsigned int shader_program = glCreateProgram(); + glAttachShader(shader_program, vertex_shader); + glAttachShader(shader_program, fragment_shader); + glLinkProgram(shader_program); + + glGetProgramiv(shader_program, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(shader_program, 512, NULL, info_log); + printf("ERROR::SHADER::PROGRAM::LINK_FAILED\n%s\n", info_log); } - unsigned int shader_program; - shader_program = glCreateProgram(); + glDeleteShader(vertex_shader); + glDeleteShader(fragment_shader); + + unsigned int VAO; + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + glEnableVertexAttribArray(0); + // Main loop while (!glfwWindowShouldClose(window)) { - glClearColor(0.1f, 0.1f, 0.25f, 0.0f); + glClearColor(0.1f, 0.1f, 0.25f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(window); + glUseProgram(shader_program); + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, 3); + glfwSwapBuffers(window); glfwPollEvents(); } -- cgit 1.4.1-2-gfad0