rotate 2D object

hi,
I’m trying to draw a 2D object (triangle or polygon) when i move the mouse the object that has been drawn must be rotate on x, y axis.
the following code is my implementation:

void draw_triangle(void){
glTranslatef(translate_x, translate_y, 0);
glRotatef(beta, 1, 0, 0);
glRotatef(alpha, 0, 1, 0);
glColor3f(1, 1, 1);

if(strcmp(_triangle->texture_path, "" ) != 0){
	glBegin(GL_TRIANGLES);
		glTexCoord2f( (_triangle->v[0]).x, (_triangle->v[0]).y);
		glVertex2f((_triangle->v[0]).x, (_triangle->v[0]).y);
		glTexCoord2f( (_triangle->v[1]).x, (_triangle->v[1]).y);
		glVertex2f((_triangle->v[1]).x, (_triangle->v[1]).y);
		glTexCoord2f ((_triangle->v[2]).x, (_triangle->v[2]).y);
		glVertex2f((_triangle->v[2]).x, (_triangle->v[2]).y);
	glEnd();
}else{
	glBegin(GL_TRIANGLES);
		glVertex2f((_triangle->v[0]).x, (_triangle->v[0]).y);
		glVertex2f((_triangle->v[1]).x, (_triangle->v[1]).y);
		glVertex2f((_triangle->v[2]).x, (_triangle->v[2]).y);
	glEnd();
}

glScalef(xscale, yscale, 1.0f);

}

void DisplayFunc_triangle(void){

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glViewport(0, 0, width / 2, height / 2);
glPushMatrix();
glTranslatef(0, 0, -10);
glRotatef(0, 1, 0, 0);
glRotatef(0, 0, 1, 0);

GLfloat light_position[] = { 0, 0, 1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
draw_axis();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
draw_triangle();
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPushMatrix();

glLoadIdentity();
glOrtho(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);
glMatrixMode(GL_MODELVIEW);

glViewport(0, height / 2 + 1, width / 2 + 1, height / 2);
glPushMatrix();
glRotatef(90, 0, 1, 0);
draw_axis();

enable_texture2D();
draw_triangle();
glPopMatrix();

disable_texture2D();
glViewport(width / 2 + 1, height / 2 + 1, width / 2, height / 2);
glPushMatrix();
draw_axis();
enable_texture2D();
draw_triangle();
glPopMatrix();
disable_texture2D();

glViewport(width / 2 + 1, 0, width / 2, height / 2);
glPushMatrix();
glRotatef(90, 1, 0, 0);
draw_axis();
enable_texture2D();
draw_triangle();
glPopMatrix();
disable_texture2D();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

glFlush();
glutSwapBuffers();

}

void MotionFunc_triangle(int x, int y){
if (GLUT_DOWN == left_click){
beta = beta + (y - yold) / 2.f;
alpha = alpha + (x - xold) / 2.f;
glutPostRedisplay();
}else if(GLUT_DOWN == right_click){
translate_x = translate_x + (x - xold )/300.0;
translate_y = translate_y + (y - yold )/300.0;
glutPostRedisplay();
}
xold = x;
yold = y;
}

when i click left button i get strange rotation… because I specify the rotation only on the x,y axis but the object move on x, y and z axis… why? can you help me please?

I may be misunderstanding you, but I believe that you want the object to always face the viewer, correct? If so the problem is that you’re rotating around the x and y axis instead of the z:

Around the x axis:

Around the y axis:

Around the z axis:

thanks mark for reply,
I want rotate the object only on the x or y or x,y axis (depend on mouse position).
I think there is an error(in my code) because the object rotate around x, y, z axis.

So you are trying to do 3D rotations? It isn’t going to look correct since you’re using glOrtho.

It you rotate, say, 45° on the x axis and then 45° on the y axis, the object will appear to rotate along the z axis since the vertices are being transformed along z. See the above pics.

I’m really not understanding what you’re trying to accomplish. It sounds like you are trying to implement a 3D trackball, however, without perspective correction, the object will just shrink on the axis of rotation. If you are trying to accomplish something like the last pic I posted, rotating around the x and y axis will not accomplish it.

I’m using 4 viewport, in first viewport you can see a perspective view in the others projected object. when I use glutSolid* the rotation work well (the object rotate only on the x, y axis). I get bad rotation of the object only when I use
glBegin (GL_TRIANGLES) … or glBegin(GL_QUADS), moreover, in the last case the object is also translated on the x,y, z axis.
how can i solve this strange rotation :)!! the viewport is necessary and is necessary display the projected object in the
others viewport.

I don’t understand why: in first case (glutSolid*) rotation works well and second case (GL_TRIANGLES) no.
I’m trying to do a 2D rotation of the object.