afxZodiacPolysoupRenderer_T3D.cpp
Engine/source/afx/afxZodiacPolysoupRenderer_T3D.cpp
Public Functions
ConsoleDocClass(afxZodiacPolysoupRenderer , "@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 polysoup <a href="/coding/class/classtsstatic/">TSStatic</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">objects.\n\n</a>" "This bin renders instances of AFX zodiac effects onto polysoup <a href="/coding/class/classtsstatic/">TSStatic</a> <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(afxZodiacPolysoupRenderer , "@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 polysoup <a href="/coding/class/classtsstatic/">TSStatic</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">objects.\n\n</a>" "This bin renders instances of AFX zodiac effects onto polysoup <a href="/coding/class/classtsstatic/">TSStatic</a> <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(afxZodiacPolysoupRenderer )
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 "collision/concretePolyList.h" 33#include "T3D/tsStatic.h" 34#include "gfx/primBuilder.h" 35 36#include "afx/ce/afxZodiacMgr.h" 37#include "afx/afxZodiacPolysoupRenderer_T3D.h" 38 39//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 40 41const RenderInstType afxZodiacPolysoupRenderer::RIT_PolysoupZodiac("PolysoupZodiac"); 42 43afxZodiacPolysoupRenderer* afxZodiacPolysoupRenderer::master = 0; 44 45IMPLEMENT_CONOBJECT(afxZodiacPolysoupRenderer); 46 47ConsoleDocClass( afxZodiacPolysoupRenderer, 48 "@brief A render bin for zodiac rendering on polysoup TSStatic objects.\n\n" 49 50 "This bin renders instances of AFX zodiac effects onto polysoup TSStatic surfaces.\n\n" 51 52 "@ingroup RenderBin\n" 53 "@ingroup AFX\n" 54); 55 56afxZodiacPolysoupRenderer::afxZodiacPolysoupRenderer() 57: RenderBinManager(RIT_PolysoupZodiac, 1.0f, 1.0f) 58{ 59 if (!master) 60 master = this; 61 shader_initialized = false; 62 zodiac_shader = NULL; 63 shader_consts = NULL; 64 projection_sc = NULL; 65 color_sc = NULL; 66} 67 68afxZodiacPolysoupRenderer::afxZodiacPolysoupRenderer(F32 renderOrder, F32 processAddOrder) 69: RenderBinManager(RIT_PolysoupZodiac, renderOrder, processAddOrder) 70{ 71 if (!master) 72 master = this; 73 shader_initialized = false; 74 zodiac_shader = NULL; 75 shader_consts = NULL; 76 projection_sc = NULL; 77 color_sc = NULL; 78} 79 80afxZodiacPolysoupRenderer::~afxZodiacPolysoupRenderer() 81{ 82 if (this == master) 83 master = 0; 84} 85 86//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 87 88void afxZodiacPolysoupRenderer::initShader() 89{ 90 if (shader_initialized) 91 return; 92 93 shader_initialized = true; 94 95 shader_consts = 0; 96 norm_norefl_zb_SB = norm_refl_zb_SB; 97 add_norefl_zb_SB = add_refl_zb_SB; 98 sub_norefl_zb_SB = sub_refl_zb_SB; 99 100 zodiac_shader = afxZodiacMgr::getPolysoupZodiacShader(); 101 if (!zodiac_shader) 102 return; 103 104 GFXStateBlockDesc d; 105 106 d.cullDefined = true; 107 d.blendDefined = true; 108 d.blendEnable = true; 109 d.zDefined = false; 110 d.zEnable = true; 111 d.zWriteEnable = false; 112 d.zFunc = GFXCmpLessEqual; 113 d.zSlopeBias = 0; 114 d.alphaDefined = true; 115 d.alphaTestEnable = true; 116 d.alphaTestRef = 0; 117 d.alphaTestFunc = GFXCmpGreater; 118 d.samplersDefined = true; 119 d.samplers[0] = GFXSamplerStateDesc::getClampLinear(); 120 121 // normal 122 d.blendSrc = GFXBlendSrcAlpha; 123 d.blendDest = GFXBlendInvSrcAlpha; 124 // 125 d.cullMode = GFXCullCCW; 126 d.zBias = arcaneFX::sPolysoupZodiacZBias; 127 norm_norefl_zb_SB = GFX->createStateBlock(d); 128 // 129 d.cullMode = GFXCullCW; 130 d.zBias = arcaneFX::sPolysoupZodiacZBias; 131 norm_refl_zb_SB = GFX->createStateBlock(d); 132 133 // additive 134 d.blendSrc = GFXBlendSrcAlpha; 135 d.blendDest = GFXBlendOne; 136 // 137 d.cullMode = GFXCullCCW; 138 d.zBias = arcaneFX::sPolysoupZodiacZBias; 139 add_norefl_zb_SB = GFX->createStateBlock(d); 140 // 141 d.cullMode = GFXCullCW; 142 d.zBias = arcaneFX::sPolysoupZodiacZBias; 143 add_refl_zb_SB = GFX->createStateBlock(d); 144 145 // subtractive 146 d.blendSrc = GFXBlendZero; 147 d.blendDest = GFXBlendInvSrcColor; 148 // 149 d.cullMode = GFXCullCCW; 150 d.zBias = arcaneFX::sPolysoupZodiacZBias; 151 sub_norefl_zb_SB = GFX->createStateBlock(d); 152 // 153 d.cullMode = GFXCullCW; 154 d.zBias = arcaneFX::sPolysoupZodiacZBias; 155 sub_refl_zb_SB = GFX->createStateBlock(d); 156 157 shader_consts = zodiac_shader->getShader()->allocConstBuffer(); 158 projection_sc = zodiac_shader->getShader()->getShaderConstHandle("$modelView"); 159 color_sc = zodiac_shader->getShader()->getShaderConstHandle("$zodiacColor"); 160} 161 162void afxZodiacPolysoupRenderer::clear() 163{ 164 Parent::clear(); 165 for (S32 i = 0; i < polysoup_zodes.size(); i++) 166 if (polysoup_zodes[i].polys) 167 delete polysoup_zodes[i].polys; 168 polysoup_zodes.clear(); 169} 170 171void afxZodiacPolysoupRenderer::addZodiac(U32 zode_idx, ConcretePolyList* polys, const Point3F& pos, F32 ang, const TSStatic* tss, F32 camDist) 172{ 173 polysoup_zodes.increment(); 174 PolysoupZodiacElem& elem = polysoup_zodes.last(); 175 176 elem.tss = tss; 177 elem.polys = polys; 178 elem.zode_idx = zode_idx; 179 elem.ang = ang; 180 elem.camDist = camDist; 181} 182 183afxZodiacPolysoupRenderer* afxZodiacPolysoupRenderer::getMaster() 184{ 185 if (!master) 186 master = new afxZodiacPolysoupRenderer; 187 return master; 188} 189 190//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 191 192GFXStateBlock* afxZodiacPolysoupRenderer::chooseStateBlock(U32 blend, bool isReflectPass) 193{ 194 GFXStateBlock* sb = 0; 195 196 switch (blend) 197 { 198 case afxZodiacData::BLEND_ADDITIVE: 199 sb = (isReflectPass) ? add_refl_zb_SB : add_norefl_zb_SB; 200 break; 201 case afxZodiacData::BLEND_SUBTRACTIVE: 202 sb = (isReflectPass) ? sub_refl_zb_SB : sub_norefl_zb_SB; 203 break; 204 default: // afxZodiacData::BLEND_NORMAL: 205 sb = (isReflectPass) ? norm_refl_zb_SB : norm_norefl_zb_SB; 206 break; 207 } 208 209 return sb; 210} 211 212void afxZodiacPolysoupRenderer::render(SceneRenderState* state) 213{ 214 PROFILE_SCOPE(afxRenderZodiacPolysoupMgr_render); 215 216 // Early out if no polysoup zodiacs to draw. 217 if (polysoup_zodes.size() == 0) 218 return; 219 220 initShader(); 221 if (!zodiac_shader) 222 return; 223 224 bool is_reflect_pass = state->isReflectPass(); 225 226 // Automagically save & restore our viewport and transforms. 227 GFXTransformSaver saver; 228 229 MatrixF proj = GFX->getProjectionMatrix(); 230 231 // Set up world transform 232 MatrixF world = GFX->getWorldMatrix(); 233 proj.mul(world); 234 shader_consts->set(projection_sc, proj); 235 236 //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 237 // RENDER EACH ZODIAC 238 // 239 for (S32 zz = 0; zz < polysoup_zodes.size(); zz++) 240 { 241 PolysoupZodiacElem& elem = polysoup_zodes[zz]; 242 243 afxZodiacMgr::ZodiacSpec* zode = &afxZodiacMgr::inter_zodes[elem.zode_idx]; 244 if (!zode) 245 continue; 246 247 if (is_reflect_pass) 248 { 249 if ((zode->zflags & afxZodiacData::SHOW_IN_REFLECTIONS) == 0) 250 continue; 251 } 252 else 253 { 254 if ((zode->zflags & afxZodiacData::SHOW_IN_NON_REFLECTIONS) == 0) 255 continue; 256 } 257 258 F32 fadebias = zode->calcDistanceFadeBias(elem.camDist); 259 if (fadebias < 0.01f) 260 continue; 261 262 F32 cos_ang = mCos(elem.ang); 263 F32 sin_ang = mSin(elem.ang); 264 265 GFXStateBlock* sb = chooseStateBlock(zode->zflags & afxZodiacData::BLEND_MASK, is_reflect_pass); 266 267 GFX->setShader(zodiac_shader->getShader()); 268 GFX->setStateBlock(sb); 269 GFX->setShaderConstBuffer(shader_consts); 270 271 // set the texture 272 GFX->setTexture(0, *zode->txr); 273 LinearColorF zode_color = (LinearColorF)zode->color; 274 zode_color.alpha *= fadebias; 275 shader_consts->set(color_sc, zode_color); 276 277 F32 inv_radius = 1.0f/zode->radius_xy; 278 279 // FILTER USING GRADIENT RANGE 280 if ((zode->zflags & afxZodiacData::USE_GRADE_RANGE) != 0) 281 { 282 bool skip_oob; 283 F32 grade_min, grade_max; 284 if (elem.tss->mHasGradients && ((zode->zflags & afxZodiacData::PREFER_DEST_GRADE) != 0)) 285 { 286 skip_oob = (elem.tss->mInvertGradientRange == false); 287 grade_min = elem.tss->mGradientRange.x; 288 grade_max = elem.tss->mGradientRange.y; 289 } 290 else 291 { 292 skip_oob = ((zode->zflags & afxZodiacData::INVERT_GRADE_RANGE) == 0); 293 grade_min = zode->grade_range.x; 294 grade_max = zode->grade_range.y; 295 } 296 297 PrimBuild::begin(GFXTriangleList, 3*elem.polys->mPolyList.size()); 298 for (U32 i = 0; i < elem.polys->mPolyList.size(); i++) 299 { 300 ConcretePolyList::Poly* poly = &elem.polys->mPolyList[i]; 301 302 const PlaneF& plane = poly->plane; 303 304 bool oob = (plane.z > grade_max || plane.z < grade_min); 305 if (oob == skip_oob) 306 continue; 307 308 S32 vertind[3]; 309 vertind[0] = elem.polys->mIndexList[poly->vertexStart]; 310 vertind[1] = elem.polys->mIndexList[poly->vertexStart + 1]; 311 vertind[2] = elem.polys->mIndexList[poly->vertexStart + 2]; 312 313 for (U32 j = 0; j < 3; j++) 314 { 315 Point3F vtx = elem.polys->mVertexList[vertind[j]]; 316 317 // compute UV 318 F32 u1 = (vtx.x - zode->pos.x)*inv_radius; 319 F32 v1 = (vtx.y - zode->pos.y)*inv_radius; 320 F32 ru1 = u1*cos_ang - v1*sin_ang; 321 F32 rv1 = u1*sin_ang + v1*cos_ang; 322 323 F32 uu = (ru1 + 1.0f)/2.0f; 324 F32 vv = 1.0f - (rv1 + 1.0f)/2.0f; 325 326 PrimBuild::texCoord2f(uu, vv); 327 PrimBuild::vertex3fv(vtx); 328 } 329 } 330 PrimBuild::end(false); 331 } 332 333 // FILTER USING OTHER FILTERS 334 else if (zode->zflags & afxZodiacData::INTERIOR_FILTERS) 335 { 336 PrimBuild::begin(GFXTriangleList, 3*elem.polys->mPolyList.size()); 337 for (U32 i = 0; i < elem.polys->mPolyList.size(); i++) 338 { 339 ConcretePolyList::Poly* poly = &elem.polys->mPolyList[i]; 340 341 const PlaneF& plane = poly->plane; 342 if (zode->zflags & afxZodiacData::INTERIOR_HORIZ_ONLY) 343 { 344 if (!plane.isHorizontal()) 345 continue; 346 347 if (zode->zflags & afxZodiacData::INTERIOR_BACK_IGNORE) 348 { 349 if (plane.whichSide(zode->pos) == PlaneF::Back) 350 continue; 351 } 352 } 353 else 354 { 355 if (zode->zflags & afxZodiacData::INTERIOR_VERT_IGNORE) 356 { 357 if (plane.isVertical()) 358 continue; 359 } 360 361 if (zode->zflags & afxZodiacData::INTERIOR_BACK_IGNORE) 362 { 363 if (plane.whichSide(zode->pos) == PlaneF::Back) 364 continue; 365 } 366 } 367 368 S32 vertind[3]; 369 vertind[0] = elem.polys->mIndexList[poly->vertexStart]; 370 vertind[1] = elem.polys->mIndexList[poly->vertexStart + 1]; 371 vertind[2] = elem.polys->mIndexList[poly->vertexStart + 2]; 372 373 for (U32 j = 0; j < 3; j++) 374 { 375 Point3F vtx = elem.polys->mVertexList[vertind[j]]; 376 377 // compute UV 378 F32 u1 = (vtx.x - zode->pos.x)*inv_radius; 379 F32 v1 = (vtx.y - zode->pos.y)*inv_radius; 380 F32 ru1 = u1*cos_ang - v1*sin_ang; 381 F32 rv1 = u1*sin_ang + v1*cos_ang; 382 383 F32 uu = (ru1 + 1.0f)/2.0f; 384 F32 vv = 1.0f - (rv1 + 1.0f)/2.0f; 385 386 PrimBuild::texCoord2f(uu, vv); 387 PrimBuild::vertex3fv(vtx); 388 } 389 } 390 PrimBuild::end(false); 391 } 392 393 // NO FILTERING 394 else 395 { 396 PrimBuild::begin(GFXTriangleList, 3*elem.polys->mPolyList.size()); 397 for (U32 i = 0; i < elem.polys->mPolyList.size(); i++) 398 { 399 ConcretePolyList::Poly* poly = &elem.polys->mPolyList[i]; 400 401 S32 vertind[3]; 402 vertind[0] = elem.polys->mIndexList[poly->vertexStart]; 403 vertind[1] = elem.polys->mIndexList[poly->vertexStart + 1]; 404 vertind[2] = elem.polys->mIndexList[poly->vertexStart + 2]; 405 406 for (U32 j = 0; j < 3; j++) 407 { 408 Point3F vtx = elem.polys->mVertexList[vertind[j]]; 409 410 // compute UV 411 F32 u1 = (vtx.x - zode->pos.x)*inv_radius; 412 F32 v1 = (vtx.y - zode->pos.y)*inv_radius; 413 F32 ru1 = u1*cos_ang - v1*sin_ang; 414 F32 rv1 = u1*sin_ang + v1*cos_ang; 415 416 F32 uu = (ru1 + 1.0f)/2.0f; 417 F32 vv = 1.0f - (rv1 + 1.0f)/2.0f; 418 419 PrimBuild::texCoord2f(uu, vv); 420 PrimBuild::vertex3fv(vtx); 421 } 422 } 423 PrimBuild::end(false); 424 } 425 } 426} 427 428//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 429