afxBillboard_T3D.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/afxBillboard.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. void afxBillboard::prepRenderImage(SceneRenderState* state)
  31. {
  32. if (!is_visible)
  33. return;
  34. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  35. ri->renderDelegate.bind(this, &afxBillboard::_renderBillboard);
  36. ri->type = RenderPassManager::RIT_ObjectTranslucent;
  37. ri->translucentSort = true;
  38. ri->defaultKey = (U32)(dsize_t)mDataBlock;
  39. ri->sortDistSq = getWorldBox().getSqDistanceToPoint( state->getCameraPosition() );
  40. state->getRenderPass()->addInst(ri);
  41. }
  42. void afxBillboard::_renderBillboard(ObjectRenderInst *ri, SceneRenderState* state, BaseMatInstance* overrideMat)
  43. {
  44. if (overrideMat)
  45. return;
  46. // predraw
  47. if (normal_sb.isNull())
  48. {
  49. GFXStateBlockDesc desc;
  50. // Culling -- it's a billboard, so no backfaces
  51. desc.setCullMode(GFXCullCW);
  52. // Blending
  53. desc.setBlend(true, mDataBlock->srcBlendFactor, mDataBlock->dstBlendFactor);
  54. desc.alphaTestEnable = (desc.blendSrc == GFXBlendSrcAlpha &&
  55. (desc.blendDest == GFXBlendInvSrcAlpha || desc.blendDest == GFXBlendOne));
  56. desc.alphaTestRef = 1;
  57. desc.alphaTestFunc = GFXCmpGreaterEqual;
  58. desc.setZReadWrite(true);
  59. desc.zFunc = GFXCmpLessEqual;
  60. desc.zWriteEnable = false;
  61. desc.samplersDefined = true;
  62. normal_sb = GFX->createStateBlock(desc);
  63. desc.setCullMode(GFXCullCCW);
  64. reflected_sb = GFX->createStateBlock(desc);
  65. }
  66. if (state->isReflectPass())
  67. GFX->setStateBlock(reflected_sb);
  68. else
  69. GFX->setStateBlock(normal_sb);
  70. GFXTransformSaver saver;
  71. GFX->multWorld(getRenderTransform());
  72. GFX->setTexture(0, mDataBlock->mTexture);
  73. MatrixF worldmod = GFX->getWorldMatrix();
  74. MatrixF viewmod = GFX->getViewMatrix();
  75. Point4F Position;
  76. MatrixF ModelView;
  77. ModelView.mul(viewmod, worldmod);
  78. ModelView.getColumn(3, &Position);
  79. ModelView.identity();
  80. ModelView.setColumn(3, Position);
  81. GFX->setWorldMatrix(ModelView);
  82. MatrixF ident;
  83. ident.identity();
  84. GFX->setViewMatrix(ident);
  85. F32 width = mDataBlock->dimensions.x * 0.5f * mObjScale.x;
  86. F32 height = mDataBlock->dimensions.y * 0.5f * mObjScale.z;
  87. Point3F points[4];
  88. points[0].set( width, 0.0f, -height);
  89. points[1].set(-width, 0.0f, -height);
  90. points[2].set(-width, 0.0f, height);
  91. points[3].set( width, 0.0f, height);
  92. PrimBuild::begin(GFXTriangleStrip, 4);
  93. {
  94. PrimBuild::color4f(live_color.red, live_color.green, live_color.blue, live_color.alpha*fade_amt);
  95. PrimBuild::texCoord2f(mDataBlock->texCoords[1].x, mDataBlock->texCoords[1].y);
  96. PrimBuild::vertex3fv(points[1]);
  97. PrimBuild::texCoord2f(mDataBlock->texCoords[2].x, mDataBlock->texCoords[2].y);
  98. PrimBuild::vertex3fv(points[0]);
  99. PrimBuild::texCoord2f(mDataBlock->texCoords[0].x, mDataBlock->texCoords[0].y);
  100. PrimBuild::vertex3fv(points[2]);
  101. PrimBuild::texCoord2f(mDataBlock->texCoords[3].x, mDataBlock->texCoords[3].y);
  102. PrimBuild::vertex3fv(points[3]);
  103. }
  104. PrimBuild::end();
  105. }
  106. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//