newbie question : glutPostReDisplay


#1

hello,

i just start doing some opengl and i experiencing a problem which i don’t know wat i did wrong.

this is wat i have

============================================================

void displayCallback(void)
{
float eye[] = {0,4,20};
float center[] = {0,0,0};
float up[] = {0,1,0};

 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 glMatrixMode(GL_MODELVIEW);
 
 glLoadIdentity();
   // position the camera in the world
 gluLookAt(eye[0],eye[1],eye[2],center[0],center[1],center[2],up[0],up[1], up[2]);

   // animate the global scene rotation
 glRotatef(Angle,1,1,0);

   // draw a wire-frame sphere;  turn off lighting in order to get solid shading
 glDisable(GL_LIGHTING);
 glColor3f(1,1,1);
 glutWireSphere(1,20,10);     // sphere: r=1, 20 slices, 10 stacks
 glEnable(GL_LIGHTING);

 glPushMatrix();
   // draw some colored teapots
 /*
 glTranslatef(-3,0,1);
 glColor3f(1,0.5,0.5);      
 glutSolidTeapot(1.0);

 glTranslatef(3,0,0);
 glColor3f(0.5,1,0.5);      
 glutSolidTeapot(1.0);
 
 glTranslatef(3,0,0);
 glColor3f(0.5,0.5,1);      
 glutSolidTeapot(1.0);

 glPopMatrix();
 */

   // draw ground plane
 glColor3f(1,1,1);
 glNormal3f(0,1,0);
 glBegin(GL_POLYGON);
 glVertex3f(-10,0,-10);
 glVertex3f(-10,0,10);
 glVertex3f(10,0,10);
 glVertex3f(10,0,-10);
 
 //draw_t(1.0,5.0,10.0,100);
 drawSquare(1);
 glEnd();

 glutSwapBuffers();

}

void keyCallback(unsigned char key, int x, int y)
{
switch(key) {
case ‘q’:
exit(0);
break;
case ‘r’:
exit(0);
break;

 case 27: // 27 = number of esc key
     exit(0);
     break;
 
 case 'a':
     Angle = (Angle+500) / 360;
     glutPostRedisplay();
     break;

 default:
     break;
 }
 glutPostRedisplay();

 // 

}



glutKeyboardFunc(keyCallback);
glutDisplayFunc(displayCallback);

when i click on ‘a’, i believe that the angle of my picture should turn down and keep turning down when i keep clicking ‘a’. however, my pricturn just move down ‘once’ when i clicked ‘a’ for the first time.

my purpose is to let it getting down when i keep pressing ‘a’ …
any idea?

another problem that i having…
i trying to move the camera around my obj
says. when i click ‘z’ the camera turn left aorund the obj (clock wise if u look from top view) …however i don’t know how to do this… can any suggest me?

and last q : wat is the case that i need to use if i wanna use my ‘left cursor’ to turn left instead of ‘z’ …

thxx very much for any suggestion.
Tom


#2

12.6 glutSpecialFunc

The glutSpecialFunc function sets the window’s special key press callback. Freeglut calls the special key press callback when the user presses a special key.

Usage

void glutSpecialFunc ( void (*func) ( int key, int x, int y ) );

func The window’s new special key press callback function
key The key whose press triggers the callback
x The x-coordinate of the mouse relative to the window at the time the key is pressed
y The y-coordinate of the mouse relative to the window at the time the key is pressed

Description

The glutSpecialFunc function specifies the function that freeglut will call when the user presses a special key on the keyboard. The callback function has one argument: the name of the function to be invoked (“called back”) at the time at which the special key is pressed. The function returns no value. Freeglut sets the current window to the window which is active when the callback is invoked. “Special keys” are the function keys, the arrow keys, the Page Up and Page Down keys, and the Insert key. The Delete key is considered to be a regular key.
Calling glutSpecialUpFunc with a NULL argument disables the call to the window’s special key press callback.

The key argument may take one of the following defined constant values:

* GLUT_KEY_F1, GLUT_KEY_F2, ..., GLUT_KEY_F12 - F1 through F12 keys
* GLUT_KEY_PAGE_UP, GLUT_KEY_PAGE_DOWN - Page Up and Page Down keys
* GLUT_KEY_HOME, GLUT_KEY_END - Home and End keys
[B]* GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN - Arrow keys[/B]
* GLUT_KEY_INSERT - Insert key

#3

>(Angle+500) / 360;

just to be on the safe side :

(Angle + 500. ) / 360.;

some compilers do strange assumptions between float / int sometimes.


#4

hi.

can u help me a bit more on ‘glutSpecialFunc’

i trying to make use of it.
but still nothign seems to work.

this is wat i have

case GLUT_KEY_UP:
Angle = (Angle + 500.0) / 360.0;
break;

// for the one below
// seems to work just the 1st time i press ‘a’
case ‘a’:

… // same as above.

and i insert this to my main
glutSpecialFunc(NULL);

any idea?


#5

This is where you say that you do not want to use the Specialkey functions.
Look here :

" void glutSpecialFunc(void (*func) (int key, int x, int y));

Parameters:

func - The name of the function that will process the special keyboard events. Passing NULL as an argument causes GLUT to ignore the special keys. "

And you are passing NULL. You have to pass your functionpointer.

Look here for more reference :
http://www.lighthouse3d.com/opengl/glut/index.php3?5

Tom


#6

The reason you are having trouble is that

(Angle+500) / 360;

Is probably not the formula you want. It will always approach a specific number when used. For example:

//Initial angle is 100
(100 + 500) / 360 = 1.666
(1.666 + 500) / 360 = 1.394
(1.394 + 500) / 360 = 1.393

and you can see that it will just stop, essentially right away. You could pick any number for
the initial angle, and this would happen regardless. You might want something like

angle = angle + 20.0/360.0;

instead.


#7

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.