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