PDA

View Full Version : Rotations with OpenGL and GLUT



darcome
07-05-2002, 01:49 PM
From what I have understood, the rotations in OpenGL, are made rotating the axis. Because rotating the axis, the polygons are rotated too, so the coordinates of the polygon, remain the same.

There's a way to rotate the objects but not the axis? Or, there's a way that after I have rotated the axis and the polygon (using the OpenGL functions which are faster when any other routine, I suppose), can I retrieve the coordinates of the polygon like I have rotated it, instead of the axis, and use these coordinates with the Identity matrix?

Jambolo
07-05-2002, 10:26 PM
OpenGL has something called "feedback" which might do what you need. I've never used it. The fastest, easiest, and most efficient way to do it is probably to just transform the points yourself.

darcome
07-06-2002, 03:09 AM
I could do it if I have only a few vertex on the screen, but what happens if my vertex become many?

Deiussum
07-06-2002, 04:37 AM
You could always keep track of the modelview matrix yourself and then do your own matrix-vector multiplication to find out the final results.

no-one
07-06-2002, 09:48 AM
after much thought i think i've figured out what you want.

heres how you might do it
try this form.

the rotations/transformations inside the glPushMatrix() and glPopMatrix(), should not affect, any outside rotations or transformations.





DrawAxes();

//glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glTranslatef(...);
glRotate(...);

DrawObject();

PopMatrix();




[This message has been edited by no-one (edited 07-06-2002).]

darcome
07-07-2002, 02:48 AM
This is what I've done with the code you provided me no_one, did I do the right thing?



#include <GL/glut.h>
#include <stdio.h>

void init ();
void display (void);
void keyboard (unsigned char, int, int);
void draw (void);
void specialkeys (int, int, int);
void initializeMatrix ();

//int matrice[9][8];
int anglex = 0, angley = 0, anglez = 0, check = 0;
int m[16];

void main (int argc, char** argv)
{
glutInit (&amp;argc, argv);

initializeMatrix ();
init ();

glutDisplayFunc (draw);
glutKeyboardFunc (keyboard);
glutSpecialFunc (specialkeys);

glutMainLoop();
}

void init (void)
{
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glMatrixMode (GL_MODELVIEW);

// creazione finestra
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Darcome");

glClearDepth (1.0); // Depth Buffer Setup
glEnable (GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc (GL_LEQUAL); // The Type Of Depth Test To Do

glShadeModel (GL_SMOOTH); // Enable Smooth Shading
glClearColor (0.0, 0.0, 0.0, 0.0); // Black Background
glClearDepth (1.0f); // Depth Buffer Setup
glEnable (GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing To Do
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

void display(void)
{
}

void keyboard (unsigned char key, int x, int y)
{
//termino il programma
if (key == 27) exit (0); // pressione tasto ESC
}

void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glLoadIdentity ();

glTranslatef(0.0, -0.5, 0.0);

/*
if (ccheck == 0)
{
glRotatef (angley, 1.0, 0.0, 0.0);
glRotatef (anglex, 0.0, 1.0, 0.0);
}
else if (check == 1)
{
glRotatef (anglex, 0.0, 1.0, 0.0);
glRotatef (angley, 1.0, 0.0, 0.0);
}
*/


glBegin (GL_LINES);

glColor3f (1.0, 0.0, 0.0);

glVertex3f (-1.0, 0.0, 0.0); // asse X
glVertex3f (1.0, 0.0, 0.0);

glColor3f (0.0, 1.0, 0.0);

glVertex3f (0.0, 0.0, 0.0); // asse Y
glVertex3f (0.0, 1.8, 0.0);

glColor3f (1.0, 1.0, 0.0);

glVertex3f (0.0, 0.0, -1.0); // asse Z
glVertex3f (0.0, 0.0, 1.0);

glEnd ();

// parete laterale sinistra della gamba sinistra

glColor3f (0.5, 0.5, 1.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.5, 0.0, -0.5);
glVertex3f (-0.5, 0.2, -0.25);
glVertex3f (-0.5, 0.2, 0.25);
glEnd ();

// parete laterale destra della gamba sinistra

glColor3f (1.0, 0.0, 0.4);

glBegin (GL_QUADS);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.4, 0.2, 0.25);
glEnd ();

// parte frontale posteriore della gamba sinistra

glColor3f (1.0, 0.0, 0.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.2, 0.25);
glVertex3f (-0.5, 0.2, 0.25);
glEnd ();

// parte frontale anteriore della gamba sinistra

glColor3f (1.0, 0.4, 0.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, -0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.5, 0.2, -0.25);
glEnd ();

// base superiore della gamba sinistra

glColor3f (0.0, 1.0, 0.3);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.2, 0.25);
glVertex3f (-0.4, 0.2, 0.25);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.5, 0.2, -0.25);
glEnd ();

// base inferiore della gamba sinistra

glColor3f (0.4, 0.5, 0.4);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.5, 0.0, -0.5);
glEnd ();

glutSwapBuffers ();

glFlush ();
}

void specialkeys (int key, int x, int y)
{
if (key == GLUT_KEY_F1) exit (0);

// rotazione dello "schermo" a destra
if (key == GLUT_KEY_RIGHT)
{
anglex = anglex - 30;
check = 1;

if (anglex == -360) anglex = 0;

glPushMatrix ();
glLoadIdentity ();

glRotatef (anglex, 0.0, 1.0, 0.0);
glRotatef (angley, 1.0, 0.0, 0.0);

draw ();

glPopMatrix();
}

// rotazione dello "schermo" a sinistra
else if (key == GLUT_KEY_LEFT)
{
anglex = anglex + 30;
check = 1;

if (anglex == 360) anglex = 0;

glPushMatrix ();
glLoadIdentity ();

glRotatef (anglex, 0.0, 1.0, 0.0);
glRotatef (angley, 1.0, 0.0, 0.0);

draw ();

glPopMatrix ();
}

// rotazione dello "schermo" in su
else if (key == GLUT_KEY_UP)
{
angley = angley + 30;
check = 0;

if (angley == 360) angley = 0;

glPushMatrix ();
glLoadIdentity ();

glRotatef (angley, 1.0, 0.0, 0.0);
glRotatef (anglex, 0.0, 1.0, 0.0);

draw ();

glPopMatrix ();
}
// rotazione dello "schermo" in basso
else if (key == GLUT_KEY_DOWN)
{
angley = angley - 30;
check = 0;

if (angley == -360) angley = 0;

glPushMatrix ();
glLoadIdentity ();

glRotatef (angley, 1.0, 0.0, 0.0);
glRotatef (anglex, 0.0, 1.0, 0.0);

draw ();

glPopMatrix ();
}

}

void initializeMatrix ()
{
/* int i, j;

for (i = 0; i < 10; i++)
for (j = 0; j < 9; j++)
matrice[i][j] = 0;*/
}

darcome
07-07-2002, 02:59 AM
for the rest of you that want to help me, this is the original code I wrote. If you wnat understand exectly what's wrong in my program, execute it, and press RIGHT, UP, UP, RIGHT and you'll see a strange rotation



#include <GL/glut.h>
#include <stdio.h>

void init ();
void display (void);
void keyboard (unsigned char, int, int);
void draw (void);
void specialkeys (int, int, int);
void initializeMatrix ();

//int matrice[9][8];
int anglex = 0, angley = 0, anglez = 0, check = 0;
int m[16];

void main (int argc, char** argv)
{
glutInit (&amp;argc, argv);

initializeMatrix ();
init ();

glutDisplayFunc (draw);
glutKeyboardFunc (keyboard);
glutSpecialFunc (specialkeys);

glutMainLoop();
}

void init (void)
{
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glMatrixMode (GL_MODELVIEW);

// creazione finestra
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Darcome");

glClearDepth (1.0); // Depth Buffer Setup
glEnable (GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc (GL_LEQUAL); // The Type Of Depth Test To Do

glShadeModel (GL_SMOOTH); // Enable Smooth Shading
glClearColor (0.0, 0.0, 0.0, 0.0); // Black Background
glClearDepth (1.0f); // Depth Buffer Setup
glEnable (GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing To Do
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

void display(void)
{
}

void keyboard (unsigned char key, int x, int y)
{
//termino il programma
if (key == 27) exit (0); // pressione tasto ESC
}

void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

glTranslatef(0.0, -0.5, 0.0);

if (check == 0)
{
glRotatef (angley, 1.0, 0.0, 0.0);
glRotatef (anglex, 0.0, 1.0, 0.0);
}
else if (check == 1)
{
glRotatef (anglex, 0.0, 1.0, 0.0);
glRotatef (angley, 1.0, 0.0, 0.0);
}

glBegin (GL_LINES);

glColor3f (1.0, 0.0, 0.0);

glVertex3f (-1.0, 0.0, 0.0); // asse X
glVertex3f (1.0, 0.0, 0.0);

glColor3f (0.0, 1.0, 0.0);

glVertex3f (0.0, 0.0, 0.0); // asse Y
glVertex3f (0.0, 1.8, 0.0);

glColor3f (1.0, 1.0, 0.0);

glVertex3f (0.0, 0.0, -1.0); // asse Z
glVertex3f (0.0, 0.0, 1.0);

glEnd ();

// parete laterale sinistra della gamba sinistra

glColor3f (0.5, 0.5, 1.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.5, 0.0, -0.5);
glVertex3f (-0.5, 0.2, -0.25);
glVertex3f (-0.5, 0.2, 0.25);
glEnd ();

// parete laterale destra della gamba sinistra

glColor3f (1.0, 0.0, 0.4);

glBegin (GL_QUADS);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.4, 0.2, 0.25);
glEnd ();

// parte frontale posteriore della gamba sinistra

glColor3f (1.0, 0.0, 0.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.2, 0.25);
glVertex3f (-0.5, 0.2, 0.25);
glEnd ();

// parte frontale anteriore della gamba sinistra

glColor3f (1.0, 0.4, 0.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, -0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.5, 0.2, -0.25);
glEnd ();

// base superiore della gamba sinistra

glColor3f (0.0, 1.0, 0.3);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.2, 0.25);
glVertex3f (-0.4, 0.2, 0.25);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.5, 0.2, -0.25);
glEnd ();

// base inferiore della gamba sinistra

glColor3f (0.4, 0.5, 0.4);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.5, 0.0, -0.5);
glEnd ();

glutSwapBuffers ();

glFlush ();
}

void specialkeys (int key, int x, int y)
{
if (key == GLUT_KEY_F1) exit (0);

// rotazione dello "schermo" a destra
if (key == GLUT_KEY_RIGHT)
{
anglex = anglex - 30;
check = 1;

if (anglex == -360) anglex = 0;

draw ();
}

// rotazione dello "schermo" a sinistra
else if (key == GLUT_KEY_LEFT)
{
anglex = anglex + 30;
check = 1;

if (anglex == 360) anglex = 0;

draw ();
}

// rotazione dello "schermo" in su
else if (key == GLUT_KEY_UP)
{
angley = angley + 30;
check = 0;

if (angley == 360) angley = 0;

draw ();
}

// rotazione dello "schermo" in basso
else if (key == GLUT_KEY_DOWN)
{
angley = angley - 30;
check = 0;

if (angley == -360) angley = 0;

draw ();
}

}

void initializeMatrix ()
{
/* int i, j;

for (i = 0; i < 10; i++)
for (j = 0; j < 9; j++)
matrice[i][j] = 0;*/
}

no-one
07-08-2002, 06:46 PM
no, not really... what i mean is encapsulate anything you don't wan transformed or rotated in between gl(Push/Pop)Matrix()

like so

is this anywhere near what you wanted?





#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>

void init ();
void display (void);
void keyboard (unsigned char, int, int);
void draw (void);
void specialkeys (int, int, int);
void initializeMatrix ();

//int matrice[9][8];
int anglex = 0, angley = 0, anglez = 0, check = 0;
int m[16];

void main (int argc, char** argv)
{
glutInit (&amp;argc, argv);

initializeMatrix ();
init ();

glutDisplayFunc (draw);
glutKeyboardFunc (keyboard);
glutSpecialFunc (specialkeys);

glutMainLoop();
}

void init (void)
{
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glMatrixMode (GL_MODELVIEW);

// creazione finestra
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Darcome");

glClearDepth (1.0);// Depth Buffer Setup
glEnable (GL_DEPTH_TEST);// Enables Depth Testing
glDepthFunc (GL_LEQUAL);// The Type Of Depth Test To Do

glShadeModel (GL_SMOOTH);// Enable Smooth Shading
glClearColor (0.0, 0.0, 0.0, 0.0);// Black Background
glClearDepth (1.0f);// Depth Buffer Setup
glEnable (GL_DEPTH_TEST);// Enables Depth Testing
glDepthFunc (GL_LEQUAL);// The Type Of Depth Testing To Do
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

void display(void)
{
}

void keyboard (unsigned char key, int x, int y)
{
//termino il programma
if (key == 27) exit (0); // pressione tasto ESC
}

void draw (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

glTranslatef(0.0, -0.5, 0.0);

if (check == 0)
{
glRotatef (angley, 1.0, 0.0, 0.0);
glRotatef (anglex, 0.0, 1.0, 0.0);
}
else if (check == 1)
{
glRotatef (anglex, 0.0, 1.0, 0.0);
glRotatef (angley, 1.0, 0.0, 0.0);
}

glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, -0.5, 0.0);
glBegin (GL_LINES);

glColor3f (1.0, 0.0, 0.0);

glVertex3f (-1.0, 0.0, 0.0);// asse X
glVertex3f (1.0, 0.0, 0.0);

glColor3f (0.0, 1.0, 0.0);

glVertex3f (0.0, 0.0, 0.0);// asse Y
glVertex3f (0.0, 1.8, 0.0);

glColor3f (1.0, 1.0, 0.0);

glVertex3f (0.0, 0.0, -1.0);// asse Z
glVertex3f (0.0, 0.0, 1.0);

glEnd ();
glPopMatrix();
// parete laterale sinistra della gamba sinistra

glColor3f (0.5, 0.5, 1.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.5, 0.0, -0.5);
glVertex3f (-0.5, 0.2, -0.25);
glVertex3f (-0.5, 0.2, 0.25);
glEnd ();

// parete laterale destra della gamba sinistra

glColor3f (1.0, 0.0, 0.4);

glBegin (GL_QUADS);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.4, 0.2, 0.25);
glEnd ();

// parte frontale posteriore della gamba sinistra

glColor3f (1.0, 0.0, 0.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.2, 0.25);
glVertex3f (-0.5, 0.2, 0.25);
glEnd ();

// parte frontale anteriore della gamba sinistra

glColor3f (1.0, 0.4, 0.0);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, -0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.5, 0.2, -0.25);
glEnd ();

// base superiore della gamba sinistra

glColor3f (0.0, 1.0, 0.3);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.2, 0.25);
glVertex3f (-0.4, 0.2, 0.25);
glVertex3f (-0.4, 0.2, -0.25);
glVertex3f (-0.5, 0.2, -0.25);
glEnd ();

// base inferiore della gamba sinistra

glColor3f (0.4, 0.5, 0.4);

glBegin (GL_QUADS);
glVertex3f (-0.5, 0.0, 0.5);
glVertex3f (-0.4, 0.0, 0.5);
glVertex3f (-0.4, 0.0, -0.5);
glVertex3f (-0.5, 0.0, -0.5);
glEnd ();

glutSwapBuffers ();

glFlush ();
}

void specialkeys (int key, int x, int y)
{
if (key == GLUT_KEY_F1) exit (0);

// rotazione dello "schermo" a destra
if (key == GLUT_KEY_RIGHT)
{
anglex = anglex - 30;
check = 1;

if (anglex == -360) anglex = 0;

draw ();
}

// rotazione dello "schermo" a sinistra
else if (key == GLUT_KEY_LEFT)
{
anglex = anglex + 30;
check = 1;

if (anglex == 360) anglex = 0;

draw ();
}

// rotazione dello "schermo" in su
else if (key == GLUT_KEY_UP)
{
angley = angley + 30;
check = 0;

if (angley == 360) angley = 0;

draw ();
}

// rotazione dello "schermo" in basso
else if (key == GLUT_KEY_DOWN)
{
angley = angley - 30;
check = 0;

if (angley == -360) angley = 0;

draw ();
}

}

void initializeMatrix ()
{
/*int i, j;

for (i = 0; i < 10; i++)
for (j = 0; j < 9; j++)
matrice[i][j] = 0;*/
}