opengl

Sample code from LearnOpenGL.com
Log | Files | Refs

commit 633aaf0ca205d94558f4231bc3f5874ada89f21a
parent f76ada37bb1470a7c15a1f56f0b83accead0097a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 25 Jun 2023 14:51:48 +0900

add color

Diffstat:
Mmain.c | 25+++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/main.c b/main.c @@ -6,10 +6,10 @@ #include <GLFW/glfw3.h> float vertices[] = { - 0.5, 0.5, 0.0, - 0.5, -0.5, -0.0, - -0.5, -0.5, 0.0, - -0.5, 0.5, 0.0, + 0.5, 0.5, 0.0, 1.0, 0.0, 0.0, + 0.5, -0.5, -0.0, 0.0, 1.0, 0.0, + -0.5, -0.5, 0.0, 0.0, 0.0, 1.0, + -0.5, 0.5, 0.0, 0.0, 1.0, 1.0, }; unsigned int indices[] = { @@ -20,23 +20,26 @@ unsigned int indices[] = { const char *vertexShaderSource = " #version 330 core layout (location = 0) in vec3 aPos; + layout (location = 1) in vec3 aColor; + out vec3 ourColor; void main() { gl_Position = vec4(aPos, 1.0); + ourColor = aColor; } "; const char *fragmentShaderSource = " #version 330 core - uniform vec4 ourColor; + in vec3 ourColor; out vec4 FragColor; void main() { - FragColor = ourColor; + FragColor = vec4(ourColor, 1.0); } "; @@ -127,9 +130,12 @@ main(void) glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), + 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); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); @@ -140,11 +146,6 @@ main(void) glClearColor(1.0, 1.0, 0.8, 1.0); glClear(GL_COLOR_BUFFER_BIT); - float timeValue = glfwGetTime(); - float greenValue = sinf(timeValue) / 2.0 + 0.5; - int vertexColorLocation = - glGetUniformLocation(shaderProgram, "ourColor"); - glUniform4f(vertexColorLocation, 0.0, greenValue, 0.0, 1.0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glfwPollEvents();