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

Re: Perspective Projection

Postby Benjamin100 on Thu Jul 07, 2016 3:54 pm

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.
Benjamin100
ES Beta Backer
ES Beta Backer
 
Posts: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Perspective Projection

Postby dandymcgee on Thu Jul 07, 2016 4:18 pm

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-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Perspective Projection

Postby Benjamin100 on Thu Jul 07, 2016 9:38 pm

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: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Perspective Projection

Postby Benjamin100 on Thu Jul 07, 2016 9:46 pm

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.
Benjamin100
ES Beta Backer
ES Beta Backer
 
Posts: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Perspective Projection

Postby dandymcgee on Fri Jul 08, 2016 11:26 am

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:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Perspective Projection

Postby Benjamin100 on Sat Jul 30, 2016 9:46 pm

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.
Benjamin100
ES Beta Backer
ES Beta Backer
 
Posts: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Perspective Projection

Postby K-Bal on Sun Jul 31, 2016 4:14 pm

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/
User avatar
K-Bal
ES Beta Backer
ES Beta Backer
 
Posts: 719
Joined: Sun Mar 15, 2009 4:21 pm
Location: Germany, Aachen

Re: Perspective Projection

Postby Benjamin100 on Fri Aug 05, 2016 1:12 am

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.
Benjamin100
ES Beta Backer
ES Beta Backer
 
Posts: 255
Joined: Tue Jul 19, 2011 10:37 pm

Re: Perspective Projection

Postby short on Sat Aug 06, 2016 12:29 am

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
User avatar
short
ES Beta Backer
ES Beta Backer
 
Posts: 569
Joined: Thu Apr 30, 2009 3:22 am
Location: Oregon, US

Previous

Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron