Commits

Frederic De Groef  committed edbc5bb

made the ogre ssao scheme work

  • Participants
  • Parent commits 5ce36f4

Comments (0)

Files changed (7)

File Dependencies/OgreCommon/ExampleApplication.h

         mCamera->setPosition(Vector3(0,0,500));
         // Look back along -Z
         mCamera->lookAt(Vector3(0,0,-300));
-        mCamera->setNearClipDistance(5);
+        mCamera->setNearClipDistance(.1);
+        mCamera->setNearClipDistance(1000);
+
 
     }
     virtual void createFrameListener(void)

File media/objects.material

     scheme lighting
     pass
     {
-      ambient 0.5 0.5 0.5 1.0
+      ambient 0.3 0.3 0.3 1.0
       diffuse  0.5 0.5 0.5
     }
   }

File media/ssao/crytek_ssao.cg

 
 
 
+
 #define PI 3.14159265
-#define NUM_SAMPLES 16
-#define INV_SAMPLES 1.0f / 16.0f
-#define R_FAR 0.6
-
 
 
 
 	return compute_sphere_cap_area(PC, r) * max(dot(n, normalize(PC)), 0.0f);
 }
 
+
+bool is_in_area(float2 top_left, float2 bottom_right, float2 pos)
+{
+    return	pos.x > top_left.x && pos.x < bottom_right.x &&  	
+			pos.y > top_left.y && pos.y < bottom_right.y;		
+}
+
 //------------------------------------------------------------------------------
 
+//------------------------------------------------------------------------------
+
+
+#define NUM_SAMPLES 16
+#define INV_SAMPLES 1.0f / 16.0f
+#define R_FAR 0.5
+
+#define TOP_LEFT float2(0.2,0.2)
+#define BOTTOM_RIGHT float2(0.8, 0.8)
+
+
 void crytek_ssao_ps(
     FragmentInput IN
     ,uniform sampler2D geomMap : TEXUNIT0
 	,uniform sampler2D randMap : TEXUNIT1
 	,uniform float radius
-	,uniform float totStrength
-	,uniform float strength
-	,uniform float offset
-	,uniform float falloff	
+	,uniform float far
+	,uniform float4x4 clip_to_image_matrix 
 	,out float4 oColor : COLOR0)
 {
-	float4 sample = tex2D(geomMap, IN.pixelPos);
-	float depth = sample.a;
-	float3 normal = sample.rgb;
 
-	float3 P = float3(IN.pixelPos, depth);
-	float occlusion = 0.0f;
-	
-	for(int i=0 ; i<2; i++)
-	{
-		for(float theta=0.0f; theta < 2*PI ; theta += PI/4)
+    #define MAX_RAND_SAMPLES 14
+
+    const float3 RAND_SAMPLES[MAX_RAND_SAMPLES] =
+    {
+        float3(1, 0, 0),
+        float3(	-1, 0, 0),
+        float3(0, 1, 0),
+        float3(0, -1, 0),
+        float3(0, 0, 1),
+        float3(0, 0, -1),
+        normalize(float3(1, 1, 1)),
+        normalize(float3(-1, 1, 1)),
+        normalize(float3(1, -1, 1)),
+        normalize(float3(1, 1, -1)),
+        normalize(float3(-1, -1, 1)),
+        normalize(float3(-1, 1, -1)),
+        normalize(float3(1, -1, -1)),
+        normalize(float3(-1, -1, -1))
+    };
+
+
+
+	//if( is_in_area(TOP_LEFT, BOTTOM_RIGHT, IN.pixelPos))
+	//{		
+
+	    float3 randN = tex2D(randMap, IN.pixelPos * 24).xyz * 2.0 - 1.0;	
+
+		float4 current_pixel_data = tex2D(geomMap, IN.pixelPos);
+		float depth = current_pixel_data.r;
+		float3 normal = current_pixel_data.gba;
+
+		float3 viewPos = IN.eyeViewVector * depth;
+
+		float occlusion = 0.0f;
+		
+		// sampling neighbours
+		for(int i=0 ; i<6; i++)
 		{
-			float2 neighbour_coords = IN.pixelPos + (radius * i * float2(cos(theta), sin(theta)));
-			float neighbour_depth = tex2D(geomMap, neighbour_coords).a;
-			float3 Qi = float3(neighbour_coords, neighbour_depth);
-			float ri = radius;
 
-			if (length(P - Qi)  < R_FAR)
-			{
-				occlusion += compute_approximate_AO(Qi, ri, P, normal);
-			}			
+			float3 direction = reflect(RAND_SAMPLES[i], randN) + normal; 
+
+			float4 neighbour_coords = mul(clip_to_image_matrix,  float4(viewPos + direction * radius, 1));
+			neighbour_coords.xy /=  neighbour_coords.w;
+
+
+			float zd = saturate(far * (depth - tex2D(geomMap, neighbour_coords.xy).r));
+		
+	        occlusion += saturate(pow(1.0 - zd, 11) + zd);	
+			//occlusion += 1.0  / (1.0+zd*zd);
 		}
-	}
-	//occlusion = 1.0f - (occlusion*50);
-	occlusion *= 10;
-	oColor=float4(occlusion, occlusion, occlusion, 1.0f);
 
+		occlusion /= 6;
+		oColor=float4(occlusion, occlusion, occlusion, 1.0f);
+	//}
+//
+	//else
+	//{		
+		//oColor=float4( 1.0);
+	//}
 }

