// This is my program.
// when I click and hold the middle button down. I want to box to rotate itself with out having to hold it down. I m trying to use idle fucntion.
Please give me pointers on how I should make it work? below is my program.
thx
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
GLfloat eyeX=0.0, eyeY=0.0, eyeZ=10.0;
GLfloat dx=0.0, dy=0.0, dz=0.0;
GLfloat ty, tx, tz;
int angle;
int spinning=0;
void myInit(int *, char **);
void mouseManager(int, int, int, int);
void displayFunc(void);
void idle(void);
int main(int argc, char **argv){
myInit(&argc, argv);
glutDisplayFunc(displayFunc);
glutMouseFunc(mouseManager);
glutIdleFunc(idle);
glutMainLoop(); //infinite loop
}
void displayFunc(void){
int i;
static done=0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, // field of view
640.0/480.0, // Aspect ratio
1.0, // Near Z
16.0 // Far Z
);
/*
glMatrixMode(GL_PROJECTION); // virtual camera
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2);
*/
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX, eyeY, eyeZ, // Eye is at (0, 0, 10)
0.0, 0.0, 0.0, // center is at (0,0,0)
0.0, 1.0, 0.0); // up is Y direction
glTranslatef( tx, -ty, tz);
glRotatef(xAngle, 1.0, 0.0, 0.0);
glRotatef(yAngle, 0.0, 1.0, 0.0);
glRotatef(zAngle, 0.0, 0.0, 1.0);
if(done)
glCallList(1);
else{
done=1;
glNewList(1, GL_COMPILE_AND_EXECUTE);
glBegin(GL_QUADS);
glColor3f(0.75, 0.75, 0.5);
//left
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, 1.0);
glColor3f(1.0, 0.9, 0.0);
//right side
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
//front
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
//back
glColor3f(0.0, 1.0, 0.0);
// glTranslatef( tx, 0.0, 0.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
//top
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
//bottom
glColor3f(0.5, 0.5, 0.5);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
glEndList();
}
/*
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-1.0, -1.0, -12);
glRotatef(2xAngle, 1.0, 0.0, 0.0);
glRotatef(2yAngle, 0.0, 1.0, 0.0);
glRotatef(2*zAngle, 0.0, 0.0, 1.0);
*/
glCallList(1);
glutSwapBuffers();
}
void mouseManager(int button, int state, int x, int y){
float alpha;
alpha=-(angle*3.1415926)/180;
switch(button){
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN){
// xAngle+=2.0;
// dx=0.4;
// spinning=1;
// moveX+=10.0;
angle=0; tx=0; ty=0; tz=0; eyeY=0, eyeZ=10;
}
else{
dx=0.0;
spinning=0;
}
break;
case GLUT_RIGHT_BUTTON:
if(state==GLUT_DOWN)
{ //dy=0.4;
// yAngle+=2.0;
spinning=1;
// moveX-=10.0;
}
else{
dy=0.0;
spinning=0;
}
break;
case GLUT_MIDDLE_BUTTON:
if(state==GLUT_DOWN){
dz=0.4;
// zAngle+=2.0;
spinning=1;
}
else{
dz=0.0;
spinning=0;
}
break;
defaut:
;
}
displayFunc();
}
void myInit(int *argc, char **argv){
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100, 150);
glutCreateWindow(argv[0]);
glClearColor(1.0,1.0,1.0,0.0); // Set clear color to white
angle=0;
ty=0;
tx=0;
tz=0;
glEnable(GL_DEPTH_TEST);
}
void idle(void){
float alpha;
alpha=-(angle*1.1)/180;
if(spinning==1){
xAngle+=dx;
yAngle+=dy;
zAngle+=dz;
// tx=tx + cos(alpha);
// tz=tz + sin(alpha);
/*
light0_position[0]-=dl;
light0_position[1]-=dl;
if(light0_position[0] <-10){
light0_position[0]=10;
light0_position[1]=10;
}
*/
};
glutPostRedisplay();
}