Commit 7d0e253b authored by Maire Nicolas's avatar Maire Nicolas

Changement du nombre de plans optimisé dans ClippingShader : changer la...

Changement du nombre de plans optimisé dans ClippingShader : changer la constante dans le shader au lieu de tout réinserer.
parent fde3961a
......@@ -52,7 +52,7 @@ public:
* @param fragShaderSrc the fragment shader source to store
* @param geomShaderSrc the geometry shader source to store
*/
ShaderMutator(const std::string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc);
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 shader source
......@@ -74,7 +74,7 @@ public:
* @param srcType the shader source to use (vertex, fragment or geometry)
* @param newVal the new value
*/
void changeIntConstantValue(shaderSrcType srcType, int newVal);
void changeIntConstantValue(shaderSrcType srcType, const std::string& constantName, int newVal);
/**
* insert code before main function into shader source
......@@ -167,7 +167,7 @@ private:
* @param newVal the new value
* @param modifiedSrc shader source code to modify
*/
bool srcChangeIntConstantValue(int newVal, std::string& modifiedSrc);
bool srcChangeIntConstantValue(int newVal, const std::string& constantName, std::string& modifiedSrc);
/**
* insert code before main function into source code
......
......@@ -260,49 +260,67 @@ void ClippingShader::setPlaneClipping(int planesCount)
" if (clip_MinDistanceToPlanes > 0.0)\n"
" gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n";
// Previous planes count
int previousPlanesCount = getClippingPlanesCount();
// If the previous planes count was zero, the previous shader source codes were the original ones. Store them
// (the planes count is initially zero when the object is constructed)
if (getClippingPlanesCount() == 0)
if (previousPlanesCount == 0)
{
originalVertShaderSrc = getVertexShaderSrc();
originalFragShaderSrc = getFragmentShaderSrc();
}
// If the given plane count is > 0, modify the original shader sources
// If the given plane count is > 0, modify the shader sources
if (planesCount > 0)
{
// Use a shader mutator
ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc, "");
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition"))
// If the previous planes count wasn't zero, there is only one constant to change in the fragment shader
if (previousPlanesCount != 0)
{
CGoGNerr
<< "ERROR - "
<< "ClippingShader::setPlaneClipping"
<< " - Could not process shader "
<< m_nameVS
<< " source code : no VertexPosition attribute found"
<< CGoGNendl;
return;
}
// Modify vertex shader source code
SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainBegin_insertion);
// Use a shader mutator
ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());
// Modify fragment shader source code
SM.setMinShadingLanguageVersion(ShaderMutator::FRAGMENT_SHADER, 120); // Following code insertions need at least shading language 120 (GLSL arrays)
SM.insertCodeBeforeMainFunction(ShaderMutator::FRAGMENT_SHADER, FS_head_insertion);
SM.insertCodeAtMainFunctionEnd(ShaderMutator::FRAGMENT_SHADER, FS_mainEnd_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::FRAGMENT_SHADER, FS_mainBegin_insertion);
// Modify the constant in the fragment shader
SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", planesCount);
// Reload both shaders
reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
// Reload fragment shader
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
}
// Else the whole clipping code must be inserted in the original shader sources
else
{
// Use a shader mutator
ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc);
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "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.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainBegin_insertion);
// Modify fragment shader source code
SM.setMinShadingLanguageVersion(ShaderMutator::FRAGMENT_SHADER, 120); // Following code insertions need at least shading language 120 (GLSL arrays)
SM.insertCodeBeforeMainFunction(ShaderMutator::FRAGMENT_SHADER, FS_head_insertion);
SM.insertCodeAtMainFunctionEnd(ShaderMutator::FRAGMENT_SHADER, FS_mainEnd_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::FRAGMENT_SHADER, FS_mainBegin_insertion);
// 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
......
......@@ -87,12 +87,12 @@ void ShaderMutator::setMinShadingLanguageVersion(shaderSrcType srcType, int vers
}
}
void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal)
void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, const std::string& constantName, int newVal)
{
switch (srcType)
{
case VERTEX_SHADER :
if (!srcChangeIntConstantValue(newVal, m_vShaderMutation))
if (!srcChangeIntConstantValue(newVal, constantName, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
......@@ -107,7 +107,7 @@ void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal)
break;
case FRAGMENT_SHADER :
if (!srcChangeIntConstantValue(newVal, m_fShaderMutation))
if (!srcChangeIntConstantValue(newVal, constantName, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
......@@ -122,7 +122,7 @@ void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal)
break;
case GEOMETRY_SHADER :
if (!srcChangeIntConstantValue(newVal, m_gShaderMutation))
if (!srcChangeIntConstantValue(newVal, constantName, m_gShaderMutation))
{
CGoGNerr
<< "ERROR - "
......@@ -465,11 +465,11 @@ bool ShaderMutator::srcSetMinShadingLanguageVersion(int version, std::string& mo
return true;
}
bool ShaderMutator::srcChangeIntConstantValue(int newVal, std::string& modifiedSrc)
bool ShaderMutator::srcChangeIntConstantValue(int newVal, const std::string& constantName, std::string& modifiedSrc)
{
// Regular expression for constant expression
// <#define> <white-space>[1 or more times] <digit>[1 or more times]
boost::regex const_re("#define\\s+(\\d+)");
// <#define> <white-space>[1 or more times] <constant name> <white-space>[1 or more times] <digit>[1 or more times]
boost::regex const_re("#define\\s+" + constantName + "\\s+(\\d+)");
// Matches results
boost::match_results <std::string::iterator> matches;
......
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