diff options
author | iurii plugatarov <[email protected]> | 2024-08-10 13:32:11 +0200 |
---|---|---|
committer | iurii plugatarov <[email protected]> | 2024-08-10 13:32:11 +0200 |
commit | 86eb1bfcf21819492e0ec55cd08127fa8a5c6769 (patch) | |
tree | 9d23841ecbd796954196b856746db6f1d154a0ea /src/main.c | |
parent | 02eda0b2aa8c91caa4116c04f411aafa0ab10676 (diff) | |
download | ogl-86eb1bfcf21819492e0ec55cd08127fa8a5c6769.tar.gz |
color vertex
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 50 |
1 files changed, 20 insertions, 30 deletions
diff --git a/src/main.c b/src/main.c index 77ed9d5..a125823 100644 --- a/src/main.c +++ b/src/main.c @@ -44,30 +44,27 @@ int main() { get_max_attrib_count(&attrib_count); float vertices[] = { - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left - }; - unsigned int indices[] = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle + // positions // colors + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top }; unsigned int VBO; glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 3, vertices, + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, vertices, GL_STATIC_DRAW); const char *vertex_shader_src = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" - "out vec4 vertexColor;\n" + "layout (location = 1) in vec3 aColor;\n" + "out vec3 ourColor;\n" "void main()\n" "{\n" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" - " vertexColor = vec4(0.5, 0.0, 0.0, 1.0);\n" + " ourColor = aColor;\n" "}\0"; unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER); @@ -85,10 +82,10 @@ int main() { const char *fragment_shader_src = "#version 330 core\n" "out vec4 FragColor;\n" - "uniform vec4 ourColor;\n" + "in vec3 ourColor;\n" "void main()\n" "{\n" - " FragColor = ourColor;\n" + " FragColor = vec4(ourColor, 1.0);\n" "}\0"; unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); @@ -119,36 +116,29 @@ int main() { glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); - unsigned int EBO; - glGenBuffers(1, &EBO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(float) * 2 * 3, indices, - GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), + (void *)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + glUseProgram(shader_program); while (!glfwWindowShouldClose(window)) { glClearColor(0.1f, 0.1f, 0.25f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - glUseProgram(shader_program); - float time_value = glfwGetTime(); - float green_value = (sin(time_value) / 2.0f) + 0.5f; - int vertex_color_location = - glGetUniformLocation(shader_program, "ourColor"); - - glUniform4f(vertex_color_location, 0.0f, green_value, 0.0f, 1.0f); - glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); + glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); glfwPollEvents(); } + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shader_program); + glfwTerminate(); return 0; } |