Trouble with uniform matrices

Started by
5 comments, last by TheChubu 9 years ago

I've begun creating my own matrix stack to get away from legacy opengl.

I'm having some difficulty with setting up a GLSL vertex shader to take two uniform matrices.

I currently have a working shader that takes a single uniform matrix (a full ModelViewProjection matrix) and renders my object correctly.

When I attempt to split out that matrix into its Projection and ModelView Matrices and pass them in, the scene does not render.

--------------------------------------------------------------------

#version 330
uniform mat4 uProjection;
uniform mat4 uModelView;
in vec3 v;
in vec3 vt;
in vec3 vn;
smooth out vec3 out_vn;
smooth out vec3 out_vt;
smooth out float depth;
void main(){
gl_Position = uProjection * uModelView * vec4(v,1);
out_vn = (uModelView * vec4(vn,0)).xyz;
out_vt = vt;
depth = gl_Position.z;
}
----------------------------------------------------------------------
uProjection is calculated just before glUniformMatrix is called (the projection is equivalent to glFrustum).
uModelview is the product of three matrices: Translation(0,0,-8) * Rotation(r,1,1,0) * Rotation(-.5f,-.5f,-.5f);

Is there anything special about using more than one uniform matrix input?

Advertisement


Is there anything special about using more than one uniform matrix input?

Not really, it's pretty common...

Can you show us some client side code? Are both your calls to glUniformMatrix4fv() correct?

The only real gotcha is to watch your glGetUniformLocation calls, making sure that they're returing valid locations and that you use these location in your subsequent glUniform calls.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I *think* my glUniformMatrix4fv calls are correct. My program has a class called 'ShaderProgram' which I use to spin up shaders, bind attributes, and get uniform locations. The object itself queries each uniform location ONCE and stores it in its own lookup table. It has a helper function that lets me update my matrix uniforms, referencing them by name.

I've stumbled upon some extra wierdness fiddling with this thing. In transitioning to the 2-matrix scheme I have set uProjection to the Identity matrix and uModelView to (Frustum x Translate x Rotate x Translate), and I set the vertex shader to set gl_Position = uProjection x uModelView x v_in. This works, but if I switch the values of uModelView and uProjection, only some faces show. This is extreemly odd since one matrix is just an Identity matrix and shouldn't really affect the output. (in other words, for Matrices M and I, I expect to see MxI=IxM, but that is not the case for some reason).

----------------------Shader Initialization Snippet:--------------------------

GL20.glAttachShader(program_id, vertex_shader_id);
GL20.glAttachShader(program_id, fragment_shader_id);
GL20.glLinkProgram(program_id);
// Delete Shader objects
GL20.glDeleteShader(vertex_shader_id);
GL20.glDeleteShader(fragment_shader_id);
// Get Uniform Locations
if (uniforms != null) {//Check if uniform locations are needed
Uniform_Location.clear();//Clear old uniform locations. needed if program is being recreated
for (int i = 0; i < uniforms.length; i++) {// For each uniform name get its location and store it in a lookup table.
Uniform_Location.put(uniforms, GL20.glGetUniformLocation(program_id, uniforms));
}
}

--------------glUniformMatrix4f usage-------------------------------

ByteBuffer matrix_buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()); //JAVA DIRECT NIO BUFFER, for use with LWJGL
public void UniformMatrix(String name, float[] m) {
Integer location = Uniform_Location.get(name);//Lookup uniform location from table
if (location != null) {
matrix_buffer.clear();//Restart write at beginning of buffer
matrix_buffer.asFloatBuffer().put(m).flip(); //Write to buffer and set its capacity
GL20.glUniformMatrix4f(location, 1, false, matrix_buffer);//Send the matrix data to opengl
}
return;
}

I've just come to the realization that I am an idiot and shouldn't be writing shaders. This is a very profound feeling.mellow.png

My Vertex Shader was fine. My client code was fine. My fragment shader was varying the intensity of colors by the dot product of their normal and the forward vector, setting alpha to 0.

Thank you gentlemen for your help.biggrin.png

Only by seeing our own limitations can we overcome them.

Keep at it!

ByteBuffer matrix_buffer = ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()); //JAVA DIRECT NIO BUFFER, for use with LWJGL

and

matrix_buffer.asFloatBuffer().put(m).flip(); //Write to buffer and set its capacity

Be careful with what you're doing there. You'll be uploading data to the GPU all the time, you should strive for it to be a garbageless process. There you're creating two new objects each call is made. Moreover, timely deallocation of direct buffers isnt guaranteed, and you're doing a new direct buffer alloc each time.

You can very easily just create one instance of a big enough buffer and pass that all around the renderer, with LWJGL 3 you dont even have to create FloatBuffer view objects since most calls have a version that receives pure ByteBuffers.

EDIT: I misread, you might not be creating a new direct buffer each call. You're still creating a new view each call though, you can (and should) hold onto that reference too instead of re creating it all the time.

Also this line here:

matrix_buffer.asFloatBuffer().put(m).flip(); //Write to buffer and set its capacity

It might not be doing what you think its doing. asFloatBuffer returns a new FloatBuffer object that basically points to the same region of memory as the ByteBuffer (well, address + current position exactly). Thing is, its a whole different object, if you call 'flip' on that view, it wont do anything to the position/limit/etc of the original ByteBuffer. 'ByteBuffer#asFloatBuffer' javadoc explains it in detail.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement