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 »

I've multiplied by each matrix separately to ensure that the order of multiplication is correct. I've tried transposing them and still it seems the perspective matrix is not working. Does it work with any near and far value?
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

I've tried multiple projection matrices. For example, what is wrong with this matrix, near=-0.1 far=-1;

Code: Select all

GLfloat projectionMatrix[]={-0.1/250.0,    0.0,   0.0,  0.0,
                                          0.0,     -0.1/250.0,    0.0,  0.0,
                                          0.0,  0.0,        1.1/-0.9, -0.2/-0.9,
                                          0.0,  0.0,         -1.0,           0.0};
I've typed out the division just to make sure I don't make any calculation errors.
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 »

Near and far should be positive values.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

OK, that's what I was thinking after I read the page he linked to.
Initially I had thought the near and far values would be negative because of the negative z axis into the distance.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

Still NOTHING with the projection matrix. Fine if I just put the triangle on the screen, but the moment I add that matrix nothing is there. Not out of proportion, just not there.
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 »

What are your triangle coordinates? What is your camera position?
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 »

K-Bal wrote:What are your triangle coordinates? What is your camera position?
As K-Bal suggests, we really don't know enough about your coordinate system to be of much help.

You should probably consider drawing (on paper) your coordinate system axes, your approximate camera frustum and your triangle's vertices to do a sanity check. If nothing is showing up and you've verified that you didn't make a weird mistake in your matrices, then your triangle is likely being culled because it is not inside the viewing frustum. This could also happen if the vertices are clockwise-ordered and you have back-face culling enabled, or you made a sign error and the camera is pointed the opposite direction. There are a lot of possibilities.

If you want to post the full code we can debug it locally and let you know what we find.
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 »

So if the near and far are positive with this projection matrix, I presume I should place my triangle on a positive location on the z axis? (which is odd because the camera SHOULD be looking in the negative direction. This is a bit odd.) But when I do this and I run through it on paper, I end up dividing by w and getting a final positive number for Z, which obviously shouldn't show. But if I place a vertex in the negative location, then it is outside of the positive area that is supposed to show.
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:So if the near and far are positive with this projection matrix, I presume I should place my triangle on a positive location on the z axis? (which is odd because the camera SHOULD be looking in the negative direction. This is a bit odd.) But when I do this and I run through it on paper, I end up dividing by w and getting a final positive number for Z, which obviously shouldn't show. But if I place a vertex in the negative location, then it is outside of the positive area that is supposed to show.
You can set near and far to whatever you want. If your coordinate system is Z- into the distance the viewer, then make near e.g. -1.0f and far e.g. -100.0f or something. If Z+ is into the distance, use 1.0f and 100.0f (for example). It might help to implement some controls which let you move the camera around, or at the very least look around. You can also use something like RenderDoc (https://renderdoc.org/builds#) to intercept the vertices being sent to the GPU and figure out exactly where the triangle is in space.

OpenGL lets you define whatever coordinate system you want. While there are some common practices, nothing is strictly enforced. This is both a very powerful feature and a total debugging nightmare. You need to do the work yourself of making sure you maintain consistency throughout your application (backface culling, depth testing, camera transforms, object transforms, texture coordinates, perspective calculations, etc.).

At this point, I'm going to stop guessing. Feel free to post your code if you still can't figure it out; otherwise, good luck!
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 »

All right, thank you.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

I'm just going to spend some more time fooling around with it on my own. I CAN say that if I just use a translation matrix to place the triangle, it shears instead of translating, and yet setting the transpose to true makes it show nothing at all. I'll spend some time tonight working on this, and if I don't get anywhere at all I will show the code.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective Projection

Post by Benjamin100 »

Well, I figured it out.

A.) I used an equation involving field of view that I found in my book.

B) I needed to transpose the model matrix, and yet not transpose the projection matrix... Even though both were made using arrays in what looks to me like the same form.
Does that even sort of make sense?

Well that was the problem.

Thanks for the help
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 »

Welcome to the world of 3D graphics programming :lol:
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: B) I needed to transpose the model matrix, and yet not transpose the projection matrix... Even though both were made using arrays in what looks to me like the same form.
Does that even sort of make sense?
Different source materials (books, articles, etc.) use different "majorness" in their documentation.
Also, when you define a matrix as a one-dimensional array in C/++, you're may be transposing it without realizing it.

For example, consider this implementation of the translation matrix (translating by X=2, Y=3, Z=4):

Code: Select all

GLfloat matrix[16] = {
    1.0f, 0.0f, 0.0f, 2.0f,
    0.0f, 1.0f, 0.0f, 3.0f,
    0.0f, 0.0f, 1.0f, 4.0f,
    0.0f, 0.0f, 0.0f, 1.0f
};
If you load this via glUniform4f into your shader (with transpose = GL_FALSE) and use it to translate your vertices, you will get incorrect results.

The matrix will be interpreted as:

Code: Select all

1 0 0 0
0 1 0 0
0 0 1 0
2 3 4 1
So you can either define it like this:

Code: Select all

GLfloat matrix[16] = {
    1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    2.0f, 3.0f, 4.0f, 1.0f
};
Or call glUniform with transpose = GL_TRUE.

Most math libraries will abstract this slightly by wrapping matrix creation code in a function that just does the transposition for you. For instance, the "graphene_matrix_init_translate" function in the graphene math library builds a translation matrix like this:
graphene_matrix_t *
graphene_matrix_init_translate (graphene_matrix_t        *m,
                                const graphene_point3d_t *p)
{
  m->value =
    graphene_simd4x4f_init (graphene_simd4f_init (1.0f, 0.0f, 0.0f, 0.0f),
                            graphene_simd4f_init (0.0f, 1.0f, 0.0f, 0.0f),
                            graphene_simd4f_init (0.0f, 0.0f, 1.0f, 0.0f),
                            graphene_simd4f_init (p->x, p->y, p->z, 1.0f));

  return m;
}
https://github.com/ebassi/graphene/blob ... rix.c#L361
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 »

I did transpose the translation matrix. The thing is, the projection matrix I wrote in the array, to look like the matrix you gave earlier, (which as you showed would actually make the transpose of the matrix.) But when I transpose THAT matrix it doesn't work.
That is to say, the final matrix sent to the shader needs to be the transpose of the one you gave me. But why?
Post Reply