afxZodiacGroundPlaneRenderer_T3D.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "materials/shaderData.h"
  26. #include "gfx/gfxTransformSaver.h"
  27. #include "scene/sceneRenderState.h"
  28. #include "collision/concretePolyList.h"
  29. #include "T3D/tsStatic.h"
  30. #include "gfx/primBuilder.h"
  31. #include "afx/ce/afxZodiacMgr.h"
  32. #include "afx/afxZodiacGroundPlaneRenderer_T3D.h"
  33. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  34. const RenderInstType afxZodiacGroundPlaneRenderer::RIT_GroundPlaneZodiac("GroundPlaneZodiac");
  35. afxZodiacGroundPlaneRenderer* afxZodiacGroundPlaneRenderer::master = 0;
  36. IMPLEMENT_CONOBJECT(afxZodiacGroundPlaneRenderer);
  37. ConsoleDocClass( afxZodiacGroundPlaneRenderer,
  38. "@brief A render bin for zodiac rendering on GroundPlane objects.\n\n"
  39. "This bin renders instances of AFX zodiac effects onto GroundPlane surfaces.\n\n"
  40. "@ingroup RenderBin\n"
  41. "@ingroup AFX\n"
  42. );
  43. afxZodiacGroundPlaneRenderer::afxZodiacGroundPlaneRenderer()
  44. : RenderBinManager(RIT_GroundPlaneZodiac, 1.0f, 1.0f)
  45. {
  46. if (!master)
  47. master = this;
  48. shader_initialized = false;
  49. zodiac_shader = NULL;
  50. shader_consts = NULL;
  51. projection_sc = NULL;
  52. color_sc = NULL;
  53. }
  54. afxZodiacGroundPlaneRenderer::afxZodiacGroundPlaneRenderer(F32 renderOrder, F32 processAddOrder)
  55. : RenderBinManager(RIT_GroundPlaneZodiac, renderOrder, processAddOrder)
  56. {
  57. if (!master)
  58. master = this;
  59. shader_initialized = false;
  60. zodiac_shader = NULL;
  61. shader_consts = NULL;
  62. projection_sc = NULL;
  63. color_sc = NULL;
  64. }
  65. afxZodiacGroundPlaneRenderer::~afxZodiacGroundPlaneRenderer()
  66. {
  67. if (this == master)
  68. master = 0;
  69. }
  70. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  71. void afxZodiacGroundPlaneRenderer::initShader()
  72. {
  73. if (shader_initialized)
  74. return;
  75. shader_initialized = true;
  76. shader_consts = 0;
  77. norm_norefl_zb_SB = norm_refl_zb_SB;
  78. add_norefl_zb_SB = add_refl_zb_SB;
  79. sub_norefl_zb_SB = sub_refl_zb_SB;
  80. zodiac_shader = afxZodiacMgr::getGroundPlaneZodiacShader();
  81. if (!zodiac_shader)
  82. return;
  83. GFXStateBlockDesc d;
  84. d.cullDefined = true;
  85. d.ffLighting = false;
  86. d.blendDefined = true;
  87. d.blendEnable = true;
  88. d.zDefined = false;
  89. d.zEnable = true;
  90. d.zWriteEnable = false;
  91. d.zFunc = GFXCmpLessEqual;
  92. d.zSlopeBias = 0;
  93. d.alphaDefined = true;
  94. d.alphaTestEnable = true;
  95. d.alphaTestRef = 0;
  96. d.alphaTestFunc = GFXCmpGreater;
  97. d.samplersDefined = true;
  98. d.samplers[0] = GFXSamplerStateDesc::getClampLinear();
  99. // normal
  100. d.blendSrc = GFXBlendSrcAlpha;
  101. d.blendDest = GFXBlendInvSrcAlpha;
  102. //
  103. d.cullMode = GFXCullCCW;
  104. d.zBias = arcaneFX::sPolysoupZodiacZBias;
  105. norm_norefl_zb_SB = GFX->createStateBlock(d);
  106. //
  107. d.cullMode = GFXCullCW;
  108. d.zBias = arcaneFX::sPolysoupZodiacZBias;
  109. norm_refl_zb_SB = GFX->createStateBlock(d);
  110. // additive
  111. d.blendSrc = GFXBlendSrcAlpha;
  112. d.blendDest = GFXBlendOne;
  113. //
  114. d.cullMode = GFXCullCCW;
  115. d.zBias = arcaneFX::sPolysoupZodiacZBias;
  116. add_norefl_zb_SB = GFX->createStateBlock(d);
  117. //
  118. d.cullMode = GFXCullCW;
  119. d.zBias = arcaneFX::sPolysoupZodiacZBias;
  120. add_refl_zb_SB = GFX->createStateBlock(d);
  121. // subtractive
  122. d.blendSrc = GFXBlendZero;
  123. d.blendDest = GFXBlendInvSrcColor;
  124. //
  125. d.cullMode = GFXCullCCW;
  126. d.zBias = arcaneFX::sPolysoupZodiacZBias;
  127. sub_norefl_zb_SB = GFX->createStateBlock(d);
  128. //
  129. d.cullMode = GFXCullCW;
  130. d.zBias = arcaneFX::sPolysoupZodiacZBias;
  131. sub_refl_zb_SB = GFX->createStateBlock(d);
  132. shader_consts = zodiac_shader->getShader()->allocConstBuffer();
  133. projection_sc = zodiac_shader->getShader()->getShaderConstHandle("$modelView");
  134. color_sc = zodiac_shader->getShader()->getShaderConstHandle("$zodiacColor");
  135. }
  136. void afxZodiacGroundPlaneRenderer::clear()
  137. {
  138. Parent::clear();
  139. groundPlane_zodiacs.clear();
  140. }
  141. void afxZodiacGroundPlaneRenderer::addZodiac(U32 zode_idx, const Point3F& pos, F32 ang, const GroundPlane* gp, F32 camDist)
  142. {
  143. groundPlane_zodiacs.increment();
  144. GroundPlaneZodiacElem& elem = groundPlane_zodiacs.last();
  145. elem.gp = gp;
  146. elem.zode_idx = zode_idx;
  147. elem.ang = ang;
  148. elem.camDist = camDist;
  149. }
  150. afxZodiacGroundPlaneRenderer* afxZodiacGroundPlaneRenderer::getMaster()
  151. {
  152. if (!master)
  153. master = new afxZodiacGroundPlaneRenderer;
  154. return master;
  155. }
  156. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  157. GFXStateBlock* afxZodiacGroundPlaneRenderer::chooseStateBlock(U32 blend, bool isReflectPass)
  158. {
  159. GFXStateBlock* sb = 0;
  160. switch (blend)
  161. {
  162. case afxZodiacData::BLEND_ADDITIVE:
  163. sb = (isReflectPass) ? add_refl_zb_SB : add_norefl_zb_SB;
  164. break;
  165. case afxZodiacData::BLEND_SUBTRACTIVE:
  166. sb = (isReflectPass) ? sub_refl_zb_SB : sub_norefl_zb_SB;
  167. break;
  168. default: // afxZodiacData::BLEND_NORMAL:
  169. sb = (isReflectPass) ? norm_refl_zb_SB : norm_norefl_zb_SB;
  170. break;
  171. }
  172. return sb;
  173. }
  174. void afxZodiacGroundPlaneRenderer::render(SceneRenderState* state)
  175. {
  176. PROFILE_SCOPE(afxRenderZodiacGroundPlaneMgr_render);
  177. // Early out if no ground-plane zodiacs to draw.
  178. if (groundPlane_zodiacs.size() == 0)
  179. return;
  180. initShader();
  181. if (!zodiac_shader)
  182. return;
  183. bool is_reflect_pass = state->isReflectPass();
  184. // Automagically save & restore our viewport and transforms.
  185. GFXTransformSaver saver;
  186. MatrixF proj = GFX->getProjectionMatrix();
  187. // Set up world transform
  188. MatrixF world = GFX->getWorldMatrix();
  189. proj.mul(world);
  190. shader_consts->set(projection_sc, proj);
  191. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  192. // RENDER EACH ZODIAC
  193. //
  194. for (S32 zz = 0; zz < groundPlane_zodiacs.size(); zz++)
  195. {
  196. GroundPlaneZodiacElem& elem = groundPlane_zodiacs[zz];
  197. afxZodiacMgr::ZodiacSpec* zode = &afxZodiacMgr::terr_zodes[elem.zode_idx];
  198. if (!zode)
  199. continue;
  200. if (is_reflect_pass)
  201. {
  202. //if ((zode->zflags & afxZodiacData::SHOW_IN_REFLECTIONS) == 0)
  203. continue;
  204. }
  205. else
  206. {
  207. if ((zode->zflags & afxZodiacData::SHOW_IN_NON_REFLECTIONS) == 0)
  208. continue;
  209. }
  210. F32 fadebias = zode->calcDistanceFadeBias(elem.camDist);
  211. if (fadebias < 0.01f)
  212. continue;
  213. F32 cos_ang = mCos(elem.ang);
  214. F32 sin_ang = mSin(elem.ang);
  215. GFXStateBlock* sb = chooseStateBlock(zode->zflags & afxZodiacData::BLEND_MASK, is_reflect_pass);
  216. GFX->setShader(zodiac_shader->getShader());
  217. GFX->setStateBlock(sb);
  218. GFX->setShaderConstBuffer(shader_consts);
  219. // set the texture
  220. GFX->setTexture(0, *zode->txr);
  221. LinearColorF zode_color = (LinearColorF)zode->color;
  222. zode_color.alpha *= fadebias;
  223. shader_consts->set(color_sc, zode_color);
  224. F32 rad_xy = zode->radius_xy;
  225. F32 inv_radius = 1.0f/rad_xy;
  226. F32 offset_xy = mSqrt(2*rad_xy*rad_xy);
  227. F32 zx = zode->pos.x;
  228. F32 zy = zode->pos.y;
  229. F32 z = 0.00001f;
  230. Point3F verts[4];
  231. verts[0].set(zx+offset_xy, zy+offset_xy, z);
  232. verts[1].set(zx-offset_xy, zy+offset_xy, z);
  233. verts[2].set(zx-offset_xy, zy-offset_xy, z);
  234. verts[3].set(zx+offset_xy, zy-offset_xy, z);
  235. S32 vertind[6];
  236. vertind[0] = 2;
  237. vertind[1] = 1;
  238. vertind[2] = 0;
  239. vertind[3] = 3;
  240. vertind[4] = 2;
  241. vertind[5] = 0;
  242. PrimBuild::begin(GFXTriangleList, 6);
  243. for (U32 i = 0; i < 2; i++)
  244. {
  245. for (U32 j = 0; j < 3; j++)
  246. {
  247. const Point3F& vtx = verts[vertind[i*3+j]];
  248. // compute UV
  249. F32 u1 = (vtx.x - zode->pos.x)*inv_radius;
  250. F32 v1 = (vtx.y - zode->pos.y)*inv_radius;
  251. F32 ru1 = u1*cos_ang - v1*sin_ang;
  252. F32 rv1 = u1*sin_ang + v1*cos_ang;
  253. F32 uu = (ru1 + 1.0f)/2.0f;
  254. F32 vv = 1.0f - (rv1 + 1.0f)/2.0f;
  255. PrimBuild::texCoord2f(uu, vv);
  256. PrimBuild::vertex3fv(vtx);
  257. }
  258. }
  259. PrimBuild::end(false);
  260. }
  261. }
  262. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//