PDA

View Full Version : Unable to bind more than one uniform



Lars Jahr
09-12-2011, 10:25 AM
I'm trying to learn OpenGL 3.3 and I'm having some trouble with loading more than one uniform in my Vertex shader.

At the top of my program I have declared


GLuint vs, fs;
GLuint program;
GLint attribute_coord3d, attribute_v_color;
GLint uniform_m_transform;
GLint uniform_mvp_matrix;


And in my setup method I have the following code after I link the the shaders and make a program object.



const char* attribute_name = "coord3d";
attribute_coord3d = glGetAttribLocation(program, attribute_name);
if (attribute_coord3d == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return false;
}

attribute_name = "v_color";
attribute_v_color = glGetAttribLocation(program, attribute_name);
if (attribute_v_color == -1) {
fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
return 0;
}


const char* uniform_name;

uniform_name = "m_transform";
uniform_m_transform = glGetUniformLocation(program, uniform_name);
if (uniform_m_transform == -1) {
fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
fprintf(stderr, "uniform_m_transform: %d\n", uniform_m_transform);

return 0;
}

uniform_name = "mvp";
uniform_mvp_matrix = glGetUniformLocation(program, uniform_name);
if (uniform_mvp_matrix == -1) {
fprintf(stderr, "Could not bind uniform %s\n", uniform_name);
return 0;
}


And my vertex shader looks like this:


#version 330
in vec3 coord3d;
in vec3 v_color;
uniform mat4 m_transform;
uniform mat4 mvp;
out vec3 f_color;
void main(void) {
gl_Position = m_transform * vec4(coord3d, 1.0);
f_color = v_color;
}


My problem is that the last if-test in my setup method becomes true, i.e glGetUniformLocation returns -1. If I only pass one uniform to my vertex shader everything works as intended. Can anyone see what I'm doing wrong?

Ilian Dinev
09-12-2011, 11:07 AM
You're not using the "mvp" uniform at all, so it becomes inactive (inexisting), so its location is -1.

Lars Jahr
09-13-2011, 01:07 AM
That did the trick, thanks!

Lars Jahr
09-13-2011, 01:13 AM
That did the trick, thanks!