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


World to Screen Math and Translate/Rotation Transforms

Recommended Posts

Hello This is sort-of a two question post. For a while now I''ve been trying to work out how to get a 3d coordinate in space to translate into a 2D screen. I found a topic in this forum where it was answered. I also found a few places on the net about it which included perspective in the caclulations. I''m probably wrong on a few things I say here so please correct me! Anyway at the moment I reset the modelview matrix and do a glTranslatef(0,0,-10) then draw a 3d tileset along the Z axis (so it''s facing the screen). The tiles look like this: for (x = 0; x < w; x++) { for (y = 0; y < h; y++) { glBegin(...) glVertex3f(x,y,0); // .. rest of the tile glEnd() } } To get the 2D screen location of each tile I do this: // 45 is my FOV (run by gluPerspective(45.0f, 1, 1, 100)) // -- but I dont think that''s right... // sw/sh: screen width/height // wx/wy/wz world co-ords // // i get wz because of the translate. wz = 10. // wx, wy is the same as veretx3f(wx,wy,0) when drawing the tile. sx = sw * tan(45.0f / (PI * 2)) * ( wx / wz ) + sw / 2 sy = sh * tan(45.0f / (PI * 2)) * ( wy / wz ) + sw / 2 This works perfectly. sx/sy lines up fine. tan(45.0f / PI2) comes out to be 1.206680. Which is odd because on calc tan 45 = 1 (45 /PI*2 is the radian equiv to 45deg''s shouldn''t it??)... I also still dont think 45 relates to my fov since i change it slightly and the result is completely off. So far so good (almost). The thing that''s completely throwing me is if I do a glTranslatef(0,0,-10) then I do a glRotatef(-30, 1, 0, 0), I have no idea on where my 3D points are. They get drawn in the place I expect -- it looks like a typical RTS layout where the lower tiles are bigger and look closer then the higher drawn ones. Should I work out the position of each tile without doing a glRotatef (and translatef)? My trig maths isn''t too bad, but I have no clue on how to do that... Your help is appreciated! Thanks, Gerald

Share this post

Link to post
Share on other sites
Ah hah!

The second part of my question isn''t as hard as it seems.

Yes, I don''t have to use glRotatef. Instead for each vertex I want to rotate on the X axis, I do:

// a being the angle
// px/py/pz being the vertex
py = x
py = cos(a) * y
pz = sin(a) * y

It''s just a simple 2D circle when you look down the X axis!

I still don''t fully understand how the tangent relates to the distance/fov. I''ll post here if I find out


Share this post

Link to post
Share on other sites