PDA

View Full Version : Newbie rotate Question



MOlOX
12-13-2009, 02:25 PM
hi,

i've 'so newbie' question. If you can help, i will be glad

Think we have 10 points V1, V2, V3, V4, V5, V6, V7 ,V8, V9, V10

When i draw these lines , everything is OK

To goal is , rotate these points on x-axis and form 3d object

when i use this code sniplet, it is ok

for(int i= 0 ; i< 360; i++)
{
glRotatef(1,1,0,0);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(10,0,0);
glVertex3f(10,10,0);
glVertex3f(0,10,0);
glEnd();
}

This code draws object that i want, but ofcourse it does not form 3d solid object.

So lathing is needed to form 3d object.

So i thought this way to do lathing the object

points i know are v1,v2,v3,v4,v5,v6,v7,v8,v9,v10
and if we say, points X degrees rotated on x-axis are v1',v2',v3',v4',v5',v6',v7',v8',v9',v10'
and again , points 2X degress rotated on x-axis are v1'',v2'',v3'',v4'',v5'',v6'',v7'',v8'',v9'',v10''
and so on for k.X = 360 degress

than i thought if i draw GL_QUADS on v1,v2,v1',v2' and v1',v1'',v2',v2'' then v1'',v1''',v2'',v2''' , so on until 360 degrees completed
then draw GL_QUADS on v2,v3,v2',v3' , v2',v3',v2'',v3'' and then v2'',v3'',v2''',v3''' for 360 degrees
and so on to the v9,v10 series


so when i draw all quads object lathing would be completed.


The Question / Problem is i dont know new coordinates of v1,v2,v3,v4,..... points' X degrees rotated coordiates v1',v2',v3'.....
(and likewise 2x rotated v1'',v2'',v3'',v4'',....)


so i tried to use glRotatef function to accomplish this.

for (int v = 0; v< 10 ; k++)
{
for (int i = 0; i< 360 ; i++)
{
glBegin(GL_QUADS);

//first 2 points of Quad with unrotated vertexes
glVertex3f(v[v].x,v[v].y,v[v].z);
glVertex3f(v[v + 1].x,v[v + 1].y,v[v + 1].z);

//rotate X degrees to get to 360 degress in total
glRotatef(1,1,0,0);

//3rd and 4th vertexes are points that are rotated 1 degrees on x-axis
glVertex3f(v[v].x,v[v].y,v[v].z);
glVertex3f(v[v + 1].x,v[v + 1].y,v[v + 1].z);

glEnd();
}
}

But as far as i get, glRotatef(1,1,0,0); does nothing between glbegin and glend , so i could not able to do lathing 360 degrees of point by drawing quads.

if somehow i could calculate v1' , v1'' (x degrees rotated on x-axis) of point v1

than i would have all points i need , so i can draw GL_QUAD and i would have been able to lathe object.


What can i do in this situation. Find math library, which calculates v[x,y,z] => [x Degrees rotate on any axis] => v'(x',y',z')

or can you advice something else for lathing

Thank you very much

mikau
12-13-2009, 04:59 PM
You need to keep in mind that OpenGL is a graphics library, not a math library. The rotation command only multiplies the current matrix by the rotation matrix, so that when you draw a new object, the coordinates are rotated before being displaced. It has no effect on the variables you supply as arguments.

Here is the math to apply a rotation on the XY plane:

x' = x cos(t) - y sin(t)
y' = x sin(t) + y cos(t)

This will rotate the point (x,y) t degrees around the origin.

Be careful! one mistake I frequently make when doing this is to say:


x = x*cos(t)-y*sin(t);
y = x*sin(t)+y*cos(t);

this is incorrect because x is changed in the first line, and you want the original x in the second line. So the correct method is:


new_x=x*cos(t)-y*sin(t);
new_y=x*sin(t)+y*cos(t);
x=new_x;
y=new_y;

The other thing you might wonder is what if we want to rotate about a point other than the origin? The simplest method I know is to first shift the points so that the axis of rotation is at the origin, perform the rotation, then shift back.

Here's a function that will rotate two points (x,y) on the XY plane by (angle) degrees about the point (cx,cy):



void rotateXY(float &amp;x, float &amp;y, float cx, float cy, float angle)
{
x-=cx; // shift the center point to the origin
y-=cy;

angle=angle*3.141592653f/180.0f; // convert to radians

// rotate point
float new_x=x*cos(angle)-y*sin(angle);
float new_y=x*sin(angle)+y*cos(angle);
x=new_x;
y=new_y;

// shift back to correct location
x+=cx;
y+=cy;
}


Lastly, if you want to perform rotations on the YZ plane, then you replace (x,y) with (y,z), and if you want to perform rotations on the ZX plane, you replace (x,y) with (z,x).

MOlOX
12-14-2009, 02:33 AM
thank you very much for your advice