Commit 4e10917d authored by Maire Nicolas's avatar Maire Nicolas

Ajout dans la classe ShaderMutator d'une méthode permettant de modifier la...

Ajout dans la classe ShaderMutator d'une méthode permettant de modifier la version de GLSL utilisée.
parent 8b23ebca
......@@ -28,6 +28,7 @@
#include "Utils/cgognStream.h"
#include <boost/regex.hpp>
#include <string>
#include <sstream>
namespace CGoGN
{
......@@ -65,6 +66,12 @@ public:
*/
bool GS_containsVariableDeclaration(const std::string& variableName);
/**
* set or change shading language version if the current version is lower
* @param version the version to set (110, 120, 150...)
*/
void VS_FS_GS_setMinShadingLanguageVersion(int version);
/**
* insert code before main function into shader vertex source code
* @param insertedCode source code to insert into shader
......@@ -179,6 +186,13 @@ private:
*/
bool containsVariableDeclaration(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);
/**
* insert code before main function into source code
* @param insertedCode source code to insert into shader
......
......@@ -75,7 +75,7 @@ void ClippingShader::setClippingPlaneEquation(Geom::Vec4f clipPlane)
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
rotMat(i, j) = m[i][j];
Geom::Vec4f rotatedVec = rotMat * m_clipPlaneEquation;
Geom::Vec4f rotatedVec = /*rotMat * */m_clipPlaneEquation;
// Send the resulting plane equation to shader
bind();
......@@ -210,6 +210,9 @@ void ClippingShader::addPlaneClippingToShaderSource()
SM.VS_insertCodeBeforeMainFunction(VS_head_insertion);
SM.VS_insertCodeAtMainFunctionBeginning(VS_mainBegin_insertion);
// Following code insertions need shading language 120 at least (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);
......@@ -219,7 +222,7 @@ void ClippingShader::addPlaneClippingToShaderSource()
reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
// Recompile shaders (automatically calls updateClippingUniforms
// Recompile shaders (automatically calls updateClippingUniforms)
recompile();
}
......
......@@ -56,6 +56,13 @@ bool ShaderMutator::GS_containsVariableDeclaration(const std::string& variableNa
return containsVariableDeclaration(variableName, m_gShaderMutation);
}
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))
......@@ -299,6 +306,61 @@ bool ShaderMutator::containsVariableDeclaration(const std::string& variableName,
return false;
}
bool ShaderMutator::setMinShadingLanguageVersion(int version, std::string& modifiedSrc)
{
// Regular expression for shading language version
// <#version> <space> <digit>[1 or more times]
boost::regex version_re("#version (\\d+)");
// Matches results
boost::match_results <std::string::iterator> matches;
// Build the version string
std::string versionStr;
std::stringstream ss;
ss << version;
versionStr = ss.str();
// Search for the first expression that matches and isn't commented
std::string::iterator start = modifiedSrc.begin();
std::string::iterator end = modifiedSrc.end();
while (regex_search(start, end, matches, version_re, boost::format_first_only))
{
// Start position of the match
size_t startPosition = std::distance(modifiedSrc.begin(), matches[0].first);
// Change the version number if the matched "#version ..." is the good one (i.e. not commented)
if (!isCommented(startPosition, modifiedSrc))
{
// Match[1] should be the version number
std::string oldVersion(matches[1].first, matches[1].second);
int oldVersionValue = atoi(oldVersion.c_str());
size_t oldVersionLength = oldVersion.length();
size_t oldVersionPosition = std::distance(modifiedSrc.begin(), matches[1].first);
// Replace the version value only if it is lower than 'version'
if (oldVersionValue < version)
{
modifiedSrc.replace(oldVersionPosition, oldVersionLength, versionStr);
return true;
}
else
return false;
}
// Else continue to search for it after last match
else
{
start = matches[0].second;
}
}
// At this point no correct match was found : insert directly the "#version ..." line
std::string versionLineStr = "#version " + versionStr + "\n";
modifiedSrc.insert(0, versionLineStr);
return true;
}
bool ShaderMutator::insertCodeBeforeMainFunction(const std::string& insertedCode, std::string& modifiedSrc)
{
// Regular expression for main function
......
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