afxZodiacMeshRoadRenderer_T3D.cpp 8.9 KB

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