ソースを参照

The new SSAO is fucken working!!!!
Needs cleaning

Panagiotis Christopoulos Charitos 14 年 前
コミット
a31475d800

ファイルの差分が大きいため隠しています
+ 0 - 1
build/debug/Makefile


ファイルの差分が大きいため隠しています
+ 0 - 0
build/release/Makefile


+ 1 - 0
shaders/Final.glsl

@@ -10,6 +10,7 @@ uniform sampler2D rasterImage;
 in vec2 vTexCoords;
 layout(location = 0) out vec3 fFragColor;
 
+
 void main()
 {
 	//if( gl_FragCoord.x > 0.5 ) discard;

+ 4 - 2
shaders/MsMpGeneric.glsl

@@ -85,9 +85,9 @@ void main()
 	#endif
 
 
-	#if defined(ENVIRONMENT_MAPPING) || defined(PARALLAX_MAPPING)
+	//#if defined(ENVIRONMENT_MAPPING) || defined(PARALLAX_MAPPING)
 		vVertPosViewSpace = vec3(modelViewMat * vec4(position, 1.0));
-	#endif
+	//#endif
 }
 
 
@@ -136,6 +136,7 @@ in vec3 eye;
 layout(location = 0) out vec2 fMsNormalFai;
 layout(location = 1) out vec3 fMsDiffuseFai;
 layout(location = 2) out vec4 fMsSpecularFai;
+layout(location = 3) out vec3 fMsPosFai;
 
 
 //======================================================================================================================
@@ -312,5 +313,6 @@ void main()
 	fMsNormalFai = packNormal(_normal_);
 	fMsDiffuseFai = _diffColl_;
 	fMsSpecularFai = _specularCol_;
+	fMsPosFai = vVertPosViewSpace;
 }
 

+ 16 - 20
shaders/Pack.glsl

@@ -1,31 +1,27 @@
-/**
- * Pack 3D normal to 2D vector
- */
-vec2 packNormal( in vec3 _normal )
+/// Pack 3D normal to 2D vector
+vec2 packNormal(in vec3 normal)
 {
     /*original code:
-    const float _scale = 1.7777;
+    const float SCALE = 1.7777;
     vec2 _enc = _normal.xy / (_normal.z+1.0);
-    _enc /= _scale;
+    _enc /= SCALE;
     _enc = _enc*0.5 + 0.5;
     return _enc;*/
 
-    const float _scale = 1.7777;
-    float scalar1 = (_normal.z+1.0)*(_scale*2.0);
-    return _normal.xy / scalar1 + 0.5;
+    const float SCALE = 1.7777;
+    float scalar1 = (normal.z + 1.0) * (SCALE * 2.0);
+    return normal.xy / scalar1 + 0.5;
 }
 
 
-/**
- * Reverse the packNormal
- */
-vec3 unpackNormal(in vec2 _enc_)
+/// Reverse the packNormal
+vec3 unpackNormal(in vec2 enc)
 {
-	const float _scale_ = 1.7777;
-	vec2 _nn_ = _enc_ * (2.0 * _scale_) - _scale_;
-	float _g_ = 2.0 / (dot(_nn_.xy, _nn_.xy) + 1.0);
-	vec3 _normal_;
-	_normal_.xy = _g_ * _nn_.xy;
-	_normal_.z = _g_ - 1.0;
-	return _normal_;
+	const float SCALE = 1.7777;
+	vec2 nn = enc * (2.0 * SCALE) - SCALE;
+	float g = 2.0 / (dot(nn.xy, nn.xy) + 1.0);
+	vec3 normal;
+	normal.xy = g * nn.xy;
+	normal.z = g - 1.0;
+	return normal;
 }

+ 62 - 30
shaders/PpsSsao-2.glsl

@@ -25,15 +25,17 @@ void main()
 
 /// @name Uniforms
 /// @{
-uniform vec2 planes; ///< for the calculation of frag pos in view space	
-uniform sampler2D msDepthFai; ///< for the calculation of frag pos in view space	
+uniform vec2 planes; ///< for the calculation of frag pos in view space
+uniform sampler2D msDepthFai; ///< for the calculation of frag pos in view space
+uniform sampler2D msPosFai;
 uniform sampler2D noiseMap;
 uniform sampler2D msNormalFai;
+uniform vec2 limitsOfNearPlane;
 uniform float noiseMapSize = 100.0; /// Used in getRandom
 uniform float sampleRad = 0.1;  /// Used in main
 uniform float scale = 1.0; /// Used in doAmbientOcclusion
