Question

I'm trying to do a simple 3D engine but I'm tearing my hair out wandering why I can't change the color of a little 3d line. The following works but always renders white. I am trying to make it draw green:

...
float color[4] = { 0.5f, 1.0f, 0.0f, 1.0f };
glUseProgram( shader.id );
//I ommitted the model matrix and stuff because it works fine
glUniformMatrix4fv( glGetUniformLocation( shader.id, "color" ), 1, GL_FALSE, color );

glBegin( GL_LINES );
    glVertex3f( x, y,    z );
    glVertex3f( x, y+10, z );
glEnd();
...

Vertex shader:

#version 330

in vec3 vertexPosition_modelspace;
uniform mat4 mvp;

void main(){
    vec4 v = vec4(vertexPosition_modelspace,1);
    gl_Position = mvp*v;
}

Fragment shader:

#version 330

uniform vec4 color = vec4(1.0,1.0,1.0,1.0);
out vec4 frag;

void main(){
        frag = color;
}

I read some documents saying that sometimes uniforms get optimized away and become invisible but this is not the case since I AM using the uniform to choose the color of the pixels. Also the return value of glGetUniform is (0) which is valid and thus it exists. So why is it screwing up? Are my drivers dead?

The only other relevant or similar question was from GLSL Shader will not render color from uniform variable and the answer I did not understand. What does he mean multiple shaders? I am just using one shader.

Was it helpful?

Solution

You using glUniformMatrix4fv when you should use glUniform4fv (because color is vec4, not mat4).

That given, always check glGetError() status when in doubt. Even better, use KHR_debug extension.

And don't assume error in driver before than checking everything in your code :-). It is extremely rare case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top