Matrix4 Issue in Shader

Hello,
I am attempting to use a 4x4 matrix to “switch” coordinates of a vertex that has been passed to the vertex shader (ex. (256,0,256) -> (256,256,0) ). All the vertices I am sending to the vertex shader are in the form (x,0,z). Yet instead of seeing the result I expected (a cube) I see this:

Just random rays. Not even triangles as that is in wireframe.
I have no idea what I am doing wrong. Even when I create the matrix in the shader the same thing happens. Here is the code that creates the matrix:
[java]
Matrix4f rotMat = new Matrix4f();
int t = 0;
if(q.face.equals(QuadMesh.faces.TOP.toString()) || q.face.equals(QuadMesh.faces.BOTTOM.toString())){
t = 1;
rotMat.m00 = 1;
rotMat.m01 = 0;
rotMat.m02 = 0;
rotMat.m03 = 0;

		    		rotMat.m10 = 0;
		    		rotMat.m11 = 0;
		    		rotMat.m12 = 0;
		    		rotMat.m13 = 0;
		    		
		    		rotMat.m20 = 1;
		    		rotMat.m21 = 0;
		    		rotMat.m22 = 0;
		    		rotMat.m23 = 0;
		    		
		    		rotMat.m30 = 0;
		    		rotMat.m31 = 0;
		    		rotMat.m32 = 0;
		    		rotMat.m33 = 1;
		    		}
		    	if(q.face.equals(QuadMesh.faces.FRONT.toString()) || q.face.equals(QuadMesh.faces.BACK.toString())){
		    		t = 2;
		    		rotMat.m00 = 1;
		    		rotMat.m01 = 0;
		    		rotMat.m02 = 0;
		    		rotMat.m03 = 0;
		    		
		    		rotMat.m10 = 1;
		    		rotMat.m11 = 0;
		    		rotMat.m12 = 0;
		    		rotMat.m13 = 0;
		    		
		    		rotMat.m20 = 0;
		    		rotMat.m21 = 0;
		    		rotMat.m22 = 0;
		    		rotMat.m23 = 0;
		    		
		    		rotMat.m30 = 0;
		    		rotMat.m31 = 0;
		    		rotMat.m32 = 0;
		    		rotMat.m33 = 1;
		    		}
		    	if(q.face.equals(QuadMesh.faces.RIGHT.toString()) || q.face.equals(QuadMesh.faces.LEFT.toString())){
		    		t = 3;
		    		rotMat.m00 = 0f;
		    		rotMat.m01 = 0f;
		    		rotMat.m02 = 0f;
		    		rotMat.m03 = 0;
		    		
		    		rotMat.m10 = 1f;
		    		rotMat.m11 = 0f;
		    		rotMat.m12 = 0f;
		    		rotMat.m13 = 0;
		    		
		    		rotMat.m20 = 1f;
		    		rotMat.m21 = 0f;
		    		rotMat.m22 = 0f;
		    		rotMat.m23 = 0;
		    		
		    		rotMat.m30 = 0f;
		    		rotMat.m31 = 0f;
		    		rotMat.m32 = 0f;
		    		rotMat.m33 = 1f;
		    		}

[/java]

The matrix is declared like so in the material file:

[java]
Matrix4 cubeMatrix
[/java]

I then utilize it in the shader:

[java]
vec4 rotatedPos = m_cubeMatrix * inPosition;
vec3 truePosition = (rotatedPos.xyz + m_faceOffset + m_meshOffset ) * m_scale;
[/java]
m_faceOffset just offsets the vertex so it is centered around the origin, m_meshOffset offsets the quad in question from the origin…scale scales the quad to it’s size.
I am at a loss for what is happening here.
Any and all help is extremely appreciated. Thanks.

I don’t get what you are trying to achieve but it looks like there is some misunderstanding on how spaces and matrices works.

If you didn’t read the docs on shaders and video tutorials I recommend doing it
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:shader_video_tutorials

Vertex shader first purpose is to project the model position of a vertex to screen for this it uses the combines worldMatrix (model to world), viewMatrix (world to view/camera), projection (view to projection) known as WorldViewProjection matrix. This matrix depend on the position of the object in world space, camera position and changes each frame. It’s passed to the shader as g_WorldViewProjectionMatrix if you declared WorldViewProjectionMatrix as a world param in your material definition file.

Your matrix, is static and wrong.

If you want a rotated cube…why don’t you just use a cube and rotate it?

@nehon said: I don't get what you are trying to achieve but it looks like there is some misunderstanding on how spaces and matrices works.

If you didn’t read the docs on shaders and video tutorials I recommend doing it
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:jme3_shaders
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:shader_video_tutorials