-uniform float intensity = 1.0; /// Used in doAmbientOcclusion
-uniform float bias = 0.001; /// Used in doAmbientOcclusion
+uniform float intensity = 2.0; /// Used in doAmbientOcclusion
+uniform float bias = 0.00; /// Used in doAmbientOcclusion
 uniform vec2 screenSize; /// Used in getRandom
 /// @}
 
@@ -49,20 +51,6 @@ layout(location = 0) out float fColor;
 /// @}
 
 
-/// Get frag position in view space
-/// globals: msDepthFai, planes, vViewVector
-vec3 getPosition(in vec2 uv)
-{
-	float depth = texture2D(msDepthFai, uv).r;
-
-	vec3 fragPosVspace;
-	vec3 vposn = normalize(vViewVector);
-	fragPosVspace.z = -planes.y / (planes.x + depth);
-	fragPosVspace.xy = vposn.xy * (fragPosVspace.z / vposn.z);
-	return fragPosVspace;
-}
-
-
 /// globals: msNormalFai
 vec3 getNormal(in vec2 uv)
 {
@@ -73,32 +61,63 @@ vec3 getNormal(in vec2 uv)
 /// globals: noiseMap, screenSize, noiseMapSize
 vec2 getRandom(in vec2 uv)
 {
-	return normalize(texture2D(noiseMap, screenSize * uv / noiseMapSize).xy * 2.0f - 1.0f);
+	return normalize(texture2D(noiseMap, screenSize * uv / noiseMapSize).xy * 2.0 - 1.0);
 }
 
 
-float doAmbientOcclusion(in vec2 tcoord, in vec2 uv, in vec3 p, in vec3 cnorm)
+/// Get frag position in view space
+/// globals: msDepthFai, planes
+vec3 getPosition(in vec2 uv)
 {
-	vec3 diff = getPosition(tcoord + uv) - p;
+	float depth = texture2D(msDepthFai, uv).r;
+
+	vec3 fragPosVspace;
+	fragPosVspace.z = -planes.y / (planes.x + depth);
+
+	/*vec3 vposn = normalize(vViewVector);
+	fragPosVspace.xy = vposn.xy * (fragPosVspace.z / vposn.z);*/
+	
+	fragPosVspace.xy = (((uv) * 2.0) - 1.0) * limitsOfNearPlane;
+	
+	float sc = -fragPosVspace.z / 0.5;
+    fragPosVspace.xy *= sc;
+
+	return fragPosVspace;
+	//return abs(fragPosVspace - texture2D(msPosFai, uv).xyz);
+	//return vec3(texture2D(msPosFai, uv).xy, fragPosVspace.z);
+}
+
+
+float doAmbientOcclusion(in vec2 tcoord, in vec2 uv, in vec3 original, in vec3 cnorm)
+{
+	vec3 newp = getPosition(tcoord + uv);
+	vec3 diff = newp - original;
 	vec3 v = normalize(diff);
 	float d = length(diff) * scale;
-	return max(0.0, dot(cnorm, v) - bias) * (1.0 / (1.0 + d)) * intensity;
+
+	float ret = max(0.0, dot(cnorm, v) - bias) * (1.0 / (1.0 + d)) * intensity;
+	return ret;
+	//return newp.x / 10.0;
 }
 
 
 void main(void)
-{	
-	const vec2 kernel[4] = vec2[](vec2(1, 0), vec2(-1, 0), vec2(0, 1), vec2(0, -1));
+{
+	//const vec2 kernel[4] = vec2[](vec2(1, 0), vec2(-1, 0), vec2(0, 1), vec2(0, -1));
 
-	vec3 p = getPosition(vTexCoords);
+	const vec2 KERNEL[16] = vec2[](vec2(0.53812504, 0.18565957), vec2(0.13790712, 0.24864247), vec2(0.33715037, 0.56794053), vec2(-0.6999805, -0.04511441), vec2(0.06896307, -0.15983082), vec2(0.056099437, 0.006954967), vec2(-0.014653638, 0.14027752), vec2(0.010019933, -0.1924225), vec2(-0.35775623, -0.5301969), vec2(-0.3169221, 0.106360726), vec2(0.010350345, -0.58698344), vec2(-0.08972908, -0.49408212), vec2(0.7119986, -0.0154690035), vec2(-0.053382345, 0.059675813), vec2(0.035267662, -0.063188605), vec2(-0.47761092, 0.2847911));
+
+	vec3 original = getPosition(vTexCoords);
 	vec3 n = getNormal(vTexCoords);
 	vec2 rand = getRandom(vTexCoords);
 
-	float ao = 0.0f;
+	//rand = rand - rand + vec2(0.0, 0.0);
+
+	fColor = 0.0;
 	float rad = sampleRad ;// p.z;
 
 	// SSAO Calculation
-	const int ITERATIONS = 4;
+	/*const int ITERATIONS = 4;
 	for(int j = 0; j < ITERATIONS; ++j)
 	{
 		vec2 coord1 = reflect(kernel[j], rand) * rad;
@@ -109,10 +128,23 @@ void main(void)
 		ao += doAmbientOcclusion(vTexCoords, coord1 * 0.75, p, n);
 		ao += doAmbientOcclusion(vTexCoords, coord2, p, n);
 	} 
-	ao /= ITERATIONS * 4.0;
+	ao /= ITERATIONS * 4.0;*/
 
-	fColor = 1 - ao;
-	
+	const int ITERATIONS = 16;
+	for(int j = 0; j < ITERATIONS; ++j)
+	{
+		vec2 coord = reflect(KERNEL[j], rand) * rad;
+		fColor += doAmbientOcclusion(vTexCoords, coord, original, n);
+	}
+
+	fColor = 1.0 - fColor / ITERATIONS;
+
+	//fColor = gl_FragCoord.y / (720.0 / 2);
+
+	/*vec3 diff = getPosition(vTexCoords) - texture2D(msPosFai, vTexCoords).xyz;
+	fColor = (diff.x + diff.y + diff.z) / 3.0;*/
+
+	//fColor = original.x;
 	
 	/*vec2 v = getRandom(vTexCoords);	
 	fColor = (fColor - fColor) + ((v.x + v.y) / 2.0);*/

+ 15 - 1
src/Main.cpp

@@ -123,7 +123,7 @@ void init()
 
 	// camera
 	PerspectiveCamera* cam = new PerspectiveCamera(false, NULL);
-	cam->setAll(MainRendererSingleton::getInstance().getAspectRatio()*toRad(60.0), toRad(60.0), 0.5, 200.0);
+	cam->setAll(toRad(100.0), toRad(100.0) / MainRendererSingleton::getInstance().getAspectRatio(), 0.5, 200.0);
 	cam->moveLocalY(3.0);
 	cam->moveLocalZ(5.7);
 	cam->moveLocalX(-0.3);
@@ -417,6 +417,20 @@ void mainLoop()
 //======================================================================================================================
 int main(int argc, char* argv[])
 {
+	/*float depth = 0.5;
+
+	Vec3 vViewVector(1.0264, 0.57735, -1);
+	Vec2 planes(-1.00251, -0.501253);
+
+	Vec3 fragPosVspace;
+	Vec3 vposn = vViewVector.getNormalized();
+	fragPosVspace.z() = -planes.y() / (planes.x() + depth);
+	fragPosVspace.x() = vposn.x() * (fragPosVspace.z() / vposn.z());
+	fragPosVspace.y() = vposn.y() * (fragPosVspace.z() / vposn.z());
+	std::cout << fragPosVspace << std::endl;
+
+	return 0;*/
+
 	try
 	{
 		AppSingleton::getInstance().init(argc, argv);

+ 1 - 1
src/Renderer/MainRenderer.cpp

@@ -100,7 +100,7 @@ void MainRenderer::render(Camera& cam_)
 	glDisable(GL_DEPTH_TEST);
 	glDisable(GL_BLEND);
 	sProg->bind();
-	//sProg->findUniVar("rasterImage")->setTexture(ms.getNormalFai(), 0);
+	//sProg->findUniVar("rasterImage")->set(ms.getNormalFai(), 0);
 	sProg->findUniVar("rasterImage")->set(pps.getSsao().ssaoFai, 0);
 	//sProg->findUniVar("rasterImage")->set(pps.getPostPassFai(), 0);
 	drawQuad();

+ 5 - 1
src/Renderer/Ms.cpp

@@ -27,7 +27,7 @@ void Ms::init(const RendererInitializer& initializer)
 		fbo.bind();
 
 		// inform in what buffers we draw
-		fbo.setNumOfColorAttachements(3);
+		fbo.setNumOfColorAttachements(4);
 
 		// create the FAIs
 		normalFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RG16F, GL_RG, GL_FLOAT);
@@ -36,15 +36,19 @@ void Ms::init(const RendererInitializer& initializer)
 		depthFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL,
 		                       GL_UNSIGNED_INT_24_8);
 
+		posFai.createEmpty2D(r.getWidth(), r.getHeight(), GL_RGB16F, GL_RGB, GL_FLOAT);
+
 		normalFai.setRepeat(false);
 		diffuseFai.setRepeat(false);
 		specularFai.setRepeat(false);
 		depthFai.setRepeat(false);
+		posFai.setRepeat(false);
 
 		// attach the buffers to the FBO
 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, normalFai.getGlId(), 0);
 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, diffuseFai.getGlId(), 0);
 		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, specularFai.getGlId(), 0);
