Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  [OpenGL] Having trouble with matrices [FIXED]  (Read 159 times)
0 Members and 1 Guest are viewing this topic.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Posted 2014-01-23 18:23:43 »

So I've been working on a couple of simple camera classes to try and improve my understanding of the matrix transformations, however my square isn't rendering in view. It's definitely a problem with the matrices, because when I don't do the matrix multiplication in the shader program, it renders as expected in NDC space. This is just orthographic projection by the way.

This is my resize method, which sets the projection matrix (super.resize simply calls glViewport):
1  
2  
3  
4  
5  
6  
public void resize(int x, int y, int width, int height) {
   super.resize(x, y, width, height);
   projection.overwrite(new float[] { 2f / width, 0f, 0f,
         (x * 2 + width) / width, 0f, 2f / height, 0f,
         (y * 2 + height) / height, 0f, 0f, 2f, 1f, 0f, 0f, 0f, 1f });
}

I get the combined projection and view matrices with this method; note that the scale, rotation and translation matrices are all identity matrices by default:
1  
2  
3  
4  
public Matrix4 update() {
   return projection.copy().multiply(scale).multiply(rotation)
         .multiply(translation).transpose(); // am I correct in thinking I should transpose the resulting matrix? I prefer to handle matrix operations row major, however since GLSL uses column major by default I thought I should be transposing it. I tried both transposing and leaving it as it is, neither time with success.
}

I'm thinking that it might be a problem with my order of multiplication, however I followed the order it states on this page.

Quote
Matrix math is not commutative: S*R is not the same as R*S. However, it is associative: (S*R)*T is the same as S*(R*T). The usual grouping for vertex transformation is this: Final = C*(S*(R*(T*position))). But this can easily be regrouped as: Final = (((C*S)*R)*T)*position.

Here's my vertex shader, nothing special:
1  
2  
3  
4  
5  
6  
7  
8  
#version 150

in vec2 position;
uniform mat4 mvp;

void main() {
   gl_Position = mvp * vec4(position, 0.0, 1.0);
}


What am I doing wrong?

Why are all OpenGL tutorials written in Brainf**k?
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #1 - Posted 2014-01-24 17:23:15 »

I decided to do some pencil and papering and I realised that I forgot to make some of the values negative. Just in case anyone else has a problem with the orthographic perspective matrix, here's the correct values.

1  
2  
3  
projection.overwrite(new float[] { 2f / width, 0f, 0f,
         -((x * 2f + width) / width), 0f, 2f / height, 0f,
         -((y * 2f + height) / height), 0f, 0f, -2f, 1f, 0f, 0f, 0f, 1f });


Note that this is row-major. You need to transpose it before sending it to the shader.

Why are all OpenGL tutorials written in Brainf**k?
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.

CogWheelz (8 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!