Hello everyone, I am a new registered user.
I just recently started learning OpenGL and I come from a WIN32 API background. To cut straight to the chase, I am stuck and I am here to ask for assistance. Basically, I have a Hexagon drawn like this:
// Position Color
GLfloat vertices[] = { -0.25f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Red
0.25f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Green
0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f , // Blue
0.25f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, // Purple
-0.25f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, // White
-0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f // Yellow
};
Here is how my vertex and fragment shaders currently look like:
Vertex
#version 430 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec3 newColor;
uniform mat4 resultantMatrix;
out vec3 output;
void main()
{
vec4 currentPos = vec4(position, 1.0f);
gl_Position = resultantMatrix * currentPos ;
output = color; //... maybe + newColor;?
// Err.... How to output 2 things in the fragment shader...?
};
Fragment
#version 430 core
in vec3 o;
out vec4 Color;
uniform float Timer;
void main()
{
vec3 tempColor = output * abs(sin(Timer));
tempColor.x = (sin(Timer) / 2) + 0.5f;
// tempColor.y = (sin(Timer) / 2) + 0.5f; // Not doing what I expect it to do
Color = vec4(tempColor, 1.0f);
};
What I am trying to accomplish is I want to make the color of each vertex change as time goes on. The issue I am having is here:
glGenBuffers(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
// glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(21 * sizeof(GLfloat))); It stores the colors in the 2nd layout but it still wouldn't work
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
// glEnableVertexAttribArray(2);
Currently my hexagon only colors 2 or 3 vertices differently, the others are all the same. I am trying to separate the data into another layout but either I’m doing it wrong or the result is not what I want. I would greatly appreciate any help you could give me because I am absolutely stuck. I prefer not to generate another VBO because that may seem a bit expensive for such a simple thing, I am sure there’s a trick I can pull here to make all vertices glow differently, but my knowledge of OpenGL is not decent yet, hence why I’m here seeking your help.
Thank you very much.