Perspective Projection

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

Code: Select all

GLfloat projectionMatrix[]={(1/0.414),    0.0,   0.0,  0.0,
                             0.0,     (1/0.414),    0.0,  0.0,
                             0.0,  0.0,        (101/-99), (-200/-99),
                             0.0,  0.0,         -1.0,           0.0};
To be in the correct form in the shader, this SHOULD need to be transposed ALSO. But it only works if I DON'T transpose it.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Perspective Projection

Post by dandymcgee »

Benjamin100 wrote:

Code: Select all

GLfloat projectionMatrix[]={(1/0.414),    0.0,   0.0,  0.0,
                             0.0,     (1/0.414),    0.0,  0.0,
                             0.0,  0.0,        (101/-99), (-200/-99),
                             0.0,  0.0,         -1.0,           0.0};
To be in the correct form in the shader, this SHOULD need to be transposed ALSO. But it only works if I DON'T transpose it.
Because, the article I sourced that matrix from was an OpenGL article, and it was using column-major order to show matrices. As such, it is already transposed. Transposing it again puts it into row-major order.

The first few paragraphs of this article reiterate the point I'm making. It also has a ton of other useful information if you care:
http://www.scratchapixel.com/lessons/3d ... ion-matrix

In summary, OpenGL programmers write matrices in column-major order, and mathematicians write them in row-major order. So if you're reading an OpenGL book, don't transpose them. If you're reading a Wikipedia article, you'll probably have to transpose them. As stated previously, the reason for this has to do with how you declare arrays in C++ and OpenGL's assumption that the 2nd element in the array is second row, first column (column-major) rather than first row, second column (row-major).

It could have happened either way, it's just bad luck and ignorance that we're stuck with differing norms than the mathematicians in this case. All the more reason to use a math library, or write some wrappers yourself to abstract this nonsense away from your daily work.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

But I didn't put it in column major order. I put it in an array, which should transpose it. Then it would need transposed again to put it back in column major order.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

BOTH my translation and projection matrices are written in arrays. Those arrays are made in such a way that they would create row major matrices.
They both should need to be transposed to make the column major matrices that the arrays look like. If I transpose the translation matrix, it works. If I transpose the projection matrix, (making IT column form,) it does NOT work. They are both created using arrays. They are both made to APPEAR column major even though they need to be transposed to be columns. But when I transpose one it works, and the other only works when not transposed.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Perspective Projection

Post by dandymcgee »

Benjamin100 wrote:BOTH my translation and projection matrices are written in arrays. Those arrays are made in such a way that they would create row major matrices.
They both should need to be transposed to make the column major matrices that the arrays look like. If I transpose the translation matrix, it works. If I transpose the projection matrix, (making IT column form,) it does NOT work. They are both created using arrays. They are both made to APPEAR column major even though they need to be transposed to be columns. But when I transpose one it works, and the other only works when not transposed.
In any case, you said it's working, right? I'd stop caring about it for now and come back later with a clear mind. Focusing on something like this for too long will drive you insane; if it works, just go with it for now. There are bigger fish. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

Well the matrices are working much better now, but I am absolutely sick of working on this obj file reading. It is just a pain needing to put everything in order, and making sure I set the right normal for each vertex and everything. Maybe I should just use a library. The thing I don't like about using somebody else's library is that they have control over it, and I need to learn their way of putting things. I don't know.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Perspective Projection

Post by K-Bal »

Use a library, it's not worth the hassle. Object files are also pretty limited and you will find yourself implementing import code for several formats.

I know that libgdx uses this import library: http://www.assimp.org/
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

I figured that I'd be using other files anyways since I can't imagine this format being very helpful for artists.
No use in trying to solve problems that have already been solved numerous times. After all, I want to do do graphics programming, not graphics file programming.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Perspective Projection

Post by short »

Benjamin100 wrote:After all, I want to do do graphics programming, not graphics file programming.
This made me chuckle.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply