#include #include #include "Go.h" #include "GoText.h" #include "GoGlutInterface.h" class Objects { public: GoVertex *square; GoVertex *cross; enum { YELLOW_SQUARE, GREEN_SQUARE, CROSS }; Objects() { square = new GoTriangleFan(4); cross = new GoLines(4); square->xyz(0, -1.0, -1.0, 0.0); square->xyz(1, 1.0, -1.0, 0.0); square->xyz(2, 1.0, 1.0, 0.0); square->xyz(3, -1.0, 1.0, 0.0); cross->xyz(0, -1.0, -1.0, 0.0); cross->xyz(1, 1.0, 1.0, 0.0); cross->xyz(2, -1.0, 1.0, 0.0); cross->xyz(3, 1.0, -1.0, 0.0); } void render(Go *go) { // // Draw yellow square with black cross. // go->identity(); go->scale(0.4, 0.4, 4.0); go->translate(-1.3, 0.0, 0.0); go->name(YELLOW_SQUARE); go->color(1.0, 1.0, 0.0); go->render(square); go->push(Go::NAME); go->name(CROSS); go->color(0.0, 0.0, 0.0); go->render(cross); go->pop(Go::NAME); // // Draw green square with black cross. // go->identity(); go->scale(0.4, 0.4, 0.4); go->translate(1.3, 0.0, 0.0); go->name(GREEN_SQUARE); go->color(0.0, 1.0, 0.0); go->render(square); go->push(Go::NAME); go->name(CROSS); go->color(0.0, 0.0, 0.0); go->render(cross); go->pop(Go::NAME); } }; class Select : public GoGlutInterface { public: // Selectable objects. Objects *objects; // Object selection text feedback. GoText *selectionText; Select(void) { // // Setup the background. // go->background(0, 0, 0); // // Create the objects that can be selected. // objects = new Objects(); // // Init object selection text feedback. // selectionText = new GoText(0.0, 0.7, 0.0, // position 1.0, 0.0, // path 0.15, 0.15, // scale GoText::CENTER_CENTER, // alignment new GoHershey("hersheyFonts/meteorology"), // font "Select something with the mouse"); // text } // // Interface render method. // void render(void) { // render objects go->clear(Go::IMAGE); objects->render(go); // render selectionText go->identity(); go->color(1.0, 1.0, 1.0); selectionText->render(go); // display swap(); } // // Interface resized method. // void resized(int width, int height) { // // Setup projection matrix. // 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->matrixMode(Go::MODELVIEW); } // // Interface mousePressed method. // void mousePressed(int x, int y) { int width = go->width(); int height = go->height(); // // Save projection matrix. // go->push(Go::PROJECTION); // // Setup projection matrix for select mode. // go->push(Go::MATRIX_MODE); go->matrixMode(Go::PROJECTION); go->identity(); go->select(x, height - y, 5.0, 5.0); 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); // // Render in select mode. // go->push(Go::RENDER_MODE); go->renderMode(Go::SELECTION); objects->render(go); go->pop(Go::RENDER_MODE); // // Restore projection matrix. // go->pop(Go::PROJECTION); // // Find out what was selected, based on the name stack. // boolean yellowSquareSelected = false; boolean greenSquareSelected = false; boolean crossSelected = false; for(GoSelection *selection = go->selection(); selection != NULL; selection = selection->next()) { int stackSize = selection->stackSize(); for(int i = 0; i < stackSize; i++) { switch(selection->name(i)) { case Objects::YELLOW_SQUARE: yellowSquareSelected = true; break; case Objects::GREEN_SQUARE: greenSquareSelected = true; break; case Objects::CROSS: crossSelected = true; break; } } } // // Setup selectionText based on what was found. // selectionText->text(""); if(yellowSquareSelected) { if(crossSelected) { selectionText->text("Cross in yellow square"); } else { selectionText->text("Yellow square"); } } else if(greenSquareSelected) { if(crossSelected) { selectionText->text("Cross in green square"); } else { selectionText->text("Green square"); } } // // Print the selection. // rerender(); // // Clear for next time. // go->clear(Go::SELECTION); } }; int main(int argc, char *argv[]) { // // Glut setup. // GoGlutInterface::init(&argc, argv); GoGlutInterface::windowName("Select"); GoGlutInterface::windowSize(450, 300); // // Create Select. // Select *select = new Select(); GoGlutInterface::mainLoop(); return(0); }