How to rotate a Single 3D Object by modifying it's coordinates

Hi Everyone,

I would like to understand how to rotate a 3D Object around it's Center, by modifying it's Vertex Coordinates.

Not by using glRotatef...

The reason is that I need to keep track of the new coordinates because of my collision system, which is based on Vertex Coords.


Here is the Code for an Object Thanks Guys!

void DrawHouse(GLfloat x1, GLfloat x2, GLfloat y1, GLfloat y2, GLfloat z1,GLfloat z2, GLfloat u1, GLfloat u2, GLfloat v1, GLfloat v2, GLuint textid)
{

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z2);
glTexCoord2f(u2,v1); glVertex3f(x2,y1,z2);
glTexCoord2f(u2,v2); glVertex3f(x2,y2,z2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u2,v2); glVertex3f(x2,y2,z2);
glTexCoord2f(u1,v2); glVertex3f(x1,y2,z2);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z1);
glTexCoord2f(u2,v1); glVertex3f(x2,y1,z1);
glTexCoord2f(u2,v2); glVertex3f(x2,y2,z1);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u2,v2); glVertex3f(x2,y2,z1);
glTexCoord2f(u1,v2); glVertex3f(x1,y2,z1);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z1);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u1,v1); glVertex3f(x2,y1,z2);
glTexCoord2f(u2,v1); glVertex3f(x2,y1,z1);
glTexCoord2f(u2,v2); glVertex3f(x2,y2,z1);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u2,v2); glVertex3f(x2,y2,z1);
glTexCoord2f(u1,v2); glVertex3f(x2,y2,z2);
glTexCoord2f(u1,v1); glVertex3f(x2,y1,z2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z2);
glTexCoord2f(u2,v1); glVertex3f(x1,y1,z1);
glTexCoord2f(u2,v2); glVertex3f(x1,y2,z1);
glEnd();

glBindTexture(GL_TEXTURE_2D, textid);

glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(u2,v2); glVertex3f(x1,y2,z1);
glTexCoord2f(u1,v2); glVertex3f(x1,y2,z2);
glTexCoord2f(u1,v1); glVertex3f(x1,y1,z2);
glEnd();

//COLLISION WITH CHARACTER
if(xtrans <= -(x1-0.2) && xtrans >= -(x2+0.2) && ztrans <= -(z1-0.2) && ztrans >= -(z2+0.2))
{

if(MoveForward)
{
xpos += 2*(float)sin(heading*piover180) * 0.0005f;
zpos += 2*(float)cos(heading*piover180) * 0.0005f;
}

if(MoveBackward)
{
xpos -= 2*(float)sin(heading*piover180) * 0.0005f;
zpos -= 2*(float)cos(heading*piover180) * 0.0005f;
}

}

DrawDoor(x2, x2 + 0.1, 0.0 , 0.5, z2 - (z2-z1)/2 + 0.25, z2 - (z2-z1)/2 - 0.25, 0.0, 1.0, 0.0, 1.0, texture[1], texture[3]);


}
I did not read your entire post, but
x' = x cos a - y sin a

y' = x sin a + y cos a

might be helpful
Last edited on
I don't understand.

What is x and what is y?

I'm trying to rotate the vertices of square around it's center...

I would have the angle and the coords of the vertices, how do I calculate the new coords of the square post rotation?
x' = x cos a - y sin a

y' = x sin a + y cos a

(x', y') are the new coordinates of the old coordinate (x,y) rotated by angle a counterclockwise. (x,y) can be the coordinates of anything (including vertices), once you've defined your origin, which from your last post appears to be the center of your square. From your first post, you seem to know some trigonometry already.
Ok I figured it out:

void CNPC::Draw()
{

glBindTexture(GL_TEXTURE_2D, m_textid);

glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(m_u1,m_v1); glVertex3f(m_xa1,m_y1,m_za1);
glTexCoord2f(m_u2,m_v1); glVertex3f(m_xa1,m_y2,m_za1);
glTexCoord2f(m_u2,m_v2); glVertex3f(m_xa2,m_y2,m_za2);
glTexCoord2f(m_u1,m_v1); glVertex3f(m_xa2,m_y1,m_za2);
glEnd();

if(TurnNPC)
{

m_Angle += 1.0f;

if(m_Angle > 360.0f)
{
m_Angle = 0.0f;
}

m_xa1 = (GLfloat)cos(m_Angle*piover180) * 0.125f + 0.875f;
m_za1 = (GLfloat)sin(m_Angle*piover180) * 0.125f - 5.0f;
m_xa2 = (GLfloat)cos((m_Angle+180)*piover180) * 0.125f + 0.875f;
m_za2 = (GLfloat)sin((m_Angle+180)*piover180) * 0.125f - 5.0f;

TurnNPC = false;
}

}


Works Like a charm.

It' awesome to counter a GLrotate that would rotate the Camera, to keep a quad facing the camera, which would enable you to create monsters a la Doom 2
Last edited on
When you're finished torturing yourself with the fixed function pipeline, check this link

http://www.arcsynthesis.org/gltut/

:)
+1 @ Luc Lieber

Any tutorial that is telling you to use glBegin/glEnd is teaching you the wrong/outdated way to use OpenGL.

The arcsynthesis tutorial is good.
Coooooooool! I'll check it out
Topic archived. No new replies allowed.