Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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
00063 if (pass>0)
00064 return;
00065
00066 if (!mAnimationStarted)
00067 return;
00068
00069 mElapsedTime = frame_clock - mAnimationStartTime;
00070
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
00096
00097 if ( glslVertexBlendEnabled() && glslprogram )
00098 {
00099
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
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
00133 mGeometry->setVertexAttribArray( mVertex2_Binding, mVertexFrames[mFrame2].get() );
00134 mGeometry->setVertexAttribArray( mNormal2_Binding, mNormalFrames[mFrame2].get() );
00135
00136 glUniform1fv(mAnim_t_Binding, 1, &mAnim_t);
00137
00138 #else // slower but simpler method:
00139
00140
00141 mGeometry->setVertexAttribArray( glslprogram->getAttribLocation("vertex2"), false, false, mVertexFrames[mFrame2].get() );
00142 mGeometry->setVertexAttribArray( glslprogram->getAttribLocation("normal2"), false, false, mNormalFrames[mFrame2].get() );
00143
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
00177
00178
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
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
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
00287
00288 mVertexFrames = morph_cb->mVertexFrames;
00289 mNormalFrames = morph_cb->mNormalFrames;
00290
00291 #if 0
00292
00293
00294
00295
00296 mGeometry = morph_cb->mGeometry;
00297 #else
00298
00299 morph_cb->mGeometry->shallowCopy( mGeometry.get() );
00300
00301
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