# rotating vertices

## Recommended Posts

phil67rpg    443

well l am able to move some vertices up and down on the screen. I am trying to get my shape to rotate around a central z-axis. here is my code. Let me know if you need more information.

void

DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glRotatef(rotation,0.0f,0.0f,1.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glVertex3f(-0.25f,-0.5f+position_Y,0.0f);
glVertex3f(-0.5f,-0.5f+position_Y,0.0f);
glVertex3f(-0.0f,0.5f+position_Y,0.0f);
glVertex3f(0.5f,-0.5f+position_Y,0.0f);
glVertex3f(0.25f,-0.5f+position_Y,0.0f);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glEnd();
glPopMatrix();
}


?

##### Share on other sites
3TATUK2    714

you need to look into basic trig rotation matrix math... it goes something like this:

float temp = x_position;
x_position = x_position * cosf( z_rotation ) - y_position * sinf( z_rotation );
y_position = temp * sinf( z_rotation ) + y_position * cosf( z_rotation );

(z_rotation in radians, - *NOT* - degrees)

then you should be able to glTranslatef( x_position, y_position, z_position );

##### Share on other sites
Sponji    2503
void DrawShip(float x, float y, float z, float rotation) {
glPushMatrix();

// First translate to the wanted position
glTranslatef(x, y, z);

// Then rotate it
glRotatef(rotation, 0.0f, 0.0f, 1.0f);

// Then render the shape of the object, you have already translated and rotated it to the right place
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}

##### Share on other sites
Irlan    4067

Instead, use Rotation Matrices or Quaternions to rotate ANYTHING. Take a look in a good mathematics book.

Once you get that, use glMultMatrixf( MyMatrix ) and it's done.

Edited by irlanrobson

##### Share on other sites
phil67rpg    443

well I tried sponji code but it still does not rotate around a its central axis after I move it up or down.

##### Share on other sites
phil67rpg    443

you need to look into basic trig rotation matrix math... it goes something like this:
float temp = x_position;
x_position = x_position * cosf( z_rotation ) - y_position * sinf( z_rotation );
y_position = temp * sinf( z_rotation ) + y_position * cosf( z_rotation );
(z_rotation in radians, - *NOT* - degrees)
then you should be able to glTranslatef( x_position, y_position, z_position );

well I tried the above code, it does not work, it rotates but it does not follow its nose, it is a space ship of vertices.

##### Share on other sites
phil67rpg    443

here is my code

void

DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glTranslatef(position_X,position_Y,position_Z);
glBegin(GL_LINE_LOOP);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glVertex3f((-0.25f*cosf(rotation)+0.5f*sinf(rotation)),(-0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((-0.5f*cosf(rotation)+0.5f*sinf(rotation)),(-0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)-0.5f*sinf(rotation)),(0.0f*sinf(rotation)+0.5f*cosf(rotation)),0.0f);
glVertex3f((0.5f*cosf(rotation)+0.5f*sinf(rotation)),(0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.25f*cosf(rotation)+0.5f*sinf(rotation)),(0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glEnd();
glPopMatrix();
}


##### Share on other sites
Migi0027    4628

Are you translating after you rotated?

To archive your spaceships rotation, you'd need the following order of transformations (I don't know if this order has a name):

(Rotate -> Scale) -> Translate

The first two can be switched.

So rotate it around 0,0,0

Then scale it if needed

Then translate it to xyz, then it looks like it rotated around itself. Though the center of the vertices/the center of the spaceships model must be around 0,0,0, in terms of vertices.

Hope this helps.

Edited by Migi0027

##### Share on other sites
RobTheBloke    2553

here is my code

void

DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glTranslatef(position_X,position_Y,position_Z);
glBegin(GL_LINE_LOOP);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glVertex3f((-0.25f*cosf(rotation)+0.5f*sinf(rotation)),(-0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((-0.5f*cosf(rotation)+0.5f*sinf(rotation)),(-0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)-0.5f*sinf(rotation)),(0.0f*sinf(rotation)+0.5f*cosf(rotation)),0.0f);
glVertex3f((0.5f*cosf(rotation)+0.5f*sinf(rotation)),(0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.25f*cosf(rotation)+0.5f*sinf(rotation)),(0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glEnd();
glPopMatrix();
}


Oh god! My eyes! They burn! :p

Don't call sin/cos so many times like that! sin/cos are non trivial functions, and by transforming the vertices like that by using the CPU, instead of the GPU, you are failing to harness any of the specially optimised GPU hardware to transform the vertices.

I think it's probably time for you to start writing your own 4x4 matrix class (or use one from a 3rd party library, e.g. Eigen), so that you can construct a matrix that performs the transformations, before passing that matrix to openGL via glMultMatrixf (incidentally, this will also help to port your code over to OpenGL 4.4 at a later date, since your transforms will already be in matrix form ready to pass into a shader).

##### Share on other sites
phil67rpg    443

well I am only doing what tatuk was suggesting

##### Share on other sites
phil67rpg    443

So rotate it around 0,0,0

Then scale it if needed

Then translate it to xyz, then it looks like it rotated around itself. Though the center of the vertices/the center of the spaceships model must be around 0,0,0, in terms of vertices.

well I tried rotating then translating but it does not move up or down.

##### Share on other sites
phil67rpg    443

I am reading the opengl superbible 4th ed. it is a really good read.

##### Share on other sites
3TATUK2    714

well I am only doing what tatuk was suggesting

Not exactly. I meant something like this:

void DrawShip(float x, float y, float z, float rotation) { // rotation in degrees
glPushMatrix();

float radr = 0.017453292519943295 * rotatation; // convert rotation from degrees to radians

float temp = x;
x = x * cosf( radr ) - y * sinf( radr );
y = temp * sinf( radr ) + y * cosf( radr );

// First translate to the wanted position
glTranslatef(x, y, z);

// Then render the shape of the object, you have already translated and rotated it to the right place
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();

Edited by 3TATUK2

##### Share on other sites
phil67rpg    443

well I tried the above code but it does not rotate, here is my code,

void DrawShip(GLfloat x,GLfloat y,GLfloat z,GLfloat rotation,GLfloat color)
{
glPushMatrix();

float radr = 0.017453292519943295 * rotation; // convert rotation from degrees to radians

float temp = x;
x = x * cosf( radr ) - y * sinf( radr );
y = temp * sinf( radr ) + y * cosf( radr );

// First translate to the wanted position
glTranslatef(x, y, z);
glColor3f(color,0.0f,0.0f);

// Then render the shape of the object, you have already translated and rotated it to the right place
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}


##### Share on other sites
3TATUK2    714

well what *does* happen? something's *got* to happen because you're changing the x,y,z position (by a rotation matrix) ... are you even supplying new rotation values? it has to gradually increase from 0->360

##### Share on other sites
L. Spiro    25622

well I tried sponji code but it still does not rotate around a its central axis after I move it up or down.

That is because he did it in the wrong order.

void DrawShip(float x, float y, float z, float rotation) {
glPushMatrix();

// First rotate.
glRotatef(rotation, 0.0f, 0.0f, 1.0f);

// THEN translate.
glTranslatef(x, y, z);

// Then render the shape of the object, you have already translated and rotated it to the right place
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f,0.0f);
glVertex3f(-0.25f,-0.5f,0.0f);
glVertex3f(-0.5f,-0.5f,0.0f);
glVertex3f(-0.0f,0.5f,0.0f);
glVertex3f(0.5f,-0.5f,0.0f);
glVertex3f(0.25f,-0.5f,0.0f);
glVertex3f(0.0f,-0.25f,0.0f);
glEnd();
glPopMatrix();
}
Case close problem solved stop using trigonometry manually.

L. Spiro

##### Share on other sites
phil67rpg    443

well spiro I did what  you said and it moves up and down, but it still rotates around the center of the screen, but does not rotate around the center of the shape. sorry spiro but I am trying. I commend you, you are a very bright person.

##### Share on other sites
L. Spiro    25622

I get the feeling you mixed what I posted with the math you have been trying to use.

The solution is my post, the whole post, and nothing but the post, so help me Todd.

L. Spiro

##### Share on other sites
Sponji    2503

well I tried sponji code but it still does not rotate around a its central axis after I move it up or down.

That is because he did it in the wrong order.

What, in wrong order? I don't understand, when would he even want to use the order you suggested? First translate, then rotate.

But still, I bet the problem is somewhere else. I think he isn't moving the object correctly, and that should be done outside the render function.

Edited by Sponji

##### Share on other sites
3TATUK2    714

stop using trigonometry manually

He needs to. He wants to rotate AROUND a point. Not at the centerpoint which glRotatef() would do.

Edited by 3TATUK2

##### Share on other sites
BitMaster    8651
That's nonsense. You can rotate around any point you please with matrices (and obviously by glRotatef). You just have to apply T^-1 * R * T instead of just R, where T is a translation matrix which moves the desired center point to the origin.

##### Share on other sites
L. Spiro    25622

He needs to. He wants to rotate AROUND a point. Not at the centerpoint which glRotatef() would do.

According to this:

but it still rotates around the center of the screen, but does not rotate around the center of the shape.

He wants to rotate around the center of the shape.
But either way raw trigonometry is unnecessary, as it was already pointed out that applying first the inverse translate of the center point prior to rotating, lastly followed by the object’s translation, can all be done with matrix math.

What, in wrong order? I don't understand, when would he even want to use the order you suggested? First translate, then rotate.

When you want to rotate around the center of the object, as he said he wants to do.
Otherwise you translate by the opposite of the center of rotation, rotate, and then apply object translation. In other words, when working with only the object’s translate and rotate, rotate always comes last.
SCALE * ROTATE * TRANSLATE.

L. Spiro

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account