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

Senior Devvie


Medals: 6
Projects: 2
Exp: 3 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?
Offline 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?
Offline 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);

And then in the shader:
1  
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Hermasetas

Senior Devvie


Medals: 6
Projects: 2
Exp: 3 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) Tongue
Offline 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) 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.
Offline 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))
Offline 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"
Offline 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?
Offline Hermasetas

Senior Devvie


Medals: 6
Projects: 2
Exp: 3 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 Smiley

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

Thanks Troubleshoots, but I knew them already Smiley
Maybe someone who doesn't know it can find it here in the future then Cheesy
Offline 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  
 
 

 
Ecumene (149 views)
2017-09-30 02:57:34

theagentd (214 views)
2017-09-26 18:23:31

cybrmynd (301 views)
2017-08-02 12:28:51

cybrmynd (288 views)
2017-08-02 12:19:43

cybrmynd (297 views)
2017-08-02 12:18:09

Sralse (291 views)
2017-07-25 17:13:48

Archive (968 views)
2017-04-27 17:45:51

buddyBro (1094 views)
2017-04-05 03:38:00

CopyableCougar4 (1670 views)
2017-03-24 15:39:42

theagentd (1430 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!