Project

General

Profile

SO3Engine
SO3DeferredLight.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
32#include "OgreHardwareBufferManager.h"
33#include "OgreCamera.h"
34#include "OgreSceneNode.h"
35#include "OgreTechnique.h"
36#include "OgreSceneManager.h"
37
38namespace SO3
39{
40
41SDeferredLight::SDeferredLight(Ogre::Light* originalLight) : ogreParentLight(originalLight)
42{
43 initialised = false; // To force the first updateFromParent call
44 currentType = ogreParentLight->getType();
47}
48
53
59
61{
62 // Check if the light type as changed.
63 Ogre::Light::LightTypes actualType = ogreParentLight->getType();
64 if((currentType != actualType) || (!initialised))
65 {
66 SO3_SAFE_DELETE(currentDeferredLight);
67 switch(actualType)
68 {
69 case Ogre::Light::LT_DIRECTIONAL:
70 currentDeferredLight = new SDeferredLightDirectional(ogreParentLight, SDeferredLightManager::getSingleton().GetDeferredLightDirectionalMaterialGenerator());
71 break;
72 case Ogre::Light::LT_POINT:
74 break;
75 case Ogre::Light::LT_SPOTLIGHT:
77 break;
78 }
79 currentType = actualType;
80 initialised = true;
81 }
82
83 //TODO : Don't do this unless something changed
85 //SetSpecularColour(ogreParentLight->getSpecularColour());
86}
87
88void SDeferredLight::UpdateFromCamera(Ogre::Camera* camera)
89{
90 //Set shader params
91 const Ogre::MaterialPtr& mat = currentDeferredLight->getMaterial();
92 if(!mat->isLoaded())
93 mat->load();
94
95 Ogre::Technique* tech = mat->getBestTechnique();
96 Ogre::Vector3 farCorner = camera->getViewMatrix(true) * camera->getWorldSpaceCorners()[4];
97
98 // get the vertex shader parameters
99 Ogre::Pass* pass = tech->getPass(0);
100 Ogre::GpuProgramParametersSharedPtr params = pass->getVertexProgramParameters();
101
102 // set the camera's far-top-right corner
103 if (params->_findNamedConstantDefinition("farCorner"))
104 params->setNamedConstant("farCorner", farCorner);
105
106 params = pass->getFragmentProgramParameters();
107 if (params->_findNamedConstantDefinition("farCorner"))
108 params->setNamedConstant("farCorner", farCorner);
109
111 if(GetCastShadows())
112 {
113 Ogre::Camera shadowCam("ShadowCameraSetupCam", 0);
114 shadowCam._notifyViewport(camera->getViewport());
115 Ogre::SceneManager* sm = ogreParentLight->_getManager();
116
117 if (!ogreParentLight->getCustomShadowCameraSetup())
118 sm->getShadowCameraSetup()->getShadowCamera(sm, camera, camera->getViewport(), ogreParentLight, &shadowCam, 0);
119 else
120 ogreParentLight->getCustomShadowCameraSetup()->getShadowCamera(sm, camera, camera->getViewport(), ogreParentLight, &shadowCam, 0);
121
122 //Get the shadow camera position
123 if (params->_findNamedConstantDefinition("shadowCamPos"))
124 params->setNamedConstant("shadowCamPos", shadowCam.getPosition());
125
126 if (params->_findNamedConstantDefinition("shadowFarClip"))
127 params->setNamedConstant("shadowFarClip", shadowCam.getFarClipDistance());
128 }
129}
130
135
136Ogre::SimpleRenderable* SDeferredLight::GetRenderable()
137{
138 assert(currentDeferredLight != 0);
140}
141
146
147void SDeferredLight::SetDebugRendering(bool enableDebugRendering)
148{
149 currentDeferredLight->SetDebugRendering(enableDebugRendering);
150}
151
152
153}
SDeferredLightImpl * currentDeferredLight
Ogre::SimpleRenderable * GetRenderable()
Ogre::Light * ogreParentLight
The light that this SDeferredLight renders.
SDeferredLight(Ogre::Light *originalLight)
void SetDebugRendering(bool enableDebugRendering)
Ogre::Light::LightTypes currentType
void UpdateFromCamera(Ogre::Camera *camera)
virtual const Ogre::MaterialPtr & getMaterial() const
void SetDebugRendering(bool enableDebugRendering)
virtual void RebuildGeometry()=0
virtual void CheckCullingMode(Ogre::Camera *camera, Ogre::Pass *pass)=0
virtual void SetAttenuation()=0
static SDeferredLightManager & getSingleton()