Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Trouble coloring my mesh's vertices (Newbie)

This topic is 647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I am a new registered user. 

I have just started learning OpenGL 2 weeks ago and I come from a WIN32 API background. To cut straight to the chase, I have a Hexagon drawn like this: 

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 are my shaders: 

Vertex Shader

#version 430 core

layout (location = 0) in vec3 position; 
layout (location = 1) in vec3 color;
layout (location = 2) in vec3 newColor; //Er... what to do?!

uniform mat4 _resultantMatrix;

out vec3 outputColor;

void main() 
vec4 currentPos = vec4(position, 1.0f);
gl_Position = _resultantMatrix * currentPos; 
outputColor = color;

Fragment Shader

#version 430 core

in vec3 outputColor;  
out vec4 Color; 

uniform float Timer;

void main()
   vec3 tempColor = outputColor * abs(sin(Timer));
   tempColor.x = (sin(Timer) / 2) + 0.5f;
//   tempColor.y = (cos(Timer) / 2) + 0.5f; Not doing what I expect it to do.
   Color = vec4(tempColor, 1.0f); 
The result is a multi-colored hexagon. However, I want my hexagon's vertices to glow, that is essentially what I am going for. I have a uniform float Timer that fades in and out quite smoothly. Though, every time I execute my program, it looks like only 2 or 3 vertices change color, while the others stay the same. I think I know what the issue here is, it has something to do with the fact that I am storing all my colors in the same VBO. 
glGenBuffers(1, &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))); Stores the data in layout 2 but don't know what to do after!

// glEnableVertexAttribArray(2);

So like in my fragment shader, when I go tempColor.x = blahblahblah; it's changing more than one vertex color. If anyone could help me out I'd greatly appreciate it as I have been stuck for 2 hours trying to figure this thing out. I want all my vertices to have different colors while the program is being executed, and so far only 3 vertices change while the other 3 stays the same. 


Thank you very much!

Edited by TheFearlessHobbit

Share this post

Link to post
Share on other sites

Problem fixed. (Woo!)

For whoever's interested, I simply created another VBO which contains the new color data and inside my fragment shader I created a timer to alternate between both outputs using if-else statements every 1 or 2 seconds. However, I sort of lost my glowing effect that way but at least I got my multi-colored vertices that change overtime. I'll work on getting some glowing effects going on. 

Thank you to whoever read my post and tried to help me out.  :)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!