Perspective Projection
Moderator: Coders of Rage
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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?
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
I've tried multiple projection matrices. For example, what is wrong with this matrix, near=-0.1 far=-1;
I've typed out the division just to make sure I don't make any calculation errors.
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};
Re: Perspective Projection
Near and far should be positive values.
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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.
Initially I had thought the near and far values would be negative because of the negative z axis into the distance.
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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.
Re: Perspective Projection
What are your triangle coordinates? What is your camera position?
- dandymcgee
- 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
As K-Bal suggests, we really don't know enough about your coordinate system to be of much help.K-Bal wrote:What are your triangle coordinates? What is your camera position?
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!
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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.
- dandymcgee
- 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
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.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.
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!
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
All right, thank you.
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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.
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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
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
Re: Perspective Projection
Welcome to the world of 3D graphics programming
- dandymcgee
- 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
Different source materials (books, articles, etc.) use different "majorness" in their documentation.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?
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
};
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
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
};
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!
-
- ES Beta Backer
- Posts: 250
- Joined: Tue Jul 19, 2011 9:37 pm
Re: Perspective Projection
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?
That is to say, the final matrix sent to the shader needs to be the transpose of the one you gave me. But why?