Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 3048)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 3050)
@@ -55,8 +55,10 @@
 	Technique();
 
-	~Technique();
+	Technique(const Technique &tech);
 	/** Sets ambient and diffuse color to color
 	*/
 	Technique(const RgbaColor &color);
+
+	~Technique();
 	/** Renders this technique.
 	*/
@@ -88,13 +90,15 @@
 	inline void SetLightingEnabled(bool l) { mLightingEnabled = l; }
 	inline void SetColorWriteEnabled(bool c) { mColorWriteEnabled = c; }
+	inline void SetDepthWriteEnabled(bool d) { mDepthWriteEnabled = d; }
 
 	inline bool IsLightingEnabled() const { return mLightingEnabled; } 
 	inline bool IsColorWriteEnabled() const { return mColorWriteEnabled; } 
+	inline bool IsDepthWriteEnabled() const { return mDepthWriteEnabled; } 
 
 	void SetFragmentProgram(ShaderProgram *p);
 	void SetVertexProgram(ShaderProgram *p);
 
-	ShaderProgram *GetFragmentProgram() { return mFragmentProgram; }
-	ShaderProgram *GetVertexProgram() { return mVertexProgram; }
+	inline ShaderProgram *GetFragmentProgram() { return mFragmentProgram; }
+	inline ShaderProgram *GetVertexProgram() { return mVertexProgram; }
 
 	/** Get the set of fragment parameters of this technique
@@ -104,6 +108,4 @@
 	*/
 	GPUProgramParameters * const GetVertexProgramParameters() { return &mVertexProgramParameters; }
-
-	static Technique GetQueryTechnique();
 
 protected:
@@ -130,4 +132,6 @@
 	/// if lighting is used
 	bool mLightingEnabled;
+	/// if depth write is enabled
+	bool mDepthWriteEnabled;
 };
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3048)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3050)
@@ -19,5 +19,31 @@
 mRenderTechnique(FORWARD),
 mUseAlphaToCoverage(true),
-mLockCullFaceEnabled(false)
+mLockCullFaceEnabled(false),
+mCurrentVboId(-1),
+mCurrentTexId(0),
+mCullFaceEnabled(true),
+mAlphaTestEnabled(false),
+mTexturesEnabled(false),
+mCurrentVertexProgram(NULL),
+mCurrentFragmentProgram(NULL),
+mLightingEnabled(true),
+mColorWriteEnabled(true),
+mDepthWriteEnabled(true),
+mCurrentTechnique(NULL)
+{
+	SetMode(RENDER);
+	
+	glEnable(GL_CULL_FACE);
+	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_TEXTURE_2D);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+	glEnable(GL_LIGHTING);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	glDepthMask(GL_TRUE);
+}
+
+
+void RenderState::Reset()
 {
 	SetMode(RENDER);
@@ -40,28 +66,4 @@
 	glDisable(GL_TEXTURE_2D);
 	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-}
-
-
-void RenderState::Reset()
-{
-	SetMode(RENDER);
-
-	mCurrentVboId = -1;
-	mCurrentTexId = 0;
-
-	mCullFaceEnabled = true;
-	mAlphaTestEnabled = false;
-	mTexturesEnabled = false;
-
-	mCurrentVertexProgram = NULL;
-	mCurrentFragmentProgram = NULL;
-
-	glEnable(GL_CULL_FACE);
-
-	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
-	glDisable(GL_ALPHA_TEST);
-
-	glDisable(GL_TEXTURE_2D);
-	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
 	ResourceManager::GetSingleton()->DisableFragmentProfile();
@@ -70,5 +72,17 @@
 	mFragmentProgramEnabled = false;
 	mVertexProgramEnabled = false;
-}
+
+	mLightingEnabled = true;
+	glEnable(GL_LIGHTING);
+
+	mColorWriteEnabled = true;
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
+	mDepthWriteEnabled = true;
+	glDepthMask(GL_TRUE);
+
+	mCurrentTechnique = NULL;
+}
+
 
 // q matt: is this function really necessary or would it be better
@@ -77,5 +91,5 @@
 {
 	///////////
-	//-- just change the modewhen necessary
+	//-- just change the mode when necessary
 
 	if (mode == mMode) return false;
@@ -85,40 +99,7 @@
 	if (mode == QUERY)
 	{
-		if (mLightingEnabled)
-			glDisable(GL_LIGHTING);
-
-		if (mColorWriteEnabled)
-			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-
-		if (mFragmentProgramEnabled)
-			ResourceManager::GetSingleton()->DisableFragmentProfile();
-			
-		if (mVertexProgramEnabled)
-			ResourceManager::GetSingleton()->DisableVertexProfile();
-		
-		glDepthMask(GL_FALSE);
-
 		/// the query box material
-		static Technique queryTech = Technique::GetQueryTechnique();
+		static Technique queryTech = GetQueryTechnique();
 		SetState(&queryTech);
-	}
-	else // mode returns to render
-	{
-		/////////////
-		//-- restore render state
-
-		if (mLightingEnabled)
-			glEnable(GL_LIGHTING);
-
-		if (mColorWriteEnabled)
-			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-		
-		if (mFragmentProgramEnabled)
-			ResourceManager::GetSingleton()->EnableFragmentProfile();
-			
-		if (mVertexProgramEnabled)
-			ResourceManager::GetSingleton()->EnableVertexProfile();
-
-		glDepthMask(GL_TRUE);
 	}
 
@@ -129,10 +110,50 @@
 void RenderState::SetState(Technique *tech)
 {
-	Texture *tex = tech->GetTexture();
+	if (mCurrentTechnique == tech)
+		return;
+
+	mCurrentTechnique = tech;
+
+	Texture * const tex = tech->GetTexture();
 
 	const bool texturing = (tex != NULL);
-
-	bool alphaTest = tech->IsAlphaTestEnabled();
-	bool cullFace = tech->IsCullFaceEnabled();
+	const bool alphaTest = tech->IsAlphaTestEnabled();
+	const bool cullFace = tech->IsCullFaceEnabled();
+	const bool lighting = tech->IsLightingEnabled();
+	const bool colorWrite = tech->IsColorWriteEnabled();
+	const bool depthWrite = tech->IsDepthWriteEnabled();
+
+	if (mDepthWriteEnabled && !depthWrite) 
+	{
+		mDepthWriteEnabled = false;
+		glDepthMask(GL_FALSE);
+	}
+	else if (!mDepthWriteEnabled && depthWrite)
+	{
+		mDepthWriteEnabled = true;
+		glDepthMask(GL_TRUE);
+	}
+
+	if (mLightingEnabled && !lighting) 
+	{
+		mLightingEnabled = false;
+		glDisable(GL_LIGHTING);
+	}
+	else if (!mLightingEnabled && lighting)
+	{
+		mLightingEnabled = true;
+		glEnable(GL_LIGHTING);
+	}
+
+	if (mColorWriteEnabled && !colorWrite) 
+	{
+		mColorWriteEnabled = false;
+		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+	}
+	else if (!mColorWriteEnabled && colorWrite)
+	{
+		mColorWriteEnabled = true;
+		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	}
 
 	if (!mLockCullFaceEnabled)
@@ -172,5 +193,5 @@
 		mTexturesEnabled = false;
 
-		if (!tech->GetFragmentProgram())
+		//if (!tech->GetFragmentProgram())
 			glDisable(GL_TEXTURE_2D);
 
@@ -179,16 +200,14 @@
 	else if (!mTexturesEnabled && texturing)
 	{
-		if (mAlphaTestEnabled)
-		{
-			mTexturesEnabled = true;
-
-			if (!tech->GetFragmentProgram())
-				glEnable(GL_TEXTURE_2D);
-
-			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-		}
-	}
-
-	///////////////////
+		mTexturesEnabled = true;
+
+		//if (!tech->GetFragmentProgram())
+			glEnable(GL_TEXTURE_2D);
+
+		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+	}
+
+
+	//////////////////
 	//-- fragment and vertex programs
 	
@@ -249,5 +268,5 @@
 	if (!mFragmentProgramEnabled)
 	{
-		// set FORWARD texture
+		// set texture for fixed function rendering
 		if (tex)
 		{
@@ -260,8 +279,22 @@
 		else
 		{
-			mCurrentTexId = 0;
+			mCurrentTexId = -1;
 			glBindTexture(GL_TEXTURE_2D, 0);
 		}
 	}
+}
+
+
+Technique RenderState::GetQueryTechnique()
+{
+	Technique tech;
+
+	tech.Init();
+
+	tech.SetColorWriteEnabled(false);
+	tech.SetLightingEnabled(false);
+	tech.SetDepthWriteEnabled(false);
+
+	return tech;
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3048)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3050)
@@ -159,5 +159,5 @@
 	//-- hack: add tree animation (should be done per material script!)
 
