#include #include #include "Go.h" #include "GoText.h" #include "GoGlutInterface.h" #define PI 3.14159265358979324 const char *fontList[] = { "hersheyFonts/cursive", "hersheyFonts/times.r", "hersheyFonts/times.g", "hersheyFonts/times.rb", "hersheyFonts/times.i", "hersheyFonts/times.ib", "hersheyFonts/astrology", "hersheyFonts/futura.m", "hersheyFonts/greek", "hersheyFonts/math.upp", "hersheyFonts/symbolic", "hersheyFonts/gothic.eng", "hersheyFonts/japanese", "hersheyFonts/meteorology", "hersheyFonts/cyrillic", "hersheyFonts/gothic.ger", "hersheyFonts/markers", "hersheyFonts/music", "hersheyFonts/futura.l", "hersheyFonts/gothic.ita", "hersheyFonts/math.low", "hersheyFonts/script" }; #define FONT_NUM (sizeof(fontList) / sizeof(char *)) class Hello : public GoGlutInterface { public: GoText *text[FONT_NUM]; int currentFont; // font currently being displayed // View variables. double eyeX, eyeY, eyeZ; double centerX, centerY, centerZ; double upX, upY, upZ; double radius; boolean perspective; Hello(void) { // // Create "Hello World" with all the different fonts. // for(int i = 0; i < FONT_NUM; i++) { text[i] = new GoText(0.0, 0.0, 0.0, /* position */ 1.0, 0.0, 0.0, /* path */ 0.0, 1.0, 0.0, /* up */ 1.0, 1.0, /* scale */ GoText::CENTER_CENTER, /* alignment */ new GoHershey((char *)fontList[i]), /* font */ "Hello World"); /* text */ } currentFont = 0; // // Setup modelview matrix. // modelview(); } // // Interface render method. // void render(void) { go->clear(Go::IMAGE); text[currentFont]->render(go); swap(); go->rotate(5, 1, 1, 1); rerender(); } // // Interface resized method. // void resized(int width, int height) { projection(); } // // Interface mousePressed method. // void mousePressed(int x, int y) { nextFont(); modelview(); projection(); } // // Set to next font in fontList. // void nextFont(void) { if(currentFont == FONT_NUM - 1) { currentFont = 0; } else { currentFont++; } } // // Sets up the modelview matrix. // void modelview(void) { // // Calculate the bounding box. // go->clear(Go::BOUND_BOX); go->renderMode(Go::BOUND_BOX); go->identity(); text[currentFont]->render(go); go->renderMode(Go::IMAGE); GoBoundBox *boundBox = go->boundBox(); // // Setup modelview variables. // double eyeMult = 3.0; eyeX = 1.3 * eyeMult; eyeY = -2.4 * eyeMult; eyeZ = 2.0 * eyeMult; centerX = (boundBox->xMin() + boundBox->xMax()) / 2.0; centerY = (boundBox->yMin() + boundBox->yMax()) / 2.0; centerZ = (boundBox->zMin() + boundBox->zMax()) / 2.0; upX = 0.0; upY = 0.0; upZ = 1.0; radius = distance(centerX, centerY, centerZ, boundBox->xMax(), boundBox->yMax(), boundBox->zMax()); perspective = true; // // Setup modelview matrix. // go->lookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ); } // // Sets up the projection matrix. // void projection() { int width = go->width(); int height = go->height(); double dx = eyeX - centerX; double dy = eyeY - centerY; double dz = eyeZ - centerZ; double d = sqrt(dx * dx + dy * dy + dz * dz); double fovy = 2.0 * 180.0/PI * atan(radius / d); double near = d - radius; double far = d + radius; double xUnits, yUnits; if(width >= height) { xUnits = 2 * radius * width / height; yUnits = 2 * radius; } else { xUnits = 2 * radius; yUnits = 2 * radius * height / width; } go->matrixMode(Go::PROJECTION); go->identity(); if(perspective) { if(width >= height) { go->perspective(fovy, xUnits / yUnits, near, far); } else { double radiusModified = radius * yUnits / xUnits; double fovyModified = atan(radiusModified / d) * 180.0 / PI * 2.0; go->perspective(fovyModified, xUnits / yUnits, near, far); } } else { go->ortho(-xUnits / 2.0, xUnits / 2.0, -yUnits / 2.0, yUnits / 2.0, near, far); } go->matrixMode(Go::MODELVIEW); } // // Calculate distance between two points. // double distance(double x0, double y0, double z0, double x1, double y1, double z1) { double dx = x1 - x0; double dy = y1 - y0; double dz = z1 - z0; return sqrt(dx*dx + dy*dy + dz*dz); } }; int main(int argc, char *argv[]) { GoGlutInterface::init(&argc, argv); // // Glut setup. // GoGlutInterface::windowName("Hello"); GoGlutInterface::windowSize(450, 300); // // Create Hello World. // Hello *hello = new Hello(); GoGlutInterface::mainLoop(); return(0); }