afxBillboard.cpp 9.2 KB

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