1
votes

I have been writing some code for a basic rendering application.

The renderer code consists of setting up Vertex Array and Vertex Buffer Objects for rendering entities, and 3 texture units are created for use as the diffuse, specular and emission component respectively. I wrote a wrapper class for my shader, to create and set uniforms.

void Shader::SetVec3Uniform(const GLchar * name, vec3 data)
{
    glUniform3f(glGetUniformLocation(this->program, name), data);
}

When I set the uniforms using the above function, it doesn't render with the expected result. But when I find the location before I set the uniform, it renders correctly. I use the function below to set it.

void Shader::SetVec3Uniform(GLint loc, vec3 data)
{
    glUniform3f(loc, data.x, data.y, data.z);
}

So my question is, is data lost, is the data not reaching the shader on the GPU in time? I am honestly stumped. No idea why this subtle difference in the way the uniforms are set is causing such a difference in its render.

Can anybody shed any light on this?

1
It shouldn't make any difference. How and when do you initialize the value that you pass in as loc in the second example? After glUseProgram?samgak
Did you set VertexAttribPointers correctly ? I would also prefix your members of the shader class with something like m_***, so you dont get confused in the calls :)Hannes Hauptmann
@samgak I find the location before I pass it to the function. Yes, after glUseProgram().Andre Rogers
@HannesHauptmann Yes I did set them properly. I know this because the object is being rendered on screen, and the so was the texture. Its just that with my first approach above the scene being rendered wasn't completely colored by my shader. The cube would be lit and colored only directly in front of the camera and the light source, while everything was black. When I switched over to the second implementation, it worked.Andre Rogers

1 Answers

0
votes

Well, according to the docs, the prototype for the function glUniform3f is:

void glUniform3f(GLint location,
                 GLfloat v0,
                 GLfloat v1,
                 GLfloat v2);

and for the function glUniform3fv it is:

void glUniform3fv(GLint location,
                  GLsizei count,
                  const GLfloat *value);

How is vec3 defined? The program would probably work if you had something like:

// struct vec3 { float x; float y; float z; };
glUseProgram(this->program);
glUniform3f(glGetUniformLocation(this->program, name), data.x, data.y, data.z);

or

// struct vec3 { float arr[3]; };
glUseProgram(this->program);
glUniform3fv(glGetUniformLocation(this->program, name), 1, &data.arr[0]);

Depending on your vec3 implementation of course.