test2d.cpp

Go to the documentation of this file.
00001 /*
00002  * test2d.cpp
00003  *
00004  * Copyright (C) 2009,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Program to test 2D glut drawing.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 #include "common/wave_ex.h"
00015 #include "glut/camera.h"
00016 #include "glut/glut.h"
00017 #include "glut/glut_2d.h"
00018 #include "glut-font/glut-font.h"
00019 #include "perf/perf.h"
00020 
00021 
00022 
00023 static const int s_defaultWidth                 = 800;
00024 static const int s_defaultHeight                = 600;
00025 
00026 static float s_dist                             = 10.0;
00027 
00028 
00029 ////////////////////////////////////////////////////////////////////////////////
00030 //
00031 //      static helper methods
00032 //
00033 ////////////////////////////////////////////////////////////////////////////////
00034 
00035 class Drawer : public glut::Host {
00036 public:
00037         ~Drawer(void) throw() { }
00038 
00039         // glut::Host class interface methods ----------------------------------
00040         void init(void) {
00041                 // other initialization
00042                 glShadeModel(GL_SMOOTH);
00043 
00044                 glEnable(GL_LIGHT0);
00045 
00046                 glFrontFace(GL_CCW);
00047                 glCullFace(GL_BACK);
00048         }
00049 
00050         void display(IN int w, IN int h) {
00051                 perf::time_t now = perf::getNow();
00052                 static glut::fps_t s_fps(now);
00053                 s_fps.tick(now);
00054 
00055                 glEnable(GL_LIGHTING);
00056                 glEnable(GL_DEPTH_TEST);
00057                 glEnable(GL_TEXTURE_2D);
00058                 glEnable(GL_CULL_FACE);
00059 
00060                 // clear buffers (takes no effect until drawing much later)
00061                 glClearColor(0.5, 0.5, 0.5, 1.0);
00062                 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
00063 
00064                 // set up camera, viewer
00065                 glut::camera_t camera;
00066                 camera.setAspect(w, h);
00067                 glut::Viewer viewer;
00068                 viewer.setPosition(point3d_t(0, 0, -s_dist));
00069 
00070                 // set up glut for drawing
00071                 glut::setOpenGLProjection(camera);
00072                 glut::setOpenGLViewer(viewer);
00073 
00074                 // set up lighting
00075                 float lightAmbient[] = { 0.8, 0.8, 0.8, 1.0 };
00076                 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient);
00077 
00078                 // rotate object
00079                 static float angle = 0.0;
00080                 angle += 0.5;
00081                 glRotatef(angle, 0.0, 1.0, 0.0);
00082 
00083                 // draw object
00084                 glutSolidSphere(4.0, 16, 16);
00085 
00086                 glDisable(GL_LIGHTING);
00087                 glDisable(GL_DEPTH_TEST);
00088                 glDisable(GL_TEXTURE_2D);
00089                 glDisable(GL_CULL_FACE);
00090         
00091                 glut::push2D(w, h);
00092 
00093                 // rect
00094                 glColor3f(0.8, 0.2, 0.8);
00095                 for (int i = -10; i < 10; ++i) {
00096                         int x0 = i * 16;
00097                         int x1 = x0 + 14;
00098                         for (int j = -10; j < 10; ++j) {
00099                                 int y0 = j * 12;
00100                                 int y1 = y0 + 10;
00101 
00102                                 glRecti(x0, y0, x1, y1);
00103                         }
00104                 }
00105 
00106                 // output FPS
00107                 glut::Font * font = glut::getDefaultFont();
00108                 glColor3f(1.0, 1.0, 1.0);
00109                 char buffer[128];
00110                 sprintf(buffer, "%d fps", (int) s_fps.getFPS());
00111                 font->display(1, 12, 0, buffer);
00112                 sprintf(buffer, "%d x %d", w, h);
00113                 font->display(w - 100, 12, 0, buffer);
00114 
00115                 glut::pop2D();
00116         }
00117 
00118         eBehavior tick(void) {
00119                 return eContinue;
00120         }
00121 
00122         int shutdown(void) {
00123                 perf::dumpTimingSummary(std::cout);
00124                 return 0;
00125         }
00126 
00127         eBehavior keyboard(IN int key, IN int mods) {
00128                 if ('=' == key)
00129                         s_dist += 1.0;
00130                 if ('-' == key)
00131                         s_dist -= 1.0;
00132         
00133                 if (s_dist < 1.0)
00134                         s_dist = 1.0;
00135 
00136                 if ('l' == key)
00137                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00138 
00139                 if ('s' == key)
00140                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00141 
00142                 if (27 == key)
00143                         return eExit;
00144 
00145                 return eContinue;
00146         }
00147 };
00148 
00149 
00150 
00151 ////////////////////////////////////////////////////////////////////////////////
00152 //
00153 //      entry point
00154 //
00155 ////////////////////////////////////////////////////////////////////////////////
00156 
00157 int
00158 main
00159 (
00160 IN int argc,
00161 IN const char * argv[]
00162 )
00163 {
00164         try {
00165                 // main loop
00166                 smart_ptr<glut::Host> host = new Drawer;
00167                 ASSERT(host, "out of memory");
00168                 glut::start(argc, argv,
00169                         s_defaultWidth, s_defaultHeight,
00170                         "wave-glut md3 model viewer",
00171                         NULL,
00172                         host);
00173 
00174         } catch (std::exception& e) {
00175                 DPRINTF("Exception: %s", e.what());
00176         }
00177 
00178         // only reach here on error
00179         return 1;
00180 }
00181