afxEA_PointLight_T3D.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "T3D/pointLight.h"
  27. #include "afx/ce/afxPointLight_T3D.h"
  28. #include "afx/afxEffectDefs.h"
  29. #include "afx/afxEffectWrapper.h"
  30. #include "afx/afxChoreographer.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_T3DPointLight
  33. class PointLightProxy;
  34. class afxEA_T3DPointLight : public afxEffectWrapper
  35. {
  36. typedef afxEffectWrapper Parent;
  37. afxT3DPointLightData* light_data;
  38. PointLightProxy* light;
  39. void do_runtime_substitutions();
  40. public:
  41. /*C*/ afxEA_T3DPointLight();
  42. /*D*/ ~afxEA_T3DPointLight();
  43. virtual void ea_set_datablock(SimDataBlock*);
  44. virtual bool ea_start();
  45. virtual bool ea_update(F32 dt);
  46. virtual void ea_finish(bool was_stopped);
  47. virtual void ea_set_scope_status(bool flag);
  48. virtual void onDeleteNotify(SimObject*);
  49. virtual void getBaseColor(LinearColorF& color);
  50. virtual bool ea_is_enabled() { return true; }
  51. };
  52. //~~~~~~~~~~~~~~~~~~~~//
  53. class PointLightProxy : public PointLight
  54. {
  55. F32 mFade_amt;
  56. public:
  57. PointLightProxy() { mFade_amt = 1.0f; }
  58. void force_ghost()
  59. {
  60. mNetFlags.clear(Ghostable | ScopeAlways);
  61. mNetFlags.set(IsGhost);
  62. }
  63. void setFadeAmount(F32 fade_amt)
  64. {
  65. mFade_amt = fade_amt;
  66. mLight->setBrightness(mBrightness*fade_amt);
  67. }
  68. void updateTransform(const MatrixF& xfm)
  69. {
  70. mLight->setTransform(xfm);
  71. LightBase::setTransform(xfm);
  72. }
  73. void initWithDataBlock(const afxT3DPointLightData* db)
  74. {
  75. mRadius = db->mRadius;
  76. mColor = db->mColor;
  77. mBrightness = db->mBrightness;
  78. mCastShadows = db->mCastShadows;
  79. mPriority = db->mPriority;
  80. mFlareData = db->mFlareData;
  81. mAnimationData = db->mAnimationData;
  82. mAnimState.active = (mAnimationData != 0);
  83. mLocalRenderViz = db->mLocalRenderViz;
  84. mLight->setType( LightInfo::Point );
  85. mLight->setBrightness( db->mBrightness );
  86. mLight->setRange( db->mRadius );
  87. mLight->setColor( db->mColor );
  88. mLight->setCastShadows( db->mCastShadows );
  89. mLight->setPriority( db->mPriority );
  90. // Update the bounds and scale to fit our light.
  91. mObjBox.minExtents.set( -1, -1, -1 );
  92. mObjBox.maxExtents.set( 1, 1, 1 );
  93. mObjScale.set( db->mRadius, db->mRadius, db->mRadius );
  94. //_conformLights();
  95. }
  96. void setLiveColor(const LinearColorF& live_color)
  97. {
  98. mLight->setColor(live_color);
  99. }
  100. void submitLights(LightManager* lm, bool staticLighting)
  101. {
  102. if (mAnimState.active && mAnimationData && mFade_amt < 1.0f)
  103. {
  104. F32 mBrightness_save = mBrightness;
  105. mBrightness *= mFade_amt;
  106. PointLight::submitLights(lm, staticLighting);
  107. mBrightness = mBrightness_save;
  108. return;
  109. }
  110. PointLight::submitLights(lm, staticLighting);
  111. }
  112. };
  113. //~~~~~~~~~~~~~~~~~~~~//
  114. afxEA_T3DPointLight::afxEA_T3DPointLight()
  115. {
  116. light_data = 0;
  117. light = 0;
  118. }
  119. afxEA_T3DPointLight::~afxEA_T3DPointLight()
  120. {
  121. if (light)
  122. light->deleteObject();
  123. if (light_data && light_data->isTempClone())
  124. delete light_data;
  125. light_data = 0;
  126. }
  127. void afxEA_T3DPointLight::ea_set_datablock(SimDataBlock* db)
  128. {
  129. light_data = dynamic_cast<afxT3DPointLightData*>(db);
  130. }
  131. bool afxEA_T3DPointLight::ea_start()
  132. {
  133. if (!light_data)
  134. {
  135. Con::errorf("afxEA_T3DPointLight::ea_start() -- missing or incompatible datablock.");
  136. return false;
  137. }
  138. do_runtime_substitutions();
  139. // create and register effect
  140. light = new PointLightProxy();
  141. light->force_ghost();
  142. if (!light->registerObject())
  143. {
  144. delete light;
  145. light = 0;
  146. Con::errorf("afxEA_T3DPointLight::ea_update() -- effect failed to register.");
  147. return false;
  148. }
  149. deleteNotify(light);
  150. light->initWithDataBlock(light_data);
  151. return true;
  152. }
  153. bool afxEA_T3DPointLight::ea_update(F32 dt)
  154. {
  155. if (light)
  156. {
  157. #if 0 // AFX_T3D_DISABLED
  158. // With sgLightObject lights, the following code block would hook
  159. // the constraint object up to the light in case the light was
  160. // configured to exclude it from flare occusions. The code remains
  161. // here in case we need to implement the same feature for T3D light.
  162. afxConstraint* pos_cons = getPosConstraint();
  163. SceneObject* cons_obj = (pos_cons) ? pos_cons->getSceneObject() : 0;
  164. light->setConstraintObject(cons_obj);
  165. #endif
  166. light->setLiveColor(mUpdated_color);
  167. if (mDo_fades)
  168. light->setFadeAmount(mFade_value*mUpdated_scale.x);
  169. light->updateTransform(mUpdated_xfm);
  170. // scale should not be updated this way. It messes up the culling.
  171. //light->setScale(updated_scale);
  172. }
  173. return true;
  174. }
  175. void afxEA_T3DPointLight::ea_finish(bool was_stopped)
  176. {
  177. if (light)
  178. {
  179. light->deleteObject();
  180. light = 0;
  181. }
  182. }
  183. void afxEA_T3DPointLight::ea_set_scope_status(bool in_scope)
  184. {
  185. if (light)
  186. light->setLightEnabled(in_scope);
  187. }
  188. void afxEA_T3DPointLight::onDeleteNotify(SimObject* obj)
  189. {
  190. if (light == dynamic_cast<PointLight*>(obj))
  191. light = 0;
  192. Parent::onDeleteNotify(obj);
  193. }
  194. void afxEA_T3DPointLight::getBaseColor(LinearColorF& color)
  195. {
  196. if (light_data)
  197. color = light_data->mColor;
  198. }
  199. void afxEA_T3DPointLight::do_runtime_substitutions()
  200. {
  201. // only clone the datablock if there are substitutions
  202. if (light_data->getSubstitutionCount() > 0)
  203. {
  204. // clone the datablock and perform substitutions
  205. afxT3DPointLightData* orig_db = light_data;
  206. light_data = new afxT3DPointLightData(*orig_db, true);
  207. orig_db->performSubstitutions(light_data, mChoreographer, mGroup_index);
  208. }
  209. }
  210. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  211. class afxEA_T3DPointLightDesc : public afxEffectAdapterDesc, public afxEffectDefs
  212. {
  213. static afxEA_T3DPointLightDesc desc;
  214. public:
  215. virtual bool testEffectType(const SimDataBlock*) const;
  216. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  217. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  218. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  219. virtual afxEffectWrapper* create() const { return new afxEA_T3DPointLight; }
  220. };
  221. afxEA_T3DPointLightDesc afxEA_T3DPointLightDesc::desc;
  222. bool afxEA_T3DPointLightDesc::testEffectType(const SimDataBlock* db) const
  223. {
  224. return (typeid(afxT3DPointLightData) == typeid(*db));
  225. }
  226. bool afxEA_T3DPointLightDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  227. {
  228. return (timing.lifetime < 0);
  229. }
  230. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//