afxZodiacPolysoupRenderer_T3D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/afxZodiacPolysoupRenderer_T3D.h"
  33. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  34. const RenderInstType afxZodiacPolysoupRenderer::RIT_PolysoupZodiac("PolysoupZodiac");
  35. afxZodiacPolysoupRenderer* afxZodiacPolysoupRenderer::master = 0;
  36. IMPLEMENT_CONOBJECT(afxZodiacPolysoupRenderer);
  37. ConsoleDocClass( afxZodiacPolysoupRenderer,
  38. "@brief A render bin for zodiac rendering on polysoup TSStatic objects.\n\n"
  39. "This bin renders instances of AFX zodiac effects onto polysoup TSStatic surfaces.\n\n"
  40. "@ingroup RenderBin\n"
  41. "@ingroup AFX\n"
  42. );
  43. afxZodiacPolysoupRenderer::afxZodiacPolysoupRenderer()
  44. : RenderBinManager(RIT_PolysoupZodiac, 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. afxZodiacPolysoupRenderer::afxZodiacPolysoupRenderer(F32 renderOrder, F32 processAddOrder)
  55. : RenderBinManager(RIT_PolysoupZodiac, 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. afxZodiacPolysoupRenderer::~afxZodiacPolysoupRenderer()
  66. {
  67. if (this == master)
  68. master = 0;
  69. }
  70. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  71. void afxZodiacPolysoupRenderer::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::getPolysoupZodiacShader();
  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 afxZodiacPolysoupRenderer::clear()
  137. {
  138. Parent::clear();
  139. for (S32 i = 0; i < polysoup_zodes.size(); i++)
  140. if (polysoup_zodes[i].polys)
  141. delete polysoup_zodes[i].polys;
  142. polysoup_zodes.clear();
  143. }
  144. void afxZodiacPolysoupRenderer::addZodiac(U32 zode_idx, ConcretePolyList* polys, const Point3F& pos, F32 ang, const TSStatic* tss, F32 camDist)
  145. {
  146. polysoup_zodes.increment();
  147. PolysoupZodiacElem& elem = polysoup_zodes.last();
  148. elem.tss = tss;
  149. elem.polys = polys;
  150. elem.zode_idx = zode_idx;
  151. elem.ang = ang;
  152. elem.camDist = camDist;
  153. }
  154. afxZodiacPolysoupRenderer* afxZodiacPolysoupRenderer::getMaster()
  155. {
  156. if (!master)
  157. master = new afxZodiacPolysoupRenderer;
  158. return master;
  159. }
  160. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  161. GFXStateBlock* afxZodiacPolysoupRenderer::chooseStateBlock(U32 blend, bool isReflectPass)
  162. {
  163. GFXStateBlock* sb = 0;
  164. switch (blend)
  165. {
  166. case afxZodiacData::BLEND_ADDITIVE:
  167. sb = (isReflectPass) ? add_refl_zb_SB : add_norefl_zb_SB;
  168. break;
  169. case afxZodiacData::BLEND_SUBTRACTIVE:
  170. sb = (isReflectPass) ? sub_refl_zb_SB : sub_norefl_zb_SB;
  171. break;
  172. default: // afxZodiacData::BLEND_NORMAL:
  173. sb = (isReflectPass) ? norm_refl_zb_SB : norm_norefl_zb_SB;
  174. break;
  175. }
  176. return sb;
  177. }
  178. void afxZodiacPolysoupRenderer::render(SceneRenderState* state)
  179. {
  180. PROFILE_SCOPE(afxRenderZodiacPolysoupMgr_render);
  181. // Early out if no polysoup zodiacs to draw.
  182. if (polysoup_zodes.size() == 0)
  183. return;
  184. initShader();
  185. if (!zodiac_shader)
  186. return;
  187. bool is_reflect_pass = state->isReflectPass();
  188. // Automagically save & restore our viewport and transforms.
  189. GFXTransformSaver saver;
  190. MatrixF proj = GFX->getProjectionMatrix();
  191. // Set up world transform
  192. MatrixF world = GFX->getWorldMatrix();
  193. proj.mul(world);
  194. shader_consts->set(projection_sc, proj);
  195. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  196. // RENDER EACH ZODIAC
  197. //
  198. for (S32 zz = 0; zz < polysoup_zodes.size(); zz++)
  199. {
  200. PolysoupZodiacElem& elem = polysoup_zodes[zz];
  201. afxZodiacMgr::ZodiacSpec* zode = &afxZodiacMgr::inter_zodes[elem.zode_idx];
  202. if (!zode)
  203. continue;
  204. if (is_reflect_pass)
  205. {
  206. if ((zode->zflags & afxZodiacData::SHOW_IN_REFLECTIONS) == 0)
  207. continue;
  208. }
  209. else
  210. {
  211. if ((zode->zflags & afxZodiacData::SHOW_IN_NON_REFLECTIONS) == 0)
  212. continue;
  213. }
  214. F32 fadebias = zode->calcDistanceFadeBias(elem.camDist);
  215. if (fadebias < 0.01f)
  216. continue;
  217. F32 cos_ang = mCos(elem.ang);
  218. F32 sin_ang = mSin(elem.ang);
  219. GFXStateBlock* sb = chooseStateBlock(zode->zflags & afxZodiacData::BLEND_MASK, is_reflect_pass);
  220. GFX->setShader(zodiac_shader->getShader());
  221. GFX->setStateBlock(sb);
  222. GFX->setShaderConstBuffer(shader_consts);
  223. // set the texture
  224. GFX->setTexture(0, *zode->txr);
  225. LinearColorF zode_color = (LinearColorF)zode->color;
  226. zode_color.alpha *= fadebias;
  227. shader_consts->set(color_sc, zode_color);
  228. F32 inv_radius = 1.0f/zode->radius_xy;
  229. // FILTER USING GRADIENT RANGE
  230. if ((zode->zflags & afxZodiacData::USE_GRADE_RANGE) != 0)
  231. {
  232. bool skip_oob;
  233. F32 grade_min, grade_max;
  234. if (elem.tss->mHasGradients && ((zode->zflags & afxZodiacData::PREFER_DEST_GRADE) != 0))
  235. {
  236. skip_oob = (elem.tss->mInvertGradientRange == false);
  237. grade_min = elem.tss->mGradientRange.x;
  238. grade_max = elem.tss->mGradientRange.y;
  239. }
  240. else
  241. {
  242. skip_oob = ((zode->zflags & afxZodiacData::INVERT_GRADE_RANGE) == 0);
  243. grade_min = zode->grade_range.x;
  244. grade_max = zode->grade_range.y;
  245. }
  246. PrimBuild::begin(GFXTriangleList, 3*elem.polys->mPolyList.size());
  247. for (U32 i = 0; i < elem.polys->mPolyList.size(); i++)
  248. {
  249. ConcretePolyList::Poly* poly = &elem.polys->mPolyList[i];
  250. const PlaneF& plane = poly->plane;
  251. bool oob = (plane.z > grade_max || plane.z < grade_min);
  252. if (oob == skip_oob)
  253. continue;
  254. S32 vertind[3];
  255. vertind[0] = elem.polys->mIndexList[poly->vertexStart];
  256. vertind[1] = elem.polys->mIndexList[poly->vertexStart + 1];
  257. vertind[2] = elem.polys->mIndexList[poly->vertexStart + 2];
  258. for (U32 j = 0; j < 3; j++)
  259. {
  260. Point3F vtx = elem.polys->mVertexList[vertind[j]];
  261. // compute UV
  262. F32 u1 = (vtx.x - zode->pos.x)*inv_radius;
  263. F32 v1 = (vtx.y - zode->pos.y)*inv_radius;
  264. F32 ru1 = u1*cos_ang - v1*sin_ang;
  265. F32 rv1 = u1*sin_ang + v1*cos_ang;
  266. F32 uu = (ru1 + 1.0f)/2.0f;
  267. F32 vv = 1.0f - (rv1 + 1.0f)/2.0f;
  268. PrimBuild::texCoord2f(uu, vv);
  269. PrimBuild::vertex3fv(vtx);
  270. }
  271. }
  272. PrimBuild::end(false);
  273. }
  274. // FILTER USING OTHER FILTERS
  275. else if (zode->zflags & afxZodiacData::INTERIOR_FILTERS)
  276. {
  277. PrimBuild::begin(GFXTriangleList, 3*elem.polys->mPolyList.size());
  278. for (U32 i = 0; i < elem.polys->mPolyList.size(); i++)
  279. {
  280. ConcretePolyList::Poly* poly = &elem.polys->mPolyList[i];
  281. const PlaneF& plane = poly->plane;
  282. if (zode->zflags & afxZodiacData::INTERIOR_HORIZ_ONLY)
  283. {
  284. if (!plane.isHorizontal())
  285. continue;
  286. if (zode->zflags & afxZodiacData::INTERIOR_BACK_IGNORE)
  287. {
  288. if (plane.whichSide(zode->pos) == PlaneF::Back)
  289. continue;
  290. }
  291. }
  292. else
  293. {
  294. if (zode->zflags & afxZodiacData::INTERIOR_VERT_IGNORE)
  295. {
  296. if (plane.isVertical())
  297. continue;
  298. }
  299. if (zode->zflags & afxZodiacData::INTERIOR_BACK_IGNORE)
  300. {
  301. if (plane.whichSide(zode->pos) == PlaneF::Back)
  302. continue;
  303. }
  304. }
  305. S32 vertind[3];
  306. vertind[0] = elem.polys->mIndexList[poly->vertexStart];
  307. vertind[1] = elem.polys->mIndexList[poly->vertexStart + 1];
  308. vertind[2] = elem.polys->mIndexList[poly->vertexStart + 2];
  309. for (U32 j = 0; j < 3; j++)
  310. {
  311. Point3F vtx = elem.polys->mVertexList[vertind[j]];
  312. // compute UV
  313. F32 u1 = (vtx.x - zode->pos.x)*inv_radius;
  314. F32 v1 = (vtx.y - zode->pos.y)*inv_radius;
  315. F32 ru1 = u1*cos_ang - v1*sin_ang;
  316. F32 rv1 = u1*sin_ang + v1*cos_ang;
  317. F32 uu = (ru1 + 1.0f)/2.0f;
  318. F32 vv = 1.0f - (rv1 + 1.0f)/2.0f;
  319. PrimBuild::texCoord2f(uu, vv);
  320. PrimBuild::vertex3fv(vtx);
  321. }
  322. }
  323. PrimBuild::end(false);
  324. }
  325. // NO FILTERING
  326. else
  327. {
  328. PrimBuild::begin(GFXTriangleList, 3*elem.polys->mPolyList.size());
  329. for (U32 i = 0; i < elem.polys->mPolyList.size(); i++)
  330. {
  331. ConcretePolyList::Poly* poly = &elem.polys->mPolyList[i];
  332. S32 vertind[3];
  333. vertind[0] = elem.polys->mIndexList[poly->vertexStart];
  334. vertind[1] = elem.polys->mIndexList[poly->vertexStart + 1];
  335. vertind[2] = elem.polys->mIndexList[poly->vertexStart + 2];
  336. for (U32 j = 0; j < 3; j++)
  337. {
  338. Point3F vtx = elem.polys->mVertexList[vertind[j]];
  339. // compute UV
  340. F32 u1 = (vtx.x - zode->pos.x)*inv_radius;
  341. F32 v1 = (vtx.y - zode->pos.y)*inv_radius;
  342. F32 ru1 = u1*cos_ang - v1*sin_ang;
  343. F32 rv1 = u1*sin_ang + v1*cos_ang;
  344. F32 uu = (ru1 + 1.0f)/2.0f;
  345. F32 vv = 1.0f - (rv1 + 1.0f)/2.0f;
  346. PrimBuild::texCoord2f(uu, vv);
  347. PrimBuild::vertex3fv(vtx);
  348. }
  349. }
  350. PrimBuild::end(false);
  351. }
  352. }
  353. }
  354. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//