skybox.cpp

Go to the documentation of this file.
00001 /*
00002  * skybox.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  * Simple wrapper to convert any model to a skybox.  In practice, "any model"
00032  * will in fact be a large cube with skybox textures on its faces. 
00033  */
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "skybox.h"             // always include our own header first
00037 
00038 #include "perf/perf.h"
00039 
00040 
00041 namespace glut {
00042 
00043 
00044 
00045 
00046 ////////////////////////////////////////////////////////////////////////////////
00047 //
00048 //      static helper methods
00049 //
00050 ////////////////////////////////////////////////////////////////////////////////
00051 
00052 class Skybox : public glut::Renderable {
00053 public:
00054         // constructor, destructor ---------------------------------------------
00055         Skybox(void) throw() { }
00056         ~Skybox(void) throw() { }
00057 
00058         // public class methods ------------------------------------------------
00059         void initialize(IN smart_ptr<Renderable>& model);
00060 
00061         // glut::Renderable class interface methods ----------------------------
00062         void render(IN const render_context_t& rc,
00063                         IN RenderQueue * rq);
00064         rect3d_t getBoundingBox(void) const throw() { return m_boundingBox; }
00065 
00066 private:
00067         // private member data -------------------------------------------------
00068         smart_ptr<Renderable>           m_model;
00069         rect3d_t                        m_boundingBox;
00070 };
00071 
00072 
00073 
00074 void
00075 Skybox::initialize
00076 (
00077 IN smart_ptr<Renderable>& model
00078 )
00079 {
00080         ASSERT(model, "null");
00081 
00082         m_model = model;
00083 
00084         // set bounding box to 2km on a side
00085         m_boundingBox.clear();
00086         m_boundingBox.inflate(1000);
00087 }
00088 
00089 
00090 
00091 void
00092 Skybox::render
00093 (
00094 IN const render_context_t& rc,
00095 IN RenderQueue * rq
00096 )
00097 {
00098         perf::Timer timer("Skybox::render");
00099         ASSERT(rq, "null");
00100         ASSERT(m_model, "null");
00101 
00102         // center skybox object wherever the viewer is
00103 //      point3d_t newPos = viewer.getPosition();
00104 
00105         //DPRINTF("Need to translate!");
00106 
00107         int iOldMode;
00108         glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &iOldMode);
00109         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00110 
00111         // draw!
00112         m_model->render(rc, rq);
00113 
00114         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, iOldMode);
00115 }
00116 
00117 
00118 
00119 ////////////////////////////////////////////////////////////////////////////////
00120 //
00121 //      public API
00122 //
00123 ////////////////////////////////////////////////////////////////////////////////
00124 
00125 smart_ptr<Renderable>
00126 createSkybox
00127 (
00128 IN smart_ptr<Renderable>& model
00129 )
00130 {
00131         ASSERT(model, "null");
00132 
00133         smart_ptr<Skybox> local = new Skybox;
00134         ASSERT(local, "out of memory");
00135 
00136         local->initialize(model);
00137 
00138         return local;
00139 }
00140 
00141 
00142 
00143 };      // glut namespace
00144