-	if (0)//numLODs > 1)
+	if (numLODs > 1)
 	{
 		ShapeContainer::iterator sstart, send;
@@ -176,5 +176,5 @@
 
 				// add the vertex animation program
-				for (int i = 0; i < 2; ++ i)
+				for (int i = 0; i < 3; ++ i)
 				{
 					Technique *tech = mat->GetTechnique(i);
@@ -320,4 +320,5 @@
 
 	deferred->SetVertexProgram(mMrtDefaultVertexProgram);
+	mat->AddTechnique(deferred);
 	
 
@@ -328,6 +329,4 @@
 	depthPass->SetColorWriteEnabled(false);
 	depthPass->SetLightingEnabled(false);
-
-	mat->AddTechnique(deferred);
 	mat->AddTechnique(depthPass);
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3048)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3050)
@@ -75,4 +75,10 @@
 protected:
 
+	/** A special query technique. This material
+		has color write, depth write, and lighting turned off
+	*/
+	static Technique GetQueryTechnique();
+
+
 	//////////////////
 
@@ -101,4 +107,7 @@
 	bool mLightingEnabled;
 	bool mColorWriteEnabled;
+
+	Technique *mCurrentTechnique;
+	bool mDepthWriteEnabled;
 };
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3048)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3050)
@@ -30,8 +30,10 @@
 	mFragmentProgram = NULL;
 
-	/// if this material can write colors
+	// if this material can write colors
 	mColorWriteEnabled = true;
-	/// if lighting is used
+	// if lighting is used
 	mLightingEnabled = true;
+
+	mDepthWriteEnabled = true;
 }
 
@@ -56,4 +58,27 @@
 }
 
+
+Technique::Technique(const Technique &tech)
+{
+	mAmbientColor = tech.mAmbientColor;
+	mDiffuseColor = tech.mDiffuseColor;
+	mSpecularColor = tech.mSpecularColor;
+	mEmmisiveColor = tech.mEmmisiveColor;
+
+	mVertexProgram = tech.mVertexProgram;
+	mFragmentProgram = tech.mFragmentProgram;
+
+	mAlphaTestEnabled = tech.mAlphaTestEnabled;
+	mCullFaceEnabled =tech.mCullFaceEnabled;
+
+	mTexture = tech.mTexture;
+
+	mVertexProgramParameters = tech.mVertexProgramParameters;
+	mFragmentProgramParameters = tech.mFragmentProgramParameters;
+
+	mColorWriteEnabled = tech.mColorWriteEnabled;
+	mLightingEnabled = tech.mLightingEnabled;
+	mDepthWriteEnabled = tech.mDepthWriteEnabled;
+}
 
 
@@ -89,17 +114,4 @@
 	mVertexProgramParameters.Reset();
 	mVertexProgramParameters.SetProgram(p);
-}
-
-
-Technique Technique::GetQueryTechnique()
-{
-	Technique tech;
-
-	tech.Init();
-
-	tech.SetColorWriteEnabled(false);
-	tech.SetLightingEnabled(false);
-
-	return tech;
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3049)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3050)
@@ -1012,5 +1012,5 @@
 	// this would conveniently solves some issues (e.g, skys without shadows)
 
-	RenderSky();
+	//RenderSky();
 
 