File media/ssao/crytek_ssao.material

             }
 
             fragment_program_ref crytek_ssao_ps
-            {
-				param_named totStrength float 1.38
-				param_named strength float 0.07
-				param_named offset float 18.0
-				param_named falloff float 0.000002
-				param_named radius float 0.006
+			{
+				param_named radius float 0.2125
             }
 
             texture_unit geomMap
 
             texture_unit randMap
             {
-                texture noise.dds
+                texture random.png
                 filtering none
 				tex_coord_set 0
 				tex_address_mode wrap

File ulb-building-visu/OgreApplication.cpp

     mCamera = mSceneMgr->createCamera("PlayerCam");
 
 
-    mCamera->setNearClipDistance(0.01);
+    mCamera->setNearClipDistance(1);
     mCamera->setFarClipDistance(1000);
 
     //mCameraBaseNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("Camera Base Node");
     // get the fragment shader parameters
     params = pass->getFragmentProgramParameters();
     //// set the projection matrix we need
-    //static const Ogre::Matrix4 CLIP_SPACE_TO_IMAGE_SPACE(
-    //    0.5,    0,    0,  0.5,
-    //    0,   -0.5,    0,  0.5,
-    //    0,      0,    1,    0,
-    //    0,      0,    0,    1);
+    static const Ogre::Matrix4 CLIP_SPACE_TO_IMAGE_SPACE(
+        0.5,    0,    0,  0.5,
+        0,   -0.5,    0,  0.5,
+        0,      0,    1,    0,
+        0,      0,    0,    1);
 
-  //
-  //if (params->_findNamedConstantDefinition("ptMat"))
-  //      params->setNamedConstant("ptMat", CLIP_SPACE_TO_IMAGE_SPACE * cam->getProjectionMatrixWithRSDepth());
-  //  else
-  //      OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
-  //                 , "Could not find parameter ptMat in material " + mat->getName()
-  //                 , "Ogre::Application::notifyMaterialRenderer()");
-  // 
-
+  
+    if (params->_findNamedConstantDefinition("clip_to_image_matrix"))
+        params->setNamedConstant("clip_to_image_matrix", CLIP_SPACE_TO_IMAGE_SPACE * cam->getProjectionMatrixWithRSDepth());
+    else
+        OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
+                   , "Could not find parameter 'clip_to_image_matrix' in material " + mat->getName()
+                   , "Ogre::Application::notifyMaterialRenderer()");
+   
 
     if (params->_findNamedConstantDefinition("far"))
         params->setNamedConstant("far", cam->getFarClipDistance());
-    //else
-    //    OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
-    //               , "Could not find parameter 'far' in material " + mat->getName()
-    //               , "Ogre::Application::notifyMaterialRenderer()");
+    else
+        OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
+                   , "Could not find parameter 'far' in material " + mat->getName()
+                   , "Ogre::Application::notifyMaterialRenderer()");
 
 
 }
+
 //-----------------------------------------------------------------------------
 void OgreApplication::_saveBuffers()
 {

File ulb-building-visu/OgreApplication.h

     void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat);
     void _saveBuffers();
 
+
 protected:
     void _createGrid(int);
     Ogre::String mTitle;
     Ogre::RenderWindow* mWindow;
     DebugOverlay *mDebugOverlay;
 
-    //Ogre::SceneNode *mCameraNode, *mCameraTargetNode, *mCameraBaseNode;
-    //Ogre::Real mRotateSpeed;
 
     OIS::InputManager *mInputManager;
     OIS::Keyboard *mKeyboard;

File ulb-building-visu/ULBBuildingApp.cpp

     ////_loadMesh("ulb_building_Z_LAYOUT", Vector3(-1300, 0, 0));
 
 
+
     Ogre::Plane plane(Ogre::Vector3::UNIT_Y, 0.0f);
     Ogre::MeshPtr planeMesh = 
         Ogre::MeshManager::getSingleton().createPlane("ground"
     _loadMesh("ogrehead", Vector3(-50, 20, 0));
 
 
-    //_loadMesh("cassini_10", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_11", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_13", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_14", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_15", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a1", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a2", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a4", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a5", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a6", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a7", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_a8", Vector3(-200, 100, 0));
-    //_loadMesh("cassini_as", Vector3(-200, 100, 0));
+    _loadMesh("cassini_10", Vector3(-200, 100, 0));
+    _loadMesh("cassini_11", Vector3(-200, 100, 0));
+    _loadMesh("cassini_13", Vector3(-200, 100, 0));
+    _loadMesh("cassini_14", Vector3(-200, 100, 0));
+    _loadMesh("cassini_15", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a1", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a2", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a4", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a5", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a6", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a7", Vector3(-200, 100, 0));
+    _loadMesh("cassini_a8", Vector3(-200, 100, 0));
+    _loadMesh("cassini_as", Vector3(-200, 100, 0));