View Full Version : Rotate vertexes in a mesh

jboss

05-11-2004, 09:49 AM

I'm trying to find a way to morph or rotate vertexes in a mesh without braking up the mesh. a short example is:

void Square()

{

glBegin(GL_QUADS);

glVertex3f(1.000000,1.000000,0.000000);

glVertex3f(1.000000,-1.000000,0.000000);

glEnd();

glRotatef(90.0,1,0,0);

glBegin(GL_QUADS);

glVertex3f(-1.000000,-1.000000,0.000000);

glVertex3f(-1.000000,1.000000,0.000000);

glEnd();

}

But since you have to stop and restart the GL_QUADS it will not work. This is what I would like to be able to do:

void Square()

{

glBegin(GL_QUADS);

glVertex3f(1.000000,1.000000,0.000000);

glVertex3f(1.000000,-1.000000,0.000000);

glRotatef(90.0,1,0,0);

glVertex3f(-1.000000,-1.000000,0.000000);

glVertex3f(-1.000000,1.000000,0.000000);

glEnd();

}

But of course you can't put a glRotate inside a glBegin and glEnd. Thanks in advance

Well I guess a faster way than going in and out with glBegin() would be to use your own rotation matrix.

Instead of using sin and cos to rotate each vertex, you call them just to rotate a vector for each axis.

{ { 1, 0, 0}, { 0, 1, 0}, { 0, 0, 1} }

{ {Xx,Xy,Xz}, {Yx,Yy,Yz}, {Zx,Zy,Zz} }

Now each vertex V should be passed:

glVertex3f( Vx*(Xx+Yx+Zx), Vy*(Xy+Yy+Zy), Vz*(Xz+Yz+Zz) );

jboss

05-12-2004, 07:03 AM

You lost me. Are you getting this from a reference that you can refer me to. If not can you explain it a little more. I have looked at your reply for some time now but can't quite get it all.

chowe6685

05-12-2004, 08:13 AM

basically he is suggesting that you manually rotate your vertices. This is not particularly difficult if you are familier with matrix math or know how to do rotations with sin and cos. You can google for matrix faq to get an idea of how to begin.

The second suggestion is actually very clever. The easiest way to understand it is by looking a coordinates in a different way. We define 3 unit vectors

i = <1,0,0>

j = <0,1,0>

k = <0,0,1>

then the point <3,4,5> becomes 3i+4j+5k

what <mov> has suggested is to use 3 different coordinate vectors representing a rotated coordinate system so

i' = Xx*i + Xy*j + Xz*k

j' = Yx*i + Yy*j + Yz*k

k' = Zx*i + Zy*j + Zz*k

now a vertex V would be

V = Vx*i' + Vy*j' + Vz*k'

but since opengl is still in the i,j,k coordinate system, we need to transform our points back to that frame. This is easy, we can just plug in our definitions for i' j' k'. This gives (with some rearrangement of terms)

V = Vx*(Xx*i+Yx*i+Zx*i)

Vy*(Xy*j+Yy*j+Zy*j)

Vz*(Xz*k+Yz*k+Zz*k)

which is what <mov> had as a final result. The reason we would bother with this is to simplyfy calculations. If we have 1000 vertices being rotated by thte same angle, then rather than transforming all of them with sin and cos we can transform the axis once, and transform all of the vertices using the axis. This reduces the number of sin/cos calculations we need to make.

I hope this makes sense, and if I have made any glaring errors, could someone please point them out.

jboss

05-12-2004, 10:40 AM

ok I see what you and <mov> are saying but what if 90 is not a const but a varable ANG that is in a for loop ANG++

void Square()

{

glBegin(GL_QUADS);

glVertex3f(1.000000,1.000000,0.000000);

glVertex3f(1.000000,-1.000000,0.000000);

glRotatef(ANG,1,0,0);

glVertex3f(-1.000000,-1.000000,0.000000);

glVertex3f(-1.000000,1.000000,0.000000);

glEnd();

}

what then

chowe6685

05-12-2004, 11:25 AM

then you generate the new coordinate axis on the fly. For example if you are rotating around the y-axis

i' = cos(theta)*i + sin(theta)*k

j' = j

k' = cos(theta)*k + sin(theta)*i

note that cos and sin take angles in radians not degrees.

Also if you want more complex rotations you should read the matrix faq (search google) to get better idea how rotations work

jboss

05-17-2004, 08:25 AM

Ok got all this but wont I have to recalculate my normals too.

bumby

05-18-2004, 07:47 AM

Sure, but if you have organized your vertices with some kind of connectivty in mind (ie Faces contain vertices so that would be a good structure to use), then you can compute the normals of the vertex. For each face compute the face normal. Then for each face that contains the vertex, you can average all the face normals to get the vertex normal...

For example...if you have 1 vertex that is connected to 6 triangles, then you would have 6 face normals to average out.

you could also apply a weighting to each of the faces based on the area of the face.

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.