afxBillboard.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "afx/arcaneFX.h"
  25. #include "gfx/gfxAPI.h"
  26. #include "math/mathIO.h"
  27. #include "afx/afxChoreographer.h"
  28. #include "afx/ce/afxBillboard.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. // afxBillboardData
  31. IMPLEMENT_CO_DATABLOCK_V1(afxBillboardData);
  32. ConsoleDocClass( afxBillboardData,
  33. "@brief A datablock that specifies a Billboard effect.\n\n"
  34. "A Billboard effect is a textured quadrangle which is always aligned to face towards the camera. It is much like a single "
  35. "static particle and is rendered in a similar fashion."
  36. "\n\n"
  37. "@ingroup afxEffects\n"
  38. "@ingroup AFX\n"
  39. "@ingroup Datablocks\n"
  40. );
  41. afxBillboardData::afxBillboardData()
  42. {
  43. color.set(1.0f, 1.0f, 1.0f, 1.0f);
  44. INIT_ASSET(Texture);
  45. dimensions.set(1.0f, 1.0f);
  46. texCoords[0].set(0.0f, 0.0f);
  47. texCoords[1].set(0.0f, 1.0f);
  48. texCoords[2].set(1.0f, 1.0f);
  49. texCoords[3].set(1.0f, 0.0f);
  50. blendStyle = BlendUndefined;
  51. srcBlendFactor = BLEND_UNDEFINED;
  52. dstBlendFactor = BLEND_UNDEFINED;
  53. }
  54. afxBillboardData::afxBillboardData(const afxBillboardData& other, bool temp_clone)
  55. : GameBaseData(other, temp_clone)
  56. {
  57. color = other.color;
  58. CLONE_ASSET(Texture);
  59. dimensions = other.dimensions;
  60. texCoords[0] = other.texCoords[0];
  61. texCoords[1] = other.texCoords[1];
  62. texCoords[2] = other.texCoords[2];
  63. texCoords[3] = other.texCoords[3];
  64. blendStyle = other.blendStyle;
  65. srcBlendFactor = other.srcBlendFactor;
  66. dstBlendFactor = other.dstBlendFactor;
  67. }
  68. #define myOffset(field) Offset(field, afxBillboardData)
  69. extern EnumTable srcBlendFactorTable;
  70. extern EnumTable dstBlendFactorTable;
  71. ImplementEnumType( afxBillboard_BlendStyle, "Possible blending types.\n" "@ingroup afxBillboard\n\n" )
  72. { afxBillboardData::BlendNormal, "NORMAL", "..." },
  73. { afxBillboardData::BlendAdditive, "ADDITIVE", "..." },
  74. { afxBillboardData::BlendSubtractive, "SUBTRACTIVE", "..." },
  75. { afxBillboardData::BlendPremultAlpha, "PREMULTALPHA", "..." },
  76. EndImplementEnumType;
  77. void afxBillboardData::initPersistFields()
  78. {
  79. addField("color", TypeColorF, myOffset(color),
  80. "The color assigned to the quadrangle geometry. The way it combines with the given "
  81. "texture varies according to the setting of the textureFunction field.");
  82. INITPERSISTFIELD_IMAGEASSET(Texture, afxBillboardData, "An image to use as the billboard's texture.");
  83. addField("dimensions", TypePoint2F, myOffset(dimensions),
  84. "A value-pair that specifies the horizontal and vertical dimensions of the billboard "
  85. "in scene units.");
  86. addField("textureCoords", TypePoint2F, myOffset(texCoords), 4,
  87. "An array of four value-pairs that specify the UV texture coordinates for the four "
  88. "corners of the billboard's quadrangle.");
  89. addField("blendStyle", TYPEID<afxBillboardData::BlendStyle>(), myOffset(blendStyle),
  90. "Selects a common blend factor preset. When set to 'user', srcBlendFactor and "
  91. "dstBlendFactor can be used to set additional blend factor combinations.\n"
  92. "Possible values: normal, additive, subtractive, premultalpha, or user.");
  93. addField("srcBlendFactor", TYPEID<GFXBlend>(), myOffset(srcBlendFactor),
  94. "Specifies source blend factor when blendStyle is set to 'user'.\n"
  95. "Possible values: GFXBlendZero, GFXBlendOne, GFXBlendDestColor, GFXBlendInvDestColor, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha, GFXBlendDestAlpha, GFXBlendInvDestAlpha, or GFXBlendSrcAlphaSat");
  96. addField("dstBlendFactor", TYPEID<GFXBlend>(), myOffset(dstBlendFactor),
  97. "Specifies destination blend factor when blendStyle is set to 'user'.\n"
  98. "Possible values: GFXBlendZero, GFXBlendOne, GFXBlendSrcColor, GFXBlendInvSrcColor, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha, GFXBlendDestAlpha, or GFXBlendInvDestAlpha");
  99. Parent::initPersistFields();
  100. }
  101. void afxBillboardData::packData(BitStream* stream)
  102. {
  103. Parent::packData(stream);
  104. stream->write(color);
  105. PACKDATA_ASSET(Texture);
  106. mathWrite(*stream, dimensions);
  107. mathWrite(*stream, texCoords[0]);
  108. mathWrite(*stream, texCoords[1]);
  109. mathWrite(*stream, texCoords[2]);
  110. mathWrite(*stream, texCoords[3]);
  111. stream->writeInt(srcBlendFactor, 4);
  112. stream->writeInt(dstBlendFactor, 4);
  113. }
  114. void afxBillboardData::unpackData(BitStream* stream)
  115. {
  116. Parent::unpackData(stream);
  117. stream->read(&color);
  118. UNPACKDATA_ASSET(Texture);
  119. mathRead(*stream, &dimensions);
  120. mathRead(*stream, &texCoords[0]);
  121. mathRead(*stream, &texCoords[1]);
  122. mathRead(*stream, &texCoords[2]);
  123. mathRead(*stream, &texCoords[3]);
  124. srcBlendFactor = (GFXBlend) stream->readInt(4);
  125. dstBlendFactor = (GFXBlend) stream->readInt(4);
  126. }
  127. bool afxBillboardData::preload(bool server, String &errorStr)
  128. {
  129. if (!Parent::preload(server, errorStr))
  130. return false;
  131. // if blend-style is set to User, check for defined blend-factors
  132. if (blendStyle == BlendUser && (srcBlendFactor == BLEND_UNDEFINED || dstBlendFactor == BLEND_UNDEFINED))
  133. {
  134. blendStyle = BlendUndefined;
  135. Con::warnf(ConsoleLogEntry::General, "afxBillboardData(%s) incomplete blend factor specification.", getName());
  136. }
  137. // silently switch Undefined blend-style to User if blend factors are both defined
  138. if (blendStyle == BlendUndefined && srcBlendFactor != BLEND_UNDEFINED && dstBlendFactor != BLEND_UNDEFINED)
  139. {
  140. blendStyle = BlendUser;
  141. }
  142. // set pre-defined blend-factors
  143. switch (blendStyle)
  144. {
  145. case BlendNormal:
  146. srcBlendFactor = GFXBlendSrcAlpha;
  147. dstBlendFactor = GFXBlendInvSrcAlpha;
  148. break;
  149. case BlendSubtractive:
  150. srcBlendFactor = GFXBlendZero;
  151. dstBlendFactor = GFXBlendInvSrcColor;
  152. break;
  153. case BlendPremultAlpha:
  154. srcBlendFactor = GFXBlendOne;
  155. dstBlendFactor = GFXBlendInvSrcAlpha;
  156. break;
  157. case BlendUser:
  158. break;
  159. case BlendAdditive:
  160. srcBlendFactor = GFXBlendSrcAlpha;
  161. dstBlendFactor = GFXBlendOne;
  162. break;
  163. case BlendUndefined:
  164. default:
  165. blendStyle = BlendNormal;
  166. srcBlendFactor = GFXBlendSrcAlpha;
  167. dstBlendFactor = GFXBlendInvSrcAlpha;
  168. break;
  169. }
  170. return true;
  171. }
  172. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  173. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  174. // afxBillboard
  175. IMPLEMENT_CO_NETOBJECT_V1(afxBillboard);
  176. ConsoleDocClass( afxBillboard,
  177. "@brief A Billboard effect as defined by an afxBillboardData datablock.\n\n"
  178. "A Billboard effect is a textured quadrangle which is always aligned to "
  179. "face towards the camera. It is much like a single static particle and is rendered "
  180. "in a similar fashion.\n"
  181. "@ingroup afxEffects\n"
  182. "@ingroup AFX\n"
  183. );
  184. afxBillboard::afxBillboard()
  185. {
  186. mNetFlags.clear();
  187. mNetFlags.set(IsGhost);
  188. mDataBlock = 0;
  189. fade_amt = 1.0f;
  190. is_visible = true;
  191. sort_priority = 0;
  192. live_color.set(1.0f, 1.0f, 1.0f, 1.0f);
  193. }
  194. afxBillboard::~afxBillboard()
  195. {
  196. }
  197. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  198. bool afxBillboard::onNewDataBlock(GameBaseData* dptr, bool reload)
  199. {
  200. mDataBlock = dynamic_cast<afxBillboardData*>(dptr);
  201. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  202. return false;
  203. live_color = mDataBlock->color;
  204. return true;
  205. }
  206. bool afxBillboard::onAdd()
  207. {
  208. if(!Parent::onAdd())
  209. return false;
  210. F32 width = mDataBlock->dimensions.x * 0.5f;
  211. F32 height = mDataBlock->dimensions.y * 0.5f;
  212. mObjBox = Box3F(Point3F(-width, -0.01f, -height), Point3F(width, 0.01f, height));
  213. addToScene();
  214. return true;
  215. }
  216. void afxBillboard::onRemove()
  217. {
  218. removeFromScene();
  219. Parent::onRemove();
  220. }
  221. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//