Q The First Timelord
Q The First Timelord

Reputation: 79

Complex OpenGL 3D Translation from Viewport Math Related Issue

I have an OpenGL custom control I've created, language neutral for now, that can be ANY reasonable size, for example the width can be from 1 to the screen width, and the height can be from 1 to the screen height.....

So when I draw into this control from my application, to make life easier for me working between 2D and 3D space I've set the upper left hand corner as x=0,y=0, and z=0, where the 'eye' is at x=width/2 and y=height/2 and z=0.

So what I'm trying to do is map 1 pixel for every OpenGL unit. So let's say I resize this control on my designer to width = 150 pixels and height = 150 pixels, I know that if I execute a glVertex3f command to draw a square precisely 10 pixels from the corner, that the control will handle setting the z depth based on my perspective (currently set to 45 degrees) situating the x and y within the GL environment so where 0,0 is the upper left and the width in GL units of the onscreen area I can see where I am drawing is the same exact as the width (150) and height (150) of my control.

For example. With a 150 height and 300 width control, I know through monkey key presses that the perfect z depth is 180. But I can't seem to figure out the equations to correlate my x and y height and width to the perspective and how to correlate that to the z depth.

And no, I'm not interested in orthogonal view, I want my controls to look 3d on the 2d screen.

AS to why I'm going through all this effort: This is a custom control I'll be using myself for 3D User Interface screen development. It's easier for me to work in pixels when drawing 3d primitives since most of the other screen controls (ie: grid views, drop downs, etc) alongside custom drawn 3D UI elements I'll be using this for will be in pixels as well, so tucking the math in the control of the translation makes the most sense. That and it's SO much easier responding to mouse commands this way.

What, precisely I am looking for:

The Math stuff. For any KNOWN WIDTH, HEIGHT AND PERSPECTIVE ANGLE 'A', What mathematically is the optimal 'Z'?

Upvotes: 0

Views: 90

Answers (2)

HolyBlackCat
HolyBlackCat

Reputation: 96699

I'm not entirely sure if I understand your question correctly, but it looks like a simple geometric problem to me.

fancy pic

(pardon my paint skills -.-)

Then it's as easy as distance = height / 2 / tan(fov_angle / 2).

Note that viewport width doesn't affect the result, since what's usually called 'FoV angle' is actually a 'vertical FoV angle'.

Upvotes: 1

Ripi2
Ripi2

Reputation: 7198

What you are asking for is how to take a 2D point and find out its original 3D coordinates. This is not possible. It lacks information.

You need the z component, apart from x,y obtained by undoing viewport transformation. You can get it from the depth buffer.

Once you have projected x,y,z coordinates, then just multiply this vector by the inverse of the projection matrix.

Upvotes: 0

Related Questions