Advertisement

fake math works, need real math (2D point -> 3D point)

Started by October 13, 2002 04:12 PM
2 comments, last by KalvinB 22 years, 4 months ago
Given the view angle, distance from camera, 2D point (where 0,0 is the top left and xres,yres is the bottom right) and the desired 3D Z location, how do I calculate the final 3D x,y position where 0,0 is the center of the screen and the abs of the distance from the 0 to the edge of the screen is unknown in both the X and Y directions? I can figure it out pretty quickly using fake math and by matching mouse movement with the pixel movement but I'd like to have a generic function that will calculate that distance for any Z value. Ben IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Tiberian Merchandise!!!! | GameShot ] [edited by - KalvinB on October 13, 2002 5:14:09 PM]

      GLvoid oglClass::SetTranslate2D(float x, float y, float z){	glTranslatef(x*4.0f/(float)screenW-1.00f,1.00f-y*3.0f/(float)screenH,-3.63f);}      


For z=-3.63 all those values work out perfectly.

How do I get 4.0f and 1.00f in the first place based on the z value? It just happens that at -3.63, 1 unit equals 256 pixels.

So I guess the equation I need is how many pixels per unit given the aspect ratio, distance from screen and z value. And how do I use that value to calculate the 4.0f and 1.00f?

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Tiberian Merchandise!!!! | GameShot ]


[edited by - KalvinB on October 13, 2002 5:50:37 PM]
Advertisement
You''ll want to use gluUnProject. It takes in the view/camera matrices, the screen (x,y,z), and gives you back the world-space (x,y,z).

Pretty much it just inverses the two matrices and puts the screen coordinates through them, so if you need to do a lot of these, you''ll probably be better off inversing the matrices yourself and caching them...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.


Actually I was hoping for something more generic and not a prewritten function. I don't want an othogonal view. I just want to reference Unit values using pixel values.


    //after fixing the OGL feature of drawing up instead of down like DX this came out correctlyglTranslatef(x*4.0f/(float)screenW-2.0f,1.5f-y*3.0f/(float)screenH,-3.63f);  


From that you can do


  float aspectRatio=yResolution/xResolution;float xRatio=4.0f;float zValue=-3.63f;glTranslatef(x*xRatio/xResolution-xRatio/2.0f,xRatio*aspectRatio/2.0f-y*xRatio*aspectRatio/yResolution,zValue);    


Where X and Y are the pixel coordinates.

But what I need to know is the significance of the value calculated by xRatio/zValue = -1.10193.

By using that ratio I can then pass any zValue and adjust the xRatio accordingly.

I assume that ratio is based off of the aspect ratio (4/3), the field of view(45 degrees) and distance to screen (100.0f) but I'm not sure what the function for that is.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Tiberian Merchandise!!!! | GameShot ]


[edited by - KalvinB on October 14, 2002 9:05:11 PM]

This topic is closed to new replies.

Advertisement