Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (769) Games in Android Showcase (230) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Matrix not working in shader  (Read 2074 times) 0 Members and 1 Guest are viewing this topic.
Hermasetas

Senior Devvie

Medals: 6
Projects: 2
Exp: 7 years

I do gamez, yes!

 « Posted 2014-03-14 14:35:12 »

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:
 1  2  3  4 `mat4 m = mat4(0.01);vec4 p = vec4(position, 0.0, 1.0);   gl_Position = m * p;`

Has the same effect as this code:
 1  2  3 `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?
Danny02
 « Reply #1 - Posted 2014-03-14 14:40:50 »

What do you expect happens when you multiply with a 4x4 matrix where all values are 0.1?
Kefwar
 « Reply #2 - Posted 2014-03-14 14:46:24 »

Another way to do this is setting the projectionview matrix using OpenGL.
 1 `glMultMatrix(matrix);`

 1 `gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;`

Hermasetas

Senior Devvie

Medals: 6
Projects: 2
Exp: 7 years

I do gamez, yes!

 « Reply #3 - Posted 2014-03-14 14:51:58 »

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)
trollwarrior1
 « Reply #4 - Posted 2014-03-14 15:01:21 »

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)

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.
Danny02
 « Reply #5 - Posted 2014-03-14 16:57:31 »

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
 `mat4(mat3(0.1))`
The Lion King
 « Reply #6 - Posted 2014-03-14 17:46:47 »

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
 `mat4(mat3(0.1))`

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

"You have to want it more than you want to breath, then you will be successful"
Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

 « Reply #7 - Posted 2014-03-14 21:48:25 »

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.

Why are all OpenGL tutorials written in Brainf**k?
Hermasetas

Senior Devvie

Medals: 6
Projects: 2
Exp: 7 years

I do gamez, yes!

 « Reply #8 - Posted 2014-03-15 02:14:37 »

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

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

Thanks Troubleshoots, but I knew them already
Maybe someone who doesn't know it can find it here in the future then
Danny02
 « Reply #9 - Posted 2014-03-15 07:30:31 »

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

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.
OpenGL wiki
Pages: [1]
 ignore  |  Print

 EgonOlsen (1668 views) 2018-06-10 19:43:48 EgonOlsen (1705 views) 2018-06-10 19:43:44 EgonOlsen (1155 views) 2018-06-10 19:43:20 DesertCoockie (1583 views) 2018-05-13 18:23:11 nelsongames (1183 views) 2018-04-24 18:15:36 nelsongames (1707 views) 2018-04-24 18:14:32 ivj94 (2531 views) 2018-03-24 14:47:39 ivj94 (1756 views) 2018-03-24 14:46:31 ivj94 (2833 views) 2018-03-24 14:43:53 Solater (969 views) 2018-03-17 05:04:08
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org