afxZodiacPlane_T3D.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/gfxTransformSaver.h"
  26. #include "gfx/primBuilder.h"
  27. #include "afx/afxChoreographer.h"
  28. #include "afx/ce/afxZodiacPlane.h"
  29. void afxZodiacPlane::prepRenderImage(SceneRenderState* state)
  30. {
  31. if (!is_visible)
  32. return;
  33. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  34. ri->renderDelegate.bind(this, &afxZodiacPlane::_renderZodiacPlane);
  35. ri->type = RenderPassManager::RIT_ObjectTranslucent;
  36. ri->translucentSort = true;
  37. ri->defaultKey = (U32)(dsize_t)mDataBlock;
  38. if (false)
  39. {
  40. ri->sortDistSq = getWorldBox().getSqDistanceToPoint( state->getCameraPosition() );
  41. }
  42. else // (sort by radius distance)
  43. {
  44. Point3F xyz_scale = getScale();
  45. F32 uni_scalar = getMax(xyz_scale.x, xyz_scale.y);
  46. uni_scalar = getMax(uni_scalar, xyz_scale.z);
  47. Point3F uni_scale(uni_scalar, uni_scalar, uni_scalar);
  48. Point3F local_cam_pos = state->getCameraPosition();
  49. getRenderWorldTransform().mulP(local_cam_pos);
  50. local_cam_pos.convolveInverse(uni_scale);
  51. switch (mDataBlock->face_dir)
  52. {
  53. case afxZodiacPlaneData::FACES_UP:
  54. case afxZodiacPlaneData::FACES_DOWN:
  55. local_cam_pos.z = 0;
  56. break;
  57. case afxZodiacPlaneData::FACES_FORWARD:
  58. case afxZodiacPlaneData::FACES_BACK:
  59. local_cam_pos.y = 0;
  60. break;
  61. case afxZodiacPlaneData::FACES_RIGHT:
  62. case afxZodiacPlaneData::FACES_LEFT:
  63. local_cam_pos.x = 0;
  64. break;
  65. }
  66. /* AFX_T3D_BROKEN -- enhanced transparency sorting of ZodiacPlanes JTF Note: evaluate this
  67. if (local_cam_pos.lenSquared() <= radius*radius)
  68. {
  69. ri->sortPoint = local_cam_pos;
  70. }
  71. else
  72. {
  73. local_cam_pos.normalize();
  74. ri->sortPoint = local_cam_pos*radius;
  75. }
  76. ri->sortPoint.convolve(uni_scale);
  77. getRenderTransform().mulP(ri->sortPoint);
  78. */
  79. }
  80. state->getRenderPass()->addInst(ri);
  81. }
  82. void afxZodiacPlane::_renderZodiacPlane(ObjectRenderInst *ri, SceneRenderState* state, BaseMatInstance* overrideMat)
  83. {
  84. if (overrideMat)
  85. return;
  86. // projection
  87. // predraw
  88. if (normal_sb.isNull())
  89. {
  90. GFXStateBlockDesc desc;
  91. // Culling
  92. desc.setCullMode((mDataBlock->double_sided) ? GFXCullNone : GFXCullCW);
  93. // Blending
  94. U32 blend = (mDataBlock->zflags & BLEND_MASK);
  95. switch (blend)
  96. {
  97. case BLEND_ADDITIVE:
  98. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendOne);
  99. break;
  100. case BLEND_SUBTRACTIVE:
  101. desc.setBlend(true, GFXBlendZero, GFXBlendInvSrcColor);
  102. break;
  103. case BLEND_NORMAL:
  104. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  105. break;
  106. }
  107. // JTF Note: check this desc.setAlphaTest((blend != BLEND_SUBTRACTIVE), GFXCmpGreater, 0);
  108. desc.setAlphaTest(true, GFXCmpGreater, 0);
  109. desc.setZReadWrite(true);
  110. desc.zFunc = GFXCmpLessEqual;
  111. desc.zWriteEnable = false;
  112. desc.samplersDefined = true;
  113. normal_sb = GFX->createStateBlock(desc);
  114. if (mDataBlock->double_sided)
  115. reflected_sb = normal_sb;
  116. else
  117. {
  118. desc.setCullMode(GFXCullCCW);
  119. reflected_sb = GFX->createStateBlock(desc);
  120. }
  121. }
  122. if (state->isReflectPass())
  123. GFX->setStateBlock(reflected_sb);
  124. else
  125. GFX->setStateBlock(normal_sb);
  126. Point3F basePoints[4];
  127. switch (mDataBlock->face_dir)
  128. {
  129. case afxZodiacPlaneData::FACES_UP:
  130. basePoints[0].set( 0.5f, -0.5f, 0.0f);
  131. basePoints[1].set(-0.5f, -0.5f, 0.0f);
  132. basePoints[2].set(-0.5f, 0.5f, 0.0f);
  133. basePoints[3].set( 0.5f, 0.5f, 0.0f);
  134. break;
  135. case afxZodiacPlaneData::FACES_DOWN:
  136. basePoints[3].set(-0.5f, 0.5f, 0.0f);
  137. basePoints[2].set( 0.5f, 0.5f, 0.0f);
  138. basePoints[1].set( 0.5f, -0.5f, 0.0f);
  139. basePoints[0].set(-0.5f, -0.5f, 0.0f);
  140. break;
  141. case afxZodiacPlaneData::FACES_FORWARD:
  142. basePoints[0].set( 0.5f, 0.0f, -0.5f);
  143. basePoints[1].set(-0.5f, 0.0f, -0.5f);
  144. basePoints[2].set(-0.5f, 0.0f, 0.5f);
  145. basePoints[3].set( 0.5f, 0.0f, 0.5f);
  146. break;
  147. case afxZodiacPlaneData::FACES_BACK:
  148. basePoints[3].set(-0.5f, 0.0f, 0.5f);
  149. basePoints[2].set( 0.5f, 0.0f, 0.5f);
  150. basePoints[1].set( 0.5f, 0.0f, -0.5f);
  151. basePoints[0].set(-0.5f, 0.0f, -0.5f);
  152. break;
  153. case afxZodiacPlaneData::FACES_RIGHT:
  154. basePoints[0].set(0.0f, 0.5f, -0.5f);
  155. basePoints[1].set(0.0f, -0.5f, -0.5f);
  156. basePoints[2].set(0.0f, -0.5f, 0.5f);
  157. basePoints[3].set(0.0f, 0.5f, 0.5f);
  158. break;
  159. case afxZodiacPlaneData::FACES_LEFT:
  160. basePoints[3].set(0.0f, -0.5f, 0.5f);
  161. basePoints[2].set(0.0f, 0.5f, 0.5f);
  162. basePoints[1].set(0.0f, 0.5f, -0.5f);
  163. basePoints[0].set(0.0f, -0.5f, -0.5f);
  164. break;
  165. }
  166. F32 len = 2*radius;
  167. Point3F points[4];
  168. Point2F texCoords[4]; // default: {{0.0,0.0}, {0.0,1.0}, {1.0,1.0}, {1.0,0.0}}
  169. texCoords[0].set(1.0,1.0);
  170. texCoords[1].set(0.0,1.0);
  171. texCoords[2].set(0.0,0.0);
  172. texCoords[3].set(1.0,0.0);
  173. for( int i=0; i<4; i++ )
  174. {
  175. points[i].x = basePoints[i].x;
  176. points[i].y = basePoints[i].y;
  177. points[i].z = basePoints[i].z;
  178. points[i] *= len;
  179. }
  180. GFXTransformSaver saver;
  181. GFX->multWorld(getRenderTransform());
  182. GFX->setTexture(0, mDataBlock->mTexture);
  183. PrimBuild::begin(GFXTriangleStrip, 4);
  184. {
  185. PrimBuild::color4f(color.red, color.green, color.blue, color.alpha);
  186. PrimBuild::texCoord2f(texCoords[1].x, texCoords[1].y);
  187. PrimBuild::vertex3f(points[1].x, points[1].y, points[1].z);
  188. PrimBuild::texCoord2f(texCoords[0].x, texCoords[0].y);
  189. PrimBuild::vertex3f(points[0].x, points[0].y, points[0].z);
  190. PrimBuild::texCoord2f(texCoords[2].x, texCoords[2].y);
  191. PrimBuild::vertex3f(points[2].x, points[2].y, points[2].z);
  192. PrimBuild::texCoord2f(texCoords[3].x, texCoords[3].y);
  193. PrimBuild::vertex3f(points[3].x, points[3].y, points[3].z);
  194. }
  195. PrimBuild::end();
  196. }
  197. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//