[GLSL] Some Questions regarding matrices

Hello everyone.

I have two (probably very noob-ish) questions.

  1. Can I change the content of a uniform multiple times without side-effects? Like this:

myShader.bind();

myShader.changeUniformMat4f(someUniformPointer,object0.objMat);
object0.draw();

myShader.changeUniformMat4f(someUniformPointer,object1.objMat);
object1.draw();

myShader.changeUniformMat4f(someUniformPointer,object2.objMat);
object2.draw();

myShader.unbind();

  1. I don’t understand matrix-multiplication very well, so, is this GLSL code correct?

void main(void)
{
    // cameraMatrix, is the matrix of my camera, containing position and rotation.
    // myObjectMatrix, is the matrix of a object in my world. It has a position and rotation.
    gl_Position = gl_ProjectionMatrix * cameraMatrix * myObjectMatrix;
    
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}

I hope I explained that well and understandable.
My english isn’t the best.

Please tell me about every mistake in the code and shader.
I wan’t to use this for my main-game project, so it has to be as bug-free as possible.

  • Longor1996

Just adding myself to the thread as I have plenty to learn about GLSL too, especially matrices.

Matrix * matrix give you a matrix. So when you try to store matrix to gl_Position you are causing big error.

This work right.


 gl_Position = projection * view * model * a_position;

But you don’t want to do 3 matrix * matrix operation per vertex so you could change that code to


 gl_Position = projection * (view * (model * a_position));

But then again why to do any unneeded work just combine those at cpu once and send that to shader. Less data, less registers, less math.


 gl_Position = modelViewProj * a_position;

Notice how I changed some names to match better with common usage. Also notice that modelViewProj is same as (projection * view * model) naming convetions related to matrix math is reason for that.

Okay, I think I got it now!

Render Method:


void renderScene(Matrix4f modelViewProjectionMatrix){
    FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
    Matrix4f modelViewProjectionObjectMatrix = new Matrix4f();
    
    for(GameObject gameObject : gameObjects)
    {
        Matrix4f.mul(modelViewProjectionMatrix, gameObject.matrix, modelViewProjectionObjectMatrix);
        
        // Transform the Matrix4f into FloatBuffer
        buf.clear();
        modelViewProjectionObjectMatrix.store(matrixBuffer);
        buf.flip();
        
        // Send the matrix to OpenGL
        GL20.glUniformMatrix4(modelViewProjObjMatrixLocation, false, matrixBuffer);
        
        // Render the model
        gameObject.render();
    }
}

Vertex-Shader:


uniform mat4 modelViewProjObjMatrix;

void main(void)
{
  gl_Position = modelViewProjObjMatrix * gl_Vertex;
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_FrontColor = gl_Color;
}

Is that correct?

Edit:
And new question instantly popped into my head:
Would it be even better to run the calculations for the matrices using OpenCL?

  • Longor1996

[just throwing in…: Changing variables without side-effect? Haha… :smiley: ]