Răsfoiți Sursa

Shadows now work

Panagiotis Christopoulos Charitos 13 ani în urmă
părinte
comite
d6388cbc60
6 a modificat fișierele cu 67 adăugiri și 10 ștergeri
  1. 38 1
      shaders/IsLpGeneric.glsl
  2. 2 2
      src/renderer/Drawer.cpp
  3. 21 5
      src/renderer/Is.cpp
  4. 1 0
      src/renderer/Ms.cpp
  5. 3 0
      src/renderer/Sm.cpp
  6. 2 2
      testapp/Main.cpp

+ 38 - 1
shaders/IsLpGeneric.glsl

@@ -5,6 +5,7 @@
 #pragma anki start vertexShader
 
 #pragma anki include "shaders/IsLpVertex.glsl"
+#pragma anki include "shaders/LinearDepth.glsl"
 
 #pragma anki start fragmentShader
 
@@ -64,7 +65,7 @@ layout(std140, row_major, binding = 1) uniform pointLightsBlock
 	Light plights[MAX_POINT_LIGHTS];
 };
 
-layout(std140, row_major, binding = 2) uniform spotLightsBlock
+layout(std140, binding = 2) uniform spotLightsBlock
 {
 	SpotLight slights[MAX_SPOT_LIGHTS];
 };
@@ -85,6 +86,8 @@ uniform sampler2D msDepthFai;
 
 uniform sampler2D lightTextures[MAX_SPOT_LIGHTS];
 uniform sampler2DShadow shadowMaps[MAX_SPOT_LIGHTS];
+
+uniform mat4 matrix; /// XXX
 /// @}
 
 /// @name Varyings
@@ -226,6 +229,7 @@ void main()
 
 	for(uint i = 0; i < spotLightsShadowCount; ++i)
 	{
+#if 1
 		uint id = i + opt;
 		uint lightId = tiles[vInstanceId].lightIndices[id / 4][id % 4];
 
@@ -238,6 +242,39 @@ void main()
 			shadowMaps[i]);
 
 		fColor += pureColor * (spotFactor * shadowFactor);
+#endif
+
+#if 0
+		uint id = i + opt;
+		uint lightId = tiles[vInstanceId].lightIndices[id / 4][id % 4];
+
+		mat4 mat = slights[lightId].texProjectionMat;
+		vec4 texCoords4 = mat * vec4(fragPosVspace, 1.0);
+		vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
+
+		if(texCoords4.w > 0.0 &&
+			texCoords3.x > 0.0 &&
+			texCoords3.x < 1.0 &&
+			texCoords3.y > 0.0 &&
+			texCoords3.y < 1.0)
+		{
+			//vec3 color = textureProj(lightTextures[0], texCoords3).rgb;
+			float color = linearizeDepth(textureProj(shadowMaps[i], texCoords3).r, 0.1, 30.0);
+
+			fColor += vec3(color);
+		}
+#endif
+
+#if 0
+		/*mat4 mat = slights[1].texProjectionMat;*/
+		vec4 lala = vec4(-1, 1, 0, 1);
+		lala = slights[1].texProjectionMat * lala;
+
+		if(lala != vec4(0, 1, 0.5, 1.0))
+			fColor = lala.rgb;
+		else
+			fColor = vec3(1.0);
+#endif
 	}
 
 #if 0

+ 2 - 2
src/renderer/Drawer.cpp

