void myDisplay( void ) //------------------------------------------------------------------------------ // Registered as our GLUT display callback function. GLUT calls us whenever we // need to re-draw the screen. // OPENGL HINTS: // glRotated(123, 1,-2,1); // rotate by 170 degrees around a 3D axis vector; // glScaled(0.2, 0.3, 0.4); // shrink along x,y,z axes // glTranslated(0.1,0.3,0.5); // translate along x,y,z axes. { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //------------------------------SHADER ANIMATION: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // lights[0].applyLamp(); // use it for lighting. glTranslated(0,0,-6); setCam.apply_TR_Matrix(); // further translate the coord system by // send it to the shader as a uniform. glPushMatrix(); if(!texdraw) drawScene(); else cube1(); //cube1(); glPopMatrix(); glFlush(); // do any and all pending openGL rendering. glutSwapBuffers(); // For double-buffering: show what we drew. }
void keyboard(unsigned char key, int x, int y) //------------------------------------------------------------------------------ // GLUT 'keyboard' Callback. User pressed an alphanumeric keyboard key. // (x,y, give current mouse position). // ('special' keys such as return, function keys, arrow keys? keyboardspecial) { int curMatl; int junk; // to stop compiler warnings junk = x; junk = y; // stops warnings of 'unused parameters' x,y switch(key) { case 27: // Esc case 'Q': case 'q': case ' ': exit(0); // Quit application break; case 'H': case 'h': cout << "\nHELP MENU FOR PROJECT C/D (PROGRAMMABLE SHADERS)" << "\n------------------------------------------------" << "\nDrag the mouse to adjust the movable 3D camera" << "\nUse the arrow keys to move the camera/lighting around" << "\nUse R (capital for model, lowercase for camera) to reset" << "\nUse M to change the color of one of the teapots (shading mix)" << "\nUse + and - to zoom in and out of the picture" << "\nUse L to turn lamps on and off" << "\nQ, ENTER or SPACE BAR will quit the program" << endl << endl; break; case 'l': case 'L': if (lamp1On) { lamps[0].removeLamp(); lamp1On = !lamp1On; } else { lamps[0].applyLamp(); lamp1On = !lamp1On; } break; case 'k': case 'K': if(lamp2On) { lamps[1].removeLamp(); lamp2On = !lamp2On; } else { lamps[1].applyLamp(); lamp2On = !lamp2On; } break; case 'r': setCam.reset(); // reset camera coord system, break; case 'R': setModel.reset(); // reset model coord system. break; case 'm': case 'M': // Cycle through all materials for stuff[0]; curMatl = stuff[0].matlType; // get ID# for current material curMatl += 1; // go to next material, but stay within if(curMatl > MATL_MAX) curMatl = MATL_RED_PLASTIC; // defined ones. stuff[0].createMatl(curMatl); // change the material. break; case 'O': // -Z Translation for model coord system setModel.z_pos -= 0.3; break; case 'P': // +Z Translation for model coord system setModel.z_pos += 0.3; break; case '+': // PhotoShop-like move in/out with +/- keys case '=': setCam.z_pos += 0.1; break; case '-': setCam.z_pos -= 0.1; break; default: printf("unknown key %c: Try arrow keys, r, R, s, S, <, >, or q",key); break; } // We might have changed something. Force a re-display glutPostRedisplay(); }
void display(void) //------------------------------------------------------------------------------ // GLUT 'display' Callback. GLUT calls this fcn when it needs you to redraw // the dislay window's contents. Your program should never call 'display()', // because it will confuse GLUT--instead, call glutPostRedisplay() if you need // to trigger a redrawing of the screen. { // Clear the frame-buffer and the Z-buffer; ready for a new drawing. // (Set both bits using bit-wise OR) glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the color and depth buffers pTime += 0.1; // advance the timestep glUniform1f(timer, pTime); // send it to the shader as a uniform. //glUniform1f(shaderX,shadeX); //shadeX += 0.1; // ============================================================================= // START CAMERA POSITIONING CODE HERE: // ============================================================================= glMatrixMode(GL_MODELVIEW); // select the model/view matrix, clear it: glLoadIdentity(); // (now cam = world = model coord. system) // By definition, we're in the 'cam' coord. system; we're at the 'root' of // a scene tree (tree of transformations). // //-------------------We ***COULD*** define an openGL light source here, // and it would cast light on the scene as if it were // attached to our camera. Our camera center-of-projection // is at 0,0,0 in cam coord system, and we're looking down // the -z axis. Want a light in front of the camera? // try positioning it at (0,0,-4)...(NOTE: openGL doesn't // render the light itself, only its effect on surfaces). //-------------------Now create our 'world' coordinate system; // --Make an exact copy of the current (cam) coord system:call it 'world': // --transform that new 'world' coord system to put the 'world' coords // at the right place in the current 'cam' coordinate system. // We **COULD** translate in -z direction to 'push away the world coord // axes, then make a new copy of those axes, and rotate them, spinning the // world around that point in front of our camera: //***CAREFUL! uncomment ONLY ONE of these THREE camera-positioning methods! // /* //BEGIN 1 of 3------------------------------- BE SURE to comment out the gluLookAt() below)) glTranslated(0.0, 0.0,-10.0); // move new origin in -z direction. glRotated(20, 1.0, 0.0, 0.0); // 20deg. rotation about that new // origin's y axis; now call that // the 'world' coord system. */ //END 1 of 3------------------------------------ // or do it with mouse control like this: //BEGIN 2 of 3----------------------------------- glTranslated(0.0, 0.0, -9.0); // push back the world along -z axis // (world origin in front of camera // which looks down cam's -z axis) setCam.apply_TR_Matrix(); // further translate the coord system by // mouse/keyboard control, then rotate it // (also from mouse/keyboard control). // TRANSLATION: -- left/rt ARROW KEYS moves it in +/- x direction. // -- up/dn ARROW KEYS move world origin in +/-y direction; // -- +/- keyboard KEYS move world origin in +/-z direction // ROTATION: -- LEFT MOUSE button drag to rotate about x,y axes. //END 2 of 3------------------------------------ // Or do it with the classic 'glLookAt()' command: //BEGIN 3 of 3------------------------------------- /* gluLookAt(-5.0, 2.0, 8.66, // VRP: eyepoint x,y,z position in world space. 0.0, 0.0, 0.0, // 'look-at' point--we're looking at origin. // (VPN = look-at-point - VRP) 0.0, 1.0, 0.0); // VUP: view 'up' vector; set 'y' as up... */ // END 3 of 3------------------------------------ // CAREFUL!! gluLookAt() is a bit strange... // Usually, we make a NEW coord system (world) from the current // one (cam) by transformations defined/measured in the *current* one (cam). // With gluLookAt(), we're making the 'world' coord system using points // and vectors DEFINED IN WORLD coords! -- we're choosing a VRP, a look-at // point, and a VUP vector in world space, and using them to make a matrix // that changes our 'cam' coord. system into the 'world' coord system. //========================================================================== // END CAMERA-POSITIONING CODE. // (Now we can draw things in the 'world-space' coord. system: //========================================================================== //drawAxes(0); // draw r,g,b axes //drawPyramid(); // Draw our adjustable pyramid in world space. // CREATE LIGHT 0:---------------------------------------------------------- // IF IN -- 'cam' coordinate system: a "headlamp" attached to camera! // -- 'world' coord system: a "ceiling lamp" fixed overhead, // -- 'model' coord system: a lamp attached to a robot arm... if (lamp1On) { lamps[0].I_pos.row[0] = 0.0f;// position our first lamp (already created in lamps[0].I_pos.row[1] = 2.0f;// myGlutSetup() fcn as LAMP_WHITE_KEY), and lamps[0].I_pos.row[2] = 3.0f; lamps[0].I_pos.row[3] = 1.0f; lamps[0].applyLamp(); // use it for lighting. } //else // lamps[0].removeLamp(); // Set materials and shading for the first teapot:------------------------ stuff[0].applyMatl(); // set openGL to use stuff[0] material params. stuff[0].showName(); // on-screen display names the material glScaled(0.38, 0.38, 0.38); glPushMatrix(); // save 'world' coord. system; glTranslated(1.8, 0.0, 0.0); // move to a starting pt away from origin, glutSolidTeapot(0.6); // draw 1st teapot using material A. // (and whatever lights are enabled) glPopMatrix(); // return to 'world' coord system; //-------------------------------------- glPushMatrix(); // save 'world' coord system; glTranslated(0.7, -0.7, 0.5); // translate to the 'hinge point' we'll use // for this model; setModel.apply_RT_Matrix(); // LEFT_MOUSE drag rotates in on x,y axes, // <, > keys translate along z axis. glTranslated(-1.4, 1.4, -0.5); // teapot offset AWAY from pivot point, so // we don't pivot around teapot's center... //================================================ // NOW we can draw in the 'model' coordinate system: //================================================== // Draw axes in model-space coordinate system: drawAxes(1); // draw cyan,magenta,yellow axes. //CREATE LIGHT 1------------------------------------------------------------ // A second light source, fixed at origin in 'model' coordinates: if(lamp2On) { lamps[1].I_pos.row[0] = -1.0f; // set position of lamp 1; at origin lamps[1].I_pos.row[1] = -1.0f; lamps[1].I_pos.row[2] = -1.0f; lamps[1].I_pos.row[3] = 0.0f; // IMPORTANT! zero-valued 'w' means lamp is // infinitely far away. w=1.0 for local lights. lamps[1].applyLamp(); // turn it on. } //else // lamps[1].removeLamp(); //END light source 1------------------------------------------------------ stuff[1].applyMatl(); // Setup openGL to use the 2nd material, glTranslated(-1.2, -0.75, 0.0); glutSolidTeapot(0.6); // use it to draw 2nd, blue teapot. // (and whatever lighting is enabled) glPopMatrix(); // return to 'world' coord system. glPushMatrix(); // save 'world' coord. system, then glTranslated(0.0,-1.2, 0.0); // translate to 3rd location, stuff[2].applyMatl(); // Set material we'll use for 3rd teapot: glutSolidTeapot(0.6); // draw 3rd teapot using that material // and whatever lighting is enabled. /* lamps[2].I_pos.row[0] = 2.0f; // set position of lamp 1; at origin lamps[2].I_pos.row[1] = 2.0f; lamps[2].I_pos.row[2] = 2.0f; lamps[2].I_pos.row[3] = 1.0f; // IMPORTANT! zero-valued 'w' means lamp is // infinitely far away. w=1.0 for local lights. lamps[2].applyLamp(); // turn it on. */ glTranslated(0.0,2.8,0.0); glutSolidTeapot(0.6); glColor3d(1.0, 1.0, 0.0); stuff[4].applyMatl(); glPopMatrix(); // return to 'world' coord. system. //drawing my own 3D objects, a triangular prism and a square prism together glPushMatrix(); glTranslated(0,0,0); glRotated(0,0,0,0); sp1.draw(); glPopMatrix(); glPushMatrix(); glTranslated(0,0.75,0); sp2.draw(); glPopMatrix(); glPushMatrix(); glTranslated(-3.5,0,0); sp2.draw(); stuff[4].applyMatl(); glPopMatrix(); glPushMatrix(); glTranslated(3.5,0,0); sp2.draw(); stuff[4].applyMatl(); glPopMatrix(); // print instructions drawText2D(helv18, -0.5, -0.85, "'H' key: print HELP in console"); stuff[0].applyMatl(); // ========================================================================= // END DRAWING CODE HERE // ========================================================================= glFlush(); glutSwapBuffers(); // Double-buffering: show the newly-drawn image. glutPostRedisplay(); }
void myKeyboard(unsigned char key, int x, int y) //------------------------------------------------------------------------------ // GLUT CALLBACK: Don't call this function in your program--GLUT does it every // time user presses an alphanumeric key on the keyboard. // xw,yw == mouse position in window-system pixels (origin at UPPER left) { int curMatl; int junk; // to stop compiler warnings junk = x; junk = y; // stops warnings of 'unused parameters' x,y switch(key) { case 27: // Esc case 'Q': case 'q': case ' ': exit(0); // Quit application break; case 'r': setCam.reset(); // reset camera coord system, break; case 'R': setModel.reset(); // reset model coord system. break; case 'm': case 'M': // Cycle through all materials for stuff[0]; break; case '<': // -Z Translation for model coord system setModel.z_pos -= 0.3; break; case '>': // +Z Translation for model coord system setModel.z_pos += 0.3; break; case 's': if(material_changeflag==0) material_changeflag=1; else material_changeflag = 0; case '+': // PhotoShop-like move in/out with +/- keys case '=': setCam.z_pos += 0.1; break; case 'h': printHelp(); case '-': setCam.z_pos -= 0.1; break; case '1': if(light1==0) light1=1; else light1 = 0; cout<<light1<<endl; break; case 't': //apply textures if(texdraw==0) texdraw = 1; else texdraw = 0; break; case '2' : if(light2==0) light2=1; else light2 = 0; break; case '0': if(light0==0) light0=1; else light0 = 0; default: printf("unknown key %c: Try arrow keys, r, R, s, S, <, >, or q",key); break; } }
void drawScene() { //glScaled(0.5,0.5,0.5); glRotated(xtheta,1,0,0); glRotated(ytheta,0,1,0); progtime+=0.05; // advance the timestep glUniform1f(time_loc, progtime); // send it to the shader as a uniform. glUniform1i(light1_loc,light1); glUniform1i(light2_loc,light2); glUniform1i(light0_loc,light0); lights[3].applyLamp(); lights[4].applyLamp(); lights[5].applyLamp(); //glUniform1f(light_number_loc,1); glPushMatrix(); glUniform1i(waveIt_loc,0); glTranslated(2,0,0); lights[0].applyLamp(); lights[1].applyLamp(); matls[0].applyMatl(); //matls[0].showName(); glUniform1i(material_change_loc,0); glUniform1i(texIt_loc,1); glutSolidTeapot(0.6); glPopMatrix(); // glUniform1f(light_number_loc,0); glPushMatrix(); glTranslated(1,1,1); setModel.apply_RT_Matrix(); lights[2].applyLamp(); glPopMatrix(); glPushMatrix(); glUniform1i(waveIt_loc,0); glTranslated(0,-1,0); //matls[1].applyMatl(); matls[1].showName(); glUniform1i(material_change_loc,0); glUniform1i(texIt_loc,0); pyr.draw(); glPopMatrix(); glPushMatrix(); glUniform1i(waveIt_loc,1); matls[2].applyMatl(); //matls[2].showName(); glUniform1i(material_change_loc,material_changeflag); glUniform1i(texIt_loc,0); glutSolidTorus(0.2,0.5,100,300); glPopMatrix(); glPushMatrix(); glUniform1i(waveIt_loc,0); glTranslated(-3,0,0); matls[3].applyMatl(); //matls[3].showName(); glUniform1i(material_change_loc,0); glUniform1i(texIt_loc,0); glutSolidCone(0.5,1,100,200); glPopMatrix(); }