Parcourir la source

Restored shadow mapping functionality in new renderer

Ivan Safrin il y a 10 ans
Parent
commit
86644f70b0

BIN
assets/default/default.pak


+ 173 - 0
assets/default/default/DefaultShaderShadows.frag

@@ -0,0 +1,173 @@
+varying vec2 texCoordVar;
+varying vec3 varNormal;
+varying vec4 varPosition;
+varying vec4 rawPosition;
+
+uniform sampler2D diffuse;
+
+uniform mat4 modelMatrix;
+uniform vec4 diffuse_color;
+uniform vec4 specular_color;
+uniform vec4 entityColor;
+uniform float shininess;
+uniform float shadowAmount;
+
+#define MAX_LIGHTS 8
+#define MAX_LIGHT_SHADOWS 2
+
+struct LightInfo {
+	vec3 position;
+	vec3 direction;
+	vec4 specular;
+	vec4 diffuse;
+
+	float spotExponent;
+	float spotCosCutoff;
+
+	float constantAttenuation;
+	float linearAttenuation;	
+	float quadraticAttenuation;
+	float shadowEnabled;
+};
+
+struct LightShadowInfo {
+	sampler2D shadowBuffer;
+	mat4 shadowMatrix;	
+};
+
+uniform LightInfo lights[MAX_LIGHTS];
+uniform LightShadowInfo lightsShadows[MAX_LIGHT_SHADOWS];
+
+float calculateAttenuation(in int i, in float dist)
+{
+    return(1.0 / (lights[i].constantAttenuation +
+                  lights[i].linearAttenuation * dist +
+                  lights[i].quadraticAttenuation * dist * dist));
+}
+
+void pointLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+	vec4 color = diffuse_color;
+	vec4 matspec = specular_color;
+	float shininess = shininess;
+	vec4 lightspec = lights[i].specular;
+	vec4 lpos = vec4(lights[i].position, 1.0);
+	vec4 s = pos-lpos; 
+	vec4 sn = -normalize(s);
+	
+	vec3 light = sn.xyz;
+	vec3 n = normalize(normal);
+	vec3 r = -reflect(light, n);
+	r = normalize(r);
+	vec3 v = -pos.xyz;
+	v = normalize(v);
+
+	float nDotL = dot(n, sn.xyz);
+	if(nDotL > 0.0) {
+		float dist = length(s);    
+		float attenuation = calculateAttenuation(i, dist);
+
+		diffuse  += color * max(0.0, nDotL) * lights[i].diffuse * attenuation;
+
+	  if (shininess != 0.0) {
+    	specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation;
+	  }
+	}
+}
+
+
+void spotLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular, in float shadow) {
+
+	vec4 color = diffuse_color;
+	vec4 matspec = specular_color;
+	float shininess = shininess;
+	vec4 lightspec = lights[i].specular;
+	vec4 lpos = vec4(lights[i].position, 1.0);
+	vec4 s = pos-lpos; 
+	vec4 sn = -normalize(s);
+
+	vec3 light = sn.xyz;
+	vec3 n = normalize(normal);
+	vec3 r = -reflect(light, n);
+	r = normalize(r);
+	vec3 v = -pos.xyz;
+	v = normalize(v);
+
+	float cos_outer_cone_angle = (1.0-lights[i].spotExponent) * lights[i].spotCosCutoff;
+	float cos_cur_angle = dot(-normalize(lights[i].direction), sn.xyz);
+	float cos_inner_cone_angle = lights[i].spotCosCutoff;
+
+	float cos_inner_minus_outer_angle = cos_inner_cone_angle - cos_outer_cone_angle;
+	float spot = 0.0;
+	spot = clamp((cos_cur_angle - cos_outer_cone_angle) / cos_inner_minus_outer_angle, 0.0, 1.0);
+	       
+	float nDotL = dot(n, sn.xyz);
+	if(nDotL > 0.0) {
+		float dist = length(s);    
+		float attenuation = calculateAttenuation(i, dist);
+		diffuse  += color * max(0.0, nDotL) * lights[i].diffuse * attenuation * spot * shadow;
+
+	  if (shininess != 0.0) {
+    	specular += lightspec * matspec * pow(max(0.0,dot(r, v)), shininess) * attenuation * spot * shadow;
+	  }
+	}
+}
+
+void doSpotLightShadow(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular, inout int shadowIndex) {
+	if(lights[i].shadowEnabled == 1.0) {
+
+		float shadow = 1.0;
+		float bias = 0.00001;
+
+		if(shadowIndex == 0) {
+			vec4 shadowCoord = lightsShadows[0].shadowMatrix * modelMatrix * rawPosition;
+			vec4 shadowCoordinateWdivide = shadowCoord / shadowCoord.w;
+			float distanceFromLight = texture2D(lightsShadows[0].shadowBuffer, shadowCoordinateWdivide.st).z;
+
+			if (shadowCoordinateWdivide.x > 0.001 && shadowCoordinateWdivide.y > 0.001 && shadowCoordinateWdivide.x < 0.999 && shadowCoordinateWdivide.y < 0.999) {
+				shadow = step(shadowCoordinateWdivide.z, distanceFromLight+bias);
+			}
+		} else {
+			vec4 shadowCoord = lightsShadows[1].shadowMatrix * modelMatrix * rawPosition;
+			vec4 shadowCoordinateWdivide = shadowCoord / shadowCoord.w;
+			float distanceFromLight = texture2D(lightsShadows[1].shadowBuffer, shadowCoordinateWdivide.st).z;
+
+			if (shadowCoordinateWdivide.x > 0.001 && shadowCoordinateWdivide.y > 0.001 && shadowCoordinateWdivide.x < 0.999 && shadowCoordinateWdivide.y < 0.999) {
+				shadow = step(shadowCoordinateWdivide.z, distanceFromLight+bias);
+			}
+		}
+		shadowIndex++;
+		spotLight(i, normal, pos, diffuse, specular, shadow);		
+	} else {
+		spotLight(i, normal, pos, diffuse, specular, 1.0);
+	}	
+}
+
+void doLights(in int numLights, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular) {
+
+	int shadowIndex = 0;
+	for (int i = 0; i < numLights; i++) {
+		if (lights[i].spotCosCutoff == 180.0) {
+			pointLight(i, normal, pos, diffuse, specular);
+		} else {		
+			doSpotLightShadow(i, normal, pos, diffuse, specular, shadowIndex);
+		}
+    }
+}
+
+
+void main()
+{
+	vec4 diffuse_val  = vec4(0.0);
+	vec4 specular_val = vec4(0.0);
+
+	doLights(MAX_LIGHTS, varNormal, varPosition, diffuse_val, specular_val);
+		
+	vec4 texColor = texture2D(diffuse, texCoordVar);		
+		
+    vec4 color = diffuse_val; 	           
+    color = clamp((color*entityColor*texColor) + specular_val, 0.0, 1.0);  
+
+	color.a = entityColor.a * texColor.a * diffuse_color.a;	
+	gl_FragColor = color;
+
+}

