Matrix not working in shader

I have a shader working with textures and all that but now I would like to add a projectview-matrix to it.
But here I run into a weird problem: The shader simply ignores the matrix multiplication!

I played around with different matricies but nothing seemed to have an effect.

Simply this code:


mat4 m = mat4(0.01);
vec4 p = vec4(position, 0.0, 1.0);
	
gl_Position = m * p;

Has the same effect as this code:


vec4 p = vec4(position, 0.0, 1.0);
	
gl_Position = p;

As I said I tried different values for the matrix but nothing happened…
Is it not possible to initiate a matrix inside glsl?

What do you expect happens when you multiply with a 4x4 matrix where all values are 0.1?

Another way to do this is setting the projectionview matrix using OpenGL.


glMultMatrix(matrix);

And then in the shader:


gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

Well only the diagonal values should be 0.1 so I expected it to scale the vertecies down but then the 4,4 value in the matrix should be 1.

I got it working now but it is funny to see that if a point is (5, 4, 0, 0.1) then gl draws it like (50, 40, 0, 1) :stuck_out_tongue:

gl doesn’t draw based on pixel values. It draws based on window size. If you try drawing something with no projection matrix setup, the origin will be the center of the window. Positive x-axis right, positive y-axis up. Point with of coordinates (0, 0) would be right at the center of the window. Point (1,1) would be the most upper right pixel of the screen. Point (-1,-1) would be most bottom left pixel. Basically, gl understands everything with float values 1f being equal half of the window size.

This does mean that if your window size is say 500,100 that if you tell opengl to render a square at 0,0 of coordinates 1,1 it would render a square which would take up 250 pixels horizontally and only 50 pixels vertically.

You need to include window width/height ratio in your projection matrix to make opengl work properly.

about the (4,5,0,0.1) => (40,50,0), this is because OpenGL devides xyz by w => 5 / 0.1 = 50

and sry I didn’t know the exact behavior of the GLSL matrix constructors, it seems that if you whish do create a 4x4 matrix with 0.1, 0.1, 0.1, 1 on the diagonal you can do this [icode]mat4(mat3(0.1))[/icode]

this would be

0.1 0 0 0
0 0.1 0 0
0 0 0.1 0
0 0 0 0

you would have to set the matrix[3][3] to 1

It’s actually faster to create a FloatBuffer containing the matrix and then passing the matrix to the program as a uniform value. This is because shaders are run for every vertex/fragment, meaning that if you recalculate the matrix in the shader, then it’s recalculated for every vertex/fragment. This is the same reason that matrix multiplication is better to be done on the CPU.

Oh and by the way, this is the orthographic projection matrix:


left, right, etc.

The translation, scale and z-Axis rotation matrices are easy to find if you don’t know them already.

I was just testing it out directly in the shader to see if it was working, and I am going to implement as a uniform now :slight_smile:

But it’s kind of funny that xyz is divided by w, I have never seen that anywhere before. But good to know :stuck_out_tongue:
Might that be an easy way of scaling instead of scaling on the cpu?

Thanks Troubleshoots, but I knew them already :slight_smile:
Maybe someone who doesn’t know it can find it here in the future then :smiley:

no:

[quote]Matrices can be constructed from other matrices as well. A matrix can only be constructed from a single other matrix. The column and row values from the input matrix are copied to their corresponding values in the output; any values of the output not filled in are filled with the identity matrix.
[/quote]
OpenGL wiki