From 2192e732c809f9b3b4c9625dbb21f5712a73838c Mon Sep 17 00:00:00 2001 From: mzernova Date: Mon, 19 Aug 2024 12:27:38 +0100 Subject: [PATCH] Add additional TypeOfAttribute for back vertex color --- src/Graphic3d/Graphic3d_ArrayOfPrimitives.cxx | 6 ++++++ src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx | 4 +++- src/Graphic3d/Graphic3d_Buffer.hxx | 1 + src/Graphic3d/Graphic3d_ShaderManager.cxx | 10 +++++++--- src/OpenGl/OpenGl_BackgroundArray.cxx | 3 ++- src/OpenGl/OpenGl_FrameStatsPrs.cxx | 4 ++++ src/OpenGl/OpenGl_PrimitiveArray.cxx | 1 + src/OpenGl/OpenGl_ShaderProgram.cxx | 1 + src/OpenGl/OpenGl_VertexBuffer.cxx | 11 ++++++++++- src/Shaders/Declarations.glsl | 1 + src/Shaders/Shaders_Declarations_glsl.pxx | 1 + 11 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.cxx b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.cxx index c2497c98ad..0e546e1bff 100644 --- a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.cxx +++ b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.cxx @@ -98,6 +98,7 @@ void Graphic3d_ArrayOfPrimitives::init (Graphic3d_TypeOfPrimitiveArray theType, myNormData = NULL; myTexData = NULL; myColData = NULL; + myColDataBack = NULL; myAttribs.Nullify(); myIndices.Nullify(); myBounds.Nullify(); @@ -171,6 +172,10 @@ void Graphic3d_ArrayOfPrimitives::init (Graphic3d_TypeOfPrimitiveArray theType, anAttribs[aNbAttribs].Id = Graphic3d_TOA_COLOR; anAttribs[aNbAttribs].DataType = Graphic3d_TOD_VEC4UB; ++aNbAttribs; + + anAttribs[aNbAttribs].Id = Graphic3d_TOA_COLOR_BACK; + anAttribs[aNbAttribs].DataType = Graphic3d_TOD_VEC4UB; + ++aNbAttribs; } if (!myAttribs->Init (theMaxVertexs, anAttribs, aNbAttribs)) @@ -185,6 +190,7 @@ void Graphic3d_ArrayOfPrimitives::init (Graphic3d_TypeOfPrimitiveArray theType, myNormData = myAttribs->ChangeAttributeData (Graphic3d_TOA_NORM, anAttribDummy, myNormStride); myTexData = myAttribs->ChangeAttributeData (Graphic3d_TOA_UV, anAttribDummy, myTexStride); myColData = myAttribs->ChangeAttributeData (Graphic3d_TOA_COLOR, anAttribDummy, myColStride); + myColDataBack = myAttribs->ChangeAttributeData (Graphic3d_TOA_COLOR_BACK, anAttribDummy, myColStrideBack); memset (myAttribs->ChangeData(), 0, size_t(myAttribs->Stride) * size_t(myAttribs->NbMaxElements())); if ((theArrayOptions & Graphic3d_ArrayFlags_AttribsMutable) == 0 diff --git a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx index 4d1daa5e49..d402d5ca99 100644 --- a/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx +++ b/src/Graphic3d/Graphic3d_ArrayOfPrimitives.hxx @@ -806,7 +806,7 @@ protected: //! @name protected constructors Standard_Integer theMaxBounds, Standard_Integer theMaxEdges, Graphic3d_ArrayFlags theArrayFlags) - : myNormData (NULL), myTexData (NULL), myColData (NULL), myPosStride (0), myNormStride (0), myTexStride (0), myColStride (0), + : myNormData (NULL), myTexData (NULL), myColData (NULL), myColDataBack (NULL), myPosStride (0), myNormStride (0), myTexStride (0), myColStride (0), myType (Graphic3d_TOPA_UNDEFINED) { init (theType, theMaxVertexs, theMaxBounds, theMaxEdges, theArrayFlags); @@ -827,10 +827,12 @@ private: //! @name private fields Standard_Byte* myNormData; Standard_Byte* myTexData; Standard_Byte* myColData; + Standard_Byte* myColDataBack; Standard_Size myPosStride; Standard_Size myNormStride; Standard_Size myTexStride; Standard_Size myColStride; + Standard_Size myColStrideBack; Graphic3d_TypeOfPrimitiveArray myType; }; diff --git a/src/Graphic3d/Graphic3d_Buffer.hxx b/src/Graphic3d/Graphic3d_Buffer.hxx index 8b84192038..27409749e7 100644 --- a/src/Graphic3d/Graphic3d_Buffer.hxx +++ b/src/Graphic3d/Graphic3d_Buffer.hxx @@ -27,6 +27,7 @@ enum Graphic3d_TypeOfAttribute Graphic3d_TOA_NORM, //!< normal Graphic3d_TOA_UV, //!< texture coordinates Graphic3d_TOA_COLOR, //!< per-vertex color + Graphic3d_TOA_COLOR_BACK, //!< per-vertex color back Graphic3d_TOA_CUSTOM, //!< custom attributes }; diff --git a/src/Graphic3d/Graphic3d_ShaderManager.cxx b/src/Graphic3d/Graphic3d_ShaderManager.cxx index 8650a554df..0a3f778e89 100644 --- a/src/Graphic3d/Graphic3d_ShaderManager.cxx +++ b/src/Graphic3d/Graphic3d_ShaderManager.cxx @@ -1019,8 +1019,10 @@ Handle(Graphic3d_ShaderProgram) Graphic3d_ShaderManager::getStdProgramUnlit (Sta if ((theBits & Graphic3d_ShaderFlags_VertColor) != 0) { aStageInOuts.Append (Graphic3d_ShaderObject::ShaderVariable ("vec4 VertColor", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT)); + aStageInOuts.Append (Graphic3d_ShaderObject::ShaderVariable ("vec4 VertColorBack", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT)); aSrcVertExtraMain += EOL" VertColor = occVertColor;"; - aSrcFragGetColor = EOL"vec4 getColor(void) { return VertColor; }"; + aSrcVertExtraMain += EOL" VertColorBack = occVertColorBack;"; + aSrcFragGetColor = EOL"vec4 getColor(void) { return gl_FrontFacing ? VertColor : VertColorBack; }"; } int aNbClipPlanes = 0; @@ -1430,7 +1432,7 @@ Handle(Graphic3d_ShaderProgram) Graphic3d_ShaderManager::getStdProgramGouraud (c if ((theBits & Graphic3d_ShaderFlags_VertColor) != 0) { - aSrcVertColor = EOL"vec4 getVertColor(void) { return occVertColor; }"; + aSrcVertColor = EOL"vec4 getVertColor(void) { return gl_FrontFacing ? VertColor : VertColorBack; }"; } int aNbClipPlanes = 0; @@ -1617,8 +1619,10 @@ Handle(Graphic3d_ShaderProgram) Graphic3d_ShaderManager::getStdProgramPhong (con if ((theBits & Graphic3d_ShaderFlags_VertColor) != 0) { aStageInOuts.Append (Graphic3d_ShaderObject::ShaderVariable ("vec4 VertColor", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT)); + aStageInOuts.Append (Graphic3d_ShaderObject::ShaderVariable ("vec4 VertColorBack", Graphic3d_TOS_VERTEX | Graphic3d_TOS_FRAGMENT)); aSrcVertExtraMain += EOL" VertColor = occVertColor;"; - aSrcFragGetVertColor = EOL"vec4 getVertColor(void) { return VertColor; }"; + aSrcVertExtraMain += EOL" VertColorBack = occVertColorBack;"; + aSrcFragGetVertColor = EOL"vec4 getVertColor(void) { return gl_FrontFacing ? VertColor : VertColorBack; }"; } int aNbClipPlanes = 0; diff --git a/src/OpenGl/OpenGl_BackgroundArray.cxx b/src/OpenGl/OpenGl_BackgroundArray.cxx index b005bf285e..4d85799e0e 100644 --- a/src/OpenGl/OpenGl_BackgroundArray.cxx +++ b/src/OpenGl/OpenGl_BackgroundArray.cxx @@ -201,7 +201,8 @@ Standard_Boolean OpenGl_BackgroundArray::createGradientArray (const Handle(OpenG Graphic3d_Attribute aGragientAttribInfo[] = { { Graphic3d_TOA_POS, Graphic3d_TOD_VEC2 }, - { Graphic3d_TOA_COLOR, Graphic3d_TOD_VEC3 } + { Graphic3d_TOA_COLOR, Graphic3d_TOD_VEC3 }, + { Graphic3d_TOA_COLOR_BACK, Graphic3d_TOD_VEC3 } }; if (!myAttribs->Init (4, aGragientAttribInfo, 2)) diff --git a/src/OpenGl/OpenGl_FrameStatsPrs.cxx b/src/OpenGl/OpenGl_FrameStatsPrs.cxx index 8213867c66..93f111c786 100644 --- a/src/OpenGl/OpenGl_FrameStatsPrs.cxx +++ b/src/OpenGl/OpenGl_FrameStatsPrs.cxx @@ -427,11 +427,13 @@ void OpenGl_FrameStatsPrs::Render (const Handle(OpenGl_Workspace)& theWorkspace) myChartVertices->Bind (aCtx); myChartVertices->bindAttribute (aCtx, Graphic3d_TOA_POS, 3, GL_FLOAT, myChartVertices->GetComponentsNb(), NULL); myChartVertices->bindAttribute (aCtx, Graphic3d_TOA_COLOR, 4, GL_UNSIGNED_BYTE, myChartVertices->GetComponentsNb(), (void* )sizeof(Graphic3d_Vec3)); + myChartVertices->bindAttribute (aCtx, Graphic3d_TOA_COLOR_BACK, 5, GL_UNSIGNED_BYTE, myChartVertices->GetComponentsNb(), (void*)sizeof(Graphic3d_Vec3)); myChartIndices->Bind (aCtx); aCtx->core15fwd->glDrawElements (GL_TRIANGLES, myChartIndices->GetElemsNb(), myChartIndices->GetDataType(), NULL); myChartIndices->Unbind (aCtx); myChartVertices->Unbind (aCtx); + myChartVertices->unbindAttribute (aCtx, Graphic3d_TOA_COLOR_BACK); myChartVertices->unbindAttribute (aCtx, Graphic3d_TOA_COLOR); myChartVertices->unbindAttribute (aCtx, Graphic3d_TOA_POS); aCtx->core15fwd->glDisable (GL_BLEND); @@ -439,8 +441,10 @@ void OpenGl_FrameStatsPrs::Render (const Handle(OpenGl_Workspace)& theWorkspace) myChartLines->Bind (aCtx); myChartLines->bindAttribute (aCtx, Graphic3d_TOA_POS, 3, GL_FLOAT, myChartLines->GetComponentsNb(), NULL); myChartLines->bindAttribute (aCtx, Graphic3d_TOA_COLOR, 4, GL_UNSIGNED_BYTE, myChartLines->GetComponentsNb(), (void* )sizeof(Graphic3d_Vec3)); + myChartLines->bindAttribute (aCtx, Graphic3d_TOA_COLOR_BACK, 5, GL_UNSIGNED_BYTE, myChartLines->GetComponentsNb(), (void*)sizeof(Graphic3d_Vec3)); aCtx->core15fwd->glDrawArrays (GL_LINES, 0, myChartLines->GetElemsNb()); myChartLines->Unbind (aCtx); + myChartLines->unbindAttribute (aCtx, Graphic3d_TOA_COLOR_BACK); myChartLines->unbindAttribute (aCtx, Graphic3d_TOA_COLOR); myChartLines->unbindAttribute (aCtx, Graphic3d_TOA_POS); diff --git a/src/OpenGl/OpenGl_PrimitiveArray.cxx b/src/OpenGl/OpenGl_PrimitiveArray.cxx index 151d1cc40a..6ea79e1052 100644 --- a/src/OpenGl/OpenGl_PrimitiveArray.cxx +++ b/src/OpenGl/OpenGl_PrimitiveArray.cxx @@ -432,6 +432,7 @@ void OpenGl_PrimitiveArray::drawArray (const Handle(OpenGl_Workspace)& theWorksp { // disable per-vertex color OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR); + OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR_BACK); } if (!myVboIndices.IsNull()) { diff --git a/src/OpenGl/OpenGl_ShaderProgram.cxx b/src/OpenGl/OpenGl_ShaderProgram.cxx index a5504af8af..6985560e81 100755 --- a/src/OpenGl/OpenGl_ShaderProgram.cxx +++ b/src/OpenGl/OpenGl_ShaderProgram.cxx @@ -510,6 +510,7 @@ Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)& SetAttributeName (theCtx, Graphic3d_TOA_NORM, "occNormal"); SetAttributeName (theCtx, Graphic3d_TOA_UV, "occTexCoord"); SetAttributeName (theCtx, Graphic3d_TOA_COLOR, "occVertColor"); + SetAttributeName (theCtx, Graphic3d_TOA_COLOR_BACK, "occVertColorBack"); // bind custom Vertex Attributes if (!myProxy.IsNull()) diff --git a/src/OpenGl/OpenGl_VertexBuffer.cxx b/src/OpenGl/OpenGl_VertexBuffer.cxx index 471e8be611..c46ce0d107 100644 --- a/src/OpenGl/OpenGl_VertexBuffer.cxx +++ b/src/OpenGl/OpenGl_VertexBuffer.cxx @@ -205,7 +205,15 @@ void OpenGl_VertexBuffer::bindFixed (const Handle(OpenGl_Context)& theCtx, { theCtx->core11ffp->glEnableClientState (GL_COLOR_ARRAY); theCtx->core11ffp->glColorPointer (theNbComp, theDataType, theStride, theOffset); - theCtx->core11ffp->glColorMaterial (GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); + theCtx->core11ffp->glColorMaterial (GL_FRONT,GL_AMBIENT_AND_DIFFUSE); + theCtx->core11fwd->glEnable (GL_COLOR_MATERIAL); + return; + } + case Graphic3d_TOA_COLOR_BACK: + { + theCtx->core11ffp->glEnableClientState (GL_COLOR_ARRAY); + theCtx->core11ffp->glColorPointer (theNbComp, theDataType, theStride, theOffset); + theCtx->core11ffp->glColorMaterial (GL_BACK, GL_AMBIENT_AND_DIFFUSE); theCtx->core11fwd->glEnable (GL_COLOR_MATERIAL); return; } @@ -229,6 +237,7 @@ void OpenGl_VertexBuffer::unbindFixed (const Handle(OpenGl_Context)& theCtx, case Graphic3d_TOA_NORM: theCtx->core11ffp->glDisableClientState (GL_NORMAL_ARRAY); return; case Graphic3d_TOA_UV: theCtx->core11ffp->glDisableClientState (GL_TEXTURE_COORD_ARRAY); return; case Graphic3d_TOA_COLOR: unbindFixedColor (theCtx); return; + case Graphic3d_TOA_COLOR_BACK: case Graphic3d_TOA_CUSTOM: { return; diff --git a/src/Shaders/Declarations.glsl b/src/Shaders/Declarations.glsl index ea4bc6ca15..972b592bb5 100644 --- a/src/Shaders/Declarations.glsl +++ b/src/Shaders/Declarations.glsl @@ -55,6 +55,7 @@ THE_ATTRIBUTE vec3 occNormal; THE_ATTRIBUTE vec4 occTexCoord; THE_ATTRIBUTE vec4 occVertColor; + THE_ATTRIBUTE vec4 occVertColorBack; #elif defined(FRAGMENT_SHADER) #if (__VERSION__ >= 130) #ifdef OCC_ENABLE_draw_buffers diff --git a/src/Shaders/Shaders_Declarations_glsl.pxx b/src/Shaders/Shaders_Declarations_glsl.pxx index 0485a491c6..e937b2de30 100644 --- a/src/Shaders/Shaders_Declarations_glsl.pxx +++ b/src/Shaders/Shaders_Declarations_glsl.pxx @@ -58,6 +58,7 @@ static const char Shaders_Declarations_glsl[] = " THE_ATTRIBUTE vec3 occNormal;\n" " THE_ATTRIBUTE vec4 occTexCoord;\n" " THE_ATTRIBUTE vec4 occVertColor;\n" + " THE_ATTRIBUTE vec4 occVertColorBack;\n" "#elif defined(FRAGMENT_SHADER)\n" " #if (__VERSION__ >= 130)\n" " #ifdef OCC_ENABLE_draw_buffers\n"