October 21, 2018
Hot Topics:

# OpenGL panning, rotation and zooming

## Pan,Zoom and Rotate the scene

In this program ,I show some simple OpenGL code.There is nothing special ,but I thing it can be useful for someone.
Features:

1. Zoom out,zoom in and pan the scene.
2. Draw the standard primiry like cylinder in ambiguous direction.
3. Draw the 3-D Mesh in a simple way.

In my program ,I use the class to wrap the detail of the realization in OpenGL.So I can use it freely at any other program without consider what will be do first ,and what be second. I have constructed a framework in Visual C++ 6.0,in which you can freely pan and rotate the scene ,without consider what is displaying in the scene. How did I do that ?

Apply the Camera

I construct CCamera and CScene class ,which take care of the rotate and pan of the camera and scene.In some program ,people use the glPushMatrix and glPopMatrix to accomplish the simple transfer.I do this in another way. By using the TransferMatrix class ,which I define to contain the MODELVIEW matrix of a special object(like the CCamera),thing will be simple.

We know that ,the OpenGL using the transform of MODELVIEW matrix to accomplish complex drawing ,and the glTranslate, glRotate is all base on the current MODELVIEW matrix. So if you want to transfer according to an ambiguous direction, it will be difficult. You have to mirror it into the current. Two suggestion to solve the problem.

The first is to use the matrix stack by using glPushMatix, glPopMatrix, but it is easy to be wrong when there are a lot of transform action. The second is to apply a matrix to the object, one matrix per object. Using the glLoadMatrix, glMultMatrix to apply the Object's matrix into the OpenGL.
So the course is like this:

```
DrawScene()
{
Init Camera ; //apply the  Matrix of camera.
Init Scene;   //apply the  Matrix of scene.

for( i=0; i < ObjectNumber; i++)
{
Init Objcet[ i];     //apply the Matrix of object[i].
drawing Object[ i];  //drawing the object[i];

for( j=0; j < SubObjectNumber; i++)   //if there is any subobject...
{
Init SubObject[ j];
drawing Subject[ j];
.......
}
......
}

}

// below is what I do in the code

m_Camera.Apply();                // set the camera transfer matrix
gluLookAt(5,5,2,0,0,0,0,0,1);    // the initial view angle
m_Scene.Apply();                 // set scene transfer matrix
```

Drawing ambiguous direction primiry

When I need a arrow pointer ,I think of the cylinder and cone. But how can I drawing it in ambiguous direction ? Thank's to a programmer I don't remember his name ,in his code , he use the cross product of the original vector and target. Vector as the rotate axis, solve the problem. For detail ,we can see the arrow.cpp.

How can we draw 3-D Mesh

There is no difficulty in doing this. But it is hard to set the normal of the point. So I use a CClixmax class to wrap the detail. And I write a function to get normal of a point by using the points around it. There is no complete project for all modules I have writed. For the space limited ,I only put the Polarize Light in the Archive. But all modules can be easily put into the project, because I have used the class to wrap it. For convenience I use the code downloaded from the URL below as a framwork, so you can see some claim of the copyright reserved. But there is no reason you have to do so, you can just use any framework you like.
http://devcentral.iftech.com/learning/tutorials/mfc-win32/opengl/

Date Posted: June 1, 1999

Comment and Contribute

(Maximum characters: 1200). You have characters left.

## Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

## Most Popular Developer Stories

Thanks for your registration, follow us on our social networks to keep up-to-date