glut-model/test/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2008,2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Program to test the glut-model library.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include <iostream>
00012 #include <fstream>
00013 
00014 #include "common/wave_ex.h"
00015 #include "glut-font/glut-font.h"
00016 #include "glut-model/glut-model.h"
00017 #include "perf/perf.h"
00018 #include "util/file.h"
00019 #include "util/token_stream.h"
00020 #include "wave-glut/camera.h"
00021 #include "wave-glut/glut_2d.h"
00022 #include "wave-glut/wave-glut.h"
00023 
00024 
00025 static const int s_defaultWidth                 = 800;
00026 static const int s_defaultHeight                = 600;
00027 
00028 static int s_retval                             = 0;
00029 
00030 
00031 static float s_dist                             = 5.0;
00032 
00033 static smart_ptr<perf::Timer> s_overallTimer;
00034 
00035 
00036 ////////////////////////////////////////////////////////////////////////////////
00037 //
00038 //      static helper methods
00039 //
00040 ////////////////////////////////////////////////////////////////////////////////
00041 
00042 static void
00043 addLight
00044 (
00045 IN int gl_enum,
00046 IN float L,
00047 IN float x,
00048 IN float y,
00049 IN float z,
00050 IN float r,
00051 IN float g,
00052 IN float b
00053 )
00054 {
00055         x *= L;
00056         y *= L;
00057         z *= L;
00058 
00059         float lightPos[] = { x, y, z, 1.0 };
00060         glLightfv(gl_enum, GL_POSITION, lightPos);
00061 
00062         float lightAmbient[] = { 0.1 + r, 0.1 + g, 0.1 + b, 1.0 };
00063         glLightfv(gl_enum, GL_AMBIENT, lightAmbient);
00064 
00065         float lightDiffuse[] = { 0.8 + r, 0.8 + g, 0.8 + b, 1.0 };
00066         glLightfv(gl_enum, GL_DIFFUSE, lightDiffuse);
00067         glLightfv(gl_enum, GL_SPECULAR, lightDiffuse);
00068 
00069         glLightf(gl_enum, GL_QUADRATIC_ATTENUATION, .1);
00070 
00071         glPushMatrix();
00072         glTranslatef(x, y, z);
00073         glutSolidSphere(1.0, 16, 16);
00074         glPopMatrix();
00075 }
00076 
00077 
00078 
00079 static void
00080 setLights
00081 (
00082 void
00083 )
00084 {
00085         static float t = 0.0;
00086         t += 0.0075;
00087 
00088         float x = 0.5 * s_dist * cos(t);
00089         float y = 0;
00090         float z = 0.5 * s_dist * sin(t);
00091 
00092         // first light: ambient white light
00093         float lightAmbient[] = { 0.8, 0.8, 0.8, 1.0 };
00094         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightAmbient);
00095 
00096         float ambientDir[] = { 1.0, 1.0, -1.0, 0.0 };
00097         glLightfv(GL_LIGHT0, GL_POSITION, ambientDir);
00098 
00099         // other lights--moving
00100         addLight(GL_LIGHT1, 6, x, y, z, 0, 0, 0.2);
00101 //      addLight(GL_LIGHT2, 8, -x, y, z, 0.2, 0, 0);
00102 //      addLight(GL_LIGHT3, 10, y, x, z, 0, 0.2, 0);
00103 
00104         glEnable(GL_LIGHTING);
00105         glEnable(GL_LIGHT0);
00106         glEnable(GL_LIGHT1);
00107 //      glEnable(GL_LIGHT2);
00108 }
00109 
00110 
00111 
00112 static void
00113 onDisplay
00114 (
00115 IN glut::Renderable * model,
00116 IN glut::RenderQueue * rq
00117 )
00118 {
00119         perf::Timer timer("onDisplay");
00120         ASSERT(model, "null");
00121         ASSERT(rq, "null");
00122 
00123         perf::time_t now = perf::getNow();
00124         static glut::fps_t s_fps(now);
00125         s_fps.tick(now);
00126 
00127         //DPRINTF("On display...");
00128         // capabilities
00129         glEnable(GL_DEPTH_TEST);
00130         glEnable(GL_COLOR_MATERIAL);
00131 
00132 //      glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00133 
00134         glFrontFace(GL_CCW);    // counter clockwise is front
00135         glCullFace(GL_BACK);
00136         glEnable(GL_CULL_FACE);
00137 
00138         // clear buffers
00139         glClearColor(0.0, 0.0, 0.0, 0.0);
00140         glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
00141 
00142         // set up camera, viewer
00143         glut::render_context_t rc;
00144         rc.viewer.setPosition(point3d_t(0, 0, -s_dist));
00145 
00146         // set up glut for drawing
00147         glut::setOpenGLProjection(rc.camera);
00148         glut::setOpenGLViewer(rc.viewer);
00149 
00150         // object materials
00151         float diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
00152         glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, diffuse);
00153         float specular[] = { 0.2, 0.2, 0.2, 1.0 };
00154         glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
00155 
00156         // set up lighting
00157         setLights();
00158 
00159         // rotate object
00160         static float angle = 0.0;
00161         angle += 0.5;
00162         glRotatef(angle, 0.0, 1.0, 0.0);
00163 //      glRotatef((angle / 7.3), 0.0, 0.5, 0.5);
00164 
00165         // draw object
00166         model->render(rc, rq);
00167 
00168         // draw axes
00169         glColor3f(0, 0, 1);     // blue: y
00170         glBegin(GL_LINES);
00171                 glVertex3f(0, 1, 0);
00172                 glVertex3f(0, 2, 0);
00173         glEnd();
00174 
00175         glColor3f(1, 0, 0);     // red: x
00176         glBegin(GL_LINES);
00177                 glVertex3f(1, 0, 0);
00178                 glVertex3f(2, 0, 0);
00179         glEnd();
00180 
00181         glColor3f(0, 1, 0);     // green: z
00182         glBegin(GL_LINES);
00183                 glVertex3f(0, 0, 1);
00184                 glVertex3f(0, 0, 2);
00185         glEnd();
00186 
00187         // draw transparent (queued) textures
00188         glEnable(GL_BLEND);
00189         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00190 
00191         // first, save and clear the model view matrix
00192         // queued polygons should have pre-transformed vertices
00193         glMatrixMode(GL_MODELVIEW);
00194         glPushMatrix();
00195         glLoadIdentity();       // reset modelview
00196         glEnable(GL_TEXTURE_2D);
00197         while (glut::poly_request_t * pr = rq->popRequest()) {
00198                 ASSERT(pr, "null");
00199                 DPRINTF("Drawing queued polygon!");
00200 
00201                 if (pr->nVertices < 3) {
00202                         DPRINTF("Skipping polygon with %d vertices",
00203                             pr->nVertices);
00204                         continue;
00205                 }
00206                 if (pr->textureId < 1) {
00207                         DPRINTF("Skipping polygon with texture id %d",
00208                             pr->textureId);
00209                         continue;
00210                 }
00211 
00212                 // set up texture and normal
00213                 glBindTexture(GL_TEXTURE_2D, pr->textureId);
00214                 glNormal3fv((GLfloat *) &pr->normal.x);
00215 
00216                 // send vertices
00217                 glBegin(GL_POLYGON);
00218                 for (int i = 0; i < pr->nVertices; ++i) {
00219                         glTexCoord2f(pr->u[i], pr->v[i]);
00220                         glVertex3fv((GLfloat *) &pr->vertex[i].x);
00221                 }
00222                 glEnd();
00223         }
00224         glDisable(GL_TEXTURE_2D);
00225         glPopMatrix();
00226 
00227         // disable expensive stuff before rendering fonts!
00228         glDisable(GL_DEPTH_TEST);
00229         glDisable(GL_COLOR_MATERIAL);
00230         glDisable(GL_BLEND);
00231 
00232         // output FPS
00233         glColor4f(1.0, 1.0, 1.0, 1.0);
00234         glut::push2D(s_defaultWidth, s_defaultHeight);
00235         char buffer[32];
00236         sprintf(buffer, "%4.1lf fps", s_fps.getFPS());
00237         glut::Font * font = glut::getDefaultFont();
00238         font->display(1, 16, 0, buffer);
00239         glut::pop2D();
00240 }
00241 
00242 
00243 
00244 class TestHost : public glut::Host {
00245 public:
00246         TestHost(void) throw() { }
00247         ~TestHost(void) throw() { }
00248 
00249         // glut::Host class interface methods ----------------------------------
00250         void init(void) {
00251 
00252                 // get material directory
00253                 std::string material_dir;
00254                 GetParentDirectory(m_file.c_str(), material_dir);
00255                 DPRINTF("Material directory: %s", material_dir.c_str());
00256 
00257                 // material loader/registry
00258                 m_registry = glut::getSimpleRegistry(material_dir.c_str());
00259                 ASSERT(m_registry, "null");
00260 
00261                 // load file
00262                 std::ifstream stream(m_file.c_str());
00263                 if (!stream.good()) {
00264                         WAVE_EX(wex);
00265                         wex << "failed to open file for reading: ";
00266                         wex << m_file;
00267                 }
00268                 glut::parseLodModel(m_registry, stream, m_model);
00269 
00270                 // get distance
00271                 rect3d_t r = m_model.getBoundingBox();
00272                 s_dist = 2.0 * r.getDiagonal();
00273 
00274                 // create render queue
00275                 int nSlots = 16;
00276                 m_rQueue = glut::RenderQueue::create(nSlots);
00277                 ASSERT(m_rQueue, "failed to create render queue");
00278         }
00279 
00280         void display(IN int w, IN int h) {
00281                         onDisplay(&m_model, m_rQueue);
00282                 }
00283 
00284         int shutdown(void) {
00285                         s_overallTimer = NULL;
00286                         perf::dumpTimingSummary(std::cerr);
00287                         return 0;
00288                 }
00289 
00290         // static factory methods ----------------------------------------------
00291         static smart_ptr<glut::Host> create(IN const char * model_file) {
00292                         ASSERT(model_file, "null");
00293 
00294                         smart_ptr<TestHost> local = new TestHost;
00295                         ASSERT(local, "out of memory");
00296 
00297                         local->m_file = model_file;
00298 
00299                         return local;
00300                 }
00301 
00302 private:
00303         // private member data -------------------------------------------------
00304         std::string                     m_file;
00305         smart_ptr<glut::MaterialRegistry> m_registry;
00306         smart_ptr<glut::RenderQueue>    m_rQueue;
00307         glut::lod_model_t               m_model;
00308 };
00309 
00310 
00311 
00312 ////////////////////////////////////////////////////////////////////////////////
00313 //
00314 //      entry point
00315 //
00316 ////////////////////////////////////////////////////////////////////////////////
00317 
00318 int
00319 main
00320 (
00321 IN int argc,
00322 IN const char * argv[]
00323 )
00324 {
00325         ASSERT(2 == argc, "Usage: glut-model-test <wgm-file>");
00326         const char * model_file = argv[1];
00327 
00328         try {
00329                 s_overallTimer = new perf::Timer("overall timer");
00330                 ASSERT(s_overallTimer, "null");
00331 
00332                 // create host object
00333                 smart_ptr<glut::Host> host = TestHost::create(model_file);
00334                 ASSERT(host, "null");
00335 
00336                 // ask glut to run
00337                 const char * filename = GetFilename(model_file);
00338                 std::string title = "glut-model test: ";
00339                 title += filename;
00340 
00341                 glut::start(argc, argv, s_defaultWidth, s_defaultHeight,
00342                     title.c_str(), NULL, host);
00343 
00344         } catch (std::exception& e) {
00345                 DPRINTF("Exception: %s", e.what());
00346                 s_retval = 1;
00347         }
00348 
00349         return 1;       // never get here!
00350 }
00351