Reputation: 2007
So if a given mesh comes with a given matrix transformation and I want to use that transformation in my vertex shader, how do I use it once it's already passed in. Let's call it "myMatrix".
So what I'm doing is:
varying vec3 modelPos = (gl_ModelViewMatrix * myMatrix * vec4(positionIn, 1)).xyz;
gl_Position = (gl_ProjectionMatrix * vec4(modelPos, 1));
where "positionIn" is the passed in vertex.
This doesn't seem to be working though (Nothing shows up to the screen). The object renders normally if I take out "myMatrix" but then of course it doesn't have its transformation applied to it.
Upvotes: 1
Views: 2294
Reputation: 4933
As everyone keeps trying to tell you, the multiplication order depends on what your matrix represents. You say "rotation". We say "rotation in what coordinate frame?"
varying vec3 modelPos = (gl_ModelViewMatrix * myMatrix * vec4(positionIn, 1)).xyz;
In the code you quote, myMatrix
is closest to your raw vertex position. So if that was a rotation of 45 degrees around the x-axis, it would be a 45 degree rotation around the x-axis of object-space, where the origin is (0,0,0) relative to your stored vertex positions and the axis are similarly the natural axis of vertex positions.
If you reverse the order such that you have: varying vec3 modelPos = (myMatrix * gl_ModelViewMatrix * vec4(positionIn, 1)).xyz;
Now the ModelView transform happens first because it's closest to your raw, object-space vertex position. So now myMatrix
is operating in camera-space, where the origin is at the camera and the z-axis is along the camera's view vector.
If you wanted to rotate around the x-axis of world-space, then there is no easy way to do that in the vertex shader while still using the old-style fixed-function transform matrices. With the fixed function matrices, you would apply your custom matrix to the ModelView matrix in software while you are setting it up.
The more modern approach is to pass all your matrices in via custom uniforms and not use the built-ins. So you could pass in the model, view, and projection transforms independently and insert your custom matrix in between Model and View.
Upvotes: 2
Reputation: 3305
Just declare it as uniform mat4 myMatrix before declaring the vert shader's main() function
Upvotes: 0