Solved

Opengl rotating shapes

Posted on 2011-03-20
7
1,557 Views
Last Modified: 2013-12-06
I am trying to rotate shapes in opengl. But I want to rotate them on there position so that they just look like they are spinning instead of rotating around a point.

At the moment this is how I am doing it.

static int weaponangle = 0;

then in the display function

glPushMatrix();
   
    glTranslatef (-1.0, 0.0, 0.0);
    glRotatef ((GLfloat) weaponangle, 1.0, 0.0, 0.0);
   // glTranslatef (1.0, 0.0, 0.0);
    glPushMatrix();
    //glScalef (2.0, 0.4, 1.0);
    DrawWeapon();
   
    glPointSize(0.2);
    glPopMatrix();
      glPopMatrix();
 
weapon is a sphere that I want to spin around. I am using user input like below

  if (key=='e')
      {
        weaponangle = (weaponangle + 10) % 360;
        frame = 0;
            bWeaponIsActive = true;
            glutPostRedisplay();
      }
0
Comment
Question by:tango2009
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 166 total points
ID: 35178443
You are currently rotating the object around the x axis. You need to rotate it around the center of the object. So instead of
glRotatef ((GLfloat) weaponangle, 1.0, 0.0, 0.0);
You need to use
glRotatef ((GLfloat) weaponangle, x, y, z);
where x,y, and z are the x,y,and z coordinates of the center of the sphere however it was defined.
0
 

Author Comment

by:tango2009
ID: 35179453
The things that I want to rotate around are a sphere is that drawn with glutSolidSphere like below

 glutSolidSphere (1.0, 20, 16);

and a torus like this

 glutSolidTorus (0.275, 0.85, 16, 16);

how do I know the coordinates of the the center of these objects.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35180856
When you create the objects, they are centered at the modeling coordinates. If it's currently rotating around a 'corner' of the object, then use radius/2 for each coordinate (so 1/2 for the sphere and .85/2 for the torus).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Accepted Solution

by:
ikework earned 167 total points
ID: 35181600
Hi tango2009,

>> But I want to rotate them on there position so that they just look like they are spinning instead of rotating around a point.

Your code looks good (except the indention ;)
If you want to rotate the object around its center at a given position, you have to 1st translate (glTranslatef) the object to that position and then rotate it (glRotatef)


  float pos[3] = { 1.0f, 2.0f, 3.0f };
  float angle = 30.0f;

  glPushMatrix();
      // put the object at its position
      glTranslatef(pos[0], pos[1], pos[2]);
      // rotate the object inplace around the x-axis
      glRotatef(angle, 1.0f, 0.0f, 0.0f);
      glutWireSphere (1.0, 20, 16);
  glPopMatrix();

If your code does not work, then most likely you make wrong translations/rotations in the DrawWeapon-function, try the aboce instead.

That's it, if you have further questions feel free to ask :)

ike
0
 
LVL 3

Assisted Solution

by:StormSeed
StormSeed earned 167 total points
ID: 35391188
Hi tango2009,

First, you need to make sure you are working with the right matrix.  Because you are working with the object, this would be the model-view matrix:

glMatrixMode(GL_MODELVIEW);

Open in new window


Assuming that your viewpoint is at position 0,0,0 and looking in the direction of the x-axis, you only want to move the model that you are rendering.  The transformation functions (glTranslate, glScale and glRotate) affect the axis system used to render the model, not the model itself, although in this case it is the same.

Rotate always rotates around the origin, scale scales things toward/away from the origin and translate moves the world so that the origin is in a new place relative to it.

This means that you should first rotate, then translate, if you want to rotate around the center of the object.

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glRotatef(angle, 1.0f, 0.0f, 0.0f);    // Rotate angle degrees around x-axis
glTranslatef(posX, posY, posZ);    // Put the object where it needs to be
DrawWeapon();    // Draw the object around position 0,0,0; the transformations will automatically put it where it belongs.

glPopMatrix();

Open in new window


0
 
LVL 20

Expert Comment

by:ikework
ID: 35391362
>> This means that you should first rotate, then translate, if you want to rotate around the center of the object.

Hey StormSeed, this is true for DirectX but not for OpenGL, because DirectX uses row-major-matrices with pre-multiplying, but OpenGL uses the opposite: column-major-matrices with post-multiplying, so all matrix transformation (multiplications) in OpenGL are excactly in the opposite order than in DirectX.

From: http://www.opengl.org/resources/faq/technical/transformations.htm
      "9.005 Are OpenGL matrices column-major or row-major?"

  "Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices"


For DirectX there is the SRT-Rule to rotate an object around its center at a given position (and additionally scale it around its center):

  DirectX: SRT -> Scale - Rotate - Translate

for OpenGL its the exact opposite order:

  OpenGL : TRS -> Translate - Rotate - Scale
 
as shown in my previous post above, so one must in fact first translate, then rotate in OpenGL in order to have the object rotating around its center at a given position.


ike  
0
 
LVL 3

Expert Comment

by:StormSeed
ID: 35391737
ike, I only ever use OpenGL anyway.  :^)

I'm pretty sure that tango2009's problem is that he does not use the matrix for translation, while still using the matrix for rotation, causing the order of the two transformations to be incorrect.

Translate followed by rotate (as you suggest) will work for transforming the object, as I can see from one of my own games' source code.

However, for moving the camera, the opposite order should be used, since we are actually moving the world.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordcount challenge 11 106
index Out OF Range Exception error 4 53
t-sql sp_addlinkedserver possible risks 3 50
What language/protocol is the Angular Chat? 2 56
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video teaches viewers about errors in exception handling.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now