mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-02-26 10:03:48 +00:00
Engine directory for ticket #1
This commit is contained in:
parent
352279af7a
commit
7dbfe6994d
3795 changed files with 1363358 additions and 0 deletions
412
Engine/source/lighting/basic/basicLightManager.cpp
Normal file
412
Engine/source/lighting/basic/basicLightManager.cpp
Normal file
|
|
@ -0,0 +1,412 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "lighting/basic/basicLightManager.h"
|
||||
|
||||
#include "platform/platformTimer.h"
|
||||
#include "console/simSet.h"
|
||||
#include "console/consoleTypes.h"
|
||||
#include "core/module.h"
|
||||
#include "core/util/safeDelete.h"
|
||||
#include "materials/processedMaterial.h"
|
||||
#include "shaderGen/shaderFeature.h"
|
||||
#include "lighting/basic/basicSceneObjectLightingPlugin.h"
|
||||
#include "shaderGen/shaderGenVars.h"
|
||||
#include "gfx/gfxShader.h"
|
||||
#include "materials/sceneData.h"
|
||||
#include "materials/materialParameters.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "materials/materialFeatureTypes.h"
|
||||
#include "math/util/frustum.h"
|
||||
#include "scene/sceneObject.h"
|
||||
#include "renderInstance/renderPrePassMgr.h"
|
||||
#include "shaderGen/featureMgr.h"
|
||||
#include "shaderGen/HLSL/shaderFeatureHLSL.h"
|
||||
#include "shaderGen/HLSL/bumpHLSL.h"
|
||||
#include "shaderGen/HLSL/pixSpecularHLSL.h"
|
||||
#include "lighting/basic/blInteriorSystem.h"
|
||||
#include "lighting/basic/blTerrainSystem.h"
|
||||
#include "lighting/common/projectedShadow.h"
|
||||
|
||||
|
||||
#ifdef TORQUE_OS_MAC
|
||||
#include "shaderGen/GLSL/shaderFeatureGLSL.h"
|
||||
#include "shaderGen/GLSL/bumpGLSL.h"
|
||||
#include "shaderGen/GLSL/pixSpecularGLSL.h"
|
||||
#endif
|
||||
|
||||
|
||||
MODULE_BEGIN( BasicLightManager )
|
||||
|
||||
MODULE_SHUTDOWN_AFTER( Scene )
|
||||
|
||||
MODULE_INIT
|
||||
{
|
||||
ManagedSingleton< BasicLightManager >::createSingleton();
|
||||
}
|
||||
|
||||
MODULE_SHUTDOWN
|
||||
{
|
||||
ManagedSingleton< BasicLightManager >::deleteSingleton();
|
||||
}
|
||||
|
||||
MODULE_END;
|
||||
|
||||
|
||||
U32 BasicLightManager::smActiveShadowPlugins = 0;
|
||||
U32 BasicLightManager::smShadowsUpdated = 0;
|
||||
U32 BasicLightManager::smElapsedUpdateMs = 0;
|
||||
|
||||
F32 BasicLightManager::smProjectedShadowFilterDistance = 40.0f;
|
||||
|
||||
static S32 QSORT_CALLBACK comparePluginScores( const void *a, const void *b )
|
||||
{
|
||||
const BasicSceneObjectLightingPlugin *A = *((BasicSceneObjectLightingPlugin**)a);
|
||||
const BasicSceneObjectLightingPlugin *B = *((BasicSceneObjectLightingPlugin**)b);
|
||||
|
||||
F32 dif = B->getScore() - A->getScore();
|
||||
return (S32)mFloor( dif );
|
||||
}
|
||||
|
||||
BasicLightManager::BasicLightManager()
|
||||
: LightManager( "Basic Lighting", "BLM" ),
|
||||
mLastShader(NULL),
|
||||
mLastConstants(NULL)
|
||||
{
|
||||
mTimer = PlatformTimer::create();
|
||||
|
||||
mInteriorSystem = new blInteriorSystem;
|
||||
mTerrainSystem = new blTerrainSystem;
|
||||
|
||||
getSceneLightingInterface()->registerSystem( mInteriorSystem );
|
||||
getSceneLightingInterface()->registerSystem( mTerrainSystem );
|
||||
|
||||
Con::addVariable( "$BasicLightManagerStats::activePlugins",
|
||||
TypeS32, &smActiveShadowPlugins,
|
||||
"The number of active Basic Lighting SceneObjectLightingPlugin objects this frame.\n"
|
||||
"@ingroup BasicLighting\n" );
|
||||
|
||||
Con::addVariable( "$BasicLightManagerStats::shadowsUpdated",
|
||||
TypeS32, &smShadowsUpdated,
|
||||
"The number of Basic Lighting shadows updated this frame.\n"
|
||||
"@ingroup BasicLighting\n" );
|
||||
|
||||
Con::addVariable( "$BasicLightManagerStats::elapsedUpdateMs",
|
||||
TypeS32, &smElapsedUpdateMs,
|
||||
"The number of milliseconds spent this frame updating Basic Lighting shadows.\n"
|
||||
"@ingroup BasicLighting\n" );
|
||||
|
||||
Con::addVariable( "$BasicLightManager::shadowFilterDistance",
|
||||
TypeF32, &smProjectedShadowFilterDistance,
|
||||
"The maximum distance in meters that projected shadows will get soft filtering.\n"
|
||||
"@ingroup BasicLighting\n" );
|
||||
|
||||
Con::addVariable( "$pref::ProjectedShadow::fadeStartPixelSize",
|
||||
TypeF32, &ProjectedShadow::smFadeStartPixelSize,
|
||||
"A size in pixels at which BL shadows begin to fade out. "
|
||||
"This should be a larger value than fadeEndPixelSize.\n"
|
||||
"@see DecalData\n"
|
||||
"@ingroup BasicLighting\n" );
|
||||
|
||||
Con::addVariable( "$pref::ProjectedShadow::fadeEndPixelSize",
|
||||
TypeF32, &ProjectedShadow::smFadeEndPixelSize,
|
||||
"A size in pixels at which BL shadows are fully faded out. "
|
||||
"This should be a smaller value than fadeStartPixelSize.\n"
|
||||
"@see DecalData\n"
|
||||
"@ingroup BasicLighting\n" );
|
||||
}
|
||||
|
||||
BasicLightManager::~BasicLightManager()
|
||||
{
|
||||
mLastShader = NULL;
|
||||
mLastConstants = NULL;
|
||||
|
||||
for (LightConstantMap::Iterator i = mConstantLookup.begin(); i != mConstantLookup.end(); i++)
|
||||
{
|
||||
if (i->value)
|
||||
SAFE_DELETE(i->value);
|
||||
}
|
||||
mConstantLookup.clear();
|
||||
|
||||
if (mTimer)
|
||||
SAFE_DELETE( mTimer );
|
||||
|
||||
SAFE_DELETE( mTerrainSystem );
|
||||
SAFE_DELETE( mInteriorSystem );
|
||||
}
|
||||
|
||||
bool BasicLightManager::isCompatible() const
|
||||
{
|
||||
// As long as we have some shaders this works.
|
||||
return GFX->getPixelShaderVersion() > 1.0;
|
||||
}
|
||||
|
||||
void BasicLightManager::activate( SceneManager *sceneManager )
|
||||
{
|
||||
Parent::activate( sceneManager );
|
||||
|
||||
if( GFX->getAdapterType() == OpenGL )
|
||||
{
|
||||
#ifdef TORQUE_OS_MAC
|
||||
FEATUREMGR->registerFeature( MFT_LightMap, new LightmapFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_ToneMap, new TonemapFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_NormalMap, new BumpFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_RTLighting, new RTLightingFeatGLSL );
|
||||
FEATUREMGR->registerFeature( MFT_PixSpecular, new PixelSpecularGLSL );
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifndef TORQUE_OS_MAC
|
||||
FEATUREMGR->registerFeature( MFT_LightMap, new LightmapFeatHLSL );
|
||||
FEATUREMGR->registerFeature( MFT_ToneMap, new TonemapFeatHLSL );
|
||||
FEATUREMGR->registerFeature( MFT_NormalMap, new BumpFeatHLSL );
|
||||
FEATUREMGR->registerFeature( MFT_RTLighting, new RTLightingFeatHLSL );
|
||||
FEATUREMGR->registerFeature( MFT_PixSpecular, new PixelSpecularHLSL );
|
||||
#endif
|
||||
}
|
||||
|
||||
FEATUREMGR->unregisterFeature( MFT_MinnaertShading );
|
||||
FEATUREMGR->unregisterFeature( MFT_SubSurface );
|
||||
|
||||
// First look for the prepass bin...
|
||||
RenderPrePassMgr *prePassBin = _findPrePassRenderBin();
|
||||
|
||||
/*
|
||||
// If you would like to use forward shading, and have a linear depth pre-pass
|
||||
// than un-comment this code block.
|
||||
if ( !prePassBin )
|
||||
{
|
||||
Vector<GFXFormat> formats;
|
||||
formats.push_back( GFXFormatR32F );
|
||||
formats.push_back( GFXFormatR16F );
|
||||
formats.push_back( GFXFormatR8G8B8A8 );
|
||||
GFXFormat linearDepthFormat = GFX->selectSupportedFormat( &GFXDefaultRenderTargetProfile,
|
||||
formats,
|
||||
true,
|
||||
false );
|
||||
|
||||
// Uncomment this for a no-color-write z-fill pass.
|
||||
//linearDepthFormat = GFXFormat_COUNT;
|
||||
|
||||
prePassBin = new RenderPrePassMgr( linearDepthFormat != GFXFormat_COUNT, linearDepthFormat );
|
||||
prePassBin->registerObject();
|
||||
rpm->addManager( prePassBin );
|
||||
}
|
||||
*/
|
||||
mPrePassRenderBin = prePassBin;
|
||||
|
||||
// If there is a prepass bin
|
||||
MATMGR->setPrePassEnabled( mPrePassRenderBin.isValid() );
|
||||
sceneManager->setPostEffectFog( mPrePassRenderBin.isValid() && mPrePassRenderBin->getTargetChainLength() > 0 );
|
||||
|
||||
// Tell the material manager that we don't use prepass.
|
||||
MATMGR->setPrePassEnabled( false );
|
||||
|
||||
GFXShader::addGlobalMacro( "TORQUE_BASIC_LIGHTING" );
|
||||
|
||||
// Hook into the SceneManager prerender signal.
|
||||
sceneManager->getPreRenderSignal().notify( this, &BasicLightManager::_onPreRender );
|
||||
|
||||
// Last thing... let everyone know we're active.
|
||||
smActivateSignal.trigger( getId(), true );
|
||||
}
|
||||
|
||||
void BasicLightManager::deactivate()
|
||||
{
|
||||
Parent::deactivate();
|
||||
|
||||
mLastShader = NULL;
|
||||
mLastConstants = NULL;
|
||||
|
||||
for (LightConstantMap::Iterator i = mConstantLookup.begin(); i != mConstantLookup.end(); i++)
|
||||
{
|
||||
if (i->value)
|
||||
SAFE_DELETE(i->value);
|
||||
}
|
||||
mConstantLookup.clear();
|
||||
|
||||
if ( mPrePassRenderBin )
|
||||
mPrePassRenderBin->deleteObject();
|
||||
mPrePassRenderBin = NULL;
|
||||
|
||||
GFXShader::removeGlobalMacro( "TORQUE_BASIC_LIGHTING" );
|
||||
|
||||
// Remove us from the prerender signal.
|
||||
getSceneManager()->getPreRenderSignal().remove( this, &BasicLightManager::_onPreRender );
|
||||
|
||||
// Now let everyone know we've deactivated.
|
||||
smActivateSignal.trigger( getId(), false );
|
||||
}
|
||||
|
||||
void BasicLightManager::_onPreRender( SceneManager *sceneManger, const SceneRenderState *state )
|
||||
{
|
||||
// Update all our shadow plugins here!
|
||||
Vector<BasicSceneObjectLightingPlugin*> *pluginInsts = BasicSceneObjectLightingPlugin::getPluginInstances();
|
||||
|
||||
Vector<BasicSceneObjectLightingPlugin*>::const_iterator pluginIter = (*pluginInsts).begin();
|
||||
for ( ; pluginIter != (*pluginInsts).end(); pluginIter++ )
|
||||
{
|
||||
BasicSceneObjectLightingPlugin *plugin = *pluginIter;
|
||||
plugin->updateShadow( (SceneRenderState*)state );
|
||||
}
|
||||
|
||||
U32 pluginCount = (*pluginInsts).size();
|
||||
|
||||
// Sort them by the score.
|
||||
dQsort( (*pluginInsts).address(), pluginCount, sizeof(BasicSceneObjectLightingPlugin*), comparePluginScores );
|
||||
|
||||
mTimer->getElapsedMs();
|
||||
mTimer->reset();
|
||||
U32 numUpdated = 0;
|
||||
U32 targetMs = 5;
|
||||
|
||||
S32 updateMs = 0;
|
||||
|
||||
pluginIter = (*pluginInsts).begin();
|
||||
for ( ; pluginIter != (*pluginInsts).end(); pluginIter++ )
|
||||
{
|
||||
BasicSceneObjectLightingPlugin *plugin = *pluginIter;
|
||||
|
||||
// If we run out of update time then stop.
|
||||
updateMs = mTimer->getElapsedMs();
|
||||
if ( updateMs >= targetMs )
|
||||
break;
|
||||
|
||||
// NOTE! Fix this all up to past const SceneRenderState!
|
||||
plugin->renderShadow( (SceneRenderState*)state );
|
||||
numUpdated++;
|
||||
}
|
||||
|
||||
smShadowsUpdated = numUpdated;
|
||||
smActiveShadowPlugins = pluginCount;
|
||||
smElapsedUpdateMs = updateMs;
|
||||
}
|
||||
|
||||
BasicLightManager::LightingShaderConstants::LightingShaderConstants()
|
||||
: mInit( false ),
|
||||
mShader( NULL ),
|
||||
mLightPosition( NULL ),
|
||||
mLightDiffuse( NULL ),
|
||||
mLightAmbient( NULL ),
|
||||
mLightInvRadiusSq( NULL ),
|
||||
mLightSpotDir( NULL ),
|
||||
mLightSpotAngle( NULL ),
|
||||
mLightSpotFalloff( NULL )
|
||||
{
|
||||
}
|
||||
|
||||
BasicLightManager::LightingShaderConstants::~LightingShaderConstants()
|
||||
{
|
||||
if (mShader.isValid())
|
||||
{
|
||||
mShader->getReloadSignal().remove( this, &LightingShaderConstants::_onShaderReload );
|
||||
mShader = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void BasicLightManager::LightingShaderConstants::init(GFXShader* shader)
|
||||
{
|
||||
if (mShader.getPointer() != shader)
|
||||
{
|
||||
if (mShader.isValid())
|
||||
mShader->getReloadSignal().remove( this, &LightingShaderConstants::_onShaderReload );
|
||||
|
||||
mShader = shader;
|
||||
mShader->getReloadSignal().notify( this, &LightingShaderConstants::_onShaderReload );
|
||||
}
|
||||
|
||||
mLightPosition = shader->getShaderConstHandle( ShaderGenVars::lightPosition );
|
||||
mLightDiffuse = shader->getShaderConstHandle( ShaderGenVars::lightDiffuse);
|
||||
mLightInvRadiusSq = shader->getShaderConstHandle( ShaderGenVars::lightInvRadiusSq );
|
||||
mLightAmbient = shader->getShaderConstHandle( ShaderGenVars::lightAmbient );
|
||||
mLightSpotDir = shader->getShaderConstHandle( ShaderGenVars::lightSpotDir );
|
||||
mLightSpotAngle = shader->getShaderConstHandle( ShaderGenVars::lightSpotAngle );
|
||||
mLightSpotFalloff = shader->getShaderConstHandle( ShaderGenVars::lightSpotFalloff );
|
||||
|
||||
mInit = true;
|
||||
}
|
||||
|
||||
void BasicLightManager::LightingShaderConstants::_onShaderReload()
|
||||
{
|
||||
if (mShader.isValid())
|
||||
init( mShader );
|
||||
}
|
||||
|
||||
void BasicLightManager::setLightInfo( ProcessedMaterial* pmat,
|
||||
const Material* mat,
|
||||
const SceneData& sgData,
|
||||
const SceneRenderState *state,
|
||||
U32 pass,
|
||||
GFXShaderConstBuffer* shaderConsts )
|
||||
{
|
||||
PROFILE_SCOPE( BasicLightManager_SetLightInfo );
|
||||
|
||||
GFXShader *shader = shaderConsts->getShader();
|
||||
|
||||
// Check to see if this is the same shader. Since we
|
||||
// sort by material we should get hit repeatedly by the
|
||||
// same one. This optimization should save us many
|
||||
// hash table lookups.
|
||||
if ( mLastShader.getPointer() != shader )
|
||||
{
|
||||
LightConstantMap::Iterator iter = mConstantLookup.find(shader);
|
||||
if ( iter != mConstantLookup.end() )
|
||||
{
|
||||
mLastConstants = iter->value;
|
||||
}
|
||||
else
|
||||
{
|
||||
LightingShaderConstants* lsc = new LightingShaderConstants();
|
||||
mConstantLookup[shader] = lsc;
|
||||
|
||||
mLastConstants = lsc;
|
||||
}
|
||||
|
||||
// Set our new shader
|
||||
mLastShader = shader;
|
||||
}
|
||||
|
||||
// Make sure that our current lighting constants are initialized
|
||||
if (!mLastConstants->mInit)
|
||||
mLastConstants->init(shader);
|
||||
|
||||
// NOTE: If you encounter a crash from this point forward
|
||||
// while setting a shader constant its probably because the
|
||||
// mConstantLookup has bad shaders/constants in it.
|
||||
//
|
||||
// This is a known crash bug that can occur if materials/shaders
|
||||
// are reloaded and the light manager is not reset.
|
||||
//
|
||||
// We should look to fix this by clearing the table.
|
||||
|
||||
_update4LightConsts( sgData,
|
||||
mLastConstants->mLightPosition,
|
||||
mLastConstants->mLightDiffuse,
|
||||
mLastConstants->mLightAmbient,
|
||||
mLastConstants->mLightInvRadiusSq,
|
||||
mLastConstants->mLightSpotDir,
|
||||
mLastConstants->mLightSpotAngle,
|
||||
mLastConstants->mLightSpotFalloff,
|
||||
shaderConsts );
|
||||
}
|
||||
134
Engine/source/lighting/basic/basicLightManager.h
Normal file
134
Engine/source/lighting/basic/basicLightManager.h
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _BASICLIGHTMANAGER_H_
|
||||
#define _BASICLIGHTMANAGER_H_
|
||||
|
||||
#ifndef _LIGHTMANAGER_H_
|
||||
#include "lighting/lightManager.h"
|
||||
#endif
|
||||
#ifndef _TDICTIONARY_H_
|
||||
#include "core/util/tDictionary.h"
|
||||
#endif
|
||||
#ifndef _GFXSHADER_H_
|
||||
#include "gfx/gfxShader.h"
|
||||
#endif
|
||||
#ifndef _SIMOBJECT_H_
|
||||
#include "console/simObject.h"
|
||||
#endif
|
||||
#ifndef _TSINGLETON_H_
|
||||
#include "core/util/tSingleton.h"
|
||||
#endif
|
||||
|
||||
class AvailableSLInterfaces;
|
||||
class GFXShaderConstHandle;
|
||||
class RenderPrePassMgr;
|
||||
class PlatformTimer;
|
||||
|
||||
class blInteriorSystem;
|
||||
class blTerrainSystem;
|
||||
|
||||
class BasicLightManager : public LightManager
|
||||
{
|
||||
typedef LightManager Parent;
|
||||
|
||||
// For access to protected constructor.
|
||||
friend class ManagedSingleton<BasicLightManager>;
|
||||
|
||||
public:
|
||||
|
||||
// LightManager
|
||||
virtual bool isCompatible() const;
|
||||
virtual void activate( SceneManager *sceneManager );
|
||||
virtual void deactivate();
|
||||
virtual void setLightInfo(ProcessedMaterial* pmat, const Material* mat, const SceneData& sgData, const SceneRenderState *state, U32 pass, GFXShaderConstBuffer* shaderConsts);
|
||||
virtual bool setTextureStage(const SceneData& sgData, const U32 currTexFlag, const U32 textureSlot, GFXShaderConstBuffer* shaderConsts, ShaderConstHandles* handles) { return false; }
|
||||
|
||||
static F32 getShadowFilterDistance() { return smProjectedShadowFilterDistance; }
|
||||
|
||||
protected:
|
||||
|
||||
// LightManager
|
||||
virtual void _addLightInfoEx( LightInfo *lightInfo ) { }
|
||||
virtual void _initLightFields() { }
|
||||
|
||||
void _onPreRender( SceneManager *sceneManger, const SceneRenderState *state );
|
||||
|
||||
// These are protected because we're a singleton and
|
||||
// no one else should be creating us!
|
||||
BasicLightManager();
|
||||
virtual ~BasicLightManager();
|
||||
|
||||
SimObjectPtr<RenderPrePassMgr> mPrePassRenderBin;
|
||||
|
||||
struct LightingShaderConstants
|
||||
{
|
||||
bool mInit;
|
||||
|
||||
GFXShaderRef mShader;
|
||||
|
||||
GFXShaderConstHandle *mLightPosition;
|
||||
GFXShaderConstHandle *mLightDiffuse;
|
||||
GFXShaderConstHandle *mLightAmbient;
|
||||
GFXShaderConstHandle *mLightInvRadiusSq;
|
||||
GFXShaderConstHandle *mLightSpotDir;
|
||||
GFXShaderConstHandle *mLightSpotAngle;
|
||||
GFXShaderConstHandle *mLightSpotFalloff;
|
||||
|
||||
LightingShaderConstants();
|
||||
~LightingShaderConstants();
|
||||
|
||||
void init( GFXShader *shader );
|
||||
|
||||
void _onShaderReload();
|
||||
};
|
||||
|
||||
typedef Map<GFXShader*, LightingShaderConstants*> LightConstantMap;
|
||||
|
||||
LightConstantMap mConstantLookup;
|
||||
GFXShaderRef mLastShader;
|
||||
LightingShaderConstants* mLastConstants;
|
||||
|
||||
/// Statics used for light manager/projected shadow metrics.
|
||||
static U32 smActiveShadowPlugins;
|
||||
static U32 smShadowsUpdated;
|
||||
static U32 smElapsedUpdateMs;
|
||||
|
||||
/// This is used to determine the distance
|
||||
/// at which the shadow filtering PostEffect
|
||||
/// will be enabled for ProjectedShadow.
|
||||
static F32 smProjectedShadowFilterDistance;
|
||||
|
||||
/// A timer used for tracking update time.
|
||||
PlatformTimer *mTimer;
|
||||
|
||||
blInteriorSystem* mInteriorSystem;
|
||||
blTerrainSystem* mTerrainSystem;
|
||||
|
||||
public:
|
||||
// For ManagedSingleton.
|
||||
static const char* getSingletonName() { return "BasicLightManager"; }
|
||||
};
|
||||
|
||||
#define BLM ManagedSingleton<BasicLightManager>::instance()
|
||||
|
||||
#endif // _BASICLIGHTMANAGER_H_
|
||||
230
Engine/source/lighting/basic/basicSceneObjectLightingPlugin.cpp
Normal file
230
Engine/source/lighting/basic/basicSceneObjectLightingPlugin.cpp
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "lighting/basic/basicSceneObjectLightingPlugin.h"
|
||||
|
||||
#include "lighting/lightManager.h"
|
||||
#include "lighting/shadowMap/shadowMapPass.h"
|
||||
#include "lighting/shadowMap/shadowMapManager.h"
|
||||
#include "lighting/common/projectedShadow.h"
|
||||
#include "T3D/shapeBase.h"
|
||||
#include "gfx/gfxDevice.h"
|
||||
#include "gfx/gfxTransformSaver.h"
|
||||
#include "ts/tsRenderState.h"
|
||||
#include "gfx/sim/cubemapData.h"
|
||||
#include "scene/reflector.h"
|
||||
#include "T3D/decal/decalManager.h"
|
||||
#include "core/module.h"
|
||||
|
||||
|
||||
MODULE_BEGIN( BasicSceneObjectLightingPlugin )
|
||||
|
||||
MODULE_INIT
|
||||
{
|
||||
BasicSceneObjectPluginFactory::createSingleton();
|
||||
}
|
||||
|
||||
MODULE_SHUTDOWN
|
||||
{
|
||||
BasicSceneObjectPluginFactory::deleteSingleton();
|
||||
}
|
||||
|
||||
MODULE_END;
|
||||
|
||||
|
||||
static const U32 shadowObjectTypeMask = PlayerObjectType | CorpseObjectType | ItemObjectType | VehicleObjectType;
|
||||
Vector<BasicSceneObjectLightingPlugin*> BasicSceneObjectLightingPlugin::smPluginInstances( __FILE__, __LINE__ );
|
||||
|
||||
BasicSceneObjectLightingPlugin::BasicSceneObjectLightingPlugin(SceneObject* parent)
|
||||
: mParentObject( parent )
|
||||
{
|
||||
mShadow = NULL;
|
||||
|
||||
// Stick us on the list.
|
||||
smPluginInstances.push_back( this );
|
||||
}
|
||||
|
||||
BasicSceneObjectLightingPlugin::~BasicSceneObjectLightingPlugin()
|
||||
{
|
||||
SAFE_DELETE( mShadow );
|
||||
|
||||
// Delete us from the list.
|
||||
smPluginInstances.remove( this );
|
||||
}
|
||||
|
||||
void BasicSceneObjectLightingPlugin::reset()
|
||||
{
|
||||
SAFE_DELETE( mShadow );
|
||||
}
|
||||
|
||||
void BasicSceneObjectLightingPlugin::cleanupPluginInstances()
|
||||
{
|
||||
for (U32 i = 0; i < smPluginInstances.size(); i++)
|
||||
{
|
||||
BasicSceneObjectLightingPlugin *plug = smPluginInstances[i];
|
||||
smPluginInstances.remove( plug );
|
||||
delete plug;
|
||||
i--;
|
||||
}
|
||||
|
||||
smPluginInstances.clear();
|
||||
}
|
||||
|
||||
void BasicSceneObjectLightingPlugin::resetAll()
|
||||
{
|
||||
for( U32 i = 0, num = smPluginInstances.size(); i < num; ++ i )
|
||||
smPluginInstances[ i ]->reset();
|
||||
}
|
||||
|
||||
const F32 BasicSceneObjectLightingPlugin::getScore() const
|
||||
{
|
||||
return mShadow ? mShadow->getScore() : 0.0f;
|
||||
}
|
||||
|
||||
void BasicSceneObjectLightingPlugin::updateShadow( SceneRenderState *state )
|
||||
{
|
||||
if ( !mShadow )
|
||||
mShadow = new ProjectedShadow( mParentObject );
|
||||
|
||||
mShadow->update( state );
|
||||
}
|
||||
|
||||
void BasicSceneObjectLightingPlugin::renderShadow( SceneRenderState *state )
|
||||
{
|
||||
// hack until new scenegraph in place
|
||||
GFXTransformSaver ts;
|
||||
|
||||
TSRenderState rstate;
|
||||
rstate.setSceneState(state);
|
||||
|
||||
F32 camDist = (state->getCameraPosition() - mParentObject->getRenderPosition()).len();
|
||||
|
||||
// Make sure the shadow wants to be rendered
|
||||
if( mShadow->shouldRender( state ) )
|
||||
{
|
||||
// Render! (and note the time)
|
||||
mShadow->render( camDist, rstate );
|
||||
}
|
||||
}
|
||||
|
||||
BasicSceneObjectPluginFactory::BasicSceneObjectPluginFactory()
|
||||
: mEnabled( false )
|
||||
{
|
||||
LightManager::smActivateSignal.notify( this, &BasicSceneObjectPluginFactory::_onLMActivate );
|
||||
|
||||
ShadowMapManager::smShadowDeactivateSignal.notify( this, &BasicSceneObjectPluginFactory::_setEnabled );
|
||||
}
|
||||
|
||||
BasicSceneObjectPluginFactory::~BasicSceneObjectPluginFactory()
|
||||
{
|
||||
LightManager::smActivateSignal.remove( this, &BasicSceneObjectPluginFactory::_onLMActivate );
|
||||
|
||||
ShadowMapManager::smShadowDeactivateSignal.remove( this, &BasicSceneObjectPluginFactory::_setEnabled );
|
||||
}
|
||||
|
||||
void BasicSceneObjectPluginFactory::_onLMActivate( const char *lm, bool enable )
|
||||
{
|
||||
_setEnabled();
|
||||
}
|
||||
|
||||
void BasicSceneObjectPluginFactory::_setEnabled()
|
||||
{
|
||||
bool enable = false;
|
||||
|
||||
// Enabled if using basic lighting.
|
||||
LightManager *lm = LightManager::getActiveLM();
|
||||
if ( lm && dStricmp( lm->getName(), "Basic Lighting" ) == 0 )
|
||||
enable = true;
|
||||
|
||||
// Disabled if all shadows are explictly disabled.
|
||||
if ( ShadowMapPass::smDisableShadows )
|
||||
enable = false;
|
||||
|
||||
// Already at the desired state.
|
||||
if ( enable == mEnabled )
|
||||
return;
|
||||
|
||||
if ( enable )
|
||||
{
|
||||
SceneObject::smSceneObjectAdd.notify(this, &BasicSceneObjectPluginFactory::addLightPlugin);
|
||||
SceneObject::smSceneObjectRemove.notify(this, &BasicSceneObjectPluginFactory::removeLightPlugin);
|
||||
|
||||
if( gDecalManager )
|
||||
gDecalManager->getClearDataSignal().notify( this, &BasicSceneObjectPluginFactory::_onDecalManagerClear );
|
||||
|
||||
addToExistingObjects();
|
||||
}
|
||||
else
|
||||
{
|
||||
SceneObject::smSceneObjectAdd.remove(this, &BasicSceneObjectPluginFactory::addLightPlugin);
|
||||
SceneObject::smSceneObjectRemove.remove(this, &BasicSceneObjectPluginFactory::removeLightPlugin);
|
||||
|
||||
if( gDecalManager )
|
||||
gDecalManager->getClearDataSignal().remove( this, &BasicSceneObjectPluginFactory::_onDecalManagerClear );
|
||||
|
||||
BasicSceneObjectLightingPlugin::cleanupPluginInstances();
|
||||
}
|
||||
|
||||
mEnabled = enable;
|
||||
}
|
||||
|
||||
void BasicSceneObjectPluginFactory::_onDecalManagerClear()
|
||||
{
|
||||
BasicSceneObjectLightingPlugin::resetAll();
|
||||
}
|
||||
|
||||
void BasicSceneObjectPluginFactory::removeLightPlugin( SceneObject *obj )
|
||||
{
|
||||
// Grab the plugin instance.
|
||||
SceneObjectLightingPlugin *lightPlugin = obj->getLightingPlugin();
|
||||
|
||||
// Delete it, which will also remove it
|
||||
// from the static list of plugin instances.
|
||||
if ( lightPlugin )
|
||||
{
|
||||
delete lightPlugin;
|
||||
obj->setLightingPlugin( NULL );
|
||||
}
|
||||
}
|
||||
|
||||
void BasicSceneObjectPluginFactory::addLightPlugin(SceneObject* obj)
|
||||
{
|
||||
bool serverObj = obj->isServerObject();
|
||||
bool isShadowType = (obj->getTypeMask() & shadowObjectTypeMask);
|
||||
|
||||
if ( !isShadowType || serverObj )
|
||||
return;
|
||||
|
||||
obj->setLightingPlugin(new BasicSceneObjectLightingPlugin(obj));
|
||||
}
|
||||
|
||||
// Some objects may not get cleaned up during mission load/free, so add our
|
||||
// plugin to existing scene objects
|
||||
void BasicSceneObjectPluginFactory::addToExistingObjects()
|
||||
{
|
||||
SimpleQueryList sql;
|
||||
gClientContainer.findObjects( shadowObjectTypeMask, SimpleQueryList::insertionCallback, &sql);
|
||||
for (SceneObject** i = sql.mList.begin(); i != sql.mList.end(); i++)
|
||||
addLightPlugin(*i);
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _BASICSCENEOBJECTLIGHTINGPLUGIN_H_
|
||||
#define _BASICSCENEOBJECTLIGHTINGPLUGIN_H_
|
||||
|
||||
#ifndef _SCENEOBJECTLIGHTINGPLUGIN_H_
|
||||
#include "scene/sceneObjectLightingPlugin.h"
|
||||
#endif
|
||||
|
||||
#ifndef _SCENEOBJECT_H_
|
||||
#include "scene/sceneObject.h"
|
||||
#endif
|
||||
|
||||
#ifndef _TSINGLETON_H_
|
||||
#include "core/util/tSingleton.h"
|
||||
#endif
|
||||
|
||||
|
||||
class ShadowBase;
|
||||
|
||||
class BasicSceneObjectLightingPlugin : public SceneObjectLightingPlugin
|
||||
{
|
||||
private:
|
||||
|
||||
ShadowBase* mShadow;
|
||||
SceneObject* mParentObject;
|
||||
|
||||
static Vector<BasicSceneObjectLightingPlugin*> smPluginInstances;
|
||||
|
||||
public:
|
||||
BasicSceneObjectLightingPlugin(SceneObject* parent);
|
||||
~BasicSceneObjectLightingPlugin();
|
||||
|
||||
static Vector<BasicSceneObjectLightingPlugin*>* getPluginInstances() { return &smPluginInstances; }
|
||||
|
||||
static void cleanupPluginInstances();
|
||||
static void resetAll();
|
||||
|
||||
const F32 getScore() const;
|
||||
|
||||
// Called from BasicLightManager
|
||||
virtual void updateShadow( SceneRenderState *state );
|
||||
virtual void renderShadow( SceneRenderState *state );
|
||||
|
||||
// Called by statics
|
||||
virtual U32 packUpdate(SceneObject* obj, U32 checkMask, NetConnection *conn, U32 mask, BitStream *stream) { return 0; }
|
||||
virtual void unpackUpdate(SceneObject* obj, NetConnection *conn, BitStream *stream) { }
|
||||
|
||||
virtual void reset();
|
||||
};
|
||||
|
||||
class BasicSceneObjectPluginFactory : public ManagedSingleton< BasicSceneObjectPluginFactory >
|
||||
{
|
||||
protected:
|
||||
|
||||
/// Called from the light manager on activation.
|
||||
/// @see LightManager::addActivateCallback
|
||||
void _onLMActivate( const char *lm, bool enable );
|
||||
|
||||
void _onDecalManagerClear();
|
||||
|
||||
void removeLightPlugin(SceneObject* obj);
|
||||
void addLightPlugin(SceneObject* obj);
|
||||
void addToExistingObjects();
|
||||
|
||||
bool mEnabled;
|
||||
|
||||
public:
|
||||
|
||||
BasicSceneObjectPluginFactory();
|
||||
~BasicSceneObjectPluginFactory();
|
||||
|
||||
// For ManagedSingleton.
|
||||
static const char* getSingletonName() { return "BasicSceneObjectPluginFactory"; }
|
||||
|
||||
void _setEnabled();
|
||||
};
|
||||
|
||||
#endif // !_BASICSCENEOBJECTLIGHTINGPLUGIN_H_
|
||||
1472
Engine/source/lighting/basic/blInteriorSystem.cpp
Normal file
1472
Engine/source/lighting/basic/blInteriorSystem.cpp
Normal file
File diff suppressed because it is too large
Load diff
57
Engine/source/lighting/basic/blInteriorSystem.h
Normal file
57
Engine/source/lighting/basic/blInteriorSystem.h
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _BLINTERIOSYSTEM_H_
|
||||
#define _BLINTERIOSYSTEM_H_
|
||||
|
||||
#ifndef _SCENELIGHTING_H_
|
||||
#include "lighting/common/sceneLighting.h"
|
||||
#endif
|
||||
#ifndef _SG_SYSTEM_INTERFACE_H
|
||||
#include "lighting/lightingInterfaces.h"
|
||||
#endif
|
||||
|
||||
|
||||
//
|
||||
// Lighting system interface
|
||||
//
|
||||
class blInteriorSystem : public SceneLightingInterface
|
||||
{
|
||||
public:
|
||||
static bool smUseVertexLighting;
|
||||
|
||||
virtual SceneLighting::ObjectProxy* createObjectProxy(SceneObject* obj, SceneLighting::ObjectProxyList* sceneObjects);
|
||||
virtual PersistInfo::PersistChunk* createPersistChunk(const U32 chunkType);
|
||||
virtual bool createPersistChunkFromProxy(SceneLighting::ObjectProxy* objproxy, PersistInfo::PersistChunk **ret);
|
||||
|
||||
virtual void init();
|
||||
virtual U32 addObjectType();
|
||||
virtual U32 addToClippingMask();
|
||||
|
||||
virtual void processLightingBegin();
|
||||
virtual void processLightingCompleted(bool success);
|
||||
|
||||
// Given a ray, this will return the color from the lightmap of this object, return true if handled
|
||||
virtual bool getColorFromRayInfo(RayInfo collision, ColorF& result);
|
||||
};
|
||||
|
||||
#endif // !_BLINTERIOSYSTEM_H_
|
||||
715
Engine/source/lighting/basic/blTerrainSystem.cpp
Normal file
715
Engine/source/lighting/basic/blTerrainSystem.cpp
Normal file
|
|
@ -0,0 +1,715 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "lighting/basic/blTerrainSystem.h"
|
||||
|
||||
#include "core/bitVector.h"
|
||||
#include "lighting/common/shadowVolumeBSP.h"
|
||||
#include "lighting/lightingInterfaces.h"
|
||||
#include "terrain/terrData.h"
|
||||
#include "lighting/basic/basicLightManager.h"
|
||||
#include "lighting/common/sceneLighting.h"
|
||||
#include "gfx/bitmap/gBitmap.h"
|
||||
#include "collision/collision.h"
|
||||
|
||||
extern SceneLighting* gLighting;
|
||||
|
||||
|
||||
struct blTerrainChunk : public PersistInfo::PersistChunk
|
||||
{
|
||||
typedef PersistInfo::PersistChunk Parent;
|
||||
|
||||
blTerrainChunk();
|
||||
~blTerrainChunk();
|
||||
|
||||
GBitmap *mLightmap;
|
||||
|
||||
bool read(Stream &);
|
||||
bool write(Stream &);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Class SceneLighting::TerrainChunk
|
||||
//------------------------------------------------------------------------------
|
||||
blTerrainChunk::blTerrainChunk()
|
||||
{
|
||||
mChunkType = PersistChunk::TerrainChunkType;
|
||||
mLightmap = NULL;
|
||||
}
|
||||
|
||||
blTerrainChunk::~blTerrainChunk()
|
||||
{
|
||||
if(mLightmap)
|
||||
delete mLightmap;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
bool blTerrainChunk::read(Stream & stream)
|
||||
{
|
||||
if(!Parent::read(stream))
|
||||
return(false);
|
||||
|
||||
mLightmap = new GBitmap();
|
||||
return mLightmap->readBitmap("png",stream);
|
||||
}
|
||||
|
||||
bool blTerrainChunk::write(Stream & stream)
|
||||
{
|
||||
if(!Parent::write(stream))
|
||||
return(false);
|
||||
|
||||
if(!mLightmap)
|
||||
return(false);
|
||||
|
||||
if(!mLightmap->writeBitmap("png",stream))
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
class blTerrainProxy : public SceneLighting::ObjectProxy
|
||||
{
|
||||
protected:
|
||||
|
||||
typedef ObjectProxy Parent;
|
||||
|
||||
BitVector mShadowMask;
|
||||
ShadowVolumeBSP * mShadowVolume;
|
||||
ColorF * mLightmap;
|
||||
|
||||
/// The dimension of the lightmap in pixels.
|
||||
const U32 mLightMapSize;
|
||||
|
||||
/// The dimension of the terrain height map sample array.
|
||||
const U32 mTerrainBlockSize;
|
||||
|
||||
ColorF *sgBakedLightmap;
|
||||
Vector<LightInfo *> sgLights;
|
||||
bool sgMarkStaticShadow(void *terrainproxy, SceneObject *sceneobject, LightInfo *light);
|
||||
//void postLight(bool lastLight);
|
||||
|
||||
void lightVector(LightInfo *);
|
||||
|
||||
struct SquareStackNode
|
||||
{
|
||||
U8 mLevel;
|
||||
U16 mClipFlags;
|
||||
Point2I mPos;
|
||||
};
|
||||
|
||||
S32 testSquare(const Point3F &, const Point3F &, S32, F32, const Vector<PlaneF> &);
|
||||
bool markObjectShadow(ObjectProxy *);
|
||||
bool sgIsCorrectStaticObjectType(SceneObject *obj);
|
||||
|
||||
inline ColorF _getValue( S32 row, S32 column );
|
||||
|
||||
public:
|
||||
|
||||
blTerrainProxy(SceneObject * obj);
|
||||
~blTerrainProxy();
|
||||
TerrainBlock * operator->() {return(static_cast<TerrainBlock*>(static_cast<SceneObject*>(mObj)));}
|
||||
TerrainBlock * getObject() {return(static_cast<TerrainBlock*>(static_cast<SceneObject*>(mObj)));}
|
||||
|
||||
bool getShadowedSquares(const Vector<PlaneF> &, Vector<U16> &);
|
||||
|
||||
// lighting
|
||||
void init();
|
||||
bool preLight(LightInfo *);
|
||||
void light(LightInfo *);
|
||||
|
||||
// persist
|
||||
U32 getResourceCRC();
|
||||
bool setPersistInfo(PersistInfo::PersistChunk *);
|
||||
bool getPersistInfo(PersistInfo::PersistChunk *);
|
||||
|
||||
virtual bool supportsShadowVolume();
|
||||
virtual void getClipPlanes(Vector<PlaneF>& planes);
|
||||
virtual void addToShadowVolume(ShadowVolumeBSP * shadowVolume, LightInfo * light, S32 level);
|
||||
|
||||
// events
|
||||
//virtual void processTGELightProcessEvent(U32 curr, U32 max, LightInfo* currlight);
|
||||
//virtual void processSGObjectProcessEvent(LightInfo* currLight);
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
// Class SceneLighting::TerrainProxy:
|
||||
//-------------------------------------------------------------------------------
|
||||
blTerrainProxy::blTerrainProxy( SceneObject *obj ) :
|
||||
Parent( obj ),
|
||||
mLightMapSize( getObject()->getLightMapSize() ),
|
||||
mTerrainBlockSize( getObject()->getBlockSize() ),
|
||||
mLightmap( NULL )
|
||||
{
|
||||
}
|
||||
|
||||
blTerrainProxy::~blTerrainProxy()
|
||||
{
|
||||
delete [] mLightmap;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
void blTerrainProxy::init()
|
||||
{
|
||||
mLightmap = new ColorF[ mLightMapSize * mLightMapSize ];
|
||||
dMemset(mLightmap, 0, mLightMapSize * mLightMapSize * sizeof(ColorF));
|
||||
mShadowMask.setSize( mTerrainBlockSize * mTerrainBlockSize );
|
||||
}
|
||||
|
||||
bool blTerrainProxy::preLight(LightInfo * light)
|
||||
{
|
||||
if(!bool(mObj))
|
||||
return(false);
|
||||
|
||||
if(light->getType() != LightInfo::Vector)
|
||||
return(false);
|
||||
|
||||
mShadowMask.clear();
|
||||
return(true);
|
||||
}
|
||||
|
||||
inline ColorF blTerrainProxy::_getValue( S32 row, S32 column )
|
||||
{
|
||||
while( row < 0 )
|
||||
row += mLightMapSize;
|
||||
row = row % mLightMapSize;
|
||||
|
||||
while( column < 0 )
|
||||
column += mLightMapSize;
|
||||
column = column % mLightMapSize;
|
||||
|
||||
U32 offset = row * mLightMapSize + column;
|
||||
|
||||
return mLightmap[offset];
|
||||
}
|
||||
|
||||
bool blTerrainProxy::markObjectShadow(ObjectProxy * proxy)
|
||||
{
|
||||
if (!proxy->supportsShadowVolume())
|
||||
return false;
|
||||
|
||||
// setup the clip planes
|
||||
Vector<PlaneF> clipPlanes;
|
||||
proxy->getClipPlanes(clipPlanes);
|
||||
|
||||
Vector<U16> shadowList;
|
||||
if(!getShadowedSquares(clipPlanes, shadowList))
|
||||
return(false);
|
||||
|
||||
// set the correct bit
|
||||
for(U32 i = 0; i < shadowList.size(); i++)
|
||||
mShadowMask.set(shadowList[i]);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void blTerrainProxy::light(LightInfo * light)
|
||||
{
|
||||
// If we don't have terrain or its not a directional
|
||||
// light then skip processing.
|
||||
TerrainBlock * terrain = getObject();
|
||||
if ( !terrain || light->getType() != LightInfo::Vector )
|
||||
return;
|
||||
|
||||
S32 time = Platform::getRealMilliseconds();
|
||||
|
||||
// reset
|
||||
mShadowVolume = new ShadowVolumeBSP;
|
||||
|
||||
// build interior shadow volume
|
||||
for(ObjectProxy ** itr = gLighting->mLitObjects.begin(); itr != gLighting->mLitObjects.end(); itr++)
|
||||
{
|
||||
ObjectProxy* objproxy = *itr;
|
||||
if (markObjectShadow(objproxy))
|
||||
objproxy->addToShadowVolume(mShadowVolume, light, SceneLighting::SHADOW_DETAIL);
|
||||
}
|
||||
|
||||
lightVector(light);
|
||||
|
||||
// set the lightmap...
|
||||
terrain->clearLightMap();
|
||||
|
||||
// Blur...
|
||||
F32 kernel[3][3] = { {1, 2, 1},
|
||||
{2, 3, 2},
|
||||
{1, 2, 1} };
|
||||
|
||||
F32 modifier = 1;
|
||||
F32 divisor = 0;
|
||||
|
||||
|
||||
for( U32 i=0; i<3; i++ )
|
||||
{
|
||||
for( U32 j=0; j<3; j++ )
|
||||
{
|
||||
if( i==1 && j==1 )
|
||||
{
|
||||
kernel[i][j] = 1 + kernel[i][j] * modifier;
|
||||
}
|
||||
else
|
||||
{
|
||||
kernel[i][j] = kernel[i][j] * modifier;
|
||||
}
|
||||
|
||||
divisor += kernel[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
for( U32 i=0; i < mLightMapSize; i++ )
|
||||
{
|
||||
for( U32 j=0; j < mLightMapSize; j++ )
|
||||
{
|
||||
|
||||
ColorF val;
|
||||
val = _getValue( i-1, j-1 ) * kernel[0][0];
|
||||
val += _getValue( i-1, j ) * kernel[0][1];
|
||||
val += _getValue( i-1, j+1 ) * kernel[0][2];
|
||||
val += _getValue( i, j-1 ) * kernel[1][0];
|
||||
val += _getValue( i, j ) * kernel[1][1];
|
||||
val += _getValue( i, j+1 ) * kernel[1][2];
|
||||
val += _getValue( i+1, j-1 ) * kernel[2][0];
|
||||
val += _getValue( i+1, j ) * kernel[2][1];
|
||||
val += _getValue( i+1, j+1 ) * kernel[2][2];
|
||||
|
||||
U32 edge = 0;
|
||||
|
||||
if( j == 0 || j == mLightMapSize - 1 )
|
||||
edge++;
|
||||
|
||||
if( i == 0 || i == mLightMapSize - 1 )
|
||||
edge++;
|
||||
|
||||
if( !edge )
|
||||
val = val / divisor;
|
||||
else
|
||||
val = mLightmap[ i * mLightMapSize + j ];
|
||||
|
||||
// clamp values
|
||||
mLightmap[ i * mLightMapSize + j ]= val;
|
||||
}
|
||||
}
|
||||
|
||||
// And stuff it into the texture...
|
||||
GBitmap *terrLightMap = terrain->getLightMap();
|
||||
for(U32 y = 0; y < mLightMapSize; y++)
|
||||
{
|
||||
for(U32 x = 0; x < mLightMapSize; x++)
|
||||
{
|
||||
ColorI color(255, 255, 255, 255);
|
||||
|
||||
color.red = mLightmap[x + y * mLightMapSize].red * 255;
|
||||
color.green = mLightmap[x + y * mLightMapSize].green * 255;
|
||||
color.blue = mLightmap[x + y * mLightMapSize].blue * 255;
|
||||
|
||||
terrLightMap->setColor(x, y, color);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// This handles matching up the outer edges of the terrain
|
||||
// lightmap when it has neighbors
|
||||
if (!terrain->isTiling())
|
||||
{
|
||||
for (S32 y = 0; y < terrLightMap->getHeight(); y++)
|
||||
{
|
||||
ColorI c;
|
||||
if (terrain->getFile()->mEdgeTerrainFiles[0])
|
||||
{
|
||||
terrLightMap->getColor(terrLightMap->getWidth()-1,y,c);
|
||||
terrLightMap->setColor(0,y,c);
|
||||
terrLightMap->setColor(1,y,c);
|
||||
}
|
||||
else
|
||||
{
|
||||
terrLightMap->getColor(0,y,c);
|
||||
terrLightMap->setColor(terrLightMap->getWidth()-1,y,c);
|
||||
terrLightMap->setColor(terrLightMap->getWidth()-2,y,c);
|
||||
}
|
||||
}
|
||||
|
||||
for (S32 x = 0; x < terrLightMap->getHeight(); x++)
|
||||
{
|
||||
ColorI c;
|
||||
if (terrain->getFile()->mEdgeTerrainFiles[1])
|
||||
{
|
||||
terrLightMap->getColor(x,terrLightMap->getHeight()-1,c);
|
||||
terrLightMap->setColor(x,0,c);
|
||||
terrLightMap->setColor(x,1,c);
|
||||
}
|
||||
else
|
||||
{
|
||||
terrLightMap->getColor(x,0,c);
|
||||
terrLightMap->setColor(x,terrLightMap->getHeight()-1,c);
|
||||
terrLightMap->setColor(x,terrLightMap->getHeight()-2,c);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
delete mShadowVolume;
|
||||
|
||||
Con::printf(" = terrain lit in %3.3f seconds", (Platform::getRealMilliseconds()-time)/1000.f);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
S32 blTerrainProxy::testSquare(const Point3F & min, const Point3F & max, S32 mask, F32 expand, const Vector<PlaneF> & clipPlanes)
|
||||
{
|
||||
expand = 0;
|
||||
S32 retMask = 0;
|
||||
Point3F minPoint, maxPoint;
|
||||
for(S32 i = 0; i < clipPlanes.size(); i++)
|
||||
{
|
||||
if(mask & (1 << i))
|
||||
{
|
||||
if(clipPlanes[i].x > 0)
|
||||
{
|
||||
maxPoint.x = max.x;
|
||||
minPoint.x = min.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxPoint.x = min.x;
|
||||
minPoint.x = max.x;
|
||||
}
|
||||
if(clipPlanes[i].y > 0)
|
||||
{
|
||||
maxPoint.y = max.y;
|
||||
minPoint.y = min.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxPoint.y = min.y;
|
||||
minPoint.y = max.y;
|
||||
}
|
||||
if(clipPlanes[i].z > 0)
|
||||
{
|
||||
maxPoint.z = max.z;
|
||||
minPoint.z = min.z;
|
||||
}
|
||||
else
|
||||
{
|
||||
maxPoint.z = min.z;
|
||||
minPoint.z = max.z;
|
||||
}
|
||||
F32 maxDot = mDot(maxPoint, clipPlanes[i]);
|
||||
F32 minDot = mDot(minPoint, clipPlanes[i]);
|
||||
F32 planeD = clipPlanes[i].d;
|
||||
if(maxDot <= -(planeD + expand))
|
||||
return(U16(-1));
|
||||
if(minDot <= -planeD)
|
||||
retMask |= (1 << i);
|
||||
}
|
||||
}
|
||||
return(retMask);
|
||||
}
|
||||
|
||||
bool blTerrainProxy::getShadowedSquares(const Vector<PlaneF> & clipPlanes, Vector<U16> & shadowList)
|
||||
{
|
||||
TerrainBlock *terrain = getObject();
|
||||
if ( !terrain )
|
||||
return false;
|
||||
|
||||
// TODO: Fix me for variable terrain sizes!
|
||||
return true;
|
||||
|
||||
/*
|
||||
SquareStackNode stack[TerrainBlock::BlockShift * 4];
|
||||
|
||||
stack[0].mLevel = TerrainBlock::BlockShift;
|
||||
stack[0].mClipFlags = 0xff;
|
||||
stack[0].mPos.set(0,0);
|
||||
|
||||
U32 stackSize = 1;
|
||||
|
||||
Point3F blockPos;
|
||||
terrain->getTransform().getColumn(3, &blockPos);
|
||||
S32 squareSize = terrain->getSquareSize();
|
||||
F32 floatSquareSize = (F32)squareSize;
|
||||
|
||||
bool marked = false;
|
||||
|
||||
// push through all the levels of the quadtree
|
||||
while(stackSize)
|
||||
{
|
||||
SquareStackNode * node = &stack[stackSize - 1];
|
||||
|
||||
S32 clipFlags = node->mClipFlags;
|
||||
Point2I pos = node->mPos;
|
||||
GridSquare * sq = terrain->findSquare(node->mLevel, pos);
|
||||
|
||||
Point3F minPoint, maxPoint;
|
||||
minPoint.set(squareSize * pos.x + blockPos.x,
|
||||
squareSize * pos.y + blockPos.y,
|
||||
fixedToFloat(sq->minHeight));
|
||||
maxPoint.set(minPoint.x + (squareSize << node->mLevel),
|
||||
minPoint.y + (squareSize << node->mLevel),
|
||||
fixedToFloat(sq->maxHeight));
|
||||
|
||||
// test the square against the current level
|
||||
if(clipFlags)
|
||||
{
|
||||
clipFlags = testSquare(minPoint, maxPoint, clipFlags, floatSquareSize, clipPlanes);
|
||||
if(clipFlags == U16(-1))
|
||||
{
|
||||
stackSize--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// shadowed?
|
||||
if(node->mLevel == 0)
|
||||
{
|
||||
marked = true;
|
||||
shadowList.push_back(pos.x + (pos.y << TerrainBlock::BlockShift));
|
||||
stackSize--;
|
||||
continue;
|
||||
}
|
||||
|
||||
// setup the next level of squares
|
||||
U8 nextLevel = node->mLevel - 1;
|
||||
S32 squareHalfSize = 1 << nextLevel;
|
||||
|
||||
for(U32 i = 0; i < 4; i++)
|
||||
{
|
||||
node[i].mLevel = nextLevel;
|
||||
node[i].mClipFlags = clipFlags;
|
||||
}
|
||||
|
||||
node[3].mPos = pos;
|
||||
node[2].mPos.set(pos.x + squareHalfSize, pos.y);
|
||||
node[1].mPos.set(pos.x, pos.y + squareHalfSize);
|
||||
node[0].mPos.set(pos.x + squareHalfSize, pos.y + squareHalfSize);
|
||||
|
||||
stackSize += 3;
|
||||
}
|
||||
|
||||
return marked;
|
||||
*/
|
||||
}
|
||||
|
||||
void blTerrainProxy::lightVector(LightInfo * light)
|
||||
{
|
||||
// Grab our terrain object
|
||||
TerrainBlock* terrain = getObject();
|
||||
if (!terrain)
|
||||
return;
|
||||
|
||||
// Get the direction to the light (the inverse of the direction
|
||||
// the light is pointing)
|
||||
Point3F lightDir = -light->getDirection();
|
||||
lightDir.normalize();
|
||||
|
||||
// Get the ratio between the light map pixel and world space (used below)
|
||||
F32 lmTerrRatio = (F32)mTerrainBlockSize / (F32) mLightMapSize;
|
||||
lmTerrRatio *= terrain->getSquareSize();
|
||||
|
||||
// Get the terrain position
|
||||
Point3F terrPos( terrain->getTransform().getPosition() );
|
||||
|
||||
U32 i = 0;
|
||||
for (U32 y = 0; y < mLightMapSize; y++)
|
||||
{
|
||||
for (U32 x = 0; x < mLightMapSize; x++)
|
||||
{
|
||||
// Get the relative pixel position and scale it
|
||||
// by the ratio between lightmap and world space
|
||||
Point2F pixelPos(x, y);
|
||||
pixelPos *= lmTerrRatio;
|
||||
|
||||
// Start with a default normal of straight up
|
||||
Point3F normal(0.0f, 0.0f, 1.0f);
|
||||
|
||||
// Try to get the actual normal from the terrain.
|
||||
// Note: this won't change the default normal if
|
||||
// it can't find a normal.
|
||||
terrain->getNormal(pixelPos, &normal);
|
||||
|
||||
// The terrain lightmap only contains shadows.
|
||||
F32 shadowed = 0.0f;
|
||||
|
||||
// Get the height at the lightmap pixel's position
|
||||
F32 height = 0.0f;
|
||||
terrain->getHeight(pixelPos, &height);
|
||||
|
||||
// Calculate the 3D position of the pixel
|
||||
Point3F pixelPos3F(pixelPos.x, pixelPos.y, height);
|
||||
|
||||
// Translate that position by the terrain's transform
|
||||
terrain->getTransform().mulP(pixelPos3F);
|
||||
|
||||
// Offset slighting along the normal so that we don't
|
||||
// raycast into ourself
|
||||
pixelPos3F += (normal * 0.1f);
|
||||
|
||||
// Calculate the light's position.
|
||||
// If it is a vector light like the sun (no position
|
||||
// just direction) then translate along that direction
|
||||
// a reasonable distance to get a point sufficiently
|
||||
// far away
|
||||
Point3F lightPos = light->getPosition();
|
||||
if(light->getType() == LightInfo::Vector)
|
||||
{
|
||||
lightPos = 1000.f * lightDir;
|
||||
lightPos = pixelPos3F + lightPos;
|
||||
}
|
||||
|
||||
// Cast a ray from the world space position of the lightmap pixel to the light source.
|
||||
// If we hit something then we are in shadow. This allows us to be shadowed by anything
|
||||
// that supports a castRay operation.
|
||||
RayInfo info;
|
||||
if(terrain->getContainer()->castRay(pixelPos3F, lightPos, STATIC_COLLISION_TYPEMASK, &info))
|
||||
{
|
||||
// Shadow the pixel.
|
||||
shadowed = 1.0f;
|
||||
}
|
||||
|
||||
// Set the final lightmap color.
|
||||
mLightmap[i++] += ColorF::WHITE * mClampF( 1.0f - shadowed, 0.0f, 1.0f );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
U32 blTerrainProxy::getResourceCRC()
|
||||
{
|
||||
TerrainBlock * terrain = getObject();
|
||||
if(!terrain)
|
||||
return(0);
|
||||
return(terrain->getCRC());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
bool blTerrainProxy::setPersistInfo(PersistInfo::PersistChunk * info)
|
||||
{
|
||||
if(!Parent::setPersistInfo(info))
|
||||
return(false);
|
||||
|
||||
blTerrainChunk * chunk = dynamic_cast<blTerrainChunk*>(info);
|
||||
AssertFatal(chunk, "blTerrainProxy::setPersistInfo: invalid info chunk!");
|
||||
|
||||
TerrainBlock * terrain = getObject();
|
||||
if(!terrain || !terrain->getLightMap())
|
||||
return(false);
|
||||
|
||||
terrain->setLightMap( new GBitmap( *chunk->mLightmap) );
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool blTerrainProxy::getPersistInfo(PersistInfo::PersistChunk * info)
|
||||
{
|
||||
if(!Parent::getPersistInfo(info))
|
||||
return(false);
|
||||
|
||||
blTerrainChunk * chunk = dynamic_cast<blTerrainChunk*>(info);
|
||||
AssertFatal(chunk, "blTerrainProxy::getPersistInfo: invalid info chunk!");
|
||||
|
||||
TerrainBlock * terrain = getObject();
|
||||
if(!terrain || !terrain->getLightMap())
|
||||
return(false);
|
||||
|
||||
if(chunk->mLightmap) delete chunk->mLightmap;
|
||||
|
||||
chunk->mLightmap = new GBitmap(*terrain->getLightMap());
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool blTerrainProxy::supportsShadowVolume()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void blTerrainProxy::getClipPlanes(Vector<PlaneF>& planes)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void blTerrainProxy::addToShadowVolume(ShadowVolumeBSP * shadowVolume, LightInfo * light, S32 level)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void blTerrainSystem::init()
|
||||
{
|
||||
}
|
||||
|
||||
U32 blTerrainSystem::addObjectType()
|
||||
{
|
||||
return TerrainObjectType;
|
||||
}
|
||||
|
||||
SceneLighting::ObjectProxy* blTerrainSystem::createObjectProxy(SceneObject* obj, SceneLighting::ObjectProxyList* sceneObjects)
|
||||
{
|
||||
if ((obj->getTypeMask() & TerrainObjectType) != 0)
|
||||
return new blTerrainProxy(obj);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PersistInfo::PersistChunk* blTerrainSystem::createPersistChunk(const U32 chunkType)
|
||||
{
|
||||
if (chunkType == PersistInfo::PersistChunk::TerrainChunkType)
|
||||
return new blTerrainChunk();
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool blTerrainSystem::createPersistChunkFromProxy(SceneLighting::ObjectProxy* objproxy, PersistInfo::PersistChunk **ret)
|
||||
{
|
||||
if (dynamic_cast<blTerrainProxy*>(objproxy) != NULL)
|
||||
{
|
||||
*ret = new blTerrainChunk();
|
||||
return true;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Given a ray, this will return the color from the lightmap of this object, return true if handled
|
||||
bool blTerrainSystem::getColorFromRayInfo(const RayInfo & collision, ColorF& result) const
|
||||
{
|
||||
TerrainBlock *terrain = dynamic_cast<TerrainBlock *>(collision.object);
|
||||
if (!terrain)
|
||||
return false;
|
||||
|
||||
Point2F uv;
|
||||
F32 terrainlength = (F32)terrain->getBlockSize();
|
||||
Point3F pos = terrain->getPosition();
|
||||
uv.x = (collision.point.x - pos.x) / terrainlength;
|
||||
uv.y = (collision.point.y - pos.y) / terrainlength;
|
||||
|
||||
// similar to x = x & width...
|
||||
uv.x = uv.x - F32(U32(uv.x));
|
||||
uv.y = uv.y - F32(U32(uv.y));
|
||||
const GBitmap* lightmap = terrain->getLightMap();
|
||||
if (!lightmap)
|
||||
return false;
|
||||
|
||||
result = lightmap->sampleTexel(uv.x, uv.y);
|
||||
// terrain lighting is dim - look into this (same thing done in shaders)...
|
||||
result *= 2.0f;
|
||||
return true;
|
||||
}
|
||||
49
Engine/source/lighting/basic/blTerrainSystem.h
Normal file
49
Engine/source/lighting/basic/blTerrainSystem.h
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _BLTERRAINSYSTEM_H_
|
||||
#define _BLTERRAINSYSTEM_H_
|
||||
|
||||
#ifndef _SCENELIGHTING_H_
|
||||
#include "lighting/common/sceneLighting.h"
|
||||
#endif
|
||||
#ifndef _SG_SYSTEM_INTERFACE_H
|
||||
#include "lighting/lightingInterfaces.h"
|
||||
#endif
|
||||
|
||||
//
|
||||
// Lighting system interface
|
||||
//
|
||||
class blTerrainSystem : public SceneLightingInterface
|
||||
{
|
||||
public:
|
||||
virtual void init();
|
||||
virtual U32 addObjectType();
|
||||
virtual SceneLighting::ObjectProxy* createObjectProxy(SceneObject* obj, SceneLighting::ObjectProxyList* sceneObjects);
|
||||
virtual PersistInfo::PersistChunk* createPersistChunk(const U32 chunkType);
|
||||
virtual bool createPersistChunkFromProxy(SceneLighting::ObjectProxy* objproxy, PersistInfo::PersistChunk **ret);
|
||||
|
||||
// Given a ray, this will return the color from the lightmap of this object, return true if handled
|
||||
virtual bool getColorFromRayInfo(const RayInfo & collision, ColorF& result) const;
|
||||
};
|
||||
|
||||
#endif // !_BLTERRAINSYSTEM_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue