1. Rotate in local coordinates

Hi, I'm new OpenGL and I have a problem with rotations.
I would like to draw a plane and rotate it in about its local axis.
Here is my code. It draws a plane with its local coordinates. I would like to use the keys 'x', 'y', 'z' to rotate about corresponding axis. This doesn't work correctly. Could you please help me?

Code :
```#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

GLfloat spin_x = 0;
GLfloat spin_y = 0;
GLfloat spin_z = 0;

void init (void){
glClearColor(0.5,0.5,0.5,0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
}

void display(void) {
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
gluLookAt(10,10,10,0.0,0.0,0.0,0.0,1.0,0.0);

glRotated(spin_x,1,0,0);
glRotated(spin_y,0,1,0);
glRotated(spin_z,0,0,1);

// Plane
glBegin(GL_POLYGON);
glVertex3f(-1,0,-1);
glVertex3f(-1,0,1);
glVertex3f(1,0,1);
glVertex3f(1,0,-1);
glEnd();

// Coordinates
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glEnd();

glutSwapBuffers();
}

void reshape(int w, int h){
glViewport(0,0,(GLsizei)w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
gluPerspective(30,1,1,200);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y){
switch(key){
case 'x': spin_x +=2;break;
case 'y': spin_y +=2;break;
case 'z': spin_z +=2;break;
}
glutPostRedisplay();
}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow ("hello");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}```

2. Re: Rotate in local coordinates

Make your drawing code sandwiched in

glMatrixMode(POJE..);glPushMatrix();

and
glPopMatrix();
glMatrixMode(MODEL...);
which from
glEnd

perhaps you not need gluLookAt for rotating the object
and
Try Mesa's demos
or simple Devcpp codes(Windows)

I hope it will work for you

3. Re: Rotate in local coordinates

Sandwiching the code as you said didn't help.

4. Re: Rotate in local coordinates

is your display function look like
Code :
```void display(void) {
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluLookAt(10,10,10,0.0,0.0,0.0,0.0,1.0,0.0);

glPushMatrix();

glRotated(spin_x,1,0,0);
glRotated(spin_y,0,1,0);
glRotated(spin_z,0,0,1);

// Plane
glBegin(GL_POLYGON);
glVertex3f(-1,0,-1);
glVertex3f(-1,0,1);
glVertex3f(1,0,1);
glVertex3f(1,0,-1);
glEnd();

// Coordinates
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glEnd();
glPopMatrix();

glMatrixMode(GL_MODELVIEW);

glutSwapBuffers();
}```
and reshape function look like
Code :
```//most of reshape function look like this
void reshape(int w, int h){
GLfloat as = (GLfloat)w/(GLfloat)h;
if(h==0)        h=1;
glMatrixMode(GL_PROJECTION);
gluPerspective(30.0f,as,1.0f,200.0f);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h);
}```

perhaps it must work???
please take seriously that what is result glfunction 's
parameter u used
I hope forcely it must work(cause i had not tried it)

5. Re: Rotate in local coordinates

With glMatrixMode(GL_PROJECTION); at the beginning of the display function I can't see anything but background. When I change to GL_MODELVIEW the polygon and lines are displayd but do not rotate as I wish

6. Re: Rotate in local coordinates

i think remove LookAt call and put rotate command in that place as ireply that code please understand i m not using pc system i m on mobile cause my father taken that please apologies me for taking your time

7. Re: Rotate in local coordinates

It also doesn't work

8. Re: Rotate in local coordinates

void idle()glutPostRedisplay();
and

glutIdleFunc(idle);
before MainLoop(.);

9. Re: Rotate in local coordinates

Well, the problem is not that I can't display the plane but that it doesn't rotete around its locl axis. It rotates about the world coordinates. I think I should change the rotations but I don't know how.

10. Re: Rotate in local coordinates

that means these 4 point not to be rotated but the plane to be rotated right??