16 SLightShaft::SLightShaft()
20 SLightShaft::SLightShaft(
SNode* parent,
float minStep,
float nearClip,
float farClip, Ogre::Radian fov)
30 mShaftMaterial.reset();
31 mDepthMaterial.reset();
32 mDepthTexture.reset();
38 mLightCamera->setProjectionType(Ogre::PT_PERSPECTIVE);
43 mBillboardSet =
mSceneMgr->createBillboardSet(mParent->
GetName() +
"_LightBillboardSet", 60);
44 mBillboardSet->setBillboardRotationType(Ogre::BBR_VERTEX);
45 mBillboardSet->setBillboardType(Ogre::BBT_POINT);
46 mBillboardSet->setCastShadows(
false);
47 mBillboardSet->setSortingEnabled(
false);
48 mBillboardSet->setDefaultWidth(1.0f);
49 mBillboardSet->setDefaultHeight(1.0f);
60 Ogre::MaterialPtr smat =
static_cast<Ogre::MaterialPtr
>(isrect ? Ogre::MaterialManager::getSingleton().getByName(
"SO3/LightShaftsRect") : Ogre::MaterialManager::getSingleton().getByName(
"SO3/LightShafts"));
63 mShaftMaterial = smat->clone(mParent->
GetName() +
"/SO3/LightShafts",
"SO3/Internal");
64 mShaftMaterial->getTechnique(0)->setSchemeName(
"SO3/LIGHTSHAFT");
65 mShaftMaterial->setReceiveShadows(
false);
66 mShaftMaterial->load();
67 mBillboardSet->setMaterialName(mShaftMaterial->getName());
70 Ogre::MaterialPtr dmat =
static_cast<Ogre::MaterialPtr
>(Ogre::MaterialManager::getSingleton().getByName(
"SO3/LightShaftsDepth"));
73 mDepthMaterial = dmat->clone(mParent->
GetName() +
"/SO3/LightShaftsDepth",
"SO3/Internal");
74 mDepthMaterial->setReceiveShadows(
false);
75 mDepthMaterial->load();
78 catch (Ogre::Exception &)
81 mShaftMaterial.reset();
82 mDepthMaterial.reset();
87 CreateLightCameraRTT();
88 Ogre::MaterialManager::getSingleton().addListener(
this,
"SO3/" + mParent->
GetName() +
"/LightShafts");
93 Ogre::MaterialManager::getSingleton().removeListener(
this,
"SO3/" + mParent->
GetName() +
"/LightShafts");
95 mBillboardSet->clear();
96 mSceneMgr->destroyBillboardSet(mBillboardSet->getName());
101 mShaftMaterial->unload();
102 Ogre::MaterialManager::getSingletonPtr()->remove(mShaftMaterial->getHandle());
107 mDepthMaterial->unload();
108 Ogre::MaterialManager::getSingletonPtr()->remove(mDepthMaterial->getHandle());
111 mShaftMaterial.reset();
112 mDepthMaterial.reset();
116 Ogre::RenderTarget* RT_Texture = mDepthTexture->getBuffer()->getRenderTarget();
117 RT_Texture->removeAllViewports();
118 Ogre::Root::getSingleton().detachRenderTarget(RT_Texture);
119 Ogre::TextureManager::getSingleton().remove(mDepthTexture->getHandle());
122 mDepthTexture.reset();
126 Ogre::Technique*
SLightShaft::handleSchemeNotFound(
unsigned short schemeIndex,
const Ogre::String& schemeName, Ogre::Material* originalMaterial,
unsigned short lodIndex,
const Ogre::Renderable* rend)
128 return mDepthMaterial->getTechniques()[0];
131 void SLightShaft::preRenderTargetUpdate(
const Ogre::RenderTargetEvent& evt)
136 mBillboardSet->setVisible(
false);
139 void SLightShaft::postRenderTargetUpdate(
const Ogre::RenderTargetEvent& evt)
141 mBillboardSet->setVisible(
true);
144 void SLightShaft::UpdatePosition()
148 mShaftMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightPosition", mParent->
GetGlobalPosition());
151 mDepthMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightPosition", mParent->
GetGlobalPosition());
154 UpdateTextureProjectionMatrix();
157 void SLightShaft::UpdateTextureProjectionMatrix()
159 const Ogre::Matrix4 PROJECTIONCLIPSPACE2DTOIMAGESPACE_PERSPECTIVE(
165 Ogre::Matrix4 TexViewProj = PROJECTIONCLIPSPACE2DTOIMAGESPACE_PERSPECTIVE * mLightCamera->getProjectionMatrixWithRSDepth() * mLightCamera->getViewMatrix();
167 mShaftMaterial->getTechnique(0)->getPass(0)->getVertexProgramParameters()->setNamedConstant(
"uTexViewProj", TexViewProj);
170 void SLightShaft::CreateLightShafts()
175 const Ogre::Vector3 *FrustumCorners = mLightCamera->getWorldSpaceCorners();
178 float NearWidth = (FrustumCorners[0] - FrustumCorners[1]).length(),
179 NearHeigth = (FrustumCorners[1] - FrustumCorners[2]).length(),
180 FarWidth = (FrustumCorners[4] - FrustumCorners[5]).length(),
181 FarHeigth = (FrustumCorners[5] - FrustumCorners[6]).length();
184 mBillboardSet->clear();
187 Ogre::Billboard *CurrentBB = 0;
189 float diff = FarWidth - NearWidth;
191 float clip = mLightCamera->getFarClipDistance() - mLightCamera->getNearClipDistance();
203 SLight* light =
static_cast<SLight*
>(mParent);
205 rect = light->GetSourceSize();
208 for (
int k = 0; dist < mLightCamera->getFarClipDistance() && (k < maxCount); k++)
210 dist = mMinStep * k * k * diff * 0.5f;
211 float width = isrect ? rect.x + (dist / clip * diff) : dist / clip * diff;
212 float height = isrect ? rect.y + (dist / clip * diff) : dist / clip * diff;
214 CurrentBB = mBillboardSet->createBillboard(Ogre::Vector3(0, 0, -dist));
215 CurrentBB->setDimensions(width, height);
229 void SLightShaft::CreateLightCameraRTT()
233 Ogre::PixelFormat rttformat;
239 rttformat = Ogre::PF_R8G8B8;
242 mDepthTexture = Ogre::TextureManager::getSingleton().createManual(mParent->
GetName() +
"_LightDepthMap",
"SO3/Internal", Ogre::TEX_TYPE_2D, 128, 128, 0, rttformat, Ogre::TU_RENDERTARGET);
243 Ogre::RenderTarget* RT_Texture = mDepthTexture->getBuffer()->getRenderTarget();
244 Ogre::Viewport* RT_Texture_Viewport = RT_Texture->addViewport(mLightCamera);
245 RT_Texture_Viewport->setClearEveryFrame(
true);
246 RT_Texture_Viewport->setBackgroundColour(Ogre::ColourValue::White);
247 RT_Texture_Viewport->setOverlaysEnabled(
false);
248 RT_Texture_Viewport->setSkiesEnabled(
false);
249 RT_Texture_Viewport->setShadowsEnabled(
false);
250 RT_Texture_Viewport->setMaterialScheme(
"SO3/" + mParent->
GetName() +
"/LightShafts");
253 RT_Texture->addListener(
this);
256 mShaftMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTexture(mDepthTexture);
278 if (farClip > 1000.0)
281 if (nearClip >= farClip)
282 nearClip = farClip / 8.0f;
284 mLightCamera->setProjectionType(Ogre::PT_PERSPECTIVE);
285 mLightCamera->setNearClipDistance(nearClip);
286 mLightCamera->setFarClipDistance(farClip);
287 mLightCamera->setAspectRatio(1.0f);
288 mLightCamera->setDebugDisplayEnabled(
false);
289 mLightCamera->setVisible(
true);
292 mShaftMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightFarClipDistance", farClip + nearClip);
295 mDepthMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightFarClipDistance", farClip + nearClip);
303 mShaftMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uAttenuation", attenuation);
308 mLightCamera->setFOVy(fov);
315 mShaftMaterial->getTechnique(0)->getPass(0)->getFragmentProgramParameters()->setNamedConstant(
"uLightColor", col);
std::string GetName() const
virtual Ogre::Technique * handleSchemeNotFound(unsigned short schemeIndex, const Ogre::String &schemeName, Ogre::Material *originalMaterial, unsigned short lodIndex, const Ogre::Renderable *rend)
void SetMinStep(const float minStep)
void SetLightClipping(float nearClip, float farClip)
void SetCookieTexture(std::string path)
void SetColor(Ogre::ColourValue col)
Ogre::SceneManager * mSceneMgr
void SetAttenuation(Ogre::Vector4 attenuation)
void SetLightFov(Ogre::Radian fov)
SScene * GetParentScene()
virtual Ogre::Vector3 GetGlobalPosition()
Ogre::SceneNode * GetOgreSceneNodePointer()
bool GetRttPixelFormat(Ogre::PixelFormat &format, bool alpha=false, bool floattex=false)
static SRoot & getSingleton()
Ogre::SceneManager * GetOgreScenePointer()