Vertex shader first purpose is to project the model position of a vertex to screen for this it uses the combines worldMatrix (model to world), viewMatrix (world to view/camera), projection (view to projection) known as WorldViewProjection matrix. This matrix depend on the position of the object in world space, camera position and changes each frame. It’s passed to the shader as g_WorldViewProjectionMatrix if you declared WorldViewProjectionMatrix as a world param in your material definition file.

Your matrix, is static and wrong.

If you want a rotated cube…why don’t you just use a cube and rotate it?


I have one vertex buffer that has an upper left coordinate of 0,0,0 and a lower right corner at (mesh_size,0,mesh_size). I use this vertex buffer for each quad in a quad tree. In order to position each quad to its correct position. If I have a quad that should be on the front face of the cube I wanted to use that matrix to do the following… (256,0256) → (256,256,0) which according to calculations it should. I am passing this matrix to the shader with every quad (they’re each individual meshes)

After I multiply the position vector by the matrix I do a few more modifications to it and then I multiply it by the WorldviewProjectionMatrix…

I do not understand what you mean when you say theyre static and wrong. Shouldn’t one be able to pass a matrix to a shader and multiply them by a vector like any other variable type?

Put GLSL 110 in the MatDef file, this shouldn’t compile properly, as you are multiplying a 4x4 mat with a vec3:

[java]vec4 rotatedPos = m_cubeMatrix * inPosition;[/java]

use m_cubeMatrix * vec4(inPosition, 0.0); if you just want rotation

@wezrule said: Put GLSL 110 in the MatDef file, this shouldn't compile properly, as you are multiplying a 4x4 mat with a vec3:

[java]vec4 rotatedPos = m_cubeMatrix * inPosition;[/java]

use m_cubeMatrix * vec4(inPosition, 0.0); if you just want rotation


That gives the same result. Thanks for the time though.

Just curious… what are those 4x4 matrixes supposed to do exactly?

It seems like you are really only using the 3x3 part and that you are essentially setting scale to 0 along one axis. Actually, reading again they are all invalid from a standard 4x4 rotation matrix… so you will get very strange results. For example: in the first case the x and z axes of the new reference frame are folded together. You are essentially projecting all geometry along the x axis.

All of them seem to be just folding different aspects of x,y,z down a single x value since every column of the rotation part is a unit x vector.

@pspeed said: Just curious... what are those 4x4 matrixes supposed to do exactly?

It seems like you are really only using the 3x3 part and that you are essentially setting scale to 0 along one axis. Actually, reading again they are all invalid from a standard 4x4 rotation matrix… so you will get very strange results. For example: in the first case the x and z axes of the new reference frame are folded together. You are essentially projecting all geometry along the x axis.

All of them seem to be just folding different aspects of x,y,z down a single x value since every column of the rotation part is a unit x vector.


all vertices that are being sent to the shader have the the format (x, 0, z). So if it is determined that a quad should be on the front face of the cube then the following matrix will be chosen:

|1, 0, 0, 0|
|1, 0, 0, 0|
|0, 0, 0, 0|
|0, 0, 0, 1|

Multiplying this by the vector (1024, 0, 1024, 1) should result as follows:

|1, 0, 0, 0| |1024 |
|1, 0, 0, 0| |0 | x = (1 * 1024) + (0 * 0) + (0 * 1024) + (0 * 1) = 1024
|0, 0, 0, 0| |1024 | y = (1 * 1024) + (0 * 0) + (0 * 1024) + (0 * 1) = 1024
|0, 0, 0, 1| |1 | z = (0 * 1024) + (0 * 0) + (0 * 1024) + (0 * 1) = 0
w = (0 * 1024) + (0 * 0) + (0 * 1024) + (1 * 1) = 1
== (1024, 1024, 0, 1)
And that is the purpose of the matrices.
Should this not be the result?

Have you tried it with a matrix on the Java side so that you can print the values to see if they are correct?

One of us may be confusing column major and row major… but I know what a rotation matrix should look like and the output and to me it yours looks grossly degenerate.

Also, assuming your test works, try multiplying by (1024, 0, 512, 1)… I think you might be surprised to get 1024, 1024 again… because as constructed, only the x value is being used. I suspect that is not your intention.

@pspeed said: Have you tried it with a matrix on the Java side so that you can print the values to see if they are correct?

One of us may be confusing column major and row major… but I know what a rotation matrix should look like and the output and to me it yours looks grossly degenerate.

Also, assuming your test works, try multiplying by (1024, 0, 512, 1)… I think you might be surprised to get 1024, 1024 again… because as constructed, only the x value is being used. I suspect that is not your intention.


Yea I think you’re right. I changed it to fix that. Thanks for your help.

1 Like