+		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, posFai.getGlId(), 0);
 
 		//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);
 		//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthFai.getGlId(), 0);

+ 2 - 0
src/Renderer/Ms.h

@@ -15,6 +15,8 @@ class RendererInitializer;
 class Ms: public RenderingPass
 {
 	public:
+		Texture posFai; ///< @todo remove it
+
 		Ms(Renderer& r_);
 
 		/// @name Accessors

+ 39 - 10
src/Renderer/Ssao.cpp

@@ -3,6 +3,7 @@
 #include "Renderer.h"
 #include "Camera.h"
 #include "RendererInitializer.h"
+#include "PerspectiveCamera.h"
 
 
 //======================================================================================================================
@@ -137,25 +138,53 @@ void Ssao::run()
 	//
 	
 	boost::array<Vec3, 4> viewVectors;
-	boost::array<float, 2> gScreenSize = {{r.getWidth(), r.getHeight()}};
+	boost::array<float, 2> gScreenSize = {{width, height}};
 	Is::calcViewVectors(gScreenSize, cam.getInvProjectionMatrix(), viewVectors);
 	viewVectorsVbo.write(&viewVectors[0]);
 	
-	Vec2 planes;
-	Is::calcPlanes(Vec2(r.getCamera().getZNear(), r.getCamera().getZFar()), planes);
+	/*BOOST_FOREACH(Vec3 v, viewVectors)
+	{
+		std::cout << v << std::endl;
+	}
+	std::cout << "----------------" << std::endl;*/
 	
+	//std::cout << planes << std::endl;
+
 	ssaoFbo.bind();
 	ssaoSProg->bind();
 	
-	ssaoSProg->findUniVar("planes")->set(&planes);
-	ssaoSProg->findUniVar("msDepthFai")->set(r.getMs().getDepthFai(), 0);
-	ssaoSProg->findUniVar("noiseMap")->set(*noiseMap, 1);
-	ssaoSProg->findUniVar("msNormalFai")->set(r.getMs().getNormalFai(), 2);
+	Vec2 planes;
+	Is::calcPlanes(Vec2(r.getCamera().getZNear(), r.getCamera().getZFar()), planes);
+	if(ssaoSProg->uniVarExists("planes"))
+		ssaoSProg->findUniVar("planes")->set(&planes);
+
+	Vec2 limitsOfNearPlane;
+	const PerspectiveCamera& pcam = static_cast<const PerspectiveCamera&>(cam);
+	limitsOfNearPlane.y() = cam.getZNear() * tan(0.5 * pcam.getFovY());
+	limitsOfNearPlane.x() = limitsOfNearPlane.y() * (pcam.getFovX() / pcam.getFovY());
+	if(ssaoSProg->uniVarExists("limitsOfNearPlane"))
+		ssaoSProg->findUniVar("limitsOfNearPlane")->set(&limitsOfNearPlane);
+
+	if(ssaoSProg->uniVarExists("msDepthFai"))
+		ssaoSProg->findUniVar("msDepthFai")->set(r.getMs().getDepthFai(), 0);
+
+	if(ssaoSProg->uniVarExists("noiseMap"))
+		ssaoSProg->findUniVar("noiseMap")->set(*noiseMap, 1);
+
+	if(ssaoSProg->uniVarExists("msNormalFai"))
+		ssaoSProg->findUniVar("msNormalFai")->set(r.getMs().getNormalFai(), 2);
+
 	Vec2 screenSize(width, height);
-	ssaoSProg->findUniVar("screenSize")->set(&screenSize);
+	if(ssaoSProg->uniVarExists("screenSize"))
+		ssaoSProg->findUniVar("screenSize")->set(&screenSize);
+
 	float noiseMapSize = noiseMap->getWidth();
-	ssaoSProg->findUniVar("noiseMapSize")->set(&noiseMapSize);
-	
+	if(ssaoSProg->uniVarExists("noiseMapSize"))
+		ssaoSProg->findUniVar("noiseMapSize")->set(&noiseMapSize);
+
+	if(ssaoSProg->uniVarExists("msPosFai"))
+		ssaoSProg->findUniVar("msPosFai")->set(r.getMs().posFai, 3);
+
 	vao.bind();
 	glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
 	vao.unbind();

+ 1 - 1
src/Resources/ShaderProg/ShaderProg.cpp

@@ -57,7 +57,7 @@ uint ShaderProg::createAndCompileShader(const char* sourceCode, const char* prep
 		infoLog.resize(infoLen + 1);
 		glGetShaderInfoLog(glId, infoLen, &charsWritten, &infoLog[0]);
 		
-		const char* shaderType;
+		const char* shaderType = "*dummy*";
 		switch(type)
 		{
 			case GL_VERTEX_SHADER:

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません