PDA

View Full Version : Independent Rotation



fanousz
05-19-2005, 12:26 AM
i am doing a acar dashboard and i want to know how to rotate the pointers without rotating the whole scene.i tried putting PUSH MATRIX and POP MATRIX,it didn't work.HELP!!!

enmaniac
05-19-2005, 01:01 AM
Hi!

Are you sure you are doing everything ok. AFAIK it should work.


glPushMatrix();

// reset matrix to identity ( if you dont want all scene transformations )
glLoadIdentity();
glRotatef(..);// rotate your pointer

// draw your pointer
// ..

glPopMatrix();Have you done sth like this ?

05-20-2005, 11:58 AM
here is some of my code.when the opengl screen comes.it's like it's going to rotate but doesn't,kinda like a rotate function without the idle func.

glPushMatrix();
glLoadIdentity();
glColor3f(0.5f,0.5f,0.5f);
glRotatef(rotTheta,0.0f,0.0f,0.5f);
glTranslatef(0.0f, 0.0f, -3.0f);
DrawCircle(1.5);
DrawPointer();
glutIdleFunc(idle);
glPopMatrix();

Ehsan Kamrani
05-20-2005, 12:47 PM
I wrote a new program for you. See the display function.Can you please compile this and see the results?(it works correctly in my system )
Note 1:It's better to use from the unit normal vectors in your OpenGL functions. although your glRotate() works correctly, but i extremely suggest you to use from the unit normal vectors.
Note 2:See the functions idle() and glutIdleFunc(idle).see that where and how i have used from these functions.

//Ehsan Kamrani
//Rotation

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

int wh = 0;

void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0f, 1.0f, 0.0f );

}

void reshape( int width, int height )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height );

wh = height;
}

void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glLoadIdentity();
static float angle = 0.0f;
angle += 0.5;
if( angle >=360.0f) angle -= 360.0f;
glRotatef(angle,0.0f,0.0f,1.0f);
glTranslatef(0.0f, 0.0f, -3.0f);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f );
glPopMatrix();
glutSwapBuffers();
}

void idle()
{
glutPostRedisplay();
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( 500, 500 );
glutCreateWindow( "Rotation" );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutIdleFunc( idle );
init();
glutMainLoop();
return 0;
}

Hope this solves your problems...
-Ehsan-

05-20-2005, 11:36 PM
it worked.thank you so much.

05-21-2005, 05:14 AM
the pointer works,but now i cant't stop it,it keeps rotating.

Ehsan Kamrani
05-21-2005, 07:12 AM
You have many options.
I updated the source code and added a keyborad function to it.
Note 1: Declare the *angle* variable as a global variable.The reason is that you want to use from this variable in both the *display* and *keyboard* functions.
Note 2:Take a look at the functions *keyboard* and *glutKeyboardFunc( keyboard )*.When you press the A or a key, the square is rotated. when you release the key, it isn't rotated.
Here's the code:

//Ehsan Kamrani
//Rotation

#include <GL/glut.h>
#include <iostream.h>
float angle = 0.0f;

int wh = 0;

void init()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0f, 1.0f, 0.0f );

}

void reshape( int width, int height )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height );

wh = height;
}

void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glLoadIdentity();
if( angle >=360.0f) angle -= 360.0f;
glRotatef(angle,0.0f,0.0f,1.0f);
glTranslatef(0.0f, 0.0f, -3.0f);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f );
glPopMatrix();
glutSwapBuffers();
}

void keyboard( unsigned char key, int x, int y )
{
switch( key )
{
case 'A':case 'a':
angle += 1.0f;
break;
}
}

void idle()
{
glutPostRedisplay();
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize( 500, 500 );
glutCreateWindow( "Rotation" );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutIdleFunc( idle );
glutKeyboardFunc( keyboard );
init();
glutMainLoop();
return 0;
}

-Ehsan-

05-21-2005, 12:07 PM
yes,i i know how to put a keyboard function,but i want the pointer to stop at a point while i am pressing.

Ehsan Kamrani
05-21-2005, 12:39 PM
OK. Now i understand your problem.
In the previous example, declare these new global variables:
int stopPoint = 1.0;
float translate_x = 0.0f;
change your keyboard function:( it's only an example )

void keyboard( unsigned char key, int x, int y )
{
if( ( key == 's' &#0124;&#0124; key == 'S' ) && ( translate_x <= stopPoint ) )
{
translate_x += 0.01;
angle += 0.5;
}
if( ( key == 'a' &#0124;&#0124; key == 'A' ) && ( translate_x >= -stopPoint ) )
{
translate_x -= 0.01;
angle -= 0.5;
}

and finally change your display function:
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glLoadIdentity();
if( angle >=360.0f) angle -= 360.0f;
glTranslatef(translate_x, 0.0f, -5.0f);
glRotatef(angle,0.0f,0.0f,1.0f);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f );
glPopMatrix();
glutSwapBuffers();
}

-Ehsan-

xiaomi
05-21-2005, 05:51 PM
how to achieve the function use the gluLookAt

xiaomi
05-21-2005, 05:53 PM
especially the transtate function

Ehsan Kamrani
05-22-2005, 12:39 AM
http://www.opengl.org/resources/faq/technical/viewing.htm#view0070

And if you don't understand, let us know

-Ehsan-

05-23-2005, 12:08 AM
i updated your code and it work fine,how can i make the square rotate backwards if i dont press 'S',i tried putting else but it didn't work.i also want to draw a right signal,i tried putting it in a for loop and changing the colors,but it didn't work.

Ehsan Kamrani
05-23-2005, 02:26 AM
You should try to study the book * OpenGL A PRIMER * by *EDWARD ANGEL*. You should be able to write such codes.And more important, if i write the codes for you, you never can understand the meaning of the GLUT and OpenGL functions.Isn't it? :rolleyes:
-Ehsan-

05-23-2005, 05:44 AM
you are right,anyways thanks so much for your help

Ehsan Kamrani
05-23-2005, 06:25 AM
So start the book and if you have any questions about it's comments, let us know.
-Ehsan-