Skip to content

Commit 7fcf69c

Browse files
committed
Fixed shadow mapping, which was broken by recent changes. Framebuffer binding in Renderer now keeps a stack, restores previous framebuffer binding on unbind, spotlight shadowmap fov now gets correctly set in SceneLight, shadow mapping now works in IDE entity editor
1 parent 71af71e commit 7fcf69c

File tree

11 files changed

+82
-41
lines changed

11 files changed

+82
-41
lines changed
11 Bytes
Binary file not shown.

Assets/Default asset pack/default/DefaultShaderShadows.frag

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ uniform sampler2D diffuse;
88
uniform sampler2D shadowMap0;
99
uniform sampler2D shadowMap1;
1010

11-
uniform mat4 shadowMatrix0;
12-
uniform mat4 shadowMatrix1;
13-
1411
uniform vec4 diffuse_color;
1512
uniform vec4 specular_color;
1613
uniform vec4 ambient_color;
1714
uniform float shininess;
1815

16+
uniform float shadowAmount;
17+
1918

2019
float calculateAttenuation(in int i, in float dist)
2120
{
@@ -57,12 +56,14 @@ void pointLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout
5756
void spotLight(in int i, in vec3 normal, in vec4 pos, inout vec4 diffuse, inout vec4 specular, sampler2D shadowMap, vec4 ShadowCoord) {
5857

5958
vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w;
60-
shadowCoordinateWdivide.z -= 0.000005;
59+
//shadowCoordinateWdivide.z -= 0.00005;
6160
float distanceFromLight = texture2D(shadowMap,shadowCoordinateWdivide.st).z;
6261
float shadow = 1.0;
63-
if (shadowCoordinateWdivide.x > 0.01 && shadowCoordinateWdivide.y > 0.01 && shadowCoordinateWdivide.x < 0.99 && shadowCoordinateWdivide.y < 0.99)
62+
if (shadowCoordinateWdivide.x > 0.001 && shadowCoordinateWdivide.y > 0.001 && shadowCoordinateWdivide.x < 0.999 && shadowCoordinateWdivide.y < 0.999)
6463
shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.0 : 1.0 ;
6564

65+
shadow = clamp(shadow+(1.0-shadowAmount), 0.0, 1.0);
66+
6667
vec4 color = diffuse_color;
6768
vec4 matspec = specular_color;
6869
float shininess = shininess;

Core/Contents/Include/PolyGLRenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
/*
33
Copyright (C) 2011 by Ivan Safrin
44

Core/Contents/Include/PolyRenderer.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ namespace Polycode {
103103
virtual void createRenderTextures(Texture **colorBuffer, Texture **depthBuffer, int width, int height, bool floatingPointBuffer) = 0;
104104

105105
virtual Texture *createFramebufferTexture(unsigned int width, unsigned int height) = 0;
106-
virtual void bindFrameBufferTexture(Texture *texture) = 0;
107-
virtual void bindFrameBufferTextureDepth(Texture *texture) = 0;
108-
virtual void unbindFramebuffers() = 0;
106+
virtual void bindFrameBufferTexture(Texture *texture);
107+
virtual void bindFrameBufferTextureDepth(Texture *texture);
108+
virtual void unbindFramebuffers();
109109

110110
virtual Image *renderScreenToImage() = 0;
111111
virtual Image *renderBufferToImage(Texture *texture) = 0;
@@ -315,6 +315,9 @@ namespace Polycode {
315315
std::stack<Color> vertexColorStack;
316316
Color currentVertexColor;
317317

318+
std::stack<Texture*> framebufferStackColor;
319+
std::stack<Texture*> framebufferStackDepth;
320+
318321
bool scissorEnabled;
319322

320323
Polycode::Rectangle scissorBox;

Core/Contents/Source/PolyGLRenderer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ void OpenGLRenderer::bindFrameBufferTextureDepth(Texture *texture) {
635635
return;
636636
OpenGLTexture *glTexture = (OpenGLTexture*)texture;
637637
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, glTexture->getFrameBufferID());
638-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
638+
Renderer::bindFrameBufferTextureDepth(texture);
639+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
639640
}
640641

641642

@@ -644,12 +645,14 @@ void OpenGLRenderer::bindFrameBufferTexture(Texture *texture) {
644645
return;
645646
OpenGLTexture *glTexture = (OpenGLTexture*)texture;
646647
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, glTexture->getFrameBufferID());
647-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
648+
Renderer::bindFrameBufferTexture(texture);
649+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
648650
}
649651

650652
void OpenGLRenderer::unbindFramebuffers() {
651653
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
652654
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
655+
Renderer::unbindFramebuffers();
653656
}
654657

655658
void OpenGLRenderer::createRenderTextures(Texture **colorBuffer, Texture **depthBuffer, int width, int height, bool floatingPointBuffer) {

Core/Contents/Source/PolyGLSLShaderModule.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ bool GLSLShaderModule::applyShaderMaterial(Renderer *renderer, Material *materia
375375
glLightf (GL_LIGHT0+lightIndex, GL_LINEAR_ATTENUATION, light.linearAttenuation);
376376
glLightf (GL_LIGHT0+lightIndex, GL_QUADRATIC_ATTENUATION, light.quadraticAttenuation);
377377

378+
Number shadowAmount = 0.0;
379+
378380
if(light.shadowsEnabled) {
379381
if(shadowMapTextureIndex < 4) {
380382
switch(shadowMapTextureIndex) {
@@ -401,22 +403,22 @@ bool GLSLShaderModule::applyShaderMaterial(Renderer *renderer, Material *materia
401403
glActiveTexture(GL_TEXTURE0 + textureIndex);
402404
glBindTexture(GL_TEXTURE_2D, ((OpenGLTexture*)light.shadowMapTexture)->getTextureID());
403405
textureIndex++;
406+
407+
LocalShaderParam *matParam = material->getShaderBinding(shaderIndex)->getLocalParamByName(matName);
408+
if(matParam) {
409+
matParam->setMatrix4(light.textureMatrix);
410+
}
404411

405-
int mloc = glGetUniformLocation(glslShader->shader_id, matName);
406-
light.textureMatrix = light.textureMatrix;
412+
shadowAmount = 1.0;
407413

408-
409-
GLfloat mat[16];
410-
for(int z=0; z < 16; z++) {
411-
mat[z] = light.textureMatrix.ml[z];
412-
}
413-
glUniformMatrix4fv(mloc, 1, false, mat);
414-
415-
416414
}
415+
417416
shadowMapTextureIndex++;
418-
} else {
419-
light.shadowsEnabled = false;
417+
}
418+
419+
LocalShaderParam *amountParam = material->getShaderBinding(shaderIndex)->getLocalParamByName("shadowAmount");
420+
if(amountParam) {
421+
amountParam->setNumber(shadowAmount);
420422
}
421423

422424
lightIndex++;
@@ -427,13 +429,12 @@ bool GLSLShaderModule::applyShaderMaterial(Renderer *renderer, Material *materia
427429
glEnable(GL_TEXTURE_2D);
428430

429431
Matrix4 modelMatrix = renderer->getModelviewMatrix() * renderer->getCameraMatrix();
430-
int mloc = glGetUniformLocation(glslShader->shader_id, "modelMatrix");
431-
GLfloat mat[16];
432-
for(int z=0; z < 16; z++) {
433-
mat[z] = modelMatrix.ml[z];
434-
}
435-
glUniformMatrix4fv(mloc, 1, false, mat);
436-
432+
LocalShaderParam *modelMatrixParam = material->getShaderBinding(shaderIndex)->getLocalParamByName("modelMatrix");
433+
434+
if(modelMatrixParam) {
435+
modelMatrixParam->setMatrix4(modelMatrix);
436+
}
437+
437438
ShaderBinding *cgBinding = material->getShaderBinding(shaderIndex);
438439

439440
for(int i=0; i < glslShader->expectedParams.size(); i++) {

Core/Contents/Source/PolyRenderer.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ void Renderer::clearLights() {
105105
spotLights.clear();
106106
}
107107

108+
109+
void Renderer::bindFrameBufferTexture(Texture *texture) {
110+
framebufferStackColor.push(texture);
111+
}
112+
113+
void Renderer::bindFrameBufferTextureDepth(Texture *texture) {
114+
framebufferStackDepth.push(texture);
115+
}
116+
117+
void Renderer::unbindFramebuffers() {
118+
if(framebufferStackColor.size() > 0) {
119+
framebufferStackColor.pop();
120+
}
121+
if(framebufferStackDepth.size() > 0) {
122+
framebufferStackDepth.pop();
123+
}
124+
125+
if(framebufferStackColor.size() > 0) {
126+
Texture *rebindTexture = framebufferStackColor.top();
127+
framebufferStackColor.pop();
128+
bindFrameBufferTexture(rebindTexture);
129+
}
130+
131+
if(framebufferStackDepth.size() > 0) {
132+
Texture *rebindTexture = framebufferStackDepth.top();
133+
framebufferStackDepth.pop();
134+
bindFrameBufferTextureDepth(rebindTexture);
135+
}
136+
137+
}
138+
108139
void Renderer::setExposureLevel(Number level) {
109140
exposureLevel = level;
110141
}

Core/Contents/Source/PolyScene.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,12 @@ void Scene::Render(Camera *targetCamera) {
245245
Vector3 direction;
246246
Vector3 position;
247247
matrixPtr = NULL;
248-
direction.x = 0;
249-
direction.y = 0;
250-
direction.z = -1;
251-
248+
direction.x = 0;
249+
direction.y = 0.0;
250+
direction.z = -1.0;
251+
direction.Normalize();
252252

253253
direction = light->getConcatenatedMatrix().rotateVector(direction);
254-
direction.Normalize();
255254

256255
Texture *shadowMapTexture = NULL;
257256
if(light->areShadowsEnabled()) {

Core/Contents/Source/PolySceneLight.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ void SceneLight::enableShadows(bool val, unsigned int resolution) {
7474
if(zBufferTexture) {
7575
CoreServices::getInstance()->getMaterialManager()->deleteTexture(zBufferTexture);
7676
}
77-
7877
CoreServices::getInstance()->getRenderer()->createRenderTextures(NULL, &zBufferTexture, resolution, resolution, false);
7978
if(!spotCamera) {
8079
spotCamera = new Camera(parentScene);
80+
/*
81+
spotCamera->setProjectionMode(Camera::ORTHO_SIZE_MANUAL);
82+
spotCamera->setOrthoSize(5.0, 5.0);
83+
*/
8184
spotCamera->editorOnly = true;
82-
// spotCamera->setPitch(-45.0f);
85+
spotCamera->setClippingPlanes(0.01, 100.0);
86+
// spotCamera->setPitch(90.0);
8387
addChild(spotCamera);
8488
}
8589
shadowMapRes = resolution;
@@ -124,22 +128,20 @@ unsigned int SceneLight::getShadowMapResolution() const {
124128
}
125129

126130
void SceneLight::renderDepthMap(Scene *scene) {
131+
spotCamera->setFOV(shadowMapFOV);
127132
Renderer* renderer = CoreServices::getInstance()->getRenderer();
128-
renderer->clearScreen();
129133
renderer->pushMatrix();
130134
renderer->loadIdentity();
131135

132136
Number vpW = renderer->getViewportWidth();
133137
Number vpH = renderer->getViewportHeight();
134138

135139
renderer->setViewportSize(shadowMapRes, shadowMapRes);
136-
Camera* camera = scene->getActiveCamera();
137-
renderer->setProjectionFromFoV(shadowMapFOV, camera->getNearClippingPlane(), camera->getFarClippingPlane());
138140
renderer->bindFrameBufferTexture(zBufferTexture);
139141

140142
scene->RenderDepthOnly(spotCamera);
141143

142-
lightViewMatrix = renderer->getModelviewMatrix() * renderer->getProjectionMatrix();
144+
lightViewMatrix = getConcatenatedMatrix().Inverse() * renderer->getProjectionMatrix();
143145
renderer->unbindFramebuffers();
144146
renderer->popMatrix();
145147
renderer->setViewportSize(vpW , vpH);

Examples/C++/Resources/default.pak

2.41 KB
Binary file not shown.

0 commit comments

Comments
 (0)