From 1fd63c30e9b956bc9275b97c6a7ed1db72a026ce Mon Sep 17 00:00:00 2001 From: curt Date: Thu, 3 Oct 2002 18:44:34 +0000 Subject: [PATCH] Fix a texture state problem that caused clouds to occasionally flash all white. --- simgear/scene/sky/clouds3d/SkyMaterial.cpp | 123 ++++++++++++++++++ simgear/scene/sky/clouds3d/SkyMaterial.hpp | 1 + .../clouds3d/SkyRenderableInstanceCloud.cpp | 7 +- .../scene/sky/clouds3d/SkySceneManager.cpp | 1 + .../scene/sky/clouds3d/SkyTextureState.cpp | 78 ++++++++++- .../scene/sky/clouds3d/SkyTextureState.hpp | 1 + 6 files changed, 205 insertions(+), 6 deletions(-) diff --git a/simgear/scene/sky/clouds3d/SkyMaterial.cpp b/simgear/scene/sky/clouds3d/SkyMaterial.cpp index 7b6aaa7a..30e95a86 100644 --- a/simgear/scene/sky/clouds3d/SkyMaterial.cpp +++ b/simgear/scene/sky/clouds3d/SkyMaterial.cpp @@ -320,3 +320,126 @@ SKYRESULT SkyMaterial::Activate() } +//------------------------------------------------------------------------------ +// Function : SkyMaterial::Force +// Description : +//------------------------------------------------------------------------------ +/** + * @fn SkyMaterial::Force() + * @brief @todo + * + * @todo + */ +SKYRESULT SkyMaterial::Force() +{ + // Update the cached current material, and only pass values that have changed to the GL. + + SkyMaterial *pCurrentMaterial = GraphicsContext::InstancePtr()->GetCurrentMaterial(); + assert(NULL != pCurrentMaterial); + + // basic material properties + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &(GetDiffuse().x)); + pCurrentMaterial->SetDiffuse(GetDiffuse()); + + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &(GetSpecular().x)); + pCurrentMaterial->SetSpecular(GetSpecular()); + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &(GetAmbient().x)); + pCurrentMaterial->SetAmbient(GetAmbient()); + + glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &(GetEmissive().x)); + pCurrentMaterial->SetEmissive(GetEmissive()); + + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, GetSpecularPower()); + pCurrentMaterial->SetSpecularPower(GetSpecularPower()); + + // lighting + if (IsLightingEnabled()) + glEnable(GL_LIGHTING); + else + glDisable(GL_LIGHTING); + pCurrentMaterial->EnableLighting(IsLightingEnabled()); + + // color material (which material property tracks color calls) + glColorMaterial(GetColorMaterialFace(), GetColorMaterialMode()); + pCurrentMaterial->SetColorMaterialFace(GetColorMaterialFace()); + pCurrentMaterial->SetColorMaterialMode(GetColorMaterialMode()); + + if (IsColorMaterialEnabled()) + glEnable(GL_COLOR_MATERIAL); + else + glDisable(GL_COLOR_MATERIAL); + pCurrentMaterial->EnableColorMaterial(IsColorMaterialEnabled()); + + // fog + glFogf(GL_FOG_MODE, GetFogMode()); + pCurrentMaterial->SetFogMode(GetFogMode()); + + glFogfv(GL_FOG_COLOR, GetFogColor()); + pCurrentMaterial->SetFogColor(GetFogColor()); + + glFogf(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY)); + pCurrentMaterial->SetFogParameter(GL_FOG_DENSITY, GetFogParameter(GL_FOG_DENSITY)); + + glFogf(GL_FOG_START, GetFogParameter(GL_FOG_START)); + pCurrentMaterial->SetFogParameter(GL_FOG_START, GetFogParameter(GL_FOG_START)); + glFogf(GL_FOG_END, GetFogParameter(GL_FOG_END)); + pCurrentMaterial->SetFogParameter(GL_FOG_END, GetFogParameter(GL_FOG_END)); + + if (IsFogEnabled()) + glEnable(GL_FOG); + else + glDisable(GL_FOG); + pCurrentMaterial->EnableFog(IsFogEnabled()); + + // depth test + glDepthFunc(GetDepthFunc()); + pCurrentMaterial->SetDepthFunc(GetDepthFunc()); + + glDepthMask(GetDepthMask()); + pCurrentMaterial->SetDepthMask(GetDepthMask()); + + if (IsDepthTestEnabled()) + glEnable(GL_DEPTH_TEST); + else + glDisable(GL_DEPTH_TEST); + pCurrentMaterial->EnableDepthTest(IsDepthTestEnabled()); + + // alpha test + glAlphaFunc(GetAlphaFunc(), GetAlphaRef()); + pCurrentMaterial->SetAlphaFunc(GetAlphaFunc()); + pCurrentMaterial->SetAlphaRef(GetAlphaRef()); + + if (IsAlphaTestEnabled()) + glEnable(GL_ALPHA_TEST); + else + glDisable(GL_ALPHA_TEST); + + // blending + glBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor()); + pCurrentMaterial->SetBlendFunc(GetBlendingSourceFactor(), GetBlendingDestFactor()); + + if (IsBlendingEnabled()) + glEnable(GL_BLEND); + else + glDisable(GL_BLEND); + pCurrentMaterial->EnableBlending(IsBlendingEnabled()); + + glCullFace(GetFaceCullingMode()); + pCurrentMaterial->SetFaceCullingMode(GetFaceCullingMode()); + + if (IsFaceCullingEnabled()) + glEnable(GL_CULL_FACE); + else + glDisable(GL_CULL_FACE); + pCurrentMaterial->EnableFaceCulling(IsFaceCullingEnabled()); + + // texturing + FAIL_RETURN(_textureState.Force()); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GetTextureApplicationMode()); + pCurrentMaterial->SetTextureApplicationMode(GetTextureApplicationMode()); + + return SKYRESULT_OK; +} + + diff --git a/simgear/scene/sky/clouds3d/SkyMaterial.hpp b/simgear/scene/sky/clouds3d/SkyMaterial.hpp index 27f570e2..feae9e37 100644 --- a/simgear/scene/sky/clouds3d/SkyMaterial.hpp +++ b/simgear/scene/sky/clouds3d/SkyMaterial.hpp @@ -51,6 +51,7 @@ public: ~SkyMaterial(); SKYRESULT Activate(); + SKYRESULT Force(); // Getters for basic material properties diff --git a/simgear/scene/sky/clouds3d/SkyRenderableInstanceCloud.cpp b/simgear/scene/sky/clouds3d/SkyRenderableInstanceCloud.cpp index df4650e1..6abe4650 100644 --- a/simgear/scene/sky/clouds3d/SkyRenderableInstanceCloud.cpp +++ b/simgear/scene/sky/clouds3d/SkyRenderableInstanceCloud.cpp @@ -239,8 +239,10 @@ SKYRESULT SkyRenderableInstanceCloud::Display(bool bDisplayFrontOfSplit /* = fal } else {//cout << "Using impostor image\n"; - if (!_pBackTexture || (bDisplayFrontOfSplit && !_pFrontTexture)) - FAIL_RETURN_MSG(SKYRESULT_FAIL, "SkyRenderableInstanceCloud::Display(): missing texture!"); + if (!_pBackTexture || (bDisplayFrontOfSplit && !_pFrontTexture)) { + cout << "texture id failure" << endl; + FAIL_RETURN_MSG(SKYRESULT_FAIL, "SkyRenderableInstanceCloud::Display(): missing texture!"); + } s_pMaterial->SetTexture(0, GL_TEXTURE_2D, bDisplayFrontOfSplit ? *_pFrontTexture : *_pBackTexture); if (_bScreenImpostor) @@ -264,6 +266,7 @@ SKYRESULT SkyRenderableInstanceCloud::Display(bool bDisplayFrontOfSplit /* = fal } s_pMaterial->Activate(); + // s_pMaterial->Force(); Vec3f x, y, z; diff --git a/simgear/scene/sky/clouds3d/SkySceneManager.cpp b/simgear/scene/sky/clouds3d/SkySceneManager.cpp index 722a0eda..9569f952 100644 --- a/simgear/scene/sky/clouds3d/SkySceneManager.cpp +++ b/simgear/scene/sky/clouds3d/SkySceneManager.cpp @@ -397,6 +397,7 @@ SKYRESULT SkySceneManager::Update(const Camera &cam) SKYRESULT SkySceneManager::Display( const Camera &cam ) { + // _clearMaterial.Force(); _clearMaterial.Activate(); //glClear(GL_DEPTH_BUFFER_BIT); diff --git a/simgear/scene/sky/clouds3d/SkyTextureState.cpp b/simgear/scene/sky/clouds3d/SkyTextureState.cpp index 5844ff7d..16d33704 100644 --- a/simgear/scene/sky/clouds3d/SkyTextureState.cpp +++ b/simgear/scene/sky/clouds3d/SkyTextureState.cpp @@ -106,12 +106,12 @@ SKYRESULT SkyTextureState::Activate() { GLenum eTarget = GetActiveTarget(i); unsigned int iID = GetTextureID(i); - if ((pCurrent->GetActiveTarget(i) != eTarget) || - (pCurrent->GetTextureID(i) != iID)) - { + // if ((pCurrent->GetActiveTarget(i) != eTarget) || + // (pCurrent->GetTextureID(i) != iID)) + // { FAIL_RETURN(pCurrent->SetTexture(i, eTarget, iID)); glBindTexture(eTarget, iID); - } + // } //GLVU::CheckForGLError("SkyTextureState::Activate(5)"); GLenum paramValue = GetTextureParameter(i, GL_TEXTURE_WRAP_S); if (pCurrent->GetTextureParameter(i, GL_TEXTURE_WRAP_S) != paramValue) @@ -158,6 +158,76 @@ SKYRESULT SkyTextureState::Activate() } +//------------------------------------------------------------------------------ +// Function : SkyTextureState::Force +// Description : +//------------------------------------------------------------------------------ +/** + * @fn SkyTextureState::Force() + * @brief @todo + * + * @todo + */ +SKYRESULT SkyTextureState::Force() +{ + SkyTextureState *pCurrent = GraphicsContext::InstancePtr()->GetCurrentTextureState(); + assert(NULL != pCurrent); + //GLVU::CheckForGLError("SkyTextureState::Activate(8)"); + for (unsigned int i = 0; i < s_iNumTextureUnits; ++i) + { +#ifdef GL_ARB_multitexture + if (s_iNumTextureUnits > 1) + glActiveTextureARB(GL_TEXTURE0_ARB + i); +#endif + bool bEnabled = IsTextureEnabled(i); + FAIL_RETURN(pCurrent->EnableTexture(i, bEnabled)); + //GLVU::CheckForGLError("SkyTextureState::Activate(7)"); + if (bEnabled) + glEnable(GetActiveTarget(i)); + else + glDisable(GetActiveTarget(i)); + + //GLVU::CheckForGLError("SkyTextureState::Activate(6)"); + GLenum eTarget = GetActiveTarget(i); + unsigned int iID = GetTextureID(i); + + FAIL_RETURN(pCurrent->SetTexture(i, eTarget, iID)); + glBindTexture(eTarget, iID); + + //GLVU::CheckForGLError("SkyTextureState::Activate(5)"); + GLenum paramValue = GetTextureParameter(i, GL_TEXTURE_WRAP_S); + FAIL_RETURN(pCurrent->SetTextureParameter(i, GL_TEXTURE_WRAP_S, paramValue)); + glTexParameteri(eTarget, GL_TEXTURE_WRAP_S, paramValue); + + //GLVU::CheckForGLError("SkyTextureState::Activate(4)"); + paramValue = GetTextureParameter(i, GL_TEXTURE_WRAP_T); + FAIL_RETURN(pCurrent->SetTextureParameter(i, GL_TEXTURE_WRAP_T, paramValue)); + glTexParameteri(eTarget, GL_TEXTURE_WRAP_T, paramValue); + + //GLVU::CheckForGLError("SkyTextureState::Activate(3)"); + paramValue = GetTextureParameter(i, GL_TEXTURE_WRAP_R); + FAIL_RETURN(pCurrent->SetTextureParameter(i, GL_TEXTURE_WRAP_R, paramValue)); + glTexParameteri(eTarget, GL_TEXTURE_WRAP_R, paramValue); + + //GLVU::CheckForGLError("SkyTextureState::Activate(2)"); + paramValue = GetTextureParameter(i, GL_TEXTURE_MIN_FILTER); + FAIL_RETURN(pCurrent->SetTextureParameter(i, GL_TEXTURE_MIN_FILTER, paramValue)); + glTexParameteri(eTarget, GL_TEXTURE_MIN_FILTER, paramValue); + + //GLVU::CheckForGLError("SkyTextureState::Activate(1)"); + paramValue = GetTextureParameter(i, GL_TEXTURE_MAG_FILTER); + FAIL_RETURN(pCurrent->SetTextureParameter(i, GL_TEXTURE_MAG_FILTER, paramValue)); + glTexParameteri(eTarget, GL_TEXTURE_MIN_FILTER, paramValue); + +#ifdef GL_ARB_multitexture + if (s_iNumTextureUnits > 1) + glActiveTextureARB(GL_TEXTURE0_ARB); +#endif + } + return SKYRESULT_OK; +} + + //------------------------------------------------------------------------------ // Function : SkyTextureState::SetTexture // Description : diff --git a/simgear/scene/sky/clouds3d/SkyTextureState.hpp b/simgear/scene/sky/clouds3d/SkyTextureState.hpp index 24312919..91c7de26 100644 --- a/simgear/scene/sky/clouds3d/SkyTextureState.hpp +++ b/simgear/scene/sky/clouds3d/SkyTextureState.hpp @@ -41,6 +41,7 @@ public: // methods SkyTextureState(); ~SkyTextureState(); + SKYRESULT Force(); SKYRESULT Activate(); SKYRESULT SetTexture(unsigned int iTextureUnit, GLenum eTarget, SkyTexture& texture); -- 2.39.5