@@ -660,14 +660,14 @@ void RenderableDrawer::setupShaderProg(
 	const Material& mtl = renderable.getMaterial();
 	const ShaderProgram& sprog = mtl.findShaderProgram(key);
 
-	if(mtl.getDepthTestingEnabled())
+	/*if(mtl.getDepthTestingEnabled())
 	{
 		GlStateSingleton::get().enable(GL_DEPTH_TEST);
 	}
 	else
 	{
 		GlStateSingleton::get().disable(GL_DEPTH_TEST);
-	}
+	}*/
 
 	sprog.bind();
 	

+ 21 - 5
src/renderer/Is.cpp

@@ -132,16 +132,17 @@ struct WriteSpotLightsUbo: ThreadJob
 			lightDir = cam->getViewMatrix().getRotationPart() * lightDir;
 			slight.lightDirection = Vec4(lightDir, 0.0);
 			
-			/*static const Mat4 biasMat4(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 
-				0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0);*/
 			static const Mat4 biasMat4(
-				0.5, 0.0, 0.0, 0.0, 
-				0.0, 0.5, 0.0, 0.0, 
-				0.0, 0.0, 0.5, 0.0, 
+				0.5, 0.0, 0.0, 0.5, 
+				0.0, 0.5, 0.0, 0.5, 
+				0.0, 0.0, 0.5, 0.5, 
 				0.0, 0.0, 0.0, 1.0);
 			slight.texProjectionMat = biasMat4 * light.getProjectionMatrix() *
 				Mat4::combineTransformations(light.getViewMatrix(),
 				Mat4(cam->getWorldTransform()));
+
+			// Transpose because of driver bug
+			slight.texProjectionMat.transpose();
 		}
 	}
 };
@@ -742,6 +743,7 @@ void Is::lightPass()
 	lightPassProg->findUniformVariable("msDepthFai").set(
 		r->getMs().getDepthFai());
 
+#if 1
 	for(U i = 0; i < spotsShadowCount; i++)
 	{
 		char str[128];
@@ -749,6 +751,20 @@ void Is::lightPass()
 
 		lightPassProg->findUniformVariable(str).set(*shadowmaps[i]);
 	}
+#endif
+
+#if 0
+	{
+	SpotLight& light = *visibleSpotShadowLights[0];
+	static const Mat4 biasMat4(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 
+				0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0);
+	Mat4 matrix = biasMat4 * light.getProjectionMatrix() *
+				Mat4::combineTransformations(light.getViewMatrix(),
+				Mat4(cam->getWorldTransform()));
+
+	lightPassProg->findUniformVariable("matrix").set(matrix);
+	}
+#endif
 
 	r->drawQuadInstanced(TILES_Y_COUNT * TILES_X_COUNT);
 }

+ 1 - 0
src/renderer/Ms.cpp

@@ -57,6 +57,7 @@ void Ms::run()
 
 	GlStateSingleton::get().setViewport(0, 0, r->getWidth(), r->getHeight());
 	GlStateSingleton::get().disable(GL_BLEND);
+	GlStateSingleton::get().enable(GL_DEPTH_TEST);
 
 	//GlStateMachineSingleton::get().enable(GL_DEPTH_TEST, true);
 	//app->getScene().skybox.Render(cam.getViewMatrix().getRotationPart());

+ 3 - 0
src/renderer/Sm.cpp

@@ -177,6 +177,9 @@ Texture* Sm::doLight(Light& light)
 	// Render
 	//
 	sm.fbo.bind();
+	glClear(GL_DEPTH_BUFFER_BIT);
+
+	//std::cout << "Shadowmap for: " << &sm << std::endl;
 
 	for(auto it = vi.getRenderablesBegin(); it != vi.getRenderablesEnd(); ++it)
 	{

+ 2 - 2
testapp/Main.cpp

@@ -118,7 +118,7 @@ void init()
 	spot->setInnerAngle(toRad(15.0));
 	spot->setLocalTransform(Transform(Vec3(5.3, 4.3, 3.0),
 		Mat3::getIdentity(), 1.0));
-	spot->setDiffuseColor(Vec4(1.0, 0.0, 0.0, 0.0));
+	spot->setDiffuseColor(Vec4(3.0, 0.0, 0.0, 0.0));
 	spot->setSpecularColor(Vec4(0.0, 0.0, 1.0, 0.0));
 	spot->loadTexture("gfx/lights/flashlight.tga");
 	spot->setDistance(30.0);
@@ -337,7 +337,7 @@ void initSubsystems(int argc, char* argv[])
 	RendererInitializer initializer;
 	initializer.ms.ez.enabled = true;
 	initializer.dbg.enabled = true;
-	initializer.is.sm.bilinearEnabled = true;
+	initializer.is.sm.bilinearEnabled = false;
 	initializer.is.sm.enabled = true;
 	initializer.is.sm.pcfEnabled = true;
 	initializer.is.sm.resolution = 1024;