Question

This is my vertex shader code:

attribute vec4 position;
attribute vec4 inputTextureCoordinate;

varying vec2 textureCoordinate;

uniform mat4 modelViewProjMatrix;

void main()
{
    gl_Position = modelViewProjMatrix * position;
textureCoordinate = inputTextureCoordinate.xy;
}

And I update uniform in this code:

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWMATRIX], 1, 0, [scaleMatrix getMatrix]);

But when I execute this string last update result(scale) resets:

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWMATRIX], 1, 0, [rotationMatrix getMatrix]);

How I can use more than one matrix?

Was it helpful?

Solution

You have to multiply the matrices together. Do this in the CPU and pass the result as the uniform.

OTHER TIPS

It is also possible to send more than one matrix to the shader. You'd have to define one (or more) additional uniform mat4 in the shader, and bind it to the shader program in your application.

You can multiply matrices on the shader with the * operator, so if you wanted to, say, have an additional rotation of the object being done before applying position, view and projection, your shader would look like this:

attribute vec4 position;
attribute vec4 inputTextureCoordinate;

varying vec2 textureCoordinate;

uniform mat4 modelViewProjMatrix;
uniform mat4 rotationMatrix; //your new matrix

void main()
{
  gl_Position = modelViewProjMatrix * rotationMatrix * position;
  textureCoordinate = inputTextureCoordinate.xy;
}

Note that in this case, you have to supply an identity Matrix as rotationMatrix to your shader if you don't want to do any rotation.

You probably already know this, but just to be sure: Matrix multiplications are from right to left, so if you want to do a rotation around the model's axis, and then move the model somewhere, you need to put your rotation matrix to the right of your translation matrix.

If anybody knows what the performance implications are of sending two matrices vs. multiplying on CPU and sending one matrix, I'd be interested to hear about that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top