• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1121

# CAD/CAM rotation using Rotation commands in OPENGL

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);

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);") ;

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.
//      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
ericcnc
• 2
1 Solution

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

Author Commented:
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

Commented:
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.

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.