From 08669adf1b1f73ccb5c01deb7eb2404ac74aa8d0 Mon Sep 17 00:00:00 2001 From: kgv Date: Tue, 10 Mar 2020 21:56:59 +0300 Subject: [PATCH] 0031419: Visualization, TKOpenGl - per-object Graphic3d_TOSM_FACET is ignored within obsolete FFP OpenGl_ShaderManager::PushState() now sets GL_SHADE_MODEL within FFP. OpenGl_Context::SetShadeModel(), added property holding cached GL_SHADE_MODEL state. --- src/OpenGl/OpenGl_Context.cxx | 26 ++++++++++++++++++++++ src/OpenGl/OpenGl_Context.hxx | 4 ++++ src/OpenGl/OpenGl_PrimitiveArray.cxx | 16 -------------- src/OpenGl/OpenGl_ShaderManager.cxx | 29 ++++++++++++++++++++----- src/OpenGl/OpenGl_ShaderManager.hxx | 32 +++++++++++++++------------- src/OpenGl/OpenGl_View_Redraw.cxx | 26 ---------------------- tests/v3d/glsl/shading_models | 10 +++++++++ 7 files changed, 81 insertions(+), 62 deletions(-) diff --git a/src/OpenGl/OpenGl_Context.cxx b/src/OpenGl/OpenGl_Context.cxx index 55dddf93b4..43d4670347 100644 --- a/src/OpenGl/OpenGl_Context.cxx +++ b/src/OpenGl/OpenGl_Context.cxx @@ -219,10 +219,12 @@ OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps) #if !defined(GL_ES_VERSION_2_0) myPointSpriteOrig (GL_UPPER_LEFT), myRenderMode (GL_RENDER), + myShadeModel (GL_SMOOTH), myPolygonMode (GL_FILL), #else myPointSpriteOrig (0), myRenderMode (0), + myShadeModel (0), myPolygonMode (0), #endif myToCullBackFaces (false), @@ -576,6 +578,7 @@ void OpenGl_Context::FetchState() if (core11 != NULL) { ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode); + ::glGetIntegerv (GL_SHADE_MODEL, &myShadeModel); } // cache read buffers state @@ -3915,6 +3918,29 @@ Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabl return anOldGlNormalize; } +// ======================================================================= +// function : SetShadeModel +// purpose : +// ======================================================================= +void OpenGl_Context::SetShadeModel (Graphic3d_TypeOfShadingModel theModel) +{ +#if !defined(GL_ES_VERSION_2_0) + if (core11 != NULL) + { + const Standard_Integer aModel = theModel == Graphic3d_TOSM_FACET + || theModel == Graphic3d_TOSM_PBR_FACET ? GL_FLAT : GL_SMOOTH; + if (myShadeModel == aModel) + { + return; + } + myShadeModel = aModel; + core11->glShadeModel (aModel); + } +#else + (void )theModel; +#endif +} + // ======================================================================= // function : SetPolygonMode // purpose : diff --git a/src/OpenGl/OpenGl_Context.hxx b/src/OpenGl/OpenGl_Context.hxx index 269be00cd1..d81ed13486 100644 --- a/src/OpenGl/OpenGl_Context.hxx +++ b/src/OpenGl/OpenGl_Context.hxx @@ -983,6 +983,9 @@ public: //! @name methods to alter or retrieve current state //! Dumps the content of openGL state into the stream Standard_EXPORT static void DumpJsonOpenGlState (Standard_OStream& theOStream, Standard_Integer theDepth = -1); + //! Set GL_SHADE_MODEL value. + Standard_EXPORT void SetShadeModel (Graphic3d_TypeOfShadingModel theModel); + private: //! Wrapper to system function to retrieve GL function pointer by name. @@ -1153,6 +1156,7 @@ private: //! @name fields tracking current state Standard_Integer myViewportVirt[4]; //!< virtual viewport Standard_Integer myPointSpriteOrig; //!< GL_POINT_SPRITE_COORD_ORIGIN state (GL_UPPER_LEFT by default) Standard_Integer myRenderMode; //!< value for active rendering mode + Standard_Integer myShadeModel; //!< currently used shade model (glShadeModel) Standard_Integer myPolygonMode; //!< currently used polygon rasterization mode (glPolygonMode) Graphic3d_PolygonOffset myPolygonOffset; //!< currently applied polygon offset bool myToCullBackFaces; //!< back face culling mode enabled state (glIsEnabled (GL_CULL_FACE)) diff --git a/src/OpenGl/OpenGl_PrimitiveArray.cxx b/src/OpenGl/OpenGl_PrimitiveArray.cxx index 8cda04ecb1..97c28f2481 100644 --- a/src/OpenGl/OpenGl_PrimitiveArray.cxx +++ b/src/OpenGl/OpenGl_PrimitiveArray.cxx @@ -919,22 +919,6 @@ void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace } } - #if !defined(GL_ES_VERSION_2_0) - // manage FFP lighting - if (aCtx->ActiveProgram().IsNull() - && aCtx->core11 != NULL) - { - if (aShadingModel == Graphic3d_TOSM_UNLIT) - { - glDisable (GL_LIGHTING); - } - else - { - glEnable (GL_LIGHTING); - } - } - #endif - // bind textures after GLSL program to set mock textures to slots used by program aCtx->BindTextures (aTextureSet, aCtx->ActiveProgram()); if (!aTextureSet.IsNull() diff --git a/src/OpenGl/OpenGl_ShaderManager.cxx b/src/OpenGl/OpenGl_ShaderManager.cxx index 4fda63fe14..334e053f5e 100644 --- a/src/OpenGl/OpenGl_ShaderManager.cxx +++ b/src/OpenGl/OpenGl_ShaderManager.cxx @@ -1422,7 +1422,8 @@ void OpenGl_ShaderManager::PushInteriorState (const Handle(OpenGl_ShaderProgram) // function : PushState // purpose : Pushes state of OCCT graphics parameters to the program // ======================================================================= -void OpenGl_ShaderManager::PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const +void OpenGl_ShaderManager::PushState (const Handle(OpenGl_ShaderProgram)& theProgram, + Graphic3d_TypeOfShadingModel theShadingModel) const { const Handle(OpenGl_ShaderProgram)& aProgram = !theProgram.IsNull() ? theProgram : myFfpProgram; PushClippingState (aProgram); @@ -1441,6 +1442,23 @@ void OpenGl_ShaderManager::PushState (const Handle(OpenGl_ShaderProgram)& thePro (float )myContext->Viewport()[2], (float )myContext->Viewport()[3])); } } +#if !defined(GL_ES_VERSION_2_0) + else if (myContext->core11 != NULL) + { + // manage FFP lighting + myContext->SetShadeModel (theShadingModel); + if (theShadingModel == Graphic3d_TOSM_UNLIT) + { + glDisable (GL_LIGHTING); + } + else + { + glEnable (GL_LIGHTING); + } + } +#else + (void )theShadingModel; +#endif } // ======================================================================= @@ -3173,7 +3191,8 @@ const Handle(Graphic3d_ShaderProgram)& OpenGl_ShaderManager::GetBgCubeMapProgram // function : bindProgramWithState // purpose : // ======================================================================= -Standard_Boolean OpenGl_ShaderManager::bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram) +Standard_Boolean OpenGl_ShaderManager::bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram, + Graphic3d_TypeOfShadingModel theShadingModel) { const Standard_Boolean isBound = myContext->BindProgram (theProgram); if (isBound @@ -3181,7 +3200,7 @@ Standard_Boolean OpenGl_ShaderManager::bindProgramWithState (const Handle(OpenGl { theProgram->ApplyVariables (myContext); } - PushState (theProgram); + PushState (theProgram, theShadingModel); return isBound; } @@ -3198,7 +3217,7 @@ Standard_Boolean OpenGl_ShaderManager::BindMarkerProgram (const Handle(OpenGl_Te if (!theCustomProgram.IsNull() || myContext->caps->ffpEnable) { - return bindProgramWithState (theCustomProgram); + return bindProgramWithState (theCustomProgram, theShadingModel); } Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false); @@ -3212,5 +3231,5 @@ Standard_Boolean OpenGl_ShaderManager::BindMarkerProgram (const Handle(OpenGl_Te aBits |= OpenGl_PO_PointSimple; } Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits); - return bindProgramWithState (aProgram); + return bindProgramWithState (aProgram, theShadingModel); } diff --git a/src/OpenGl/OpenGl_ShaderManager.hxx b/src/OpenGl/OpenGl_ShaderManager.hxx index 7089b5c280..755be61a70 100644 --- a/src/OpenGl/OpenGl_ShaderManager.hxx +++ b/src/OpenGl/OpenGl_ShaderManager.hxx @@ -117,19 +117,19 @@ public: Standard_Boolean theEnableMeshEdges, const Handle(OpenGl_ShaderProgram)& theCustomProgram) { - if (!theCustomProgram.IsNull() - || myContext->caps->ffpEnable) - { - return bindProgramWithState (theCustomProgram); - } - const Graphic3d_TypeOfShadingModel aShadeModelOnFace = theShadingModel != Graphic3d_TOSM_UNLIT && (theTextures.IsNull() || theTextures->IsModulate()) ? theShadingModel : Graphic3d_TOSM_UNLIT; + if (!theCustomProgram.IsNull() + || myContext->caps->ffpEnable) + { + return bindProgramWithState (theCustomProgram, aShadeModelOnFace); + } + const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theInteriorStyle, theHasVertColor, theEnableEnvMap, theEnableMeshEdges); Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (aShadeModelOnFace, aBits); - return bindProgramWithState (aProgram); + return bindProgramWithState (aProgram, aShadeModelOnFace); } //! Bind program for line rendering @@ -143,7 +143,7 @@ public: if (!theCustomProgram.IsNull() || myContext->caps->ffpEnable) { - return bindProgramWithState (theCustomProgram); + return bindProgramWithState (theCustomProgram, theShadingModel); } Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false); @@ -153,7 +153,7 @@ public: } Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits); - return bindProgramWithState (aProgram); + return bindProgramWithState (aProgram, theShadingModel); } //! Bind program for point rendering @@ -169,7 +169,7 @@ public: if (!theCustomProgram.IsNull() || myContext->caps->ffpEnable) { - return bindProgramWithState (theCustomProgram); + return bindProgramWithState (theCustomProgram, Graphic3d_TOSM_UNLIT); } if (myFontProgram.IsNull()) @@ -177,7 +177,7 @@ public: prepareStdProgramFont(); } - return bindProgramWithState (myFontProgram); + return bindProgramWithState (myFontProgram, Graphic3d_TOSM_UNLIT); } //! Bind program for outline rendering @@ -198,7 +198,7 @@ public: { prepareStdProgramUnlit (aProgram, aBits, true); } - return bindProgramWithState (aProgram); + return bindProgramWithState (aProgram, Graphic3d_TOSM_UNLIT); } //! Bind program for FBO blit operation. @@ -244,7 +244,7 @@ public: { prepareStdProgramBoundBox(); } - return bindProgramWithState (myBoundBoxProgram); + return bindProgramWithState (myBoundBoxProgram, Graphic3d_TOSM_UNLIT); } //! Returns bounding box vertex buffer. @@ -454,7 +454,8 @@ public: public: //! Pushes current state of OCCT graphics parameters to specified program. - Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const; + Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram, + Graphic3d_TypeOfShadingModel theShadingModel = Graphic3d_TOSM_UNLIT) const; public: @@ -725,7 +726,8 @@ protected: Standard_Boolean theHasEmissive = true); //! Bind specified program to current context and apply state. - Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram); + Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram, + Graphic3d_TypeOfShadingModel theShadingModel); //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms Standard_EXPORT void switchLightPrograms(); diff --git a/src/OpenGl/OpenGl_View_Redraw.cxx b/src/OpenGl/OpenGl_View_Redraw.cxx index 18b12102b3..499351feef 100644 --- a/src/OpenGl/OpenGl_View_Redraw.cxx +++ b/src/OpenGl/OpenGl_View_Redraw.cxx @@ -112,26 +112,7 @@ void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace) || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_CENTERED || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_NONE)) { - #if !defined(GL_ES_VERSION_2_0) - GLint aShadingModelOld = GL_SMOOTH; - if (aCtx->core11 != NULL - && aCtx->caps->ffpEnable) - { - aCtx->core11fwd->glDisable (GL_LIGHTING); - aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld); - aCtx->core11->glShadeModel (GL_SMOOTH); - } - #endif - myBackgrounds[Graphic3d_TOB_GRADIENT]->Render(theWorkspace); - - #if !defined(GL_ES_VERSION_2_0) - if (aCtx->core11 != NULL - && aCtx->caps->ffpEnable) - { - aCtx->core11->glShadeModel (aShadingModelOld); - } - #endif } // Drawing background image if it is defined @@ -1072,13 +1053,6 @@ void OpenGl_View::render (Graphic3d_Camera::Projection theProjection, { aContext->SetGlNormalizeEnabled (Standard_False); } - - // Apply InteriorShadingMethod - if (aContext->core11 != NULL) - { - aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET - || myShadingModel == Graphic3d_TOSM_UNLIT ? GL_FLAT : GL_SMOOTH); - } #endif aManager->SetShadingModel (OpenGl_ShaderManager::PBRShadingModelFallback (myShadingModel, checkPBRAvailability())); diff --git a/tests/v3d/glsl/shading_models b/tests/v3d/glsl/shading_models index c11441a00b..6334ed1b21 100644 --- a/tests/v3d/glsl/shading_models +++ b/tests/v3d/glsl/shading_models @@ -24,6 +24,11 @@ vdrawparray p7 triangles v 40 55 0 v 40 75 0 v 60 55 0 v 40 55 0 v 40 75 0 v 40 vtop vrotate -0.9 0.1 0 vfit + +vcaps -ffp 1 +vdump $::imagedir/${::casename}_defaults_ffp.png + +vcaps -ffp 0 vdump $::imagedir/${::casename}_defaults.png # customize shading models @@ -42,4 +47,9 @@ vdrawtext t6 Graphic3d_TOSM_DEFAULT -pos 10 65 10 -color RED -aspect BOLD vaspects p7 -setShadingModel FACET vdrawtext t7 Graphic3d_TOSM_FACET -pos 50 65 10 -color RED -aspect BOLD vdisplay -topmost t1 t2 t3 t4 t5 t6 t7 + +vcaps -ffp 1 +vdump $::imagedir/${::casename}_ffp.png + +vcaps -ffp 0 vdump $::imagedir/${::casename}.png