Jump to content

View more

View more

Image of the Day

Full Syncs #screenshotsaturday favourites https://t.co/i1Flnwcg3l #xbox #ps4 https://t.co/m0v2F1SxGs
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.

Sign up now

Trouble coloring my mesh's vertices (Newbie)

2: Adsense
  • You cannot reply to this topic
1 reply to this topic

#1 TheFearlessHobbit   Members   


Posted 08 March 2017 - 08:30 PM

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, 08 March 2017 - 08:31 PM.

#2 TheFearlessHobbit   Members   


Posted 09 March 2017 - 07:15 PM

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.  :)