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

x
Solved

# CAD/CAM rotation using Rotation commands in OPENGL

Posted on 2004-09-16
Medium Priority
1,068 Views
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
Question by:ericcnc
[X]
###### 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
• 2

LVL 17

Expert Comment

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

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?
0

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.

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

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…
###### Suggested Courses
Course of the Month11 days, 5 hours left to enroll