Jump to content

  • Log In with Google      Sign In   
  • Create Account


rotating vertices


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 23 September 2013 - 03:26 PM

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();
}

 

 



Sponsor:

#2 3TATUK2   Members   -  Reputation: 643

Like
0Likes
Like

Posted 23 September 2013 - 11:03 PM

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 );


#3 Sponji   Members   -  Reputation: 1011

Like
0Likes
Like

Posted 23 September 2013 - 11:07 PM

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();
}

Derp

#4 Irlan   Members   -  Reputation: 942

Like
0Likes
Like

Posted 24 September 2013 - 08:57 AM

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

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


Edited by irlanrobson, 24 September 2013 - 08:59 AM.


#5 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 24 September 2013 - 11:37 AM

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



#6 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 24 September 2013 - 10:32 PM


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.



#7 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 24 September 2013 - 10:33 PM

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();
}

 

 



#8 Migi0027   Crossbones+   -  Reputation: 1234

Like
0Likes
Like

Posted 24 September 2013 - 11:04 PM

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, 24 September 2013 - 11:04 PM.


#9 RobTheBloke   Crossbones+   -  Reputation: 2230

Like
0Likes
Like

Posted 25 September 2013 - 04:05 AM

 

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). 



#10 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 25 September 2013 - 12:55 PM

well I am only doing what tatuk was suggesting



#11 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 25 September 2013 - 06:40 PM


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.



#12 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 27 September 2013 - 07:25 PM

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



#13 3TATUK2   Members   -  Reputation: 643

Like
0Likes
Like

Posted 27 September 2013 - 08:18 PM

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, 27 September 2013 - 08:19 PM.


#14 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 28 September 2013 - 07:20 PM

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();
}


#15 3TATUK2   Members   -  Reputation: 643

Like
0Likes
Like

Posted 28 September 2013 - 11:07 PM

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



#16 L. Spiro   Crossbones+   -  Reputation: 9818

Like
0Likes
Like

Posted 29 September 2013 - 05:51 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#17 phil67rpg   Members   -  Reputation: 747

Like
0Likes
Like

Posted 29 September 2013 - 05:59 PM

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.



#18 L. Spiro   Crossbones+   -  Reputation: 9818

Like
0Likes
Like

Posted 29 September 2013 - 11:18 PM

Show your code.

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#19 Sponji   Members   -  Reputation: 1011

Like
1Likes
Like

Posted 29 September 2013 - 11:24 PM

 

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, 29 September 2013 - 11:37 PM.

Derp

#20 3TATUK2   Members   -  Reputation: 643

Like
0Likes
Like

Posted 30 September 2013 - 07:42 AM


stop using trigonometry manually

 

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


Edited by 3TATUK2, 30 September 2013 - 07:43 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS