decalInstance.cpp
Engine/source/T3D/decal/decalInstance.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 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 "platform/platform.h" 25#include "T3D/decal/decalInstance.h" 26#include "scene/sceneRenderState.h" 27 28DecalInstance::DecalInstance() 29 : mDataBlock(NULL), 30 mRotAroundNormal(0.0f), 31 mSize(0.0f), 32 mCreateTime(0), 33 mVisibility(1.0f), 34 mLastAlpha(1.0f), 35 mTextureRectIdx(0), 36 mVerts(NULL), 37 mIndices(NULL), 38 mVertCount(0), 39 mIndxCount(0), 40 mFlags(0), 41 mRenderPriority(0), 42 mId(-1), 43 mCustomTex(NULL) 44{} 45void DecalInstance::getWorldMatrix( MatrixF *outMat, bool flip ) 46{ 47 outMat->setPosition( mPosition ); 48 49 Point3F fvec; 50 mCross( mNormal, mTangent, &fvec ); 51 52 outMat->setColumn( 0, mTangent ); 53 outMat->setColumn( 1, fvec ); 54 outMat->setColumn( 2, mNormal ); 55} 56 57F32 DecalInstance::calcPixelSize( U32 viewportHeight, const Point3F &cameraPos, F32 worldToScreenScaleY ) const 58{ 59 // If fadeStartPixelSize is set less than zero this is interpreted to mean 60 // pixelSize based fading is disabled and the decal always renders. 61 // Returning a value of F32_MAX should be treated by the caller as 62 // meaning "big enough to render at normal quality, dont LOD me.". 63 if ( mDataBlock->fadeStartPixelSize < 0.0f ) 64 return F32_MAX; 65 66 // This is an approximation since mSize is actually the xyz size of the 67 // cube we use to clip geometry for the decal. In this case we are assuming 68 // it is appropriate to use the radius of the sphere 'inscribed' in that 69 // cube as the equivalent of mShape->radius for purposes of calculating 70 // the pixelScale ( see TSShapeInstance::setDetailFromDistance ). 71 const F32 radius = mSize * 0.5f; 72 73 // Approximate distance to the decal. 74 const F32 distance = ( cameraPos - mPosition ).len() - radius; 75 76 // We are inside the decal's volume. There is no useful pixelSize for us 77 // to return, it is essentially the entire screen. 78 if ( distance <= 0.0f ) 79 return F32_MAX; 80 81 // Lod is relative to a viewport with a heigh of 300. Could prescale this into decalSize... 82 const F32 pixelScale = viewportHeight / 300.0f; 83 84 // Inline of SceneState::projectRadius. 85 const F32 pixelSize = mSize / distance * worldToScreenScaleY * pixelScale; 86 87 // Optionally scale pixelSize by a detail adjust value here... 88 // eg. pixelSize *= TSDetailAdjust... 89 90 return pixelSize; 91} 92