afxBillboard.cpp 9.9 KB

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