afxZodiacTerrainRenderer_T3D.cpp
Engine/source/afx/afxZodiacTerrainRenderer_T3D.cpp
Classes:
class
Public Functions
ConsoleDocClass(afxZodiacTerrainRenderer , "@brief A <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> bin <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> zodiac rendering on Terrain <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">objects.\n\n</a>" "This bin renders instances of AFX zodiac effects onto Terrain <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">surfaces.\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderBin\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AFX\n</a>" )
Detailed Description
Public Functions
ConsoleDocClass(afxZodiacTerrainRenderer , "@brief A <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> bin <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> zodiac rendering on Terrain <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">objects.\n\n</a>" "This bin renders instances of AFX zodiac effects onto Terrain <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">surfaces.\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderBin\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AFX\n</a>" )
IMPLEMENT_CONOBJECT(afxZodiacTerrainRenderer )
1 2 3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames 5// Copyright (C) 2015 Faust Logic, Inc. 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to 9// deal in the Software without restriction, including without limitation the 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11// sell copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in 15// all copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23// IN THE SOFTWARE. 24// 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 26 27#include "afx/arcaneFX.h" 28 29#include "materials/shaderData.h" 30#include "gfx/gfxTransformSaver.h" 31#include "scene/sceneRenderState.h" 32#include "terrain/terrData.h" 33#include "terrain/terrCell.h" 34 35#include "gfx/primBuilder.h" 36 37#include "afx/ce/afxZodiacMgr.h" 38#include "afx/afxZodiacTerrainRenderer_T3D.h" 39#include "afx/util/afxTriBoxCheck2D_T3D.h" 40 41//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 42 43class TerrCellSpy : public TerrCell 44{ 45public: 46 static const U32 getMinCellSize() { return smMinCellSize; } 47}; 48 49//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 50 51const RenderInstType afxZodiacTerrainRenderer::RIT_TerrainZodiac("TerrainZodiac"); 52 53afxZodiacTerrainRenderer* afxZodiacTerrainRenderer::master = 0; 54 55IMPLEMENT_CONOBJECT(afxZodiacTerrainRenderer); 56 57ConsoleDocClass( afxZodiacTerrainRenderer, 58 "@brief A render bin for zodiac rendering on Terrain objects.\n\n" 59 60 "This bin renders instances of AFX zodiac effects onto Terrain surfaces.\n\n" 61 62 "@ingroup RenderBin\n" 63 "@ingroup AFX\n" 64); 65 66afxZodiacTerrainRenderer::afxZodiacTerrainRenderer() 67: RenderBinManager(RIT_TerrainZodiac, 1.0f, 1.0f) 68{ 69 if (!master) 70 master = this; 71 shader_initialized = false; 72 zodiac_shader = NULL; 73 shader_consts = NULL; 74 projection_sc = NULL; 75 color_sc = NULL; 76} 77 78afxZodiacTerrainRenderer::afxZodiacTerrainRenderer(F32 renderOrder, F32 processAddOrder) 79: RenderBinManager(RIT_TerrainZodiac, renderOrder, processAddOrder) 80{ 81 if (!master) 82 master = this; 83 shader_initialized = false; 84 zodiac_shader = NULL; 85 shader_consts = NULL; 86 projection_sc = NULL; 87 color_sc = NULL; 88} 89 90afxZodiacTerrainRenderer::~afxZodiacTerrainRenderer() 91{ 92 if (this == master) 93 master = 0; 94} 95 96//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 97 98void afxZodiacTerrainRenderer::initShader() 99{ 100 if (shader_initialized) 101 return; 102 103 shader_initialized = true; 104 105 shader_consts = 0; 106 norm_norefl_zb_SB = norm_refl_zb_SB; 107 add_norefl_zb_SB = add_refl_zb_SB; 108 sub_norefl_zb_SB = sub_refl_zb_SB; 109 110 zodiac_shader = afxZodiacMgr::getTerrainZodiacShader(); 111 if (!zodiac_shader) 112 return; 113 114 GFXStateBlockDesc d; 115 116 d.cullDefined = true; 117 d.blendDefined = true; 118 d.blendEnable = true; 119 d.zDefined = false; 120 d.zEnable = true; 121 d.zWriteEnable = false; 122 d.zFunc = GFXCmpLessEqual; 123 d.zSlopeBias = 0; 124 d.alphaDefined = true; 125 d.alphaTestEnable = true; 126 d.alphaTestRef = 0; 127 d.alphaTestFunc = GFXCmpGreater; 128 d.samplersDefined = true; 129 d.samplers[0] = GFXSamplerStateDesc::getClampLinear(); 130 131 // normal 132 d.blendSrc = GFXBlendSrcAlpha; 133 d.blendDest = GFXBlendInvSrcAlpha; 134 // 135 d.cullMode = GFXCullCCW; 136 d.zBias = arcaneFX::sTerrainZodiacZBias; 137 norm_norefl_zb_SB = GFX->createStateBlock(d); 138 // 139 d.cullMode = GFXCullCW; 140 d.zBias = arcaneFX::sTerrainZodiacZBias; 141 norm_refl_zb_SB = GFX->createStateBlock(d); 142 143 // additive 144 d.blendSrc = GFXBlendSrcAlpha; 145 d.blendDest = GFXBlendOne; 146 // 147 d.cullMode = GFXCullCCW; 148 d.zBias = arcaneFX::sTerrainZodiacZBias; 149 add_norefl_zb_SB = GFX->createStateBlock(d); 150 // 151 d.cullMode = GFXCullCW; 152 d.zBias = arcaneFX::sTerrainZodiacZBias; 153 add_refl_zb_SB = GFX->createStateBlock(d); 154 155 // subtractive 156 d.blendSrc = GFXBlendZero; 157 d.blendDest = GFXBlendInvSrcColor; 158 // 159 d.cullMode = GFXCullCCW; 160 d.zBias = arcaneFX::sTerrainZodiacZBias; 161 sub_norefl_zb_SB = GFX->createStateBlock(d); 162 // 163 d.cullMode = GFXCullCW; 164 d.zBias = arcaneFX::sTerrainZodiacZBias; 165 sub_refl_zb_SB = GFX->createStateBlock(d); 166 167 shader_consts = zodiac_shader->getShader()->allocConstBuffer(); 168 projection_sc = zodiac_shader->getShader()->getShaderConstHandle("$modelView"); 169 color_sc = zodiac_shader->getShader()->getShaderConstHandle("$zodiacColor"); 170} 171 172void afxZodiacTerrainRenderer::clear() 173{ 174 Parent::clear(); 175 176 terrain_zodes.clear(); 177} 178 179void afxZodiacTerrainRenderer::addZodiac(U32 zode_idx, const Point3F& pos, F32 ang, 180 const TerrainBlock* block, const TerrCell* cell, 181 const MatrixF& mRenderObjToWorld, F32 camDist) 182{ 183 terrain_zodes.increment(); 184 TerrainZodiacElem& elem = terrain_zodes.last(); 185 186 elem.block = block; 187 elem.cell = cell; 188 elem.zode_idx = zode_idx; 189 elem.ang = ang; 190 elem.mRenderObjToWorld = mRenderObjToWorld; 191 elem.camDist = camDist; 192} 193 194afxZodiacTerrainRenderer* afxZodiacTerrainRenderer::getMaster() 195{ 196 if (!master) 197 master = new afxZodiacTerrainRenderer; 198 return master; 199} 200 201//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 202 203GFXStateBlock* afxZodiacTerrainRenderer::chooseStateBlock(U32 blend, bool isReflectPass) 204{ 205 GFXStateBlock* sb = 0; 206 207 switch (blend) 208 { 209 case afxZodiacData::BLEND_ADDITIVE: 210 sb = (isReflectPass) ? add_refl_zb_SB : add_norefl_zb_SB; 211 break; 212 case afxZodiacData::BLEND_SUBTRACTIVE: 213 sb = (isReflectPass) ? sub_refl_zb_SB : sub_norefl_zb_SB; 214 break; 215 default: // afxZodiacData::BLEND_NORMAL: 216 sb = (isReflectPass) ? norm_refl_zb_SB : norm_norefl_zb_SB; 217 break; 218 } 219 220 return sb; 221} 222 223void afxZodiacTerrainRenderer::render(SceneRenderState* state) 224{ 225 PROFILE_SCOPE(afxRenderZodiacTerrainMgr_render); 226 227 // Early out if no terrain zodiacs to draw. 228 if (terrain_zodes.size() == 0) 229 return; 230 231 initShader(); 232 if (!zodiac_shader) 233 return; 234 235 bool is_reflect_pass = state->isReflectPass(); 236 237 // Automagically save & restore our viewport and transforms. 238 GFXTransformSaver saver; 239 240 MatrixF proj = GFX->getProjectionMatrix(); 241 242 // Set up world transform 243 MatrixF world = GFX->getWorldMatrix(); 244 proj.mul(world); 245 shader_consts->set(projection_sc, proj); 246 247 //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 248 // RENDER EACH ZODIAC 249 // 250 for (S32 zz = 0; zz < terrain_zodes.size(); zz++) 251 { 252 TerrainZodiacElem& elem = terrain_zodes[zz]; 253 254 TerrainBlock* block = (TerrainBlock*) elem.block; 255 256 afxZodiacMgr::ZodiacSpec* zode = &afxZodiacMgr::terr_zodes[elem.zode_idx]; 257 if (!zode) 258 continue; 259 260 if (is_reflect_pass) 261 { 262 if ((zode->zflags & afxZodiacData::SHOW_IN_REFLECTIONS) == 0) 263 continue; 264 } 265 else 266 { 267 if ((zode->zflags & afxZodiacData::SHOW_IN_NON_REFLECTIONS) == 0) 268 continue; 269 } 270 271 F32 fadebias = zode->calcDistanceFadeBias(elem.camDist); 272 if (fadebias < 0.01f) 273 continue; 274 275 F32 cos_ang = mCos(elem.ang); 276 F32 sin_ang = mSin(elem.ang); 277 278 GFXStateBlock* sb = chooseStateBlock(zode->zflags & afxZodiacData::BLEND_MASK, is_reflect_pass); 279 280 GFX->setShader(zodiac_shader->getShader()); 281 GFX->setStateBlock(sb); 282 GFX->setShaderConstBuffer(shader_consts); 283 284 // set the texture 285 GFX->setTexture(0, *zode->txr); 286 LinearColorF zode_color = (LinearColorF)zode->color; 287 zode_color.alpha *= fadebias; 288 shader_consts->set(color_sc, zode_color); 289 290 Point3F half_size(zode->radius_xy,zode->radius_xy,zode->radius_xy); 291 292 F32 inv_radius = 1.0f/zode->radius_xy; 293 294 GFXPrimitive cell_prim; 295 GFXVertexBufferHandle<TerrVertex> cell_verts; 296 GFXPrimitiveBufferHandle primBuff; 297 elem.cell->getRenderPrimitive(&cell_prim, &cell_verts, &primBuff); 298 299 U32 n_nonskirt_tris = TerrCellSpy::getMinCellSize()*TerrCellSpy::getMinCellSize()*2; 300 301 const Point3F* verts = ((TerrCell*)elem.cell)->getZodiacVertexBuffer(); 302 const U16 *tris = block->getZodiacPrimitiveBuffer(); 303 if (!tris) 304 continue; 305 306 PrimBuild::begin(GFXTriangleList, 3*n_nonskirt_tris); 307 308 ///////////////////////////////// 309 U32 n_overlapping_tris = 0; 310 U32 idx = 0; 311 for (U32 i = 0; i < n_nonskirt_tris; i++) 312 { 313 Point3F tri_v[3]; 314 tri_v[0] = verts[tris[idx++]]; 315 tri_v[1] = verts[tris[idx++]]; 316 tri_v[2] = verts[tris[idx++]]; 317 318 elem.mRenderObjToWorld.mulP(tri_v[0]); 319 elem.mRenderObjToWorld.mulP(tri_v[1]); 320 elem.mRenderObjToWorld.mulP(tri_v[2]); 321 322 if (!afxTriBoxOverlap2D(zode->pos, half_size, tri_v[0], tri_v[1], tri_v[2])) 323 continue; 324 325 n_overlapping_tris++; 326 327 for (U32 j = 0; j < 3; j++) 328 { 329 // compute UV 330 F32 u1 = (tri_v[j].x - zode->pos.x)*inv_radius; 331 F32 v1 = (tri_v[j].y - zode->pos.y)*inv_radius; 332 F32 ru1 = u1*cos_ang - v1*sin_ang; 333 F32 rv1 = u1*sin_ang + v1*cos_ang; 334 335 F32 uu = (ru1 + 1.0f)/2.0f; 336 F32 vv = 1.0f - (rv1 + 1.0f)/2.0f; 337 338 PrimBuild::texCoord2f(uu, vv); 339 PrimBuild::vertex3fv(tri_v[j]); 340 } 341 } 342 343 ///////////////////////////////// 344 345 PrimBuild::end(false); 346 } 347 // 348 // RENDER EACH ZODIAC 349 //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 350} 351 352//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 353