Solved

CAD/CAM rotation using Rotation commands in OPENGL

Posted on 2004-09-16
3
1,035 Views
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.
15,-5,-10

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();
      ASSERT_VALID(pDoc);
      // TODO: add draw code for native data here
         // Select the palette.
    CPalette* ppalOld = NULL;
      if (m_pPal)
      {
          ppalOld = pDC->SelectPalette(m_pPal, 0);
          pDC->RealizePalette();
      }
      
      // 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) ;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//      glLoadIdentity();

      glPushMatrix();
      glTranslated(m_xTranslation,m_yTranslation,m_zTranslation);
      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);
      glBegin(GL_LINES);
            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);
      glEnd();*/

      // Draw      
      DrawScene();



      glPopMatrix();

      //Swap Buffers
      SwapBuffers(pDC->m_hDC) ;//pDC->m_hDC
      m_hgldc=pDC->m_hDC;
      glFlush();

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

      wglMakeCurrent(NULL, NULL) ;
}



DrawScene()
{
      // 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
    glEnable(GL_DEPTH_TEST);              
      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) ;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      // 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_MODELVIEW);
//      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);
      glBegin(GL_LINES);
            glVertex3d( 0.0, 10.0, 0.0);
            glVertex3d( 0.0, 0.0, 0.0);
      glEnd();*/

      glColor3d(1.0,1.0,1.0);
      glBegin(GL_LINES);
            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);
      glEnd();

      glBegin(GL_LINES);
            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);
      glEnd();
            
}
0
Comment
Question by:ericcnc
  • 2
3 Comments
 
LVL 17

Expert Comment

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

Author Comment

by:ericcnc
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?
0
 
LVL 17

Accepted Solution

by:
davebytes earned 250 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.

Others:
http://www.codecolony.de/opengl.htm#camera
http://personal.nbnet.nb.ca/daveg/opengl/camera/ (more raw code)
and
http://www.gametutorials.com/Tutorials/OpenGL/OpenGL_Pg1.htm

-d
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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