Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2957)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2958)
@@ -11,13 +11,17 @@
 	float3 color: COLOR;
 	float2 texcoord: TEXCOORD0;
-	float3 tangent : TEXCOORD1;
 };
 
 
-struct VertexOutput
+// vtx output
+struct vtxout
 {
-	float4 position: POSITION;
-	float4 vs_color: COLOR;
+	float4 position: POSITION; // eye space
+	float4 texCoord: TEXCOORD0;    
 
+	float4 color: COLOR0;  
+	float4 worldPos: TEXCOORD1; // world position
+	float3 normal: TEXCOORD2;
+	float4 mypos: TEXCOORD3;
 };
 
@@ -35,24 +39,23 @@
 
 
-VertexOutput default_vs(VertexInput IN,
-						uniform float3 lightDir,
-						uniform float2 thetaSun,
-						uniform float3 zenithColor,
-						uniform float3 aColor,
-						uniform float3 bColor,
-						uniform float3 cColor,
-						uniform float3 dColor,
-						uniform float3 eColor)
+vtxout default_vs(VertexInput IN,
+				  uniform float3 lightDir,
+				  uniform float2 thetaSun,
+				  uniform float3 zenithColor,
+				  uniform float3 aColor,
+				  uniform float3 bColor,
+				  uniform float3 cColor,
+				  uniform float3 dColor,
+				  uniform float3 eColor,
+				  uniform float4x4 ModelView)
 {
-	VertexOutput OUT;
-	
-	//OUT.position = mul(float4(IN.position.xyz, 1.0), WorldViewProjection);
-	//OUT.position = mul(float4(IN.position.xyz, 1.0), glstate.matrix.mvp);
+	vtxout OUT;
+
 	OUT.position = mul(glstate.matrix.mvp, IN.position);
 
-	
+
 	const float dotLN = dot(lightDir, IN.normal);   
 	const float cos2gamma = dotLN * dotLN; 
-    
+
 	const float gamma = acos(dotLN);    
 	const float theta = dot(float3(0.0, 0.0, 1.0), IN.normal);
@@ -68,9 +71,4 @@
 	XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z;   
 	
-	/*XYZ.x = (xyY.x / xyY.z) * xyY.y;                                                                        
-    XYZ.y = ((1.0 - xyY.x - xyY.z) / xyY.z) * xyY.y;
-    XYZ.z = xyY.y;
-	*/
-	//XYZ = float3(1, 1, 0);
     const static float3x3 conv_Mat = 
 		float3x3(3.240479, -1.537150, -0.498535,
@@ -79,18 +77,13 @@
 
 	float3 hcol = mul(conv_Mat, XYZ);
+	OUT.color = float4(hcol, 1.0);
 
-	/*hcol.x = 3.240479f*XYZ.x -1.537150f*XYZ.y-0.498535f*XYZ.z;
-	hcol.y = -0.969256f*XYZ.x +1.875992f*XYZ.y +0.041556f*XYZ.z;	
-	hcol.z = 0.055648f*XYZ.x -0.204043f*XYZ.y +1.057311f*XYZ.z;
-*/
-	//OUT.vs_color = float4(mul(transpose(conv_Mat), XYZ), 1.0); 
-	//OUT.vs_color = float4(mul(XYZ, transpose(conv_Mat)), 1.0); 
-	//OUT.vs_color = float4(mul(xyY, conv_Mat), 1.0); 
-	OUT.vs_color = float4(hcol, 1.0);
-	//OUT.vs_color = float4(zenithColor, 1.0);
+	OUT.color.rgb *= 5e-5f; 
 
-	//OUT.vs_color = float4(float3(dotLN), 1.0);
-	//OUT.vs_color.rgb *= 0.0003f; 
-	OUT.vs_color.rgb *= 5e-5f; 
+	//OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
+	OUT.worldPos = mul(ModelView, IN.position);
+
+	OUT.normal = IN.normal;
+	OUT.mypos = OUT.position;
 
 	return OUT;
@@ -98,17 +91,21 @@
 
 
+pixel frag_skydome(fragin IN, 
+				   uniform float maxDepth)
+{
+	pixel pix;
 
+	pix.col = IN.color;
+	pix.pos = IN.worldPos;// * maxDepth;
 
-//--------------------------------------------------------------------------------------
-// Pixel Shaders
-//--------------------------------------------------------------------------------------
+	pix.norm.xyz = IN.normal;
+	
+	// hack: squeeze some information about the ambient term into the target
+	pix.norm.w = 0;
+	pix.pos.w = IN.mypos.w;
+	
+	// the projected depth
+	pix.col.w = IN.mypos.z / IN.mypos.w;
 
-
-PixelOutput default_ps(VertexOutput IN)
-{
-	PixelOutput OUT;
-
-	OUT.color = IN.vs_color;
-	
-	return OUT;
+	return pix;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2957)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2958)
@@ -30,7 +30,5 @@
 	float4 projPos: WPOS;
 	float4 worldPos: TEXCOORD1; // world position
-
 	float3 normal: TEXCOORD2;
-
 	float4 mypos: TEXCOORD3;
 };
@@ -55,11 +53,10 @@
 	OUT.texCoord = IN.texCoord;
 
+	//OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
+	OUT.worldPos = mul(ModelView, IN.position);
 	// transform the vertex position into eye space
 	OUT.position = mul(glstate.matrix.mvp, IN.position);
-	//OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
-	OUT.worldPos = mul(ModelView, IN.position);
 
 	OUT.normal = IN.normal;
-
 	OUT.mypos = OUT.position;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2957)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 2958)
@@ -82,5 +82,4 @@
 	float sun_theta[2];
 
-	//sun_theta[0] = acos(sunDir.y);
 	sun_theta[0] = acos(sunDir.z);
 
@@ -124,10 +123,8 @@
 	ABCDE_Y[4] = -0.06696 * mTurbidity + 0.37027;
 
-	//F().getRenderDevice().setDefaultDepthStencilMode(FRenderDevice::DEPTHSTENCIL_TEST_NO_OVERWRITE);
 
 	// Move skybox with camera.
 	Vector3 position = camera->GetPosition();
 
-
 	const float scaleFactor = 100.0f;
 
@@ -137,5 +134,4 @@
 	Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor);
 	mSkyDome->GetTransform()->SetMatrix(s * m);
-
 	
 	cgGLSetParameter3f(sLightDirParam, sunDir.x, sunDir.y, sunDir.z);
@@ -149,16 +145,8 @@
 	cgGLSetParameter3f(sEColorParam, ABCDE_x[4], ABCDE_y[4], ABCDE_Y[4]);
 
-	/*mEffect->setVariableVector3("LightDirection", D().getScene().getLight().getDirection());
-	mEffect->setVariableVector2("ThetaSun", sun_theta);
-	mEffect->setVariableVector3("ZenithColor", zenithColor);
-	mEffect->setVariableVector3("AColor",FVector3((FFloat)ABCDE_x[0], (FFloat)ABCDE_y[0], (FFloat)ABCDE_Y[0]));
-	mEffect->setVariableVector3("BColor",FVector3((FFloat)ABCDE_x[1], (FFloat)ABCDE_y[1], (FFloat)ABCDE_Y[1]));
-	mEffect->setVariableVector3("CColor",FVector3((FFloat)ABCDE_x[2], (FFloat)ABCDE_y[2], (FFloat)ABCDE_Y[2]));
-	mEffect->setVariableVector3("DColor",FVector3((FFloat)ABCDE_x[3], (FFloat)ABCDE_y[3], (FFloat)ABCDE_Y[3]));
-	mEffect->setVariableVector3("EColor",FVector3((FFloat)ABCDE_x[4], (FFloat)ABCDE_y[4], (FFloat)ABCDE_Y[4]));
-*/
-
 	cgGLEnableProfile(RenderState::sCgVertexProfile);
 	cgGLBindProgram(sCgSkyProgram);
+	
+	
 
 	// Render sky dome.
@@ -166,35 +154,54 @@
 
 	// Render additively blended sun disc.
-	RenderSunDisk();
-}
-
-
-void SkyPreetham::RenderSunDisk()
-{
-	/*
-	F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_ADDITIVE);
+	//RenderSunDisk(sunDir, camera);
+}
+
+
+void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
+{
+	// Move skybox with camera.
+	Vector3 position = camera->GetPosition();
+
+	const float scaleFactor = 100.0f;
+	position.z -= 10 * scaleFactor;
 
 	// Set world matrix to sun disc position.
-	FMatrix4 sunposition;
-	sunposition.translate(D().getScene().getCamera().getPosition());
-	sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
-	sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
-	F().getRenderDevice().getTransform().setWorldMatrix(sunposition);
-
-	FVector3 lightdirection = D().getScene().getLight().getDirection();
-	FFloat size = 0.5f + (1.0f - lightdirection.y) * 0.5f;
-
-	mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor());
-	mSunEffect->setVariableFloat("SunSize", size);
-	mSunEffect->setVariableTexture2D("SunTexture", mSunTexture);
-
-	mSunEffect->activate();
+	Matrix4x4 sunPos = TranslationMatrix(position);
+
+	Vector3 ndir = -Normalize(sunDir);
+
+	const float pitch = -atan2(ndir.x, ndir.y);
+	const float yaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y)));
+
+	Matrix4x4 roty = RotationYMatrix(pitch);
+	Matrix4x4 rotx = RotationXMatrix(yaw);
+
+	sunPos *= roty;
+	sunPos *= rotx;;
+
+	//sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
+	//sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
+	
+	glMatrixMode(GL_MODELVIEW);
+	glMultMatrixf((float *)sunPos.x);
+
+	float ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
+
+	glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
+
+	//Float size = 0.5f + (1.0f - lightdirection.y) * 0.5f;
+
+	//mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor());
+	//mSunEffect->setVariableFloat("SunSize", size);
+	//mSunEffect->setVariableTexture2D("SunTexture", mSunTexture);
+
+	/*mSunEffect->activate();
 	
 	mSunQuad->render();
 
 	mSunEffect->deactivate();
-
-	F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);
-	*/
+	F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
+
+	glPopMatrix();
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 2957)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 2958)
@@ -28,5 +28,5 @@
 protected:
 
-	void RenderSunDisk();
+	void RenderSunDisk(const CHCDemoEngine::Vector3 &lightDir, CHCDemoEngine::Camera *camera);
 	void CreateSunQuad();
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ObjConverter.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ObjConverter.cpp	(revision 2957)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ObjConverter.cpp	(revision 2958)
@@ -112,4 +112,7 @@
 	//	Material *mat = new Material(RgbaColor(0, 1, 0, 1));
 	Material *mat = new Material();
+	mat->SetAmbient(RgbaColor(0, 0, 0, 0));
+	mat->SetDiffuse(RgbaColor(0, 0, 0, 0));
+
 	Geometry *geom = new Geometry(_vertices, _normals, _texcoords, numElements, false);
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2957)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2958)
@@ -171,5 +171,4 @@
 
 
-
 /// the used render type for this render pass
 enum RenderMethod
@@ -274,4 +273,5 @@
 static CGparameter sMaxDepthParamTex;
 static Matrix4x4 oldViewProjMatrix;
+static CGprogram sCgMrtFragmentSkyDomeProgram = NULL;
 
 
@@ -547,24 +547,25 @@
 		cerr << "fragment tex program failed to load" << endl;
 
-	RenderState::sCgMrtFragmentProgram = 
+	sCgMrtFragmentSkyDomeProgram = 
 		cgCreateProgramFromFile(sCgContext, 
 								CG_SOURCE,
-								"src/shaders/mrt.cg", 
+								"src/shaders/sky_preetham.cg", 
 								RenderState::sCgFragmentProfile,
-								"frag",
+								"frag_skydome",
 								NULL);
 
-	if (RenderState::sCgMrtFragmentProgram != NULL)
-	{
-		cgGLLoadProgram(RenderState::sCgMrtFragmentProgram);
-
-		sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
+	if (sCgMrtFragmentSkyDomeProgram != NULL)
+	{
+		cgGLLoadProgram(sCgMrtFragmentSkyDomeProgram);
+
+		/*sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
 		Material::sDiffuseParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "diffuse");
 		Material::sAmbientParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "ambient");
 
 		cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
+		*/
 	}
 	else
-		cerr << "fragment program failed to load" << endl;
+		cerr << "fragment skyprogram failed to load" << endl;
 
 	PrintGLerror("init");
@@ -617,5 +618,5 @@
 void InitGLstate() 
 {
-	glClearColor(0.2f, 0.2f, 0.8f, 1.0f);
+	glClearColor(0.3f, 0.3f, 0.4f, 1.0f);
 	
 	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -675,5 +676,6 @@
 	//////////////////////////////
 
-	GLfloat lmodel_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
+	//GLfloat lmodel_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
+	GLfloat lmodel_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
 
 	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
@@ -1958,4 +1960,13 @@
 void RenderSky()
 {
+	glEnable(GL_LIGHTING);
+	cgGLEnableProfile(RenderState::sCgVertexProfile);
+
+	if (state.GetRenderPassType() == RenderState::DEFERRED)
+	{
+		cgGLEnableProfile(RenderState::sCgFragmentProfile);
+		cgGLBindProgram(sCgMrtFragmentSkyDomeProgram);
+	}
+
 	SceneEntityContainer::const_iterator sit, sit_end = skyGeometry.end();
 
@@ -1968,5 +1979,7 @@
 	preetham->Compute(-light->GetDirection(), camera, &state);
 	glEnable(GL_CULL_FACE);
+
 	cgGLDisableProfile(RenderState::sCgVertexProfile);
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
 }
 
