# Panning zooming and rotation in OpenGL

### WEBINAR:On-Demand

Application Security Testing: An Integral Part of DevOps

## 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/

• #### good work

Posted by Legacy on 08/06/2003 12:00am

Originally posted by: mdrosdi

very good

• #### Great Help

Posted by Legacy on 10/17/1999 12:00am

Originally posted by: Holger

Thanx man, great work you did, it helped me as an OpenGL-beginner very much.

• #### Disagree

Posted by Legacy on 07/18/1999 12:00am

Originally posted by: Robbie

I disagree with the the above mail. Using vectors is not incorrect, but it does incure a performace hit. Each vector operation is evauated by GL to a matrix op (64 mults) so doing a single matrix op is better than doing several vector ops

• #### suggestion

Posted by Legacy on 06/20/1999 12:00am

Originally posted by: Xing Zhaohui

Vector can do everywork of matrix. You can replace matrix
with vectors in processing translate-scale-rotate.

• #### Good work

Posted by Legacy on 06/18/1999 12:00am

Originally posted by: Xing Zhaohui

^O^

• You must have javascript enabled in order to post comments.

## Top White Papers and Webcasts

• As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
• The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
• Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

## Most Popular Programming Stories

• There have been no articles posted today.