frustum.cpp

Go to the documentation of this file.
00001 /*
00002  * frustum.cpp
00003  *
00004  * Copyright (C) 2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Retrieves view frustum from OpenGL matrices.  See frustum.h
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "frustum.h"                    // always include our own header first!
00036 
00037 #include <math.h>
00038 
00039 #include "wave-glut.h"
00040 
00041 
00042 
00043 namespace glut {
00044 
00045 
00046 static const float s_radiansPerDegree   = M_PI / 180.0;
00047 
00048 
00049 ////////////////////////////////////////////////////////////////////////////////
00050 //
00051 //      static helper methods
00052 //
00053 ////////////////////////////////////////////////////////////////////////////////
00054 
00055 
00056 
00057 ////////////////////////////////////////////////////////////////////////////////
00058 //
00059 //      public API
00060 //
00061 ////////////////////////////////////////////////////////////////////////////////
00062 
00063 void
00064 getViewFrustum
00065 (
00066 IN const camera_t& camera,
00067 IN const Viewer& viewer,
00068 OUT frustum_t& f                ///< view frustum
00069 )
00070 throw()
00071 {
00072         // just use the basic geometry libraries
00073         ASSERT(createViewFrustum(viewer.getPosition(),
00074                 viewer.getFacing(),
00075                 viewer.getUp(),
00076                 camera.getFovy() * s_radiansPerDegree,
00077                 camera.getZNear(),
00078                 camera.getZFar(),
00079                 camera.getAspect(),
00080                 f),
00081             "Failed to create view frustum--bad inputs?");
00082 }
00083 
00084 
00085 
00086 void
00087 drawFrustumEdges
00088 (
00089 IN const frustum_t& f,
00090 IN const glut_color_t& c
00091 )
00092 throw()
00093 {
00094         // save existing color and lighting
00095         int oldLighting;
00096         glGetIntegerv(GL_LIGHTING, &oldLighting);
00097 //      DPRINTF("Old lighting: %d", oldLighting);
00098         if (oldLighting) {
00099                 glDisable(GL_LIGHTING);
00100         }
00101         float oldColor[4];
00102         glGetFloatv(GL_CURRENT_COLOR, (GLfloat *) oldColor);
00103 
00104         // set new color and draw
00105         glColor4b(c.red, c.green, c.blue, c.alpha);
00106         glBegin(GL_LINES);
00107         point3d_t p0, p1;
00108         for (int i = 0; i < frustum_t::eEdgeCount; ++i) {
00109                 f.getEdge(i, p0, p1);
00110                 glVertex3fv((GLfloat *) &p0.x);
00111                 glVertex3fv((GLfloat *) &p1.x);
00112         }
00113         glEnd();
00114 
00115         // restore color and lighting
00116         glColor4fv((GLfloat *) oldColor);
00117         if (oldLighting) {
00118                 glEnable(GL_LIGHTING);
00119         }
00120 }
00121 
00122 
00123 
00124 };      // glut namespace
00125