Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Orthographic Matrix Issue  (Read 996 times)
0 Members and 1 Guest are viewing this topic.
Offline Troncoso

JGO Coder


Medals: 20



« Posted 2013-09-07 16:41:30 »

My orthographic matrix doesn't seem to be properly translating/scaling the pixels on my screen.
Here is how it is built:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
   public static Matrix4f orthographicMatrix(float near, float far)
   {
      float width = Display.getWidth();
      float height = Display.getHeight();
     
      return orthographicMatrix(width, height, near, far);
   }
   
   public static Matrix4f orthographicMatrix(float width, float height, float near, float far)
   {
      return orthographicMatrix(0, width, height, 0, near, far);
   }
   
   public static Matrix4f orthographicMatrix(float left, float right, float bottom, float top, float near, float far)
   {
      Matrix4f mat = new Matrix4f();
     
      mat.m00 = 2 / (right - left);
      mat.m11 = 2 / (top - bottom);
      mat.m22 = -2 / (far - near);
     
      mat.m03 = -1 * ((right + left) / (right - left));
      mat.m13 = -1 * ((top + bottom) / (top - bottom));
      mat.m23 = -1 * ((far + near)   / (far - near));
     
      return mat;
   }


Here's where I create a new one:

1  
ortho = Matrix4f.orthographicMatrix(1, -1);


Here's how it's sent to the vertex shader:

1  
program.setUniformMatrix4("ortho", ortho);


And here's the vertex shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
# version 330

uniform mat4 ortho;

in vec3 vert;

void main()
{
   gl_Position = ortho * vec4(vert, 1);
}


When I pass in 2 vectors (0, 0, 0) and (100f, 100f, 0) and draw them as points, this is what I get:



The screen size for that is 800x600. It seems that the x and y directions reversed as intended, but the origin is still in the center. I can't find what it is that I missed though for that too happen.
The calculated matrix is the same as one that I calculated by hand. So, I feel that I have the wrong expressions to compute the orthographic matrix. Though, I've verified from several locations that this is how you calculate it.
Offline quew8

JGO Coder


Medals: 33



« Reply #1 - Posted 2013-09-07 19:49:26 »

It's hard to say considering you haven't shown us the setUniformMatrix4() method of your ShaderProgram class? (whatever type program is). My guess is that you tell it to load the transpose of the matrix when you shouldn't or vice versa.
Offline Troncoso

JGO Coder


Medals: 20



« Reply #2 - Posted 2013-09-07 20:21:01 »

Ahh. Well, it turns out I'm suppose to use the transpose? I don't know why. I set it to true, and that fixed the issue. I thought that should have been false, but oh well. Thanks for the suggestion.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 33



« Reply #3 - Posted 2013-09-07 20:52:42 »

That is very odd. Provided you're using LWJGL Matrix4f.load() (as opposed to loadTranspose() or your own function) it should be set to false. In GLSL if you get the order of the matrix/vector multiplication wrong (ie vector * matrix instead of matrix * vector) then it is the equivalent of multiplying by the transpose of the matrix. (That's a mathematical thing not just GLSL) But I'm certain that the order you have posted as using here is the right one.

So in conclusion I have no idea.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!