OpenGL - Rotate

Soldato
Joined
22 Oct 2005
Posts
2,879
Location
Moving...
I've drawn an aeroplane in openGL and all I want to do is make it rotate around the x axis. However I want to rotate it around itself rather than around 0,0,0. The centre of the aeroplane is (50000,50000,-10000)(x,y,z).

I've tried all kinds of combinations of numbers but I cant get it to to rotate correctly. I'm using glRotatef(angle, X, Y, Z). I thought it would simply be either glRotatef(angle, 1, 0, 0) or glRotatef(angle, 50000, 0, 0) neither of these work, the closest I've come is using glRotatef(angle, 50000, 50000, -10000), this rotates around itself but not around the x-axis it seems to go round all 3!! :confused:

Sorry this is a really stupid question, but what am I doing wrong?

Thanks.
 
Code:
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	//blue square
	glPushMatrix();
		glRotatef(spin, 0.0, 0.0, 1.0);
		glTranslatef(20,0,0);
		glRotatef(spin, 0.0, 0.0, 1.0);
		glColor3f(0.5,0.5,1.0);
		glBegin(GL_POLYGON);
			glVertex2f(-4,-4);
			glVertex2f(-4,4);
			glVertex2f(4,4);
			glVertex2f(4,-4);
		glEnd();
		//green triangle
		glPushMatrix();
			glRotatef(spin, 0.0, 0.0, 1.0);
			glTranslatef(9,0,0);
			glRotatef(spin, 0.0, 0.0, 1.0);
			glColor3f(0.5,1.0,0.5);
			glBegin(GL_TRIANGLES);
				glVertex2f(-3,-3);
				glVertex2f(3,-3);
				glVertex2f(0,3);
			glEnd();
		glPopMatrix();
	glPopMatrix();

	//spinning red square
	glPushMatrix();
	
	glRotatef(spin, 0.0, 0.0, 1.0);
	

	glColor3f(1.0,0.0,0.0);
	glBegin(GL_POLYGON);
		glVertex2f(-8,-8);
		glVertex2f(-8,8);
		glVertex2f(8,8);
		glVertex2f(8,-8);
	glEnd();

	glPointSize(4.0);
	glColor3f(1,1,0);
	glBegin(GL_POINTS);
		glVertex2f(0,0);
	glEnd();
	glPointSize(1.0);
	glPopMatrix();

	glutSwapBuffers();
}


void spinDisplay(void)
{
	spin += spin_incr;
	if(spin>360.0)spin-=360.0;
	glutPostRedisplay();
}

IIRC this should do what you want for at least one of the shapes, though it's been a while since I've played around with it (and don't have Visual Studio at the moment) so you'll have to work it out from that yourself.

Try it out and see how you get on.

Also, when you say around the X axis, do you mean you want it to flip 'in' to the screen or rotate like a wheel? If the latter you want to be rotating around the Z axis.
 
Last edited:
Thanks Phil, that code does work but that rotates it around 0,0,0 because that is the centre of each of the shapes. The problem I have is that mine isn't at 0,0,0, it's at 50000,50000,-10000 but I think it's still rotating it around 0,0,0 rather than 50000,50000,-10000 which is messing things up
 
In OpenGl all rotations occur around (0,0,0). The three parameters after angle in glRotatef define the axis of rotation, which by definition passes through (0,0,0).

Typically you should always create objects centered on (0,0,0) then scale them, then rotate them and finally translate them to their correct position. This way the rotation happens when the object is still situated at the origin.

So you should create your plane at (0,0,0), then do the rotation, then translate it to (50000,50000,-10000).
 
Sure. Here it is:

Code:
float xCord =50000.0; 
float yCord =50500.0;
float zCord =-10000.0; 
float xRotP =90.0;
float yRotP =90.0;
float xTest = 0.0;


void display (void){


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_VERTEX_ARRAY);

	glColorPointer(3, GL_FLOAT, 0, colors);
	glVertexPointer(3, GL_FLOAT, 0, vertices);

	glDrawElements(GL_TRIANGLES, indexSize, GL_UNSIGNED_INT, index);


	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	getNames();	
	drawNames();
	
	drawPlane();

	glPopMatrix();
	glutSwapBuffers();
}



void drawPlane(){

	glRotatef(xTest, 1.0, 0.0, 0.0);   //******PROBLEM!!!!*******

	//Draw Plane
	glColor4f(1.0,1.0,1.0,0.5);
	quadratic=gluNewQuadric();
	
	//Main part
	glPushMatrix();	 
	glTranslatef(xCord,yCord,zCord);
	glRotatef(yRotP,0.0,1.0,0.0); //Parallel with ground
	glRotatef(xRotP,1.0,0.0,0.0); //Direction
	gluCylinder(quadratic,700.0,700.0,5000.0,30,30);	
	glPopMatrix();
	
	//Back of plane
	glPushMatrix(); 
	glTranslatef(xCord,yCord+3000.0,zCord);
	glRotatef(yRotP,0.0,1.0,0.0); 
	glRotatef(xRotP,1.0,0.0,0.0); 
	gluCylinder(quadratic,50.0,700.0,3000.0,30,30);
	glPopMatrix();
	
	//Front of plane
	glPushMatrix(); 
	glTranslatef(xCord,yCord-5000.0,zCord);
	glRotatef(yRotP,0.0,1.0,0.0);
	glRotatef(xRotP,1.0,0.0,0.0); 
	gluSphere(quadratic,700.0,30,30);
	glPopMatrix();

	

	glColor4f(1.0,1.0,1.0,0.75);
	
	//Right Wing
	glPushMatrix();
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0); glVertex3f(xCord+700.0, yCord-3500.0, zCord); 
	glTexCoord2f(0.0, 1.0); glVertex3f(xCord+700.0, yCord-1500, zCord); 
	glTexCoord2f(1.0, 1.0); glVertex3f(xCord+7000.0, yCord, zCord); 
	glTexCoord2f(1.0, 0.0); glVertex3f(xCord+7000.0, yCord-2000, zCord); 
	glEnd();
	glPopMatrix();

	
	//Left Wing
	glPushMatrix();	
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0); glVertex3f(xCord-7000.0, yCord-2000.0, zCord); 
	glTexCoord2f(0.0, 1.0); glVertex3f(xCord-7000.0, yCord, zCord); 
	glTexCoord2f(1.0, 1.0); glVertex3f(xCord-700.0, yCord-1500.0, zCord); 
	glTexCoord2f(1.0, 0.0); glVertex3f(xCord-700.0, yCord-3500.0, zCord); 
	glEnd();
	glPopMatrix();
}

I think that's all you need, let me know if it isn't.

Basically the program is a map, and on the map I want to include an plane which can be moved around the map. All the stuff in the display function displays the map (this all works) and the function drawPlane() draws the plane after the terrain has been drawn.

In the drawPlane() I set up the parts of the plane, it consists of a main 'hull', front nose, tail, and two wings. These are all defined individually in the drawPlane() function, they each have their own glTranslate and glRotate to get them into the correct position initially. The idea is then use the rotate function right at the top of the function to rotate the entire plane as if it were just a single object. The angle of rotation is controlled by the xTest variable, this is changed via the keyboard.

I think I just need to change the parameters inside this problematic rotate function, but I dont know what to!!

Thanks again
 
Back
Top Bottom