Commit fde3961a authored by Maire Nicolas's avatar Maire Nicolas

Rajout d'une méthode dans ShaderMutator pour changer la valeur d'une constante...

Rajout d'une méthode dans ShaderMutator pour changer la valeur d'une constante de type int + gros cleanup.
parent 0c7e7d06
......@@ -24,7 +24,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
......@@ -269,22 +269,6 @@
</widget>
<resources/>
<connections>
<connection>
<sender>check_drawFaces</sender>
<signal>toggled(bool)</signal>
<receiver>combo_faceLighting</receiver>
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>65</x>
<y>98</y>
</hint>
<hint type="destinationlabel">
<x>65</x>
<y>127</y>
</hint>
</hints>
</connection>
<connection>
<sender>check_drawNormals</sender>
<signal>toggled(bool)</signal>
......
......@@ -27,9 +27,6 @@
#include "Utils/GLSLShader.h"
#include "Geometry/vector_gen.h"
#include "glm/glm.hpp"
#include "Geometry/matrix.h"
#include "Utils/trackball.h"
#include "Utils/cgognStream.h"
#include "Utils/shaderMutator.h"
#include "Utils/drawer.h"
......
......@@ -40,6 +40,12 @@ class ShaderMutator
{
public:
/**
* enums used to choose which shader src to modify
*/
enum shaderSrcType { VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER };
/**
* constructor
* @param vertShaderSrc the vertex shader source to store
......@@ -49,85 +55,48 @@ 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
* check if a variable is declared in the shader source
* @param srcType the shader source to use (vertex, fragment or geometry)
* @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
* @param variableName the variable to search for
*/
bool FS_containsVariableDeclaration(const std::string& variableName);
bool containsVariableDeclaration(shaderSrcType srcType, const std::string& variableName);
/**
* 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);
/**
* set or change shading language version if the current version is lower
* set or change shading language version in the shader source
* - only if the current version is lower
* @param srcType the shader source to use (vertex, fragment or geometry)
* @param version the version to set (110, 120, 150...)
*/
void VS_FS_GS_setMinShadingLanguageVersion(int version);
void setMinShadingLanguageVersion(shaderSrcType srcType, int version);
/**
* insert code before main function into shader vertex source code
* @param insertedCode source code to insert into shader
*/
void VS_insertCodeBeforeMainFunction(const std::string& insertedCode);
/**
* insert code before main function into shader fragment source code
* @param insertedCode source code to insert into shader
*/
void FS_insertCodeBeforeMainFunction(const std::string& insertedCode);
/**
* insert code before main function into shader geometry source code
* @param insertedCode source code to insert into shader
* change int constant value in the shader source
* @param srcType the shader source to use (vertex, fragment or geometry)
* @param newVal the new value
*/
void GS_insertCodeBeforeMainFunction(const std::string& insertedCode);
/**
* insert code at the beginning of main function into shader vertex source code
* @param insertedCode source code to insert into shader
*/
void VS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode);
/**
* insert code at the beginning of main function into shader fragment source code
* @param insertedCode source code to insert into shader
*/
void FS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode);
/**
* insert code at the beginning of main function into shader geometry source code
* @param insertedCode source code to insert into shader
*/
void GS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode);
void changeIntConstantValue(shaderSrcType srcType, int newVal);
/**
* insert code at the beginning of main function into shader vertex source code
* @warning takes the number of opening and closing braces of main function into account
* insert code before main function into shader source
* @param srcType the shader source to use (vertex, fragment or geometry)
* @param insertedCode source code to insert into shader
*/
void VS_insertCodeAtMainFunctionEnd(const std::string& insertedCode);
void insertCodeBeforeMainFunction(shaderSrcType srcType, const std::string& insertedCode);
/**
* insert code at the beginning of main function into shader fragment source code
* @warning takes the number of opening and closing braces of main function into account
* insert code at the beginning of main function into shader source
* @param srcType the shader source to use (vertex, fragment or geometry)
* @param insertedCode source code to insert into shader
*/
void FS_insertCodeAtMainFunctionEnd(const std::string& insertedCode);
void insertCodeAtMainFunctionBeginning(shaderSrcType srcType, const std::string& insertedCode);
/**
* insert code at the beginning of main function into shader geometry source code
* insert code at the end of main function into shader source
* @warning takes the number of opening and closing braces of main function into account
* @param srcType the shader source to use (vertex, fragment or geometry)
* @param insertedCode source code to insert into shader
*/
void GS_insertCodeAtMainFunctionEnd(const std::string& insertedCode);
void insertCodeAtMainFunctionEnd(shaderSrcType srcType, const std::string& insertedCode);
/**
* returns the modified vertex shader source code
......@@ -167,52 +136,59 @@ private:
std::string m_gShaderMutation;
/**
* verify if the given position in the string is commented or not
* check if the given position in the source is commented
* @param pos the position
* @param str the string to analyze
* @param src the source to analyze
*/
bool isCommented(size_t pos, const std::string& str);
bool srcIsCommented(size_t pos, const std::string& src);
/**
* verify if the given position in the string is commented with a one-line comment or not
* check if the given position in the source is commented with a one-line comment
* @param pos the position
* @param str the string to analyze
* @param src the source to analyze
*/
bool isOneLineCommented(size_t pos, const std::string& str);
bool srcIsOneLineCommented(size_t pos, const std::string& src);
/**
* check if a variable is declared in a source code or not
* check if a variable is declared in a source code
* @param variable the variable to search for
*/
bool containsVariableDeclaration(const std::string& variableName, std::string& src);
bool srcContainsVariableDeclaration(const std::string& variableName, std::string& src);
/**
* set or change shading language version if the current version is lower
* @param version the version to set (110, 120, 150...)
* @param modifiedSrc shader source code to modify
*/
bool setMinShadingLanguageVersion(int version, std::string& modifiedSrc);
bool srcSetMinShadingLanguageVersion(int version, std::string& modifiedSrc);
/**
* change int constant value
* @param newVal the new value
* @param modifiedSrc shader source code to modify
*/
bool srcChangeIntConstantValue(int newVal, std::string& modifiedSrc);
/**
* insert code before main function into source code
* @param insertedCode source code to insert into shader
* @param modifiedSrc shader source code to modify
*/
bool insertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc);
bool srcInsertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc);
/**
* insert code at the beginning of main function into source code
* @param insertedCode source code to insert into shader
* @param modifiedSrc shader source code to modify
*/
bool insertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc);
bool srcInsertCodeAtMainFunctionBeginning(const std::string& insertedCode, std::string& modifiedSrc);
/**
* insert code at the end of main function into source code
* @param insertedCode source code to insert into shader
* @param modifiedSrc shader source code to modify
*/
bool insertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc);
bool srcInsertCodeAtMainFunctionEnd(const std::string& insertedCode, std::string& modifiedSrc);
};
......
......@@ -261,7 +261,8 @@ void ClippingShader::setPlaneClipping(int planesCount)
" gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n";
// If the previous plane count was zero, the previous shader source codes were the original ones. Store them
// 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)
{
originalVertShaderSrc = getVertexShaderSrc();
......@@ -276,7 +277,7 @@ void ClippingShader::setPlaneClipping(int planesCount)
ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc, "");
// First check if the vertex shader contains the VertexPosition attribute
if (!SM.VS_containsVariableDeclaration("VertexPosition"))
if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition"))
{
CGoGNerr
<< "ERROR - "
......@@ -289,16 +290,15 @@ void ClippingShader::setPlaneClipping(int planesCount)
}
// 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);
SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion);
SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_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);
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());
......@@ -356,11 +356,6 @@ void ClippingShader::updateClippingUniforms()
sendColorAttenuationFactorUniform();
}
void ClippingShader::displayClippingPlane()
{
m_planeDrawer->callList();
}
void ClippingShader::sendClippingPlanesUniform()
{
bind();
......@@ -373,6 +368,11 @@ void ClippingShader::sendColorAttenuationFactorUniform()
glUniform1f(m_unif_colorAttenuationFactor, m_colorAttenuationFactor);
}
void ClippingShader::displayClippingPlane()
{
m_planeDrawer->callList();
}
} // namespace Utils
} // namespace CGoGN
......
......@@ -326,8 +326,6 @@ void GLWidget::keyPressEvent(QKeyEvent* event)
m_state_modifier = event->modifiers();
std::cout << "Press : " << m_state_modifier << std::endl;
int k = event->key();
if ( (k >= 65) && (k <= 91) && !(event->modifiers() & Qt::ShiftModifier) )
k += 32;
......@@ -343,8 +341,6 @@ void GLWidget::keyReleaseEvent(QKeyEvent *event)
m_state_modifier = event->modifiers();
int k = event->key();
std::cout << "Up : " << m_state_modifier << std::endl;
// align on axis
if ((k == 'Z') && (event->modifiers() & Qt::ShiftModifier))
{
......
......@@ -30,6 +30,12 @@ namespace CGoGN
namespace Utils
{
/***********************************************
*
* Public Section
*
***********************************************/
ShaderMutator::ShaderMutator(const std:: string& shaderName, const std::string& vertShaderSrc, const std::string& fragShaderSrc, const std::string& geomShaderSrc)
{
// Store the shader name
......@@ -41,167 +47,265 @@ ShaderMutator::ShaderMutator(const std:: string& shaderName, const std::string&
m_gShaderMutation = geomShaderSrc;
}
bool ShaderMutator::VS_containsVariableDeclaration(const std::string& variableName)
bool ShaderMutator::containsVariableDeclaration(shaderSrcType srcType, const std::string& variableName)
{
return containsVariableDeclaration(variableName, m_vShaderMutation);
}
bool result = false;
bool ShaderMutator::FS_containsVariableDeclaration(const std::string& variableName)
{
return containsVariableDeclaration(variableName, m_fShaderMutation);
}
switch (srcType)
{
case VERTEX_SHADER :
result = srcContainsVariableDeclaration(variableName, m_vShaderMutation);
break;
bool ShaderMutator::GS_containsVariableDeclaration(const std::string& variableName)
{
return containsVariableDeclaration(variableName, m_gShaderMutation);
}
case FRAGMENT_SHADER :
result = srcContainsVariableDeclaration(variableName, m_fShaderMutation);
break;
void ShaderMutator::VS_FS_GS_setMinShadingLanguageVersion(int version)
{
setMinShadingLanguageVersion(version, m_vShaderMutation);
setMinShadingLanguageVersion(version, m_fShaderMutation);
setMinShadingLanguageVersion(version, m_gShaderMutation);
}
void ShaderMutator::VS_insertCodeBeforeMainFunction(const std::string& insertedCode)
{
if (!insertCodeBeforeMainFunction(insertedCode, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::VS_insertCodeBeforeMainFunction : "
<< "Unable to insert source code in vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
case GEOMETRY_SHADER :
result = srcContainsVariableDeclaration(variableName, m_gShaderMutation);
break;
}
}
void ShaderMutator::FS_insertCodeBeforeMainFunction(const std::string& insertedCode)
{
if (!insertCodeBeforeMainFunction(insertedCode, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::FS_insertCodeBeforeMainFunction : "
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
}
return result;
}
void ShaderMutator::GS_insertCodeBeforeMainFunction(const std::string& insertedCode)
void ShaderMutator::setMinShadingLanguageVersion(shaderSrcType srcType, int version)
{
if (!insertCodeBeforeMainFunction(insertedCode, m_gShaderMutation))
switch (srcType)
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::GS_insertCodeBeforeMainFunction : "
<< "Unable to insert source code in geometry shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
}
}
case VERTEX_SHADER :
srcSetMinShadingLanguageVersion(version, m_vShaderMutation);
break;
void ShaderMutator::VS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode)
{
if (!insertCodeAtMainFunctionBeginning(insertedCode, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::VS_insertCodeAtMainFunctionBeginnning : "
<< "Unable to insert source code vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
}
}
case FRAGMENT_SHADER :
srcSetMinShadingLanguageVersion(version, m_fShaderMutation);
break;
void ShaderMutator::FS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode)
{
if (!insertCodeAtMainFunctionBeginning(insertedCode, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::FS_insertCodeAtMainFunctionBeginnning : "
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
case GEOMETRY_SHADER :
srcSetMinShadingLanguageVersion(version, m_gShaderMutation);
break;
}
}
void ShaderMutator::GS_insertCodeAtMainFunctionBeginning(const std::string& insertedCode)
void ShaderMutator::changeIntConstantValue(shaderSrcType srcType, int newVal)
{
if (!insertCodeAtMainFunctionBeginning(insertedCode, m_gShaderMutation))
switch (srcType)
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::GS_insertCodeAtMainFunctionBeginnning : "
<< "Unable to insert source code in geometry shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
case VERTEX_SHADER :
if (!srcChangeIntConstantValue(newVal, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::changeIntConstantValue : "
<< "Unable to change int constant value in vertex shader of "
<< m_shaderName
<< ". Constant declaration not found"
<< CGoGNendl;
return;
}
break;
case FRAGMENT_SHADER :
if (!srcChangeIntConstantValue(newVal, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::changeIntConstantValue : "
<< "Unable to change int constant value in fragment shader of "
<< m_shaderName
<< ". Constant declaration not found"
<< CGoGNendl;
return;
}
break;
case GEOMETRY_SHADER :
if (!srcChangeIntConstantValue(newVal, m_gShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::changeIntConstantValue : "
<< "Unable to change int constant value in geometry shader of "
<< m_shaderName
<< ". Constant declaration not found"
<< CGoGNendl;
return;
}
break;
}
}
void ShaderMutator::VS_insertCodeAtMainFunctionEnd(const std::string& insertedCode)
void ShaderMutator::insertCodeBeforeMainFunction(shaderSrcType srcType, const std::string& insertedCode)
{
if (!insertCodeAtMainFunctionEnd(insertedCode, m_vShaderMutation))
switch (srcType)
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::VS_insertCodeAtMainFunctionEnd : "
<< "Unable to insert source code in vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}' in main"
<< CGoGNendl;
case VERTEX_SHADER :
if (!srcInsertCodeBeforeMainFunction(insertedCode, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeBeforeMainFunction : "
<< "Unable to insert source code in vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
return;
}
break;
case FRAGMENT_SHADER :
if (!srcInsertCodeBeforeMainFunction(insertedCode, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeBeforeMainFunction : "
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
return;
}
break;
case GEOMETRY_SHADER :
if (!srcInsertCodeBeforeMainFunction(insertedCode, m_gShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeBeforeMainFunction : "
<< "Unable to insert source code in geometry shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
return;
}
break;
}
}
void ShaderMutator::FS_insertCodeAtMainFunctionEnd(const std::string& insertedCode)
void ShaderMutator::insertCodeAtMainFunctionBeginning(shaderSrcType srcType, const std::string& insertedCode)
{
if (!insertCodeAtMainFunctionEnd(insertedCode, m_fShaderMutation))
switch (srcType)
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::FS_insertCodeAtMainFunctionEnd : "
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}' in main"
<< CGoGNendl;
case VERTEX_SHADER :
if (!srcInsertCodeAtMainFunctionBeginning(insertedCode, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeAtMainFunctionBeginnning : "
<< "Unable to insert source code in vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
return;
}
break;
case FRAGMENT_SHADER :
if (!srcInsertCodeAtMainFunctionBeginning(insertedCode, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeAtMainFunctionBeginnning : "
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
return;
}
break;
case GEOMETRY_SHADER :
if (!srcInsertCodeAtMainFunctionBeginning(insertedCode, m_gShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeAtMainFunctionBeginnning : "
<< "Unable to insert source code in geometry shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration"
<< CGoGNendl;
return;
}
break;
}
}
void ShaderMutator::GS_insertCodeAtMainFunctionEnd(const std::string& insertedCode)
void ShaderMutator::insertCodeAtMainFunctionEnd(shaderSrcType srcType, const std::string& insertedCode)
{
if (!insertCodeAtMainFunctionEnd(insertedCode, m_gShaderMutation))
switch (srcType)
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::GS_insertCodeAtMainFunctionEnd : "
<< "Unable to insert source code in geometry shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}' in main"
<< CGoGNendl;
}
}
case VERTEX_SHADER :
if (!srcInsertCodeAtMainFunctionEnd(insertedCode, m_vShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeAtMainFunctionEnd : "
<< "Unable to insert source code in vertex shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}' in main"
<< CGoGNendl;
return;
}
break;
case FRAGMENT_SHADER :
if (!srcInsertCodeAtMainFunctionEnd(insertedCode, m_fShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeAtMainFunctionEnd : "
<< "Unable to insert source code in fragment shader of "
<< m_shaderName
<< ". You should check if the shader has a main function declaration "
<< "and as many '{' as '}' in main"
<< CGoGNendl;
return;
}
break;
case GEOMETRY_SHADER :
if (!srcInsertCodeAtMainFunctionEnd(insertedCode, m_gShaderMutation))
{
CGoGNerr
<< "ERROR - "
<< "ShaderMutator::insertCodeAtMainFunctionEnd : "
<< "Unable to insert source code in geometry shader of "
<< m_shaderName