PDA

View Full Version : Rotate in local coordinates



jurek1
07-20-2009, 05:44 AM
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? :)


#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);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}

void display(void) {
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glLoadIdentity();
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);
glLoadIdentity();
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;
}

vivek vyas
07-20-2009, 05:56 AM
Make your drawing code sandwiched in

glMatrixMode(POJE..);glPushMatrix();

and
glPopMatrix();
glMatrixMode(MODEL...);
which from
glLoadIdentity to
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 :sleeping:

jurek1
07-20-2009, 06:52 AM
Sandwiching the code as you said didn't help.

vivek vyas
07-20-2009, 09:54 AM
is your display function look like

void display(void) {
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
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


//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);
glLoadIdentity();
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)
:sick:

jurek1
07-20-2009, 10:19 AM
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 :p

vivek vyas
07-20-2009, 10:44 AM
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

jurek1
07-20-2009, 10:51 AM
It also doesn't work :)

vivek vyas
07-20-2009, 11:08 AM
void idle()glutPostRedisplay();
and

glutIdleFunc(idle);
before MainLoop(.);

jurek1
07-20-2009, 12:15 PM
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.

vivek vyas
07-20-2009, 12:29 PM
that means these 4 point not to be rotated but the plane to be rotated right??
use glLoadIdentity();
to remove these kind of transformation and rotation of points

vivek vyas
07-20-2009, 12:37 PM
i think you are looking for this
or something like it


#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);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}

void display(void) {
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glLoadIdentity();
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();

glPopMatrix(); // 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);
glLoadIdentity();
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;
}

(perhaps in any forum solvng other problem by giving code is not good sign)
any way
i REALLY HOPE U WANT THIS

jurek1
07-20-2009, 12:51 PM
I want the plane to rotate around the axis that are drawn after


// Coordinates
glColor3f(1,1,1);
...

Try to get a PC and you will see :D

Edit: The above code is not what I mean. These white lines make the local coordinate system so they rotate with the plane. I want the plane to rotate around those axes.

vivek vyas
07-20-2009, 01:26 PM
i have given the code now u manipulate the position of glPopMatrix(); to these end of 3 lines
so that u can rotate whole scene by your key
like this one

#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);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}

void display(void) {
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glLoadIdentity();
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();
glutSwapBuffers();
}

void reshape(int w, int h){
glViewport(0,0,(GLsizei)w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
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;
}

and must read docs and
uses of these function
I HOPE IT WILL HELP CAUSE

THIS JOB WAS TOO SIMPLE I JUST WANT TO SAY THAT READ SOME EXAMPLES :D

jurek1
07-20-2009, 01:35 PM
Ok i will try to explain it with pictures :)

This is the scene at the beginning with no rotations (spin_* = 0)
http://avatar02.republika.pl/dump/1.png
Now I rotated it about axis z (spin_z = 300). This is OK.
http://avatar02.republika.pl/dump/2.png
Now I want to rotate it around the y axis of the plane (the one perpendicular to the plane) but it rotates around the green axis which is a global y axis.
http://avatar02.republika.pl/dump/3.png

So how to rotate around the white axis perpendicular to the plane?

vivek vyas
07-20-2009, 02:00 PM
its your mean original axis with Local axis

is plane has to rotate rotate in white lines axis
(is it your school/colledge assignment)

vivek vyas
07-20-2009, 02:12 PM
Use Some Maths here how i don't know but which portion of math

I think trignometric maths is good
sin cos tan thats clue i can give and
I HOPE YOU GET UNDERSTAND (belong to spin_x/y/z)
i think i need to wear googles now
:cool: