# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/MorphingCallback.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY 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 ON    */
    00026 /*  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
    00032 #include <vlGraphics/MorphingCallback.hpp>
    00033 #include <vlCore/ResourceDatabase.hpp>
    00034 #include <vlGraphics/GLSL.hpp>
    00035
    00036 using namespace vl;
    00037
    00038 //-----------------------------------------------------------------------------
    00039 // MorphingCallback
    00040 //-----------------------------------------------------------------------------
    00041 MorphingCallback::MorphingCallback()
    00042 {
    00043   VL_DEBUG_SET_OBJECT_NAME()
    00044
    00045   mGeometry = new Geometry;
    00046   setAnimation(0,0,0);
    00047   resetGLSLBindings();
    00048   setGLSLVertexBlendEnabled(false);
    00049
    00050   mAnim_t = 0.0f;
    00051   mFrame1 = -1;
    00052   mFrame2 = -1;
    00053   mLastUpdate = -1;
    00054 }
    00055 //-----------------------------------------------------------------------------
    00056 MorphingCallback::~MorphingCallback()
    00057 {
    00058 }
    00059 //-----------------------------------------------------------------------------
    00060 void MorphingCallback::onActorRenderStarted(Actor*, Real frame_clock, const Camera*, Renderable*, const Shader* shader, int pass)
    00061 {
    00062   // perform only on the first pass
    00063   if (pass>0)
    00064     return;
    00065
    00066   if (!mAnimationStarted)
    00067     return;
    00068
    00069   mElapsedTime = frame_clock - mAnimationStartTime;
    00070   // 30 fps update using the CPU vertex blending or continuous update if using the GPU
    00071   bool do_update = mLastUpdate == -1 || (mElapsedTime - mLastUpdate) > 1.0f/30.0f || glslVertexBlendEnabled();
    00072   if ( do_update )
    00073   {
    00074     mLastUpdate = mElapsedTime;
    00075     Real ft = mElapsedTime / mAnimationPeriod;
    00076     ft = ft - (int)ft;
    00077     int frame_count = mAnimationEnd - mAnimationStart + 1;
    00078     mAnim_t  = (float)(ft * frame_count - (int)(ft * frame_count));
    00079     mFrame1 = (int)(ft * frame_count);
    00080     mFrame2 = (mFrame1 + 1) % frame_count;
    00081     mFrame1 += mAnimationStart;
    00082     mFrame2 += mAnimationStart;
    00083     VL_CHECK(mFrame1 >= 0)
    00084     VL_CHECK(mLastUpdate>=0)
    00085   }
    00086
    00087   VL_CHECK(mFrame1 != -1)
    00088   VL_CHECK(mLastUpdate != -1)
    00089
    00090   if (mLastUpdate == -1 || mFrame1 == -1)
    00091     return;
    00092
    00093   const GLSLProgram* glslprogram = shader->glslProgram();
    00094
    00095   // from here you can change uniforms or query uniform binding location
    00096
    00097   if ( glslVertexBlendEnabled() && glslprogram )
    00098   {
    00099     // vertex/normals frame 1
    00100     mGeometry->setVertexArray( mVertexFrames[mFrame1].get() );
    00101     mGeometry->setNormalArray( mNormalFrames[mFrame1].get() );
    00102
    00103     if (!mVertexFrames[mFrame1]->gpuBuffer()->handle())
    00104       mVertexFrames[mFrame1]->updateVBO();
    00105
    00106     if (!mVertexFrames[mFrame2]->gpuBuffer()->handle())
    00107       mVertexFrames[mFrame2]->updateVBO();
    00108
    00109     if (!mNormalFrames[mFrame1]->gpuBuffer()->handle())
    00110       mNormalFrames[mFrame1]->updateVBO();
    00111
    00112     if (!mNormalFrames[mFrame2]->gpuBuffer()->handle())
    00113       mNormalFrames[mFrame2]->updateVBO();
    00114
    00115     VL_CHECK( mVertexFrames[mFrame1]->gpuBuffer()->handle() )
    00116     VL_CHECK( mVertexFrames[mFrame2]->gpuBuffer()->handle() )
    00117     VL_CHECK( mNormalFrames[mFrame1]->gpuBuffer()->handle() )
    00118     VL_CHECK( mNormalFrames[mFrame2]->gpuBuffer()->handle() )
    00119
    00120     #if 1 // faster method:
    00121
    00122       // vertex attrib and uniform animation
    00123       if (mVertex2_Binding == -1)
    00124         mVertex2_Binding = glslprogram->getAttribLocation("vertex2");
    00125
    00126       if (mNormal2_Binding == -1)
    00127         mNormal2_Binding = glslprogram->getAttribLocation("normal2");
    00128
    00129       if (mAnim_t_Binding  == -1)
    00130         mAnim_t_Binding = glslprogram->getUniformLocation("anim_t");
    00131
    00132       // vertex/normals frame 2
    00133       mGeometry->setVertexAttribArray( mVertex2_Binding, mVertexFrames[mFrame2].get() );
    00134       mGeometry->setVertexAttribArray( mNormal2_Binding, mNormalFrames[mFrame2].get() );
    00135       // frame interpolation ratio
    00136       glUniform1fv(mAnim_t_Binding, 1, &mAnim_t);
    00137
    00138     #else // slower but simpler method:
    00139
    00140       // vertex/normals frame 2
    00141       mGeometry->setVertexAttribArray( glslprogram->getAttribLocation("vertex2"), false, false, mVertexFrames[mFrame2].get() );
    00142       mGeometry->setVertexAttribArray( glslprogram->getAttribLocation("normal2"), false, false, mNormalFrames[mFrame2].get() );
    00143       // frame interpolation ratio
    00144       glUniform1fv(glslprogram->getUniformLocation("anim_t"), 1, &mAnim_t);
    00145     #endif
    00146   }
    00147   else
    00148   if ( do_update )
    00149   {
    00150     if (mGeometry->vertexArray() == NULL)
    00151       mGeometry->setVertexArray(mVertices.get());
    00152
    00153     if (mGeometry->normalArray() == NULL)
    00154       mGeometry->setNormalArray(mNormals.get());
    00155
    00156     blendFrames(mFrame1, mFrame2, mAnim_t);
    00157   }
    00158 }
    00159 //-----------------------------------------------------------------------------
    00160 void MorphingCallback::bindActor(Actor* actor)
    00161 {
    00162   actor->actorEventCallbacks()->push_back( this );
    00163   actor->setLod(0, mGeometry.get());
    00164 }
    00165 //-----------------------------------------------------------------------------
    00166 void MorphingCallback::init(ResourceDatabase* res_db)
    00167 {
    00168   if (res_db->count<Geometry>() == 0)
    00169     return;
    00170
    00171   Geometry* geometry = res_db->get<Geometry>(0);
    00172   geometry->shallowCopy( mGeometry.get() );
    00173   mVertices = new ArrayFloat3;
    00174   mNormals  = new ArrayFloat3;
    00175
    00176   // setup Geometry vertex attributes
    00177
    00178   // copy vertex frames
    00179
    00180   for(unsigned i=0, count=res_db->count<ArrayAbstract>(); i<count; ++i)
    00181   {
    00182     ArrayFloat3* buffer = dynamic_cast<ArrayFloat3*>(res_db->get<ArrayAbstract>(i));
    00183     if (buffer && buffer->objectName() == "vertex_frame")
    00184     {
    00185       mVertexFrames.push_back(buffer);
    00186     }
    00187     else
    00188     if (buffer && buffer->objectName() == "normal_frame")
    00189     {
    00190       mNormalFrames.push_back(buffer);
    00191     }
    00192   }
    00193
    00194   if (mVertexFrames.empty())
    00195   {
    00196     Log::error("MorphingCallback::init(): no ArrayFloat3 named 'vertex_frame' found.\n");
    00197     return;
    00198   }
    00199
    00200   if (mNormalFrames.empty())
    00201   {
    00202     Log::error("MorphingCallback::init(): no ArrayFloat3 named 'normal_frame' found.\n");
    00203     return;
    00204   }
    00205
    00206   if (mVertexFrames.size() != mNormalFrames.size())
    00207   {
    00208     Log::error("MorphingCallback::init(): vertex frame count differs from normal frame count.\n");
    00209     return;
    00210   }
    00211
    00212   // compute AABB using the first frame
    00213
    00214   mGeometry->setVertexArray(mVertexFrames[0].get() );
    00215   mGeometry->setNormalArray(mNormalFrames[0].get() );
    00216   mGeometry->computeBounds();
    00217
    00218   mGeometry->setVertexArray(NULL);
    00219   mGeometry->setNormalArray(NULL);
    00220 }
    00221 //-----------------------------------------------------------------------------
    00222 void MorphingCallback::blendFrames(int a, int b, float t)
    00223 {
    00224   // allocate interpolation buffers
    00225   if (mVertices->size() != mVertexFrames[0]->size() ||
    00226       mNormals->size()  != mNormalFrames[0]->size() )
    00227   {
    00228     mVertices->resize( mVertexFrames[0]->size() );
    00229     mNormals->resize(  mNormalFrames[0]->size() );
    00230   }
    00231
    00232   #if 1
    00233     float Ha = 1-t;
    00234     float Hb = t;
    00235   #else
    00236     float Ha = 2*t*t*t - 3*t*t + 1;
    00237     float Hb = -2*t*t*t + 3*t*t;
    00238   #endif
    00239
    00240   for(size_t i=0; i<mVertices->size(); ++i)
    00241   {
    00242     mVertices->at(i) = mVertexFrames[ a ]->at(i)*Ha + mVertexFrames[ b ]->at(i)*Hb;
    00243     mNormals->at(i)  = mNormalFrames[ a ]->at(i)*Ha + mNormalFrames[ b ]->at(i)*Hb;
    00244   }
    00245
    00246   if (mGeometry->vboEnabled() && (GLEW_VERSION_1_5||GLEW_ARB_vertex_buffer_object))
    00247   {
    00248     mVertices->gpuBuffer()->setBufferData(BU_DYNAMIC_DRAW, false);
    00249     mNormals ->gpuBuffer()->setBufferData(BU_DYNAMIC_DRAW, false);
    00250   }
    00251 }
    00252 //-----------------------------------------------------------------------------
    00253 void MorphingCallback::setAnimation(int start, int end, float period)
    00254 {
    00255   mFrame1 = -1;
    00256   mFrame2 = -1;
    00257   mLastUpdate = -1;
    00258   mElapsedTime = 0;
    00259   mAnimationStartTime = 0;
    00260   mAnimationStart   = start;
    00261   mAnimationEnd     = end;
    00262   mAnimationPeriod  = period;
    00263   mAnimationStarted = false;
    00264 }
    00265 //-----------------------------------------------------------------------------
    00266 void MorphingCallback::startAnimation(Real start_time)
    00267 {
    00268   mAnimationStarted = true;
    00269   mFrame1 = -1;
    00270   mFrame2 = -1;
    00271   mLastUpdate = -1;
    00272   mElapsedTime = 0;
    00273   mAnimationStartTime = start_time;
    00274 }
    00275 //-----------------------------------------------------------------------------
    00276 void MorphingCallback::stopAnimation()
    00277 {
    00278   mAnimationStarted = false;
    00279 }
    00280 //-----------------------------------------------------------------------------
    00281 void MorphingCallback::initFrom(MorphingCallback* morph_cb)
    00282 {
    00283   mVertices = new ArrayFloat3;
    00284   mNormals  = new ArrayFloat3;
    00285
    00286   // copy vertex frames
    00287
    00288   mVertexFrames = morph_cb->mVertexFrames;
    00289   mNormalFrames = morph_cb->mNormalFrames;
    00290
    00291   #if 0
    00292     // Geometry sharing method: works only wiht GLSL
    00293
    00294     // we can have a single shared Geometry since our MorphingCallback setups the
    00295     // appropriate position and normal arrays for every Actor just before the rendering!
    00296     mGeometry = morph_cb->mGeometry;
    00297   #else
    00298     // Geometry copy method
    00299     morph_cb->mGeometry->shallowCopy( mGeometry.get() );
    00300
    00301     // compute AABB using the first frame
    00302
    00303     mGeometry->setVertexArray(morph_cb->mVertexFrames[0].get() );
    00304     mGeometry->setNormalArray(morph_cb->mNormalFrames[0].get() );
    00305     mGeometry->computeBounds();
    00306
    00307     mGeometry->setVertexArray(NULL);
    00308     mGeometry->setNormalArray(NULL);
    00309   #endif
    00310
    00311   setAnimation(0,0,0);
    00312 }
    00313 //-----------------------------------------------------------------------------
    00314 void MorphingCallback::resetGLSLBindings()
    00315 {
    00316   mVertex2_Binding = -1;
    00317   mNormal2_Binding = -1;
    00318   mAnim_t_Binding  = -1;
    00319 }
    00320 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:24.
Permission is granted to use this page to write and publish articles regarding Visualization Library.