Panning zooming and rotation in OpenGL

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.

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:

   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.

Download demo project including source - 250 KB


  • 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


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds