setting up a perspective matrix

Started by
2 comments, last by manic_man 16 years, 11 months ago
Hi everyone, this is my first post here so be gentle :) I am currently working on a small project which involves representing large datasets in multiple ways. One of the ways is going to be a 3D scatter chart. I am using OpenGL via Tao in C# with multiple contexts held in child windows in an MDI application. My problem is setting up my viewing matrix. Im afraid im a bit new to OpenGL. Basically I have a set of points which translate to my cartesian co ordinates and what I need is for the chart to always "fill" the GL context regardless of its upper and lower limits. I have values for x/y/zMin and x/y/zMax. I was hoping someone could push me in the right direction for calculating a field of view which will always encompass the whole of my chart. I have been reading the red book and have tried a few techniques from in there, including one which required I calculated a "bounding sphere" and then calculated the FOV from that. Whether I was doing it wrong im not sure but I found if all three max and mins were the same I always got an angle of 90 and if they wernt then the angle generated was close to being correct but not quite big enough. Any help anyone could give would be much appreciated! (or alternative suggestions, I dont mind even using an Ortho projection matrix as perspective isnt imperative, but im struggling to produce a 3d chart in an ortho view)
Advertisement
Look at the mathematical definition of a perspective matrix found here:

http://pyopengl.sourceforge.net/documentation/manual/gluPerspective.3G.html

Create that same matrix yourself.
What follows is not optimal, but it will give you a field of view that guarantees that no point of your chart is outside the view.

- Take the center of your box
- compute the radius of the sphere containing your box
- make your camera look at the center of the sphere
- if the perspective angle is set to a constant value a, you can compute the distance from your camera to the sphere with tan(a) = radius/distance ; to be sure that ALL points of your chart are in the view, distance must be the closest distance from camera to sphere surface (not center), otherwise the cone of the camera could intersect the sphere and some points might be outside ; so the distance from the camera to the center of the box should be distance + radius
- finally, the near clipping plane must be at distance and the far clipping plane at distance + 2*radius

- enter these parameters into the perspective matrix

Again, it's not optimal and your chart may not be centered and will not completely fill the view. I believe it would be quite complex to go beyond that in perspective mode, as perspective projection introduces non-linear behaviour, so my guess is that you would have to use some kind of iterative method. But with ortho view, things are much easier ! Just compute the transformed (screen) coordinates of the 8 points of the box, take the min and max X and Y values, then move and scale your camera so that all points are in the view. If ortho is OK, use ortho !

Ok thanks for that guys, much appreciated.

I'll have a go at your suggestions and get back later to say how I got on :)

This topic is closed to new replies.

Advertisement