diff options
| author | Moritz Ruge <ruge.moritz@gmail.com> | 2026-08-06 16:07:30 +0200 |
|---|---|---|
| committer | Moritz Ruge <ruge.moritz@gmail.com> | 2026-08-06 16:36:14 +0200 |
| commit | b716b5067c4844ab15ea29beb3059e7efdedaef3 (patch) | |
| tree | c1bca0369074345f9efa37cdf34b55a571350c73 | |
| parent | 6fd20124218af64fd4c56ba5a62bcec6ab9c2cb3 (diff) | |
triangle achieved
| -rw-r--r-- | main.c | 129 |
1 files changed, 122 insertions, 7 deletions
@@ -1,4 +1,3 @@ -#include <KHR/khrplatform.h> #define GLFW_INCLUDE_NONE #include <glad/glad.h> #include <GLFW/glfw3.h> @@ -13,6 +12,42 @@ void processInput(GLFWwindow* window); // END int main(void) { + int w_width = 1280; + int w_height = 820; + int shader_success; + unsigned int VBO, VAO, EBO; + // fragment shader + unsigned int vertex_shader; + unsigned int fragment_shader; + unsigned int shader_program; + + char info_Log[512]; + // vertex shader source + const char* vertex_shader_source = + "#version 460 core\n" + "layout (location = 0) in vec3 aPos;\n" + "\n" + "void main() {\n" + "gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" + "}\n\0"; + // Fragment shader + // --------------- + const char* fragment_shader_source = + "#version 460 core\n" + "out vec4 FragColor;\n" + "\n" + "void main() {\n" + "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" + "}\n\0"; + + // Triangle shenanigans + // -------------------- + float verticies[] = { + -0.5f, -0.5f, 0.0f, // left + 0.5f, -0.5f, 0.0f, // right + 0.0f, 0.5f, 0.0f // top + }; + // GLFW settings init and which version of opengl we should use // and which subset of opengl profiles @@ -23,11 +58,8 @@ int main(void) { - // Window settings - // create window - int w_height = 1280; - int w_width = 820; - GLFWwindow* window = glfwCreateWindow(w_height, w_width, "Test", NULL, NULL); + // glfw window creation + GLFWwindow* window = glfwCreateWindow(w_width, w_height, "Test", NULL, NULL); if (window == NULL) { printf("Failed to create GLFW window\n"); glfwTerminate(); @@ -46,23 +78,106 @@ int main(void) { // opengl viewport framebuffer_size_callback(window, w_width, w_height); + + // build and compile our shader program + // ------------------------------------ + + // vertex_shader + vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL); + glCompileShader(vertex_shader); + + // check for compilation error + glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_success); + if (!shader_success) { + glGetShaderInfoLog(vertex_shader, 512, NULL, info_Log); + printf("ERROR SHADER VERTEX COMPILATION FAILED: %s\n", info_Log); + } + + // fragment_shader + fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL); + glCompileShader(fragment_shader); + + // check for compilation error + glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_success); + if (!shader_success) { + glGetShaderInfoLog(fragment_shader, 512, NULL, info_Log); + printf("ERROR SHADER FRAGMENT COMPILATION FAILED: %s\n", info_Log); + } + + // create shader program + // --------------------- + shader_program = glCreateProgram(); + // attach to shader program id + glAttachShader(shader_program, vertex_shader); + glAttachShader(shader_program, fragment_shader); + glLinkProgram(shader_program); + + // check for linking erros + glGetProgramiv(shader_program, GL_LINK_STATUS, &shader_success); + if (!shader_success) { + glGetProgramInfoLog(shader_program, 512, NULL, info_Log); + printf("ERROR WHILE LINKIN THE SHADERS TO THE PROGRAM: %s\n", info_Log); + } + + // delete now unneeded shaders + glDeleteShader(vertex_shader); + glDeleteShader(fragment_shader); + + // set up vertex data (and buffers) and configure vertex attributes + // ---------------------------------------------------------------- + + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + + // bind the vertex array object first, then bind and set vertex buffers, and then configure vertex attributes. + glBindVertexArray(VAO); + + // 0. copy our vertices array in a buffer for OpenGL to use + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW); + // 1. then set the vertex attributes pointer + // location of array, size of vertex attribute (vec3), type of data(float), normialition?, stride (space between data in array), offset of where the position data begins in the buffer + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + // ------------------------------------------------------------------------- // render loop // ------------------------------------------------------------------------- while (!glfwWindowShouldClose(window)) { // input + // ----- processInput(window); // rendering commands here // ----------------------- - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + static GLclampf c = 0.0f; + glClearColor(c, c, c, 0.0f); glClear(GL_COLOR_BUFFER_BIT); + c += 1.0f / 256.0f; + if (c >= 1.0f) c = 0.0f; + + // render triangle + glUseProgram(shader_program); + glBindVertexArray(VAO); + glad_glDrawArrays(GL_TRIANGLES, 0, 3); + // check and call events and swap the buffers + // ------------------------------------------ glfwSwapBuffers(window); glfwPollEvents(); } + // de-allocate all resources + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shader_program); + + // Terminate program + // ----------------- glfwTerminate(); return 0; } |
