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

Download demo project including source - 250 KB



Comments

  • good work

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

    Originally posted by: mdrosdi

    very good

    Reply
  • 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.

    Reply
  • 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

    Reply
  • 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.

    Reply
  • Good work

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

    Originally posted by: Xing Zhaohui

    ^O^

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

Top White Papers and Webcasts

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • As more and more organizations migrate to the cloud, many are faced with hidden costs emerging from unexpected places. Two non-obvious and non-trivial factors can drive up costs. First are separate charges for everything from server memory to intrusion detection. Second are the high personnel costs for early-generation, manually operated clouds. These costs can rack up quickly, creating total cost of ownership (TCO) surprises. Keeping TCO low in the cloud is essentially a matter of management strategy. IT …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds