Commit ccbaf38f authored by Maire Nicolas's avatar Maire Nicolas

Gestion correcte du passage de zéro plans de clipping à n plans et vice versa

parent 1915119d
......@@ -105,7 +105,7 @@ public:
/**
* get the clipping planes count used for plane clipping
*/
int getClippingPlanesCount() { return (int)m_clipPlanesEquations.size(); }
int getClippingPlanesCount() { return (int)m_clipPlanesEquations.size() / 4; }
/**
* update uniforms (get their locations and resend their values) for clipping
......@@ -130,6 +130,16 @@ private:
*/
void sendColorAttenuationFactorUniform();
/**
* stored original vertex shader source code (without clipping)
*/
std::string originalVertShaderSrc;
/**
* stored original fragment shader source code (without clipping)
*/
std::string originalFragShaderSrc;
/**
* clip planes equations (a, b, c, d)*planes count
*/
......
......@@ -49,19 +49,19 @@ public:
ShaderMutator(const std::string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc);
/**
* check if a variable is declared in the vertex shader source or not
* check if a variable is declared in the vertex shader source
* @param variableName the variable to search for
*/
bool VS_containsVariableDeclaration(const std::string& variableName);
/**
* check if a variable is declared in the fragment shader source or not
* check if a variable is declared in the fragment shader source
* @param variableName the variable to search for
*/
bool FS_containsVariableDeclaration(const std::string& variableName);
/**
* check if a variable is declared in the geometry shader source or not
* check if a variable is declared in the geometry shader source
* @param variableName the variable to search for
*/
bool GS_containsVariableDeclaration(const std::string& variableName);
......
......@@ -148,15 +148,20 @@ float ClippingShader::getClippingColorAttenuationFactor()
void ClippingShader::setPlaneClipping(int planesCount)
{
// Verify that the given clipping planes count is valid
if (planesCount < 0)
{
CGoGNerr
<< "ERROR - "
<< "ClippingShader::setPlanesClipping"
<< " - Given clipping planes count given is not positive !"
<< CGoGNendl;
return;
}
// Shader name
std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;
// String for clipping planes count
std::string planesCountStr;
std::stringstream ss;
ss << planesCount;
planesCountStr = ss.str();
// Verify that the shader has been well created
if (!isCreated())
{
......@@ -183,16 +188,11 @@ void ClippingShader::setPlaneClipping(int planesCount)
return;
}
// Verify that the given clipping planes count is valid
if (planesCount < 0)
{
CGoGNerr
<< "ERROR - "
<< "ClippingShader::setPlanesClipping"
<< " - Given clipping planes count given is not positive !"
<< CGoGNendl;
return;
}
// String for clipping planes count
std::string planesCountStr;
std::stringstream ss;
ss << planesCount;
planesCountStr = ss.str();
// Strings that will be inserted into the source code
......@@ -259,38 +259,58 @@ void ClippingShader::setPlaneClipping(int planesCount)
" if (clip_MinDistanceToPlanes > 0.0)\n"
" gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n";
// Use a shader mutator
ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc(), "");
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.VS_containsVariableDeclaration("VertexPosition"))
// If the previous plane count was zero, the previous shader source codes were the original ones. Store them
if (getClippingPlanesCount() == 0)
{
CGoGNerr
<< "ERROR - "
<< "ClippingShader::setPlaneClipping"
<< " - Could not process shader "
<< m_nameVS
<< " source code : no VertexPosition attribute found"
<< CGoGNendl;
return;
originalVertShaderSrc = getVertexShaderSrc();
originalFragShaderSrc = getFragmentShaderSrc();
}
// If the given plane count is > 0, modify the original shader sources
if (planesCount > 0)
{
// Use a shader mutator
ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc, "");
// Modify vertex shader source code
SM.VS_insertCodeBeforeMainFunction(VS_head_insertion);
SM.VS_insertCodeAtMainFunctionBeginning(VS_mainBegin_insertion);
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.VS_containsVariableDeclaration("VertexPosition"))
{
CGoGNerr
<< "ERROR - "
<< "ClippingShader::setPlaneClipping"
<< " - Could not process shader "
<< m_nameVS
<< " source code : no VertexPosition attribute found"
<< CGoGNendl;
return;
}
// Modify vertex shader source code
SM.VS_insertCodeBeforeMainFunction(VS_head_insertion);
SM.VS_insertCodeAtMainFunctionBeginning(VS_mainBegin_insertion);
// Following code insertions need at least shading language 120 (GLSL arrays)
SM.VS_FS_GS_setMinShadingLanguageVersion(120);
// Following code insertions need at least shading language 120 (GLSL arrays)
SM.VS_FS_GS_setMinShadingLanguageVersion(120);
// Modify fragment shader source code
SM.FS_insertCodeBeforeMainFunction(FS_head_insertion);
SM.FS_insertCodeAtMainFunctionEnd(FS_mainEnd_insertion);
SM.FS_insertCodeAtMainFunctionBeginning(FS_mainBegin_insertion);
// Modify fragment shader source code
SM.FS_insertCodeBeforeMainFunction(FS_head_insertion);
SM.FS_insertCodeAtMainFunctionEnd(FS_mainEnd_insertion);
SM.FS_insertCodeAtMainFunctionBeginning(FS_mainBegin_insertion);
// Reload both shaders
reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
// Reload both shaders
reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
}
// Else no clipping is wanted anymore, so get back the original shader sources
else
{
// Reload both original shaders
reloadVertexShaderFromMemory(originalVertShaderSrc.c_str());
reloadFragmentShaderFromMemory(originalFragShaderSrc.c_str());
}
// Resize the planes equations uniform to the right size
m_clipPlanesEquations.resize(4*(size_t)planesCount, 0.0);
......@@ -301,6 +321,10 @@ void ClippingShader::setPlaneClipping(int planesCount)
void ClippingShader::updateClippingUniforms()
{
// These uniforms only exist if the clipping planes count is > 0
if (getClippingPlanesCount() <= 0)
return;
// Shader name
std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment