xform-model.cpp

Go to the documentation of this file.
00001 /*
00002  * xform-model.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  * Transformation wrapper for other model types (see xform-model.h)
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "xform-model.h"                // always include our own header first
00036 
00037 #include "common/wave_ex.h"
00038 #include "perf/perf.h"
00039 
00040 
00041 namespace glut {
00042 
00043 
00044 
00045 ////////////////////////////////////////////////////////////////////////////////
00046 //
00047 //      static helper methods
00048 //
00049 ////////////////////////////////////////////////////////////////////////////////
00050 
00051 
00052 ////////////////////////////////////////////////////////////////////////////////
00053 //
00054 //      XformModel -- class that implements the Renderable interface for
00055 //              Open Asset Import library aiScene xformects.
00056 //
00057 ////////////////////////////////////////////////////////////////////////////////
00058 
00059 class XformModel : public Renderable {
00060 public:
00061         // constructor, destructor ---------------------------------------------
00062         XformModel(void) throw();
00063         ~XformModel(void) throw() { }
00064 
00065         // public class methods ------------------------------------------------
00066         void initialize(IN const matrix4_t& xform,
00067                                 IN smart_ptr<Renderable>& model);
00068 
00069         // glut::Renderable class interface methods ----------------------------
00070         rect3d_t getBoundingBox(void) const throw() { return m_bounds; }
00071         void render(IN const glut::render_context_t& rc,
00072                                 IN RenderQueue * rq);
00073         bool isAnimateable(void) const throw();
00074         void getAnimationState(OUT std::string &state);
00075         bool setAnimationState(IN const char * state);
00076         void getAnimationVerbs(OUT VecString& verbs);
00077         bool setAnimation(IN const char * verb);
00078 
00079 private:
00080         // private typedefs ----------------------------------------------------
00081 
00082         // private helper methods ----------------------------------------------
00083 
00084         // private member data -------------------------------------------------
00085         rect3d_t                        m_bounds;
00086         std::string                     m_timerName;
00087         matrix4_t                       m_xform;        // transformation
00088         matrix4_t                       m_xformTranspose;// transpose of xform
00089         smart_ptr<Renderable>           m_model;        // base model object
00090 };
00091 
00092 
00093 
00094 XformModel::XformModel
00095 (
00096 void
00097 )
00098 throw()
00099 {
00100 }
00101 
00102 
00103 
00104 void
00105 XformModel::initialize
00106 (
00107 IN const matrix4_t& xform,
00108 IN smart_ptr<Renderable>& model
00109 )
00110 {
00111         ASSERT(model, "null");
00112 
00113         m_xform = xform;
00114         m_model = model;
00115 
00116         // keep a copy of the transposed matrix (handy for OpenGL glMultMatrix)
00117         m_xformTranspose = m_xform;
00118         m_xformTranspose.transpose();
00119 
00120         // TODO: transform bounding box!
00121         m_bounds = model->getBoundingBox();
00122 }
00123 
00124 
00125 
00126 void
00127 XformModel::render
00128 (
00129 IN const glut::render_context_t& rc,
00130 IN RenderQueue * rq
00131 )
00132 {
00133         perf::Timer timer(m_timerName.c_str());
00134         ASSERT(rq, "null");
00135         ASSERT(m_model, "null");
00136 
00137         // push state
00138         glMatrixMode(GL_MODELVIEW);
00139         glPushMatrix();
00140 
00141         // add our own transform
00142         glMultMatrixf(m_xformTranspose.m);
00143 
00144         // ask model to render
00145         m_model->render(rc, rq);
00146 
00147         // pop state
00148         glMatrixMode(GL_MODELVIEW);
00149         glPopMatrix();
00150 }
00151 
00152 
00153 
00154 bool
00155 XformModel::isAnimateable
00156 (
00157 void
00158 )
00159 const
00160 throw()
00161 {
00162         ASSERT(m_model, "null");
00163 
00164         return m_model->isAnimateable();
00165 }
00166 
00167 
00168 
00169 void
00170 XformModel::getAnimationState
00171 (
00172 OUT std::string &state
00173 )
00174 {
00175         ASSERT(m_model, "null");
00176 
00177         m_model->getAnimationState(state);
00178 }
00179 
00180 
00181 
00182 bool
00183 XformModel::setAnimationState
00184 (
00185 IN const char * state
00186 )
00187 {
00188         ASSERT(state, "null");
00189         ASSERT(m_model, "null");
00190 
00191         return m_model->setAnimationState(state);
00192 }
00193 
00194 
00195 
00196 void
00197 XformModel::getAnimationVerbs
00198 (
00199 OUT VecString& verbs
00200 )
00201 {
00202         ASSERT(m_model, "null");
00203 
00204         return m_model->getAnimationVerbs(verbs);
00205 }
00206 
00207 
00208 
00209 bool
00210 XformModel::setAnimation
00211 (
00212 IN const char * verb
00213 )
00214 {
00215         ASSERT(m_model, "null");
00216 
00217         return m_model->setAnimation(verb);
00218 }
00219 
00220 
00221 
00222 ////////////////////////////////////////////////////////////////////////////////
00223 //
00224 //      XformModel -- private helper methods
00225 //
00226 ////////////////////////////////////////////////////////////////////////////////
00227 
00228 ////////////////////////////////////////////////////////////////////////////////
00229 //
00230 //      public API
00231 //
00232 ////////////////////////////////////////////////////////////////////////////////
00233 
00234 smart_ptr<Renderable>
00235 createXformModel
00236 (
00237 IN const matrix4_t& xform,
00238 IN smart_ptr<Renderable>& model
00239 )
00240 {
00241         ASSERT(model, "null");
00242 
00243         smart_ptr<XformModel> local = new XformModel;
00244         ASSERT(local, "out of memory");
00245 
00246         local->initialize(xform, model);
00247 
00248         return local;
00249 }
00250 
00251 
00252 
00253 };      // glut namespace
00254