Translating virtual space into real-world space in OpenGL

Started by
5 comments, last by Safe_haven 22 years ago
I realize this is a rather obnoxious question, but given my absolute inability to find a way to solve it I need to ask... I''m designing a vision test program in which I''m using OpenGL to simulate a virtual space ''inside'' the monitor. For the purpose of post-test calculations, I need a way to get a sense of how large an object or how far it moves in a real-world units as opposed to the virtual ones. In other words, I translate a square on my 19" monitor -10.0f into the screen...how many inches is that? I''m assuming the question will deal with the appropriate frustrum settings as well as having some idea as to the exact algorithm OpenGL uses in its perspective calculations. I''m currently using a 45 degree view plane with a range of 0.1f to 100f depth on my view plane gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f) In terms of finding the solution to this problem, what do I do next? Thanks in advance for any help....
Advertisement
You''re supposed to define the scale yourself.

Just say that 1 inch = 1 GL unit, 1 foot = 1 GL unit, whatever.
I think Safe_haven realizes that its necessary to properly define the scale in the view frustum, and part of the question is HOW to choose the CORRECT scale so that objects appear at the correct real-world size within the volume of a 19" monitor. (e.g., virtual objects located in Z at the screen position are real-life sized.)

OpenGL calculates the view frustum in the standard way, as documented in Foley et al.''s book "Computer Graphics: Principles and Practice" and in the OpenGL red book (standard Programmer''s Guide).

You may not want to use gluPerspective, since it disguises the actual details of creating the frustum. Use glFrustum() instead. If you are using Visual C++, the help on glFrustum() gives some detail, but unfortunately no image.

The width and height of the view frustum actually represent the dimensions of the near clip plane, which (!) happens to correspond to the exact location of your monitor screen! This makes life quite easy, believe me! A 19" diagonal monitor is 11.4" tall, and 15.2" wide (because of the aspect ratio = 4/3). So in the call to glFrustrum you would set width = 15.2 and height = 11.4. Its that simple. Any object located *at* the near clip plane will then be scaled accurately to its real-world size!

glFrustum doesn''t let you specify a field-of-view angle directly. Instead you have to specify a distance to the near clip plane and distance to the far clip plane. If you consider the camera to be at the origin, and the near clip plane to be some distance along the Z axis from that point, then given the width and height you already specified you will see that a triangle is formed. Since I can''t draw a picture, I''ll give you equations that you can use.

fov_angle = atan2(width/2, near_clip_plane_distance);

If you already know the field of view angle (fov_angle), you can sort of reverse that equation to calculate the required distance to the near clip plane to get your chosen fov_angle:

near_clip_plane_distance = (width/2) / tan(fov_angle);

Make sure you convert fov_angle into radians first, if you''ve chosen a value in degrees!

So, now you can easily specify width and height, and the near clip plane distance. All you need now is the far clip plane distance.

The far clip plane is somewhat more arbitrary---it has nothing to do with determining field-of-view or scaling, and your objects will be sized the same no matter what far clip plane distance you choose. The far clip plane is just used to determine the Z-buffer range. It must be greater than near_clip_plane_distance, but you can adjust as necessary. Don''t make it too large though or you''ll get rendering artifacts. And make sure its large enough to avoid clipping your objects!

Good luck!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Thats exactly what I was looking for. Thanks!
quote:Original post by Safe_haven
Thats exactly what I was looking for. Thanks!


Egggcelentttt.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Beware, though, the "commercial" dimensions of a monitor often include more than just the viewable portion.

Cédric
quote:Original post by cedricl
Beware, though, the "commercial" dimensions of a monitor often include more than just the viewable portion.

Cédric


Whelp, according to my handy-dandy tape rule, NEC seems to be less that a tenth of an inch off...so far so good! But I know what you mean...I''ve seen a few weird claims before.

This topic is closed to new replies.

Advertisement