Archived

This topic is now archived and is closed to further replies.

ph33r

Need help with perspective/projection transformation(screen shot)

Recommended Posts

Hello, I'm working with a friend on our own 3d rasterizing engine and I'm stuck on the transformations. I've got the world transformations finished and I assume a fixed camera position so I don't have to deal with the camera transformation. Though for some reason between perspective and projection I'm not doing something right. You can download the demo at www.coderdave.com/CON3D.zip and see how the program looks so far. Or just look at the screen shot If you don't want to download the demo - I can describe the problem. I have a triangle that has the vertices 10 units each away from the origin. At z = 1 unit back, you would assume that triangle would take up the entire screen but it only appears to take up a small portion. The z scaling is definatly wrong I just have no idea why To run the program -------------------------- 1) Click the upper left corner of the window and go to properties. Change the font size to 8x8 so the ASCII pixels are the same size in width and height. 2) Increase the speed of the rotation and check the Z rotation box. **Do not check any other rotations because each vertex is 10 units in length and if you rotate on the y for example you will go pass the screen because the z coordinate is 1 away from the camera. Clipping is not yet implemented. Now as you may have noticed, the z coordinate of the model is 1 unit away from the camera and each vertex is 10 units away from the origin of the model. Normally, the triangle should fill up the screen having the z value so close to the camera. Unfortunately, this is not the case so somewhere between the perspective transformation and the projection transformation I am messing up the z projection. Here is my how my code works.
// Set the untransformed point to the vertex in the VBuffer

untransPoint = activeVB->untransVBuffer[s].pos;

// Multiply the untransformed point by world matrix to get the

transformedpoint
worldTransPoint = trans_world * untransPoint;

// Multply the world transformed point by projection matrix

transPoint = trans_projection * worldTransPoint;
 
// Set transformed point into the transformed vbuffer

// and normalize the coordinates

activeVB->transVBuffer[s] = activeVB->untransVBuffer[s];
activeVB->transVBuffer[s].pos.X() = transPoint.X() / transPoint.W();
// Normalize


activeVB->transVBuffer[s].pos.Y() = transPoint.Y() / transPoint.W();
// Normalize


activeVB->transVBuffer[s].pos.Z() = transPoint.Z();

// Transform the coordinates to the viewport

int halfWidth = round ( ( viewport.right - viewport.left ) / 2.0f );
int halfHeight = round ( ( viewport.bottom - viewport.top ) / 2.0f );

activeVB->transVBuffer[s].pos.X() += halfWidth;
activeVB->transVBuffer[s].pos.Y() += halfHeight;

// Round the vertices

activeVB->transVBuffer[s].pos.X() = ( float ) round (
activeVB->transVBuffer[s].pos.X() );

activeVB->transVBuffer[s].pos.Y() = ( float ) round (
activeVB->transVBuffer[s].pos.Y() );
My projection matrix in row major form looks like (2n/r-l) 0 0 0 0 (2n/b-t) 0 0 (r+l/r-l)(t+b/b-t)(f/f-n) 1 0 0 -(fn/f-n) 0 Thank you for your time, Dave Neubelt EDIT: Added a screen shot [edited by - ph33r on April 6, 2004 9:25:52 PM]

Share this post


Link to post
Share on other sites
my bet is that you''ve set your field of view to be too large. changing field of view can make zooming effects. try making it a smaller or bigger number and i bet you''ll notice the triangle getting, respectively, bigger and smaller.

-me

Share this post


Link to post
Share on other sites
quote:
Original post by Palidine
my bet is that you''ve set your field of view to be too large. changing field of view can make zooming effects. try making it a smaller or bigger number and i bet you''ll notice the triangle getting, respectively, bigger and smaller.

-me



(2n/r-l) 0 0 0
0 (2n/b-t) 0 0
(r+l/r-l)(t+b/b-t)(f/f-n) 1
0 0 -(fn/f-n) 0


I''m using D3D''s projection matrix where you pass it the height and width of the screen.

In my program I used -.5 to +.5 for the height and width. As you can see, my Model is -10 units to +10 units in width and height. At only 2 z units back the model should be a lot bigger. So I don''t think it''s in how I''m setting up my projection matrix unless I''m missing something fundamental.

- Dave Neubelt

Share this post


Link to post
Share on other sites
I guess my question is this

After you multiply your vertex by your projection matrix and divide by w, should the vertex information fall in the range of [-w/2, w/2] and [-h/2, h/2]?

My projection is way outside that range. If you do the steps with the vertex info of -10,-10,2 with a w of 1 and h of 1 you will get back 10,10 for the transformed vertices.

Something must be wrong.

Share this post


Link to post
Share on other sites
Hi, I really don''t know why there is no 1 in the lower right corner of your matrix - the w-coordinate''s 1 always belongs into a corner in my eyes - but else I would say your projection is perfectly fine. You have exactly 10*1 ascii characters in each direction! If you want it to fill up the screen you would have to multiply the matrix with the screen size. As Palidine said, you have a zoom effect when the window size is not propperly stuffed into the matrix.
I think some good web site explaining matrix calculations and projections would help here. (Really :])

Share this post


Link to post
Share on other sites
I just looked into how I would achieve a projection matrix in OpenGL without the use of helper routines. The 1 in the third row is in deed right. So sorry for the confusion.

Share this post


Link to post
Share on other sites