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 3 of 3

Thread: Implementing rotation function like glRotate

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    3

    Implementing rotation function like glRotate

    I am trying to implement my own rotation function in OpenGL on C++, i.e. something like glRotate. My code for rotating around y looks like this:
    Code :
        void do_rotateY(GLdouble angle)
        {
        	//rotate around y axis
        	GLdouble s = angle;
        	GLdouble c[4][4];
        	c[1][1] = c[3][3]= 1.0;
        	c[0][0] = c[2][2] = cos(s);
        	c[2][0] = sin(s);
        	c[0][2] = -c[2][0];
        	glMultMatrixd(*c);
        }
    When I'm doing a call like:

    Code :
        glPushMatrix();
        do_rotateY(100);
        ...draw something...
        glPopMatrix();
    nothing appears on the screen. When I replace do_rotateY(...) with glRotatef(...) I can see everything correctly.

    Thanks for your help!

  2. #2
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    98
    Hi,

    taken from a ES-Framework, this may help you:

    Code :
    void ESUTIL_API
    esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
    {
       GLfloat sinAngle, cosAngle;
       GLfloat mag = sqrtf(x * x + y * y + z * z);
     
       sinAngle = sinf ( angle * PI / 180.0f );
       cosAngle = cosf ( angle * PI / 180.0f );
       if ( mag > 0.0f )
       {
          GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs;
          GLfloat oneMinusCos;
          ESMatrix rotMat;
     
          x /= mag;
          y /= mag;
          z /= mag;
     
          xx = x * x;
          yy = y * y;
          zz = z * z;
          xy = x * y;
          yz = y * z;
          zx = z * x;
          xs = x * sinAngle;
          ys = y * sinAngle;
          zs = z * sinAngle;
          oneMinusCos = 1.0f - cosAngle;
     
          rotMat.m[0][0] = (oneMinusCos * xx) + cosAngle;
          rotMat.m[0][1] = (oneMinusCos * xy) - zs;
          rotMat.m[0][2] = (oneMinusCos * zx) + ys;
          rotMat.m[0][3] = 0.0F; 
     
          rotMat.m[1][0] = (oneMinusCos * xy) + zs;
          rotMat.m[1][1] = (oneMinusCos * yy) + cosAngle;
          rotMat.m[1][2] = (oneMinusCos * yz) - xs;
          rotMat.m[1][3] = 0.0F;
     
          rotMat.m[2][0] = (oneMinusCos * zx) - ys;
          rotMat.m[2][1] = (oneMinusCos * yz) + xs;
          rotMat.m[2][2] = (oneMinusCos * zz) + cosAngle;
          rotMat.m[2][3] = 0.0F; 
     
          rotMat.m[3][0] = 0.0F;
          rotMat.m[3][1] = 0.0F;
          rotMat.m[3][2] = 0.0F;
          rotMat.m[3][3] = 1.0F;
     
          esMatrixMultiply( result, &rotMat, result );
       }
    }

    cu
    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net

  3. #3
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    456
    You need to zero out the terms of c that are meant to contain zero, because local variables aren't initialized so will contain garbage.

Posting Permissions

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