/* * This demo shows how to draw a power bar * on top of a 3d scene. */ #include #include #include #include #include "geometry.h" using namespace std; #define PI 3.14159 // function prototypes void display(void); void reshape(int width, int height); void keyboard(unsigned char key, int x, int y); void motion(int x, int y); void init(void); // viewpoint double theta=0, phi=0, distanceToViewpoint=200; // window int width=400; int height=400; int main(int argc, char **argv) { // set up window glutInitWindowSize(width,height); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Power Bar demo"); // register callback functions glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMotionFunc(motion); // initalize opengl parameters init(); // loop until something happens glutMainLoop(); return 0; } void init() { // initialize viewing system glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, 1.0, 10.0, 1000.0); glMatrixMode(GL_MODELVIEW); // initialize background color to black glClearColor(0.0,0.0,0.0,0.0); // enable depth buffering glEnable(GL_DEPTH_TEST); } void reshape(int newWidth, int newHeight) { width=newWidth; height=newHeight; glViewport(0,0,width,height); } void display() { // compute eye position glLoadIdentity(); Tuple eye(0,0,0); eye[0] = (float) -distanceToViewpoint * cos(theta) * sin(phi); eye[1] = (float) distanceToViewpoint * sin(theta); eye[2] = (float) distanceToViewpoint * cos(theta) * cos(phi); // compute up vector // the is the vector orthogonal to eye that lies in the same // plane as eye and (0,1,0) and has a positive dot product with // (0,1,0) Tuple up = Tuple(0,1,0)-eye/eye.length(); up.normalize(); gluLookAt(eye[0],eye[1],eye[2], 0,0,0,up[0], up[1], up[2]); // clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw a blue square glColor3f(0,0,1); glBegin(GL_QUADS); glVertex3f(0,0,0); glVertex3f(10,0,0); glVertex3f(10,10,0); glVertex3f(0,10,0); glEnd(); // draw power bar in ortho 2d mode // set up ortho -- but save current projection matrix glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(-1,1,-1,1); //set up modelview -- but save current modelview matrix glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // disable the depth test -- draw on top of everything glDisable(GL_DEPTH_TEST); // draw red power bar glColor3f(1,0,0); glBegin(GL_QUADS); glVertex2f(0.75f,-0.75f); glVertex2f(0.8f,-0.75f); glVertex2f(0.8f,-0.65f); glVertex2f(0.75,-0.65f); glEnd(); // restore modelview matrix glPopMatrix(); // restore projection matrix glMatrixMode(GL_PROJECTION); glPopMatrix(); // return to modelview mode -- glMatrixMode(GL_MODELVIEW); // enable depth buffering glEnable(GL_DEPTH_TEST); glutSwapBuffers(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'Z': distanceToViewpoint+=10; break; case 'z': if (distanceToViewpoint>10) distanceToViewpoint-=10; break; case 'h': case 'H': case '?': cout << "Use 'z'/'Z' to zoom in/out" << endl; break; default: break; } glutPostRedisplay(); } /******************************************************************* * BMP reader from: Program: Chapter 7 Bitmap Example 4 * Author: Kevin Hawkins * ********************************************************************/ void motion(int x, int y) { static int currX=-1; static int currY=-1; if (currX>0 && abs(x-currX) < width/6 && abs(y-currY) < height/6) { phi += (double) (x-currX)/ (double) width * 3.14159; theta += (double) (y-currY)/ (double) height * 3.14159; // limit theta to -4pi/9 and 4pi/9 if (theta < -4*3.14159/9.0) theta = -4*3.14159/9.0; if (theta > 4*3.14159/9.0) theta = 4*3.14159/9.0; } currX = x; currY = y; glutPostRedisplay(); }