+ 39 - 0
assets/default/default/DefaultShaderShadows.vert

@@ -0,0 +1,39 @@
+#ifdef GLES2
+	precision mediump float;
+#endif
+
+attribute vec4 position;
+attribute vec3 normal;
+attribute vec2 texCoord;
+
+uniform mat4 modelMatrix;
+uniform mat4 viewMatrix;
+uniform mat4 projectionMatrix;
+
+varying vec2 texCoordVar;
+varying vec3 varNormal;
+varying vec4 varPosition;
+varying vec4 rawPosition;
+
+mat3 mat3_emu(mat4 m4) {
+  return mat3(
+      m4[0][0], m4[0][1], m4[0][2],
+      m4[1][0], m4[1][1], m4[1][2],
+      m4[2][0], m4[2][1], m4[2][2]);
+}
+
+void main()
+{
+	mat4 modelViewMatrix = viewMatrix * modelMatrix;
+	vec4 p = modelViewMatrix  * position;
+
+	mat3 rotN = mat3_emu(modelViewMatrix);
+	varNormal = normalize(rotN * normal);
+
+	rawPosition = position;
+
+	varPosition = modelViewMatrix  * position;
+	gl_Position = projectionMatrix * p;
+
+	texCoordVar = texCoord;
+}

+ 9 - 1
assets/default/default/default.mat

@@ -5,6 +5,10 @@
 			<vp source="default/DefaultShader.vert"/>
 			<fp source="default/DefaultShader.frag"/>
 		</shader>
+		<shader type="glsl" name="DefaultShaderShadows" numPointLights="6" numSpotLights="2">
+			<vp source="default/DefaultShaderShadows.vert"/>
+			<fp source="default/DefaultShaderShadows.frag"/>
+		</shader>		
 		<shader type="glsl" name="DefaultUntexturedShader" numPointLights="6" numSpotLights="2">
 			<vp source="default/DefaultShader.vert"/>
 			<fp source="default/DefaultUntextured.frag"/>
@@ -38,7 +42,11 @@
 		<material name="DefaultTextured">
 			<shader name="DefaultShader">
 			</shader>
-		</material>		
+		</material>
+		<material name="DefaultTexturedShadows">
+			<shader name="DefaultShaderShadows">
+			</shader>
+		</material>	
 		<material name="Unlit">
 			<shader name="Unlit">
 			</shader>

+ 10 - 2
include/polycode/core/PolyRenderer.h

@@ -36,6 +36,7 @@ THE SOFTWARE.
 #include <queue>  
 
 #define RENDERER_MAX_LIGHTS 8
+#define RENDERER_MAX_LIGHT_SHADOWS 2
 
 namespace Polycode {
     
@@ -98,7 +99,7 @@ namespace Polycode {
             unsigned int drawCallsProcessed;
             unsigned int timeTaken;
     };
-    
+
     class LightInfoBinding {
         public:
             LocalShaderParam *position;
@@ -110,6 +111,13 @@ namespace Polycode {
             LocalShaderParam *constantAttenuation;
             LocalShaderParam *linearAttenuation;
             LocalShaderParam *quadraticAttenuation;
+            LocalShaderParam *shadowEnabled;
+    };
+    
+    class LightShadowInfoBinding {
+    public:
+        LocalShaderParam *shadowMatrix;
+        LocalShaderParam *shadowBuffer;
     };
     
     class _PolyExport RenderThread : public Threaded {
@@ -158,7 +166,7 @@ namespace Polycode {
             LocalShaderParam *modelMatrixParam;
         
             LightInfoBinding lights[RENDERER_MAX_LIGHTS];
-        
+            LightShadowInfoBinding lightShadows[RENDERER_MAX_LIGHT_SHADOWS];
     };
 
     class _PolyExport Renderer : public PolyBase {

+ 1 - 2
include/polycode/core/PolyScene.h

@@ -128,8 +128,7 @@ namespace Polycode {
 		bool isEnabled();		
 		void setEnabled(bool enabled);
 		
-		void Render(Camera *targetCamera = NULL, RenderBuffer *targetFramebuffer = NULL);
-		void RenderDepthOnly(Camera *targetCamera);
+		void Render(Camera *targetCamera, RenderBuffer *targetFramebuffer, Material *overrideMaterial, bool sendLights);
         
         void setOverrideMaterial(Material *material);
 		

+ 2 - 2
include/polycode/core/PolySceneLight.h

@@ -81,8 +81,6 @@ namespace Polycode {
 			int getType() const;
 			
 			void renderDepthMap(Scene *scene);
-		
-			const Matrix4& getLightViewMatrix() const;
 			
 			static const int POINT_LIGHT = 0;
 			static const int SPOT_LIGHT = 1;
@@ -192,6 +190,8 @@ namespace Polycode {
         
             LightInfo lightInfo;
 
+            RenderBuffer *shadowMapRenderBuffer;
+            Material *unlitMaterial;
 			Camera *spotCamera;
 			Scene *parentScene;
 		

+ 5 - 1
src/core/PolyOpenGLGraphicsInterface.cpp

@@ -173,7 +173,11 @@ void OpenGLGraphicsInterface::setParamInShader(Shader *shader, ProgramParam *par
             glUniform1i(paramLocation, textureIndex);
             if(localParam) {
                 Texture* texture = localParam->getTexture();
-                glBindTexture(GL_TEXTURE_2D, *((GLuint*) texture->platformData));
+                if(texture) {
+                    glBindTexture(GL_TEXTURE_2D, *((GLuint*) texture->platformData));
+                } else {
+                    glBindTexture(GL_TEXTURE_2D, 0);                    
+                }
             } else {
                 glBindTexture(GL_TEXTURE_2D, 0);
             }

+ 23 - 0
src/core/PolyRenderer.cpp

@@ -50,6 +50,14 @@ RenderThread::RenderThread() : graphicsInterface(NULL) {
         lights[i].constantAttenuation = rendererShaderBinding->addParam(ProgramParam::PARAM_NUMBER, "lights["+String::IntToString(i)+"].constantAttenuation");
         lights[i].linearAttenuation = rendererShaderBinding->addParam(ProgramParam::PARAM_NUMBER, "lights["+String::IntToString(i)+"].linearAttenuation");
         lights[i].quadraticAttenuation = rendererShaderBinding->addParam(ProgramParam::PARAM_NUMBER, "lights["+String::IntToString(i)+"].quadraticAttenuation");
+        lights[i].shadowEnabled = rendererShaderBinding->addParam(ProgramParam::PARAM_NUMBER, "lights["+String::IntToString(i)+"].shadowEnabled");
+
+    }
+    
+    for(int i=0; i < RENDERER_MAX_LIGHT_SHADOWS; i++) {
+        lightShadows[i].shadowMatrix = rendererShaderBinding->addParam(ProgramParam::PARAM_MATRIX, "lightsShadows["+String::IntToString(i)+"].shadowMatrix");
+        lightShadows[i].shadowBuffer = rendererShaderBinding->addParam(ProgramParam::PARAM_TEXTURE, "lightsShadows["+String::IntToString(i)+"].shadowBuffer");
+        lightShadows[i].shadowBuffer->setTexture(NULL);
         
     }
     
@@ -87,6 +95,8 @@ void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
     projectionMatrixParam->setMatrix4(buffer->projectionMatrix);
     viewMatrixParam->setMatrix4(buffer->viewMatrix);
 
+    int lightShadowIndex = 0;
+    
     for(int i=0; i <RENDERER_MAX_LIGHTS; i++) {
         if(i < buffer->lights.size()) {
             lights[i].diffuse->setColor(buffer->lights[i].diffuseColor * buffer->lights[i].intensity);
@@ -105,10 +115,23 @@ void RenderThread::processDrawBuffer(GPUDrawBuffer *buffer) {
             lights[i].linearAttenuation->setNumber(buffer->lights[i].linearAttenuation);
             lights[i].quadraticAttenuation->setNumber(buffer->lights[i].quadraticAttenuation);
             
+            
+            if(buffer->lights[i].shadowsEnabled) {
+                lightShadows[lightShadowIndex].shadowMatrix->setMatrix4(buffer->lights[i].lightViewMatrix);
+                lightShadows[lightShadowIndex].shadowBuffer->setTexture(buffer->lights[i].shadowMapTexture);
+                lights[i].shadowEnabled->setNumber(1.0);
+                
+                if(lightShadowIndex < RENDERER_MAX_LIGHT_SHADOWS-1) {
+                    lightShadowIndex++;
+                }
+            } else {
+                lights[i].shadowEnabled->setNumber(0.0);
+            }
         } else {
             lights[i].diffuse->setColor(Color(0.0, 0.0, 0.0, 1.0));
             lights[i].specular->setColor(Color(0.0, 0.0, 0.0, 1.0));
             lights[i].spotCosCutoff->setNumber(180.0);
+            lights[i].shadowEnabled->setNumber(0.0);
         }
     }
     

+ 43 - 80
src/core/PolyScene.cpp

@@ -215,9 +215,10 @@ void Scene::setEntityVisibility(Entity *entity, Camera *camera) {
     }
 }
 
-void Scene::Render(Camera *targetCamera, RenderBuffer *targetFramebuffer) {
+void Scene::Render(Camera *targetCamera, RenderBuffer *targetFramebuffer, Material *overrideMaterial, bool sendLights) {
     if(!targetCamera && !activeCamera)
         return;
+    
     if(!targetCamera)
         targetCamera = activeCamera;
     
@@ -227,69 +228,54 @@ void Scene::Render(Camera *targetCamera, RenderBuffer *targetFramebuffer) {
     drawBuffer->clearDepthBuffer = useClearDepth;
     drawBuffer->targetFramebuffer = targetFramebuffer;
     drawBuffer->viewport = targetCamera->getViewport();
-    drawBuffer->globalMaterial = overrideMaterial;	
-		
-	//make these the closest
-	
+    
+    if(overrideMaterial) {
+        drawBuffer->globalMaterial = overrideMaterial;
+    } else {
+        drawBuffer->globalMaterial = this->overrideMaterial;
+    }
+    
 	Matrix4 textureMatrix;
-	Matrix4 *matrixPtr;
-	
-	
 	targetCamera->rebuildTransformMatrix();
     
     drawBuffer->projectionMatrix = targetCamera->createProjectionMatrix();
     drawBuffer->viewMatrix = targetCamera->getConcatenatedMatrix().Inverse();
     drawBuffer->cameraMatrix = targetCamera->getConcatenatedMatrix();
-/*
-	if(useClearColor) {
-		CoreServices::getInstance()->getRenderer()->setClearColor(clearColor.r,clearColor.g,clearColor.b, clearColor.a);	
-	}
-	CoreServices::getInstance()->getRenderer()->setAmbientColor(ambientColor.r,ambientColor.g,ambientColor.b);
-*/
 	
-	for(int i=0; i < lights.size(); i++) {
-		SceneLight *light = lights[i];
-		if(!light->enabled)
-			continue;
-			
-		Vector3 direction;
-		Vector3 position;
-		matrixPtr = NULL;				
-		direction.x = 0;
-		direction.y = 0.0;
-		direction.z = -1.0;
-		direction.Normalize();
-		
-		direction = light->getConcatenatedMatrix().rotateVector(direction);
-        direction = drawBuffer->viewMatrix.rotateVector(direction);
-        
-        
-		Texture *shadowMapTexture = NULL;
-		if(light->areShadowsEnabled()) {
-			if(light->getType() == SceneLight::SPOT_LIGHT) {
-				Matrix4 matTexAdj(0.5f,	0.0f,	0.0f,	0.0f,
-								  0.0f,	0.5f,	0.0f,	0.0f,
-								  0.0f,	0.0f,	0.5f,	0.0f,
-								  0.5f,	0.5f,	0.5f,	1.0f );
-				
-				light->renderDepthMap(this);
-				textureMatrix = light->getLightViewMatrix() * matTexAdj;				
-				matrixPtr = &textureMatrix;
-				shadowMapTexture = light->getZBufferTexture();
-			}
-		}
-		
-		position = light->getPosition();
-		if(light->getParentEntity() != NULL) {
-			position = light->getParentEntity()->getConcatenatedMatrix() * position;
-		}
-        position = drawBuffer->viewMatrix * position;
-        
-        drawBuffer->lights.push_back(light->getLightInfo());
-        drawBuffer->lights[drawBuffer->lights.size()-1].position = position;
-        drawBuffer->lights[drawBuffer->lights.size()-1].direction = direction;
-	}	
-		
+    if(sendLights) {
+        for(int i=0; i < lights.size(); i++) {
+            SceneLight *light = lights[i];
+            if(!light->enabled)
+                continue;
+                
+            Vector3 direction;
+            Vector3 position;
+            
+            direction.x = 0;
+            direction.y = 0.0;
+            direction.z = -1.0;
+            direction.Normalize();
+            
+            direction = light->getConcatenatedMatrix().rotateVector(direction);
+            direction = drawBuffer->viewMatrix.rotateVector(direction);
+            
+            if(light->areShadowsEnabled()) {
+                if(light->getType() == SceneLight::SPOT_LIGHT) {
+                    light->renderDepthMap(this);
+                }
+            }
+            
+            position = light->getPosition();
+            if(light->getParentEntity() != NULL) {
+                position = light->getParentEntity()->getConcatenatedMatrix() * position;
+            }
+            position = drawBuffer->viewMatrix * position;
+            
+            drawBuffer->lights.push_back(light->getLightInfo());
+            drawBuffer->lights[drawBuffer->lights.size()-1].position = position;
+            drawBuffer->lights[drawBuffer->lights.size()-1].direction = direction;
+        }
+    }
     
     if(_doVisibilityChecking) {
         targetCamera->buildFrustumPlanes();
@@ -300,29 +286,6 @@ void Scene::Render(Camera *targetCamera, RenderBuffer *targetFramebuffer) {
     renderer->processDrawBuffer(drawBuffer);
 }
 
-
-void Scene::RenderDepthOnly(Camera *targetCamera) {
-    // RENDERER_TODO
-    /*
-	CoreServices::getInstance()->getRenderer()->cullFrontFaces(true);
-
-	targetCamera->rebuildTransformMatrix();	
-	targetCamera->doCameraTransform();
-	
-	CoreServices::getInstance()->getRenderer()->setTexture(NULL);
-	CoreServices::getInstance()->getRenderer()->enableShaders(false);
-		
-    if(_doVisibilityChecking) {
-        targetCamera->buildFrustumPlanes();
-        setEntityVisibility(&rootEntity, targetCamera);
-    }
-	rootEntity.transformAndRender();	
-	
-	CoreServices::getInstance()->getRenderer()->enableShaders(true);
-	CoreServices::getInstance()->getRenderer()->cullFrontFaces(false);	
-     */
-}
-
 Ray Scene::projectRayFromCameraAndViewportCoordinate(Camera *camera, Vector2 coordinate) {
 
 	Polycode::Rectangle viewport = camera->getViewport();

+ 41 - 34
src/core/PolySceneLight.cpp

@@ -25,8 +25,10 @@
 #include "polycode/core/PolyCore.h"
 #include "polycode/core/PolyCoreServices.h"
 #include "polycode/core/PolyMesh.h"
+#include "polycode/core/PolyResourceManager.h"
 #include "polycode/core/PolyRenderer.h"
 #include "polycode/core/PolyScenePrimitive.h"
+#include "polycode/core/PolyTexture.h"
 #include "polycode/core/PolyScene.h"
 
 using namespace Polycode;
@@ -43,7 +45,9 @@ SceneLight::SceneLight(int type, Scene *parentScene, Number intensity, Number co
 		
     shadowMapRes = 256;
 	this->depthWrite = false;
-	    
+    
+    shadowMapRenderBuffer = NULL;
+    
 	shadowMapFOV = 60.0f;
 	lightInfo.shadowMapTexture = NULL;
 	spotCamera = NULL;
@@ -52,6 +56,8 @@ SceneLight::SceneLight(int type, Scene *parentScene, Number intensity, Number co
 	lightInfo.diffuseColor.setColor(1.0f,1.0f,1.0f,1.0f);
 	setSpotlightProperties(40,0.1);
 	
+    unlitMaterial = (Material*) Services()->getResourceManager()->getGlobalPool()->getResource(Resource::RESOURCE_MATERIAL, "Unlit");
+    
 	lightInfo.importance = 0;
 }
 
@@ -68,26 +74,29 @@ int SceneLight::getLightImportance() const {
 }
 
 void SceneLight::enableShadows(bool val, unsigned int resolution) {
-        // RENDERER_TODO
-    /*
+
 	if(val) {
-        if(zBufferTexture) {
-            CoreServices::getInstance()->getMaterialManager()->deleteTexture(zBufferTexture);
+        if(shadowMapRenderBuffer) {
+            Services()->getRenderer()->destroyRenderBuffer(shadowMapRenderBuffer);
         }
-        CoreServices::getInstance()->getRenderer()->createRenderTextures(NULL, &zBufferTexture, resolution, resolution, false);
+        
+        shadowMapRenderBuffer = Services()->getRenderer()->createRenderBuffer(resolution, resolution, true);
+        
 		if(!spotCamera) {
 			spotCamera = new Camera(parentScene);
             spotCamera->editorOnly = true;
-            spotCamera->setClippingPlanes(0.01, 100.0);
-//            spotCamera->setPitch(90.0);
+            spotCamera->setClippingPlanes(0.01, 50.0);
 			addChild(spotCamera);	
 		}
+        
 		shadowMapRes = resolution;
-		shadowsEnabled = true;
+		lightInfo.shadowsEnabled = true;
 	} else {
-		shadowsEnabled = false;
+		lightInfo.shadowsEnabled = false;
+        if(shadowMapRenderBuffer) {
+            Services()->getRenderer()->destroyRenderBuffer(shadowMapRenderBuffer);
+        }
 	}
-*/
 }
 
 bool SceneLight::areShadowsEnabled() const {
@@ -114,10 +123,18 @@ Number SceneLight::getShadowMapFOV() const {
 }
 
 SceneLight::~SceneLight() {
+    
+    if(shadowMapRenderBuffer) {
+        Services()->getRenderer()->destroyRenderBuffer(shadowMapRenderBuffer);
+    }
+    
+    if(!ownsChildren) {
+        delete spotCamera;
+    }
+    
     if(parentScene) {
         parentScene->removeLight(this);
     }
-	printf("Destroying scene light...\n");
 }
 
 unsigned int SceneLight::getShadowMapResolution() const {
@@ -125,26 +142,20 @@ unsigned int SceneLight::getShadowMapResolution() const {
 }
 
 void SceneLight::renderDepthMap(Scene *scene) {
-        // RENDERER_TODO
-    /*
+    if(!unlitMaterial) {
+        return;
+    }
     spotCamera->setFOV(shadowMapFOV);
-	Renderer* renderer = CoreServices::getInstance()->getRenderer();
-	renderer->pushMatrix();
-	renderer->loadIdentity();
-
-    Number vpW = renderer->getViewportWidth();
-    Number vpH = renderer->getViewportHeight();
+    spotCamera->setViewport(Polycode::Rectangle(0, 0, shadowMapRes, shadowMapRes));
+    scene->Render(spotCamera, shadowMapRenderBuffer, unlitMaterial, false);
     
-	renderer->setViewportSize(shadowMapRes, shadowMapRes);
-	renderer->bindFrameBufferTexture(zBufferTexture);
-
-	scene->RenderDepthOnly(spotCamera);
-		
-	lightViewMatrix = getConcatenatedMatrix().Inverse() *  renderer->getProjectionMatrix();
-	renderer->unbindFramebuffers();
-	renderer->popMatrix();
-	renderer->setViewportSize(vpW , vpH);
-     */
+    Matrix4 matTexAdj(0.5f,	0.0f,	0.0f,	0.0f,
+                      0.0f,	0.5f,	0.0f,	0.0f,
+                      0.0f,	0.0f,	0.5f,	0.0f,
+                      0.5f,	0.5f,	0.5f,	1.0f);
+    
+    lightInfo.lightViewMatrix = getConcatenatedMatrix().Inverse() *  spotCamera->getProjectionMatrix() * matTexAdj;
+    lightInfo.shadowMapTexture = shadowMapRenderBuffer->depthTexture;
 }
 
 LightInfo SceneLight::getLightInfo() const {
@@ -215,10 +226,6 @@ Camera *SceneLight::getSpotlightCamera() {
     return spotCamera;
 }
 
-const Matrix4& SceneLight::getLightViewMatrix() const {
-	return lightInfo.lightViewMatrix;
-}
-
 Texture *SceneLight::getZBufferTexture() const {
 	return lightInfo.shadowMapTexture;
 }

+ 1 - 1
src/core/PolySceneManager.cpp

@@ -89,7 +89,7 @@ void SceneManager::Render(const Polycode::Rectangle &viewport) {
 				scene->getActiveCamera()->drawFilter();
 			} else {
                 scene->getActiveCamera()->setViewport(viewport);
-				scene->Render();
+				scene->Render(NULL, NULL, NULL, true);
 			}
 		}
 	}

+ 1 - 1
src/core/PolySceneRenderTexture.cpp

@@ -74,7 +74,7 @@ void SceneRenderTexture::Render() {
        // targetCamera->drawFilter(targetTexture, targetTexture->getWidth(), targetTexture->getHeight(), filterColorBufferTexture, filterZBufferTexture);
   //  } else {
         targetCamera->setViewport(Polycode::Rectangle(0.0, 0.0, targetFramebuffer->getWidth(), targetFramebuffer->getHeight()));
-        targetScene->Render(targetCamera, targetFramebuffer);
+        targetScene->Render(targetCamera, targetFramebuffer, NULL, true);
    // }
 }