Independent Rotation

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!!!

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 ?

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();

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-

it worked.thank you so much.

the pointer works,but now i cant’t stop it,it keeps rotating.

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-

yes,i i know how to put a keyboard function,but i want the pointer to stop at a point while i am pressing.

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’ || key == ‘S’ ) && ( translate_x <= stopPoint ) )
{
translate_x += 0.01;
angle += 0.5;
}
if( ( key == ‘a’ || 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-

how to achieve the function use the gluLookAt

especially the transtate function

http://www.opengl.org/resources/faq/technical/viewing.htm#view0070

And if you don’t understand, let us know

-Ehsan-

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.

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-

you are right,anyways thanks so much for your help

So start the book and if you have any questions about it’s comments, let us know.
-Ehsan-