#include "Go.h" #include "GoGlutInterface.h" #include "GoSphere.h" class GoLightDemo : public GoGlutInterface { public: GoSphere *sphere; GoMatrix currentLightPosition; GoMatrix mouseDraggedLightPosition; int xStart; int yStart; GoLightDemo(void) { // // Create a sphere to illustrate this demo. A sphere // is a good shape for understanding how lighting works. // sphere = new GoSphere(1.0, 16, 16); // // Turn on LIGHT_0 // go->light(Go::LIGHT_0, true); // // Set position of LIGHT_0 bases the current modelview matrix, // which is at the point the identity matrix. // setLightPosition(); } // // Interface render method. // void render(void) { // // Clear. // go->clear(Go::IMAGE); // // Draw. // go->color(0, 0, 1); // blue sphere->renderSolid(go); go->color(1, 1, 0); // yellow sphere->renderWire(go); // // Display. // swap(); } // // Interface resized method. // void resized(int width, int height) { // // Setup projection matrix. // go->push(Go::MATRIX_MODE); go->matrixMode(Go::PROJECTION); go->identity(); if(width > height) { go->ortho(-1.0 * width / height, 1.0 * width / height, -1.0, 1.0, -1.0, 1.0); } else { go->ortho(-1.0, 1.0, -1.0 * height / width, 1.0 * height / width, -1.0, 1.0); } go->pop(Go::MATRIX_MODE); } // // Interface mousePressed method. // void mousePressed(int x, int y) { // // Save values that will be used to track the // mouse in the mouseDragged() method. // xStart = x; yStart = y; } // // Interface mouseReleased method. // void mouseReleased(int x, int y) { // // Replace the currentLightPosition with the final result // of mouseDraggedLightPosition. // for(int i = 0; i < 16; i++) { currentLightPosition.m[i] = mouseDraggedLightPosition.m[i]; } } // // Interface mouseDragged method. // void mouseDragged(int x, int y) { int xEnd = x; int yEnd = y; // // Save the modelview matrix. // go->push(Go::MODELVIEW); // // Setup modelview matrix based on mouse position. // go->identity(); double xTheta = (yEnd - yStart) / 2; double yTheta = (xEnd - xStart) / 2; go->rotate(xTheta, 1, 0, 0); go->rotate(yTheta, 0, 1, 0); // // Post multiply current light position. // go->multiply(¤tLightPosition); // // Setup the light position with the current modelview matrix. // setLightPosition(); // // Save the "mouse dragged" light position so that // currentLightPosition can be updated in mouseReleased(). // go->getModelview(&mouseDraggedLightPosition); // // Restore the modelview matrix. // go->pop(Go::MODELVIEW); // // Redisplay. // rerender(); } // // Light position. // void setLightPosition(void) { go->light(Go::LIGHT_0, Go::POSITIONAL, 0.0, 0.0, 1.75); } }; int main(int argc, char *argv[]) { GoGlutInterface::init(&argc, argv); new GoLightDemo(); GoGlutInterface::mainLoop(); return(0); }