PDA

View Full Version : Problem with passing uniform data



vreal
02-10-2012, 07:10 AM
I've got a problem with passing down a uniform variable (or better a matrix).
When I call glUniformMatrix4fv(...) my program crashes.
I draw my object by using a vertex index array.

Here is a part of my program:


void initialize() {
// create VBOs
glGenBuffers(1, &vboVId);
glBindBuffer(GL_ARRAY_BUFFER, vboVId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &vboIId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

// create shaders
[...]
shaderProgramId = glCreateProgram();
glAttachShader(shaderProgramId, vertexShaderId);
glAttachShader(shaderProgramId, fragmentShaderId);
glLinkProgram(shaderProgramId);
glUseProgram(shaderProgramId);

// map locations
locVertices = glGetAttribLocation(shaderProgramId, "a_position");
locMvpMatrix = glGetUniformLocation(shaderProgramId, "u_mvpMatrix");

glEnableVertexAttribArray(locVertices);
glVertexAttribPointer(locVertices, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
}


void loop() {
mvpMatrix.loadIdentity();
mvpMatrix.multiply(projectionMatrix);
glUniformMatrix4fv(locMvpMatrix, 1, 0, (float*)&amp;mvpMatrix); // <-- when commented, the program works
glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_INT, 0);
glfwSwapBuffers();
}

BionicBytes
02-10-2012, 07:33 AM
Is the correct shader bound when you call glUniformMatrix4fv?
Looking at your code you don't do that.
Then again the code snippet is minimal and I can't tell if you have only the 1 shader or many (which you are not showing us).
If you do have many, then there is a need to issue glUseProgram(shaderProgramId) during the render loop.

Also, check the windows pointer function definition for glUniformMatrix4fv - there might be something wrong there if you have never used this command before in all your previous OpenGL projects.

menzel
02-10-2012, 09:34 AM
locMvpMatrix could be -1 if the uniform does not exist but even then and even if the shader program is not bound at that moment, GL would give you an error and ignore the call, it would not crash.

So either glUniformMatrix4fv is NULL (are you using an extension loader? check if that had errors) or the mvpMatrix can't be treated as plain old data. Tried to upload a float[16] array just to check if that wouldn't crash?

vreal
02-10-2012, 10:15 AM
I found the problem. The loop function was called too early. That was all. The code seems to be fine.
Sorry and thank you!

But anyway:
@BionicBytes
What did you mean with "windows pointer function definition"?
Can you give me a keyword to search for on the internet?