afxZodiacGroundPlaneRenderer_T3D.cpp
Engine/source/afx/afxZodiacGroundPlaneRenderer_T3D.cpp
Public Functions
ConsoleDocClass(afxZodiacGroundPlaneRenderer , "@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 <a href="/coding/class/classgroundplane/">GroundPlane</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">objects.\n\n</a>" "This bin renders instances of AFX zodiac effects onto <a href="/coding/class/classgroundplane/">GroundPlane</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(afxZodiacGroundPlaneRenderer , "@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 <a href="/coding/class/classgroundplane/">GroundPlane</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">objects.\n\n</a>" "This bin renders instances of AFX zodiac effects onto <a href="/coding/class/classgroundplane/">GroundPlane</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(afxZodiacGroundPlaneRenderer )
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/afxZodiacGroundPlaneRenderer_T3D.h" 38 39//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 40 41const RenderInstType afxZodiacGroundPlaneRenderer::RIT_GroundPlaneZodiac("GroundPlaneZodiac"); 42 43afxZodiacGroundPlaneRenderer* afxZodiacGroundPlaneRenderer::master = 0; 44 45IMPLEMENT_CONOBJECT(afxZodiacGroundPlaneRenderer); 46 47ConsoleDocClass( afxZodiacGroundPlaneRenderer, 48 "@brief A render bin for zodiac rendering on GroundPlane objects.\n\n" 49 50 "This bin renders instances of AFX zodiac effects onto GroundPlane surfaces.\n\n" 51 52 "@ingroup RenderBin\n" 53 "@ingroup AFX\n" 54); 55 56afxZodiacGroundPlaneRenderer::afxZodiacGroundPlaneRenderer() 57: RenderBinManager(RIT_GroundPlaneZodiac, 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 68afxZodiacGroundPlaneRenderer::afxZodiacGroundPlaneRenderer(F32 renderOrder, F32 processAddOrder) 69: RenderBinManager(RIT_GroundPlaneZodiac, 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 80afxZodiacGroundPlaneRenderer::~afxZodiacGroundPlaneRenderer() 81{ 82 if (this == master) 83 master = 0; 84} 85 86//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 87 88void afxZodiacGroundPlaneRenderer::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::getGroundPlaneZodiacShader(); 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 afxZodiacGroundPlaneRenderer::clear() 163{ 164 Parent::clear(); 165 groundPlane_zodiacs.clear(); 166} 167 168void afxZodiacGroundPlaneRenderer::addZodiac(U32 zode_idx, const Point3F& pos, F32 ang, const GroundPlane* gp, F32 camDist) 169{ 170 groundPlane_zodiacs.increment(); 171 GroundPlaneZodiacElem& elem = groundPlane_zodiacs.last(); 172 173 elem.gp = gp; 174 elem.zode_idx = zode_idx; 175 elem.ang = ang; 176 elem.camDist = camDist; 177} 178 179afxZodiacGroundPlaneRenderer* afxZodiacGroundPlaneRenderer::getMaster() 180{ 181 if (!master) 182 master = new afxZodiacGroundPlaneRenderer; 183 return master; 184} 185 186//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 187 188GFXStateBlock* afxZodiacGroundPlaneRenderer::chooseStateBlock(U32 blend, bool isReflectPass) 189{ 190 GFXStateBlock* sb = 0; 191 192 switch (blend) 193 { 194 case afxZodiacData::BLEND_ADDITIVE: 195 sb = (isReflectPass) ? add_refl_zb_SB : add_norefl_zb_SB; 196 break; 197 case afxZodiacData::BLEND_SUBTRACTIVE: 198 sb = (isReflectPass) ? sub_refl_zb_SB : sub_norefl_zb_SB; 199 break; 200 default: // afxZodiacData::BLEND_NORMAL: 201 sb = (isReflectPass) ? norm_refl_zb_SB : norm_norefl_zb_SB; 202 break; 203 } 204 205 return sb; 206} 207 208void afxZodiacGroundPlaneRenderer::render(SceneRenderState* state) 209{ 210 PROFILE_SCOPE(afxRenderZodiacGroundPlaneMgr_render); 211 212 // Early out if no ground-plane zodiacs to draw. 213 if (groundPlane_zodiacs.size() == 0) 214 return; 215 216 initShader(); 217 if (!zodiac_shader) 218 return; 219 220 bool is_reflect_pass = state->isReflectPass(); 221 222 // Automagically save & restore our viewport and transforms. 223 GFXTransformSaver saver; 224 225 MatrixF proj = GFX->getProjectionMatrix(); 226 227 // Set up world transform 228 MatrixF world = GFX->getWorldMatrix(); 229 proj.mul(world); 230 shader_consts->set(projection_sc, proj); 231 232 //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~// 233 // RENDER EACH ZODIAC 234 // 235 for (S32 zz = 0; zz < groundPlane_zodiacs.size(); zz++) 236 { 237 GroundPlaneZodiacElem& elem = groundPlane_zodiacs[zz]; 238 239 afxZodiacMgr::ZodiacSpec* zode = &afxZodiacMgr::terr_zodes[elem.zode_idx]; 240 if (!zode) 241 continue; 242 243 if (is_reflect_pass) 244 { 245 //if ((zode->zflags & afxZodiacData::SHOW_IN_REFLECTIONS) == 0) 246 continue; 247 } 248 else 249 { 250 if ((zode->zflags & afxZodiacData::SHOW_IN_NON_REFLECTIONS) == 0) 251 continue; 252 } 253 254 F32 fadebias = zode->calcDistanceFadeBias(elem.camDist); 255 if (fadebias < 0.01f) 256 continue; 257 258 F32 cos_ang = mCos(elem.ang); 259 F32 sin_ang = mSin(elem.ang); 260 261 GFXStateBlock* sb = chooseStateBlock(zode->zflags & afxZodiacData::BLEND_MASK, is_reflect_pass); 262 263 GFX->setShader(zodiac_shader->getShader()); 264 GFX->setStateBlock(sb); 265 GFX->setShaderConstBuffer(shader_consts); 266 267 // set the texture 268 GFX->setTexture(0, *zode->txr); 269 LinearColorF zode_color = (LinearColorF)zode->color; 270 zode_color.alpha *= fadebias; 271 shader_consts->set(color_sc, zode_color); 272 273 F32 rad_xy = zode->radius_xy; 274 F32 inv_radius = 1.0f/rad_xy; 275 F32 offset_xy = mSqrt(2*rad_xy*rad_xy); 276 277 F32 zx = zode->pos.x; 278 F32 zy = zode->pos.y; 279 F32 z = 0.00001f; 280 281 Point3F verts[4]; 282 verts[0].set(zx+offset_xy, zy+offset_xy, z); 283 verts[1].set(zx-offset_xy, zy+offset_xy, z); 284 verts[2].set(zx-offset_xy, zy-offset_xy, z); 285 verts[3].set(zx+offset_xy, zy-offset_xy, z); 286 287 S32 vertind[6]; 288 vertind[0] = 2; 289 vertind[1] = 1; 290 vertind[2] = 0; 291 vertind[3] = 3; 292 vertind[4] = 2; 293 vertind[5] = 0; 294 295 PrimBuild::begin(GFXTriangleList, 6); 296 for (U32 i = 0; i < 2; i++) 297 { 298 for (U32 j = 0; j < 3; j++) 299 { 300 const Point3F& vtx = verts[vertind[i*3+j]]; 301 302 // compute UV 303 F32 u1 = (vtx.x - zode->pos.x)*inv_radius; 304 F32 v1 = (vtx.y - zode->pos.y)*inv_radius; 305 F32 ru1 = u1*cos_ang - v1*sin_ang; 306 F32 rv1 = u1*sin_ang + v1*cos_ang; 307 308 F32 uu = (ru1 + 1.0f)/2.0f; 309 F32 vv = 1.0f - (rv1 + 1.0f)/2.0f; 310 311 PrimBuild::texCoord2f(uu, vv); 312 PrimBuild::vertex3fv(vtx); 313 } 314 } 315 PrimBuild::end(false); 316 } 317} 318 319//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 320