From ea9c3fdd3afc5d0e3c839ef37bd4030c22d8133b Mon Sep 17 00:00:00 2001 From: ktyl Date: Tue, 6 Jul 2021 00:58:55 +0100 Subject: [PATCH] vertex colours --- res/shader/shader.frag | 4 ++-- res/shader/shader.vert | 9 +++++---- src/main.c | 16 +++++++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/res/shader/shader.frag b/res/shader/shader.frag index 9218613..fdeb6ab 100644 --- a/res/shader/shader.frag +++ b/res/shader/shader.frag @@ -1,9 +1,9 @@ #version 330 core out vec4 FragColor; -uniform vec4 ourColor; +in vec3 ourColor; void main() { - FragColor = ourColor; + FragColor = vec4(ourColor, 1.0); } diff --git a/res/shader/shader.vert b/res/shader/shader.vert index 27b97ea..721f5ef 100644 --- a/res/shader/shader.vert +++ b/res/shader/shader.vert @@ -1,10 +1,11 @@ #version 330 core -layout (location = 0) in vec3 aPos; +layout (location = 0) in vec3 aPos; // position has attribute position 0 +layout (location = 1) in vec3 aColor; // color has attribute position 1 -out vec4 vertexColor; +out vec3 ourColor; void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); - vertexColor = vec4(0.5, 0.0, 0.0, 1.0); + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; } diff --git a/src/main.c b/src/main.c index 73599ef..2894ac1 100644 --- a/src/main.c +++ b/src/main.c @@ -3,10 +3,11 @@ #include "gfx.h" 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 + // position color + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, // top right + 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.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top left }; unsigned int indices[] = { @@ -50,8 +51,13 @@ int main() glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // set vertex attributes - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); // TODO: wtf + + // position attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0); // TODO: wtf glEnableVertexAttribArray(0); + // color attribute + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); glUseProgram(shaderProgram);