Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: gluLookAt() function to rotate around drawn object

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    The Netherlands
    Posts
    9

    gluLookAt() function to rotate around drawn object

    Hi, i like to use the gluLookAt() function to look around my drawn objects. I have a openGL program that draws 4 cubes and i would like to have them all rotate around 1 center/point. I can not get this to work. I attached the code i have until now, perhaps someone can help me out?

    Code :
    /*
     *  cube.c
     */
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <math.h>
     
    static int rotationAngleX = 0;
    static int rotationAngleY = 0;
    static int rotationAngleZ = 0;
     
    float g_fDistance = -4.5f;
    float g_fSpinX = 0.0f;
    float g_fSpinY = 0.0f;
    float g_fSpinZ = 0.0f;
    float zoomFactor = 1.0f;
     
     
    static void draw3DRectangle(double dMinX_kls, double dMaxX_kls, double dMinY_kls, double dMaxY_kls, double dMinZ_kls, double dMaxZ_kls)
    { // werkt goed!!
     
      double MinX = fabs(dMinX_kls);
      double MaxX = fabs(dMaxX_kls);
      double MinY = fabs(dMinY_kls);
      double MaxY = fabs(dMaxY_kls);
      double MinZ = fabs(dMinZ_kls);
      double MaxZ = fabs(dMaxZ_kls);
     
      double sizeX = fabs( fabs(dMaxX_kls)-fabs(dMinX_kls) );
      double sizeY = fabs( fabs(dMaxY_kls)-fabs(dMinY_kls) );
      double sizeZ = fabs( fabs(dMaxZ_kls)-fabs(dMinZ_kls) );
     
      GLdouble V0[] = { 0.0, 0.0, 0.0};
      GLdouble V1[] = { sizeX, 0.0, 0.0};
      GLdouble V2[] = { sizeX, sizeY, 0.0};
      GLdouble V3[] = { 0.0, sizeY, 0.0};
      GLdouble V4[] = { 0.0, 0.0, sizeZ};
      GLdouble V5[] = { sizeX, 0.0, sizeZ};
      GLdouble V6[] = { sizeX, sizeY, sizeZ};
      GLdouble V7[] = { 0.0, sizeY, sizeZ};
     
      glPushMatrix();
      glTranslatef(dMinX_kls, dMinY_kls, dMinZ_kls);
     
      glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
     
      glBegin(GL_QUADS);
    	glVertex3dv(V0); glVertex3dv(V1); glVertex3dv(V2); glVertex3dv(V3);        // Surface 1
    	glVertex3dv(V1); glVertex3dv(V5); glVertex3dv(V6); glVertex3dv(V2);        // Surface 2
    	glVertex3dv(V5); glVertex3dv(V4); glVertex3dv(V7); glVertex3dv(V6);        // Surface 3
    	glVertex3dv(V4); glVertex3dv(V0); glVertex3dv(V3); glVertex3dv(V7);        // Surface 4
    	glVertex3dv(V3); glVertex3dv(V2); glVertex3dv(V6); glVertex3dv(V7);        // Surface 5
    	glVertex3dv(V0); glVertex3dv(V4); glVertex3dv(V5); glVertex3dv(V1);        // Surface 6
      glEnd();
     
      glPopMatrix();
     
    }
     
    void drawaxes(void)
    {
        glColor3ub(255, 0, 0);
        glBegin(GL_LINE_STRIP);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(0.75, 0.25, 0.0);
        glVertex3f(0.75, -0.25, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
        glVertex3f(0.75, 0.0, 0.25);
        glVertex3f(0.75, 0.0, -0.25);
        glVertex3f(1.0, 0.0, 0.0);
        glEnd();
        glBegin(GL_LINE_STRIP);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
        glVertex3f(0.0, 0.75, 0.25);
        glVertex3f(0.0, 0.75, -0.25);
        glVertex3f(0.0, 1.0, 0.0);
        glVertex3f(0.25, 0.75, 0.0);
        glVertex3f(-0.25, 0.75, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
        glEnd();
        glBegin(GL_LINE_STRIP);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 0.0, 1.0);
        glVertex3f(0.25, 0.0, 0.75);
        glVertex3f(-0.25, 0.0, 0.75);
        glVertex3f(0.0, 0.0, 1.0);
        glVertex3f(0.0, 0.25, 0.75);
        glVertex3f(0.0, -0.25, 0.75);
        glVertex3f(0.0, 0.0, 1.0);
        glEnd();
     
        glColor3ub(255, 255, 0);
        glRasterPos3f(1.1, 0.0, 0.0);
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'x');
        glRasterPos3f(0.0, 1.1, 0.0);
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'y');
        glRasterPos3f(0.0, 0.0, 1.1);
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'z');
    }
     
     
    void Display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        //gluLookAt(3.0, 2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    	//gluLookAt(3.0, 2.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    	//gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    	gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0);
        glColor3f(0.0, 1.0, 0.0);
     
    	// rotate complete axis system
    	glRotatef(-90, 1.0, 0.0, 0.0); // rotate around x axis
        glRotatef(90, 0.0, 0.0, 1.0); // rotate around z axis
     
    	// cabinets
    	glColor3f(0.5, 0.5, 0.5); // grey
        draw3DRectangle(0.684, 1.528, 0.75, 2, 0.0, 2.980);
    	draw3DRectangle(0.684, 1.528, -2, -0.75, 0.0, 2.980);
        draw3DRectangle(1.883, 2.727, -2, -0.75, 0.0, 2.980);
        draw3DRectangle(1.883, 2.727, 0.75, 2, 0.0, 2.980);
     
    	drawaxes();
     
        glutSwapBuffers();
    }
     
    void Init(void)
    {
        glClearColor(0.0, 0.0, 0.0, 0.0);
    }
     
    void Resize(int width, int height)
    {
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        //gluPerspective(60.0, width/height, 0.1, 10.0);
     
    	glOrtho(-4, 4, -0.5, 4, 0.1, 1000.0);
    	glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
     
    int main(int argc, char **argv)
    {
        glutInit(&amp;argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowSize(400, 400);
        glutInitWindowPosition(200, 200);
        glutCreateWindow("3D Object in OpenGL");
        Init();
        glutDisplayFunc(Display);
        glutReshapeFunc(Resize);
        glutMainLoop();
        return 0;
    }

  2. #2
    Advanced Member Frequent Contributor scratt's Avatar
    Join Date
    May 2008
    Location
    Thailand
    Posts
    556

    Re: gluLookAt() function to rotate around drawn ob

    Welcome..

    Here's a good tutorial that will explain this better than I can..
    http://www.lighthouse3d.com/opengl/glut/index.php?6

  3. #3
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,370

    Re: gluLookAt() function to rotate around drawn ob

    If you just need to rotate around a point you can perform global rotation of your scene then a translation to the camera position. It looks like something like this:

    Code :
     
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( cam.x, cam.y, cam.z );
    glRotate( ... );
     
    // draw the scene
     
    ...

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    The Netherlands
    Posts
    9

    Re: gluLookAt() function to rotate around drawn ob

    Ok and under // draw the scene i have to call glMatrixMode(GL_PROJECTION) right?

  5. #5
    Advanced Member Frequent Contributor scratt's Avatar
    Join Date
    May 2008
    Location
    Thailand
    Posts
    556

    Re: gluLookAt() function to rotate around drawn ob

    glMatrixMode simply affects which matrix is affected by matrix manipulation commands.

    You normally only affect the projection matrix when you are setting up the view.
    Take a look at the example I posted and you'll see it's typicaly in the "reshape" or view setup functions only.

  6. #6
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    386

    Re: gluLookAt() function to rotate around drawn ob

    By posting your code you make it MUCH easier for people to help you. Instead of asking some vague question that would probably be misinterpreted, you have given us something specific to work with. I took your code, compiled and ran it on my PC. It did not generate any 'cubes' as you state. It generated two rectangles and a flattened set of axes. The screen capture is here

    <http://imagebin.ca/view/iRvciZ.html>.

    I can help you further when you post a version of your code that actually displays 4 cubes. Sorry to be so anal.
    Am I doing your homework for you?

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    The Netherlands
    Posts
    9

    Re: gluLookAt() function to rotate around drawn ob

    ok, by setting the following value in the gluLookAt() function i get the 4 rectangular cubes.

    Code :
    gluLookAt(1.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    Now i would like to know how to rotate the 4 cubes around a point (or the Z axis as drawn).

  8. #8
    Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    386

    Re: gluLookAt() function to rotate around drawn ob

    Quote Originally Posted by glSpider
    Now i would like to know how to rotate the 4 cubes around a point (or the Z axis as drawn).
    Now you're talking. I've posted a solution at

    http://pastebin.com/m1095b123

    This is your code with some minor reformatting done to make it more paletable to my eye. All that was really necessary was to put in a glRotate command just before you draw the cubes and axes. This is done on line 146 of my code. I've added the ability to interactively rotate your cubes using the left and right arrow keys using a function called 'Special_Keys' referenced at line 187, and defined on lines 110-124. Note that we are simply rotating the cubes here, not fooling around with the LookAt function (because that is a little messier). Good luck.
    Am I doing your homework for you?

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2009
    Location
    The Netherlands
    Posts
    9

    Re: gluLookAt() function to rotate around drawn ob

    Thank you very much for your time and effort MaxH, it works great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •