seperate color buffer

This commit is contained in:
Luca Conte 2025-03-11 10:53:06 +01:00
parent 9f7ce25ef2
commit d22e2b8e81
2 changed files with 53 additions and 6 deletions

View File

@ -168,6 +168,44 @@ void init(void) {
0.35f, -0.7f
};
GLfloat colors[] = {
// R // G // B
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.3f,
1.0f, 0.3f, 0.0f,
1.0f, 0.3f, 0.3f,
1.0f, 0.3f, 0.6f,
1.0f, 0.6f, 0.3f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.3f,
0.3f, 1.0f, 0.0f,
0.3f, 1.0f, 0.3f,
0.3f, 1.0f, 0.6f,
0.6f, 1.0f, 0.3f,
0.3f, 0.3f, 1.0f,
0.3f, 0.6f, 1.0f,
0.6f, 0.3f, 1.0f,
0.3f, 0.3f, 1.0f,
0.3f, 0.6f, 1.0f,
0.6f, 0.3f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.1f,
1.0f, 1.0f, 0.2f,
1.0f, 1.0f, 0.7f,
1.0f, 1.0f, 0.8f,
1.0f, 1.0f, 0.9f
};
DEBUG("Creating vertext buffer");
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
@ -175,6 +213,13 @@ void init(void) {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
DEBUG("Creating colour buffer");
GLuint colorBuffer;
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
DEBUG("Creating vertex array object");
// create vertex array object
glGenVertexArrays(1, &vertexArrayObject);
@ -192,6 +237,12 @@ void init(void) {
);
glEnableVertexAttribArray(0);
// vertex color data
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
@ -214,10 +265,6 @@ void draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
GLuint colorLocation = glGetUniformLocation(program, "uColor");
ColorRGB color = hslToRgb(currentHue, 1.0f, 0.5f);
glUniform3f(colorLocation, color.r, color.g, color.b);
GLuint positionLocation = glGetUniformLocation(program, "uPosition");
glUniform2f(positionLocation, cos(currentHue * 2 * M_PI) * 0.2f, sin(currentHue * 2 * M_PI) * 0.2f);

View File

@ -1,11 +1,11 @@
#version 330 core
layout (location = 0) in vec2 aPosition;
uniform vec3 uColor;
layout (location = 1) in vec3 aColor;
uniform vec2 uPosition;
out vec3 vertexColor;
void main() {
vertexColor = uColor;
vertexColor = aColor;
gl_Position = vec4(uPosition + aPosition, 0.0, 1.0);
}