Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


CAD/CAM rotation using Rotation commands in OPENGL

Posted on 2004-09-16
Medium Priority
Last Modified: 2013-12-06
I have a cube or NURB surface, or a modal etc...

They are NOT at the orgin they are out in space somewhere

what I want to do use use OPENGL to  fit the modal to the screen, then when I rotate, I rotate about some point internal in the model (Rotate about a point NOT at the origin)

So lets say I have a cube at  1x1x1 cube centered at 0 ,0,0 I also have another cube at 15,-5,-10 its a 2x2x2 cube,

I just zoomed in on the 2x2x2 cube and now when I rotate, I want to rotate the 2x2x2 cube and the point of rotation will seem to eminate from the center of this cube i.e.

How do you do this such that the rotation of everthing is from the center of this cube, and the parts or cubes are still at their orginal x,y,z cartesioan coordinates...

These are the functions I'm playing with.....

(USE OPENGL CODE EXAMPLES to explain as well )  I'm working from a CAD perspective

OnDraw(CDC* pDC)
      CGrahDoc* pDoc = GetDocument();
      // TODO: add draw code for native data here
         // Select the palette.
    CPalette* ppalOld = NULL;
      if (m_pPal)
          ppalOld = pDC->SelectPalette(m_pPal, 0);
      // Make the HGLRC current
    BOOL bResult = wglMakeCurrent(pDC->m_hDC, m_hrc);
      if (!bResult)
            TRACE("wglMakeCurrent Failed %x\r\n", GetLastError() ) ;

      glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;

//      glLoadIdentity();

      glRotatef(m_xRotation, 1.0, 0.0, 0.0);
      glRotatef(m_yRotation, 0.0, 1.0, 0.0);

//      glRotatef(m_zRotation, 0.0, 0.0, 1.0);
//      glScalef(m_xScaling,m_yScaling,m_zScaling);

//      glRotated(angle,1,0,0);
//      glRotated(angle,0,1,0);
/*      glColor3d(1.0,1.0,1.0);
            glVertex3d( 0.0, 100.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
            glVertex3d( 100.0, 0.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
            glVertex3d( 0.0, 0.0, 200.0);

      // Draw      


      //Swap Buffers
      SwapBuffers(pDC->m_hDC) ;//pDC->m_hDC

    // select old palette if we altered it
    if (ppalOld)
            pDC->SelectPalette(ppalOld, 0);

      wglMakeCurrent(NULL, NULL) ;

      // Set up some colors
      static GLdouble red[3] = {0.8, 0.0, 0.0 } ;
      static GLdouble green[3] = {0.0, 0.75, 0.0} ;
      static GLdouble purple[3] = {1.0, 0.14, 0.6667} ;
      static GLdouble redTrue[3] = {1, 0.0, 0.0 } ;

      // Enable lighting calculations
//      glEnable(GL_LIGHTING) ;             
//      OutputGlError("glEnable (GL_LIGHTING) ;") ;
//      glEnable(GL_LIGHT0) ;                  
//      OutputGlError("glEnable (GL_LIGHT0) ;") ;
      // Enable depth calculations
      OutputGlError("glEnable (GL_DEPTH_TEST);") ;
      glShadeModel(GL_SMOOTH);                                          // Enable Smooth Shading
      glDepthFunc(GL_LEQUAL);                                                // The Type Of Depth Testing To Do
      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);      // Really Nice Perspective Calculations

      // Clear the color and depth buffers
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;

      // Set the material color to follow the current color
      glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) ;
      glEnable(GL_COLOR_MATERIAL) ;
    // Draw the box.
//      glMatrixMode(GL_PROJECTION);
//      OutputGlError("MatrixMode") ;
      // Translate and rotate the axis.
//      glLoadIdentity();
//      gluLookAt(m_fEye[0],m_fEye[1],m_fEye[2],m_fCenterPoint[0],m_fCenterPoint[1],m_fCenterPoint[2],0,1,0);
//      glFrustum(-50,50,-50,50,-50,50);
//      gluPerspective(45,1,.1,1000.0);

      // Change the current color to green.
      glColor3dv(green) ;

//      glPushMatrix();
      // Draw the box.
      glCallList(1) ;

      glColor3dv(redTrue) ;

      glBegin(GL_TRIANGLES);                  // Drawing Using Triangles
            glColor3d(1.0,0.0,0.0);            // Set The Color To Red
            glVertex3d( 0.0, 1.0, 0.0);      // Top
            glColor3d(0.0,1.0,0.0);            // Set The Color To Green
            glVertex3d(-1.0,-1.0, 0.0);      // Bottom Left
            glColor3d(0.0,0.0,1.0);            // Set The Color To Blue
            glVertex3d( 1.0,-1.0, 0.0);      // Bottom Right
      glEnd();                        // Finished Drawing The Triangle

/*      glColor3d(1.0,1.0,1.0);
            glVertex3d( 0.0, 10.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);

            glVertex3d( 0.0, 5.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
            glVertex3d( 5.0, 0.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
            glVertex3d( 0.0, 0.0, 5.0);

            glVertex3d( 15.0, 0.0, 0.0);
            glVertex3d( 20.0, 0.0, 0.0);
            glVertex3d( 15.0, 0.0, 0.0);
            glVertex3d( 15.0, 10.0, 0.0);
            glVertex3d( 15.0, 0.0, 0.0);
            glVertex3d( 15.0, 0.0, 5.0);
Question by:ericcnc
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 17

Expert Comment

ID: 12089582
are you trying to fly the camera around the object, or actually rotate the object itself?

Author Comment

ID: 12091555
If what you mean by "actually rotate the object " is in the cartesian coordinates, use transformation matrixes to get new point values then  no.....  (If you actually pick the object and get its points, it will still be at its orginal position, but appear to have moved.)  

And if I understand opengl correctly, it would be the camera. BUT, the part itself doesnt move with repect to the coordinate system, BUT, will appear to have done so by rotation actually.

(If you down load any type of CAD package that does 3D and play with it, I want to do the same thing as that)


As a side question, when you do a glTranslate, does it actually move the ojbect relative to the world coordinate system? Or does it move it visually?
LVL 17

Accepted Solution

davebytes earned 1000 total points
ID: 12096185
I think you have some confusing terminology... ;)

OpenGL doesn't expressly 'move' any of your source data -- your data remains as is.  However, by using say glTranslate, you can make it appear as if your source data has moved by offsetting it by some amount prior to rendering.  The same goes for glRotate.

If you apply glTranslate/Rotate to the model matrix (or otherwise build up a model matrix yourself), you can make it appear as if the model data has moved.

But it sounds like you want to move the camera, not move/rotate the object itself.  

one sample you can download is: http://www.codeguru.com/Cpp/G-M/opengl/article.php/c2683/
nehe.gamedev.net has a lot of openGL samples that should show similar approaches.

Basically, if I have this correctly, you want to do a glPushMatrix, glTranslate, glRotate, render your world, glPopMatrix.

However, it's easier to just just gluLookAt from the GLUT library, and manipulate where the camera is, and where it is looking at.  At least until you have enough of your own code to manipulate the matrices directly the way you want.  Try: http://www.acm.jhu.edu/~upe/member_sites/duca/node6.html for a quick overview.

http://personal.nbnet.nb.ca/daveg/opengl/camera/ (more raw code)


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question