52 #include <QStringList>
59 #include <ACG/GL/gl.hh>
60 #include <ACG/Config/ACGDefines.hh>
70 #define SG_MAX_SHADER_LIGHTS 8
72 enum ShaderGenLightType
74 SG_LIGHT_DIRECTIONAL = 0,
77 SG_LIGHT_FORCE_DWORD = 0xFFFFFFFF
80 enum ShaderGenShadeMode
86 SG_SHADE_FORCE_DWORD = 0xFFFFFFFF
96 shadeMode(SG_SHADE_UNLIT),
98 vertexTemplateFile(
""),
99 tessControlTemplateFile(
""),
100 tessEvaluationTemplateFile(
""),
101 geometryTemplateFile(
""),
102 fragmentTemplateFile(
""),
104 colorMaterialMode(GL_AMBIENT_AND_DIFFUSE),
107 texGenMode(GL_EYE_LINEAR),
108 texGenPerFragment(
false)
110 for (
unsigned int i = 0 ; i < SG_MAX_SHADER_LIGHTS ; ++i)
111 lightTypes[i] = SG_LIGHT_DIRECTIONAL;
144 ShaderGenLightType lightTypes[SG_MAX_SHADER_LIGHTS];
146 ShaderGenShadeMode shadeMode;
151 QString vertexTemplateFile;
152 QString tessControlTemplateFile;
153 QString tessEvaluationTemplateFile;
154 QString geometryTemplateFile;
155 QString fragmentTemplateFile;
173 GLenum colorMaterialMode;
186 const std::map<size_t,TextureType>& textureTypes()
const {
return textureTypes_;}
196 textureTypes_[_stage] = t;
202 bool textured()
const {
return !textureTypes_.empty();}
217 bool texGenPerFragment;
219 void enableTexGenObjectLinear(
int _dim = 2)
221 texGenDim = std::max(std::min(_dim, 4), 0);
222 texGenMode = GL_OBJECT_LINEAR;
225 void enableTexGenEyeLinear(
int _dim = 2)
227 texGenDim = std::max(std::min(_dim, 4), 0);
228 texGenMode = GL_EYE_LINEAR;
231 void enableTexGenSphericalMap(
int _dim = 2)
233 texGenDim = std::max(std::min(_dim, 2), 0);
234 texGenMode = GL_SPHERE_MAP;
237 void enableTexGenNormalMap(
int _dim = 3)
239 texGenDim = std::max(std::min(_dim, 3), 0);
240 texGenMode = GL_NORMAL_MAP;
243 void enableTexGenReflectionMap(
int _dim = 3)
245 texGenDim = std::max(std::min(_dim, 3), 0);
246 texGenMode = GL_REFLECTION_MAP;
249 void disableTexGen() { texGenDim = 0; }
253 bool operator == (
const ShaderGenDesc& _rhs)
const
255 if (numLights != _rhs.numLights)
258 if (shadeMode != _rhs.shadeMode)
261 if (vertexColors != _rhs.vertexColors)
264 if (textured() != _rhs.textured())
269 if (vertexColorsInterpolator != _rhs.vertexColorsInterpolator)
272 if (colorMaterialMode != _rhs.colorMaterialMode)
276 if (fragmentTemplateFile != _rhs.fragmentTemplateFile)
279 if (geometryTemplateFile != _rhs.geometryTemplateFile)
282 if (vertexTemplateFile != _rhs.vertexTemplateFile)
285 if (tessControlTemplateFile != _rhs.tessControlTemplateFile)
288 if (tessEvaluationTemplateFile != _rhs.tessEvaluationTemplateFile)
291 if (macros != _rhs.macros)
294 if (texGenDim != _rhs.texGenDim)
299 if (texGenMode != _rhs.texGenMode)
302 if (texGenPerFragment != _rhs.texGenPerFragment)
307 return memcmp(lightTypes, _rhs.lightTypes, numLights *
sizeof(ShaderGenLightType)) == 0;
547 void setGLSLVersion(
int _version);
551 void addIncludeFile(QString _fileName);
580 void initDefaultUniforms();
590 void addInput(
const QString& _input);
600 void addInput(
const QString& _type,
const QString& _varname) {
addInput(_type + QString(
" ") + _varname); }
610 void addOutput(
const QString& _output);
620 void addOutput(
const QString& _type,
const QString& _varname) {
addOutput(_type + QString(
" ") + _varname); }
631 void addUniform(QString _uniform, QString _comment =
"");
640 void addDefine(
const QString& _define);
645 void addIODefine(
const QString& _macroName,
const QString& _resolvedName);
650 void addMacros(
const QStringList& _macros);
659 bool hasDefine(QString _define)
const;
668 void addLayout(QString _layout);
689 void addLight(
int lightIndex_, ShaderGenLightType _light);
696 void buildShaderCode(QStringList* _pMainCode,
const QStringList& _defaultLightingFunctions);
700 const QStringList& getShaderCode();
706 void saveToFile(
const char* _fileName);
721 void matchInputs(
const ShaderGenerator* _previousShaderStage,
bool _passToNextStage, QString _inputPrefix =
"outVertex", QString _outputPrefix =
"outGeometry");
733 void defineIOAbstraction(
const DefaultIODesc* _iodesc,
bool _vs,
bool _fs);
737 int getNumOutputs()
const;
743 QString getOutputName(
int _id)
const;
747 int getNumInputs()
const;
753 QString getInputName(
int _id)
const;
760 QString getIOMapName(
int _inId)
const;
772 QString macro_requestPosVS,
774 macro_requestTexcoord,
775 macro_requestVertexColor,
776 macro_requestNormalVS,
777 macro_requestNormalOS,
779 macro_requestRenormalize;
782 QString macro_inputPosVS,
788 macro_inputVertexColor,
793 macro_outputNormalVS,
794 macro_outputNormalOS,
795 macro_outputTexcoord,
796 macro_outputVertexColor;
808 QString vs_inputPrefix,
816 QString vs_inputPosition,
826 vs_outputVertexColor,
827 fs_outputFragmentColor;
838 void addStringToList(QString _str, QStringList* _list, QString _prefix =
"", QString _postfix =
"");
843 void addIOToCode(
const QStringList& _cmds);
854 QStringList outputs_;
855 QStringList uniforms_;
856 QStringList genDefines_;
857 QStringList layouts_;
1095 unsigned int getID() {
return modifierID_;}
1134 operator std::vector<unsigned int>()
const {
return std::vector<unsigned int>(1,modifierID_);}
1136 std::vector<unsigned int> operator | (
const std::vector<unsigned int>& _v)
const
1138 std::vector<unsigned int> r(1 + _v.size(), modifierID_);
1139 for (
size_t i = 0; i < _v.size(); ++i)
1145 unsigned int modifierID_;
1160 static void setShaderDir(QString _dir);
1165 static QString getShaderDir();
1213 void saveVertexShToFile(
const char* _fileName);
1214 void saveGeometryShToFile(
const char* _fileName);
1215 void saveFragmentShToFile(
const char* _fileName);
1219 const QStringList& getVertexShaderCode();
1223 const QStringList& getTessControlShaderCode();
1227 const QStringList& getTessEvaluationShaderCode();
1231 const QStringList& getGeometryShaderCode();
1235 const QStringList& getFragmentShaderCode();
1240 int getNumActiveModifiers()
const;
1257 bool hasGeometryShader()
const;
1261 bool hasTessControlShader()
const;
1265 bool hasTessEvaluationShader()
const;
1269 void generateShaders();
1276 static bool loadStringListFromFile(QString _fileName, QStringList* _out);
1281 static QString getAbsFilePath(QString _fileName);
1287 void loadShaderTemplateFromFile();
1291 void scanShaderTemplate(QStringList& _templateSrc, QString _templateFilename, QStringList* _outLayoutDirectives = 0);
1296 void init(
const ShaderGenDesc* _desc,
const unsigned int* _modifiers,
unsigned int _numActiveMods);
1298 void buildVertexShader();
1299 void buildTessControlShader();
1300 void buildTessEvalShader();
1301 void buildGeometryShader();
1302 void buildFragmentShader();
1304 void addVertexBeginCode(QStringList* _code);
1305 void addVertexEndCode(QStringList* _code);
1307 void addFragmentBeginCode(QStringList* _code);
1308 void addFragmentEndCode(QStringList* _code);
1314 void addLightingFunctions(QStringList* _code);
1318 void addLightingCode(QStringList* _code);
1322 void modifyLightingCode(QStringList* _code,
ShaderModifier* _modifier);
1326 void addTexGenCode(QStringList* _code,
bool _fragmentShader);
1329 static QString getPathName(QString _strFileName);
1333 int checkForIncludes(QString _str,
ShaderGenerator* _gen, QString _includePath);
1337 int checkForIncludes(QString _str, QStringList* _outImport, QString _includePath);
1343 static void loadLightingFunctions();
1351 QStringList vertexTemplate_;
1352 QStringList tessControlTemplate_;
1353 QStringList tessEvalTemplate_;
1354 QStringList geometryTemplate_;
1355 QStringList fragmentTemplate_;
1358 std::vector<ShaderModifier*> activeMods_;
1363 static std::vector<ShaderModifier*> registeredModifiers_;
1367 QString tessControlShaderFile_;
1368 QString tessEvalShaderFile_;
1369 QString geometryShaderFile_;
1370 QString fragmentShaderFile_;
1374 QStringList tessEvalLayout_;
1384 static QString shaderDir_;
1385 static QStringList lightingCode_;
bool normalizeTexColors
Defines if the textureVariable is normalized or not, if multiple textures are used.
void addOutput(const QString &_type, const QString &_varname)
Add one GLSL output specifier.
QStringList tessControlLayout_
layout() directives scanned from loaded templates
virtual void modifyTessControlIO(ShaderGenerator *_shader)
Add your own inputs/outputs to the tessellation control shader.
Namespace providing different geometric functions concerning angles.
static int numRegisteredModifiers_
registered shader modifier
QString outputPrefix_
prefix of outputs of this shader
void addRawIOBlock(QStringList _codeBlock)
Add a raw glsl IO code block.
virtual bool replaceDefaultLightingCode()
Specify whether this modifier replaces or extends the default lighting code.
void addTextureType(GLenum _type, bool _shadow, size_t _stage)
adds a texture type to the shader and enables texturing.
virtual void modifyVertexBeginCode(QStringList *_code)
Append code the the vertex shader.
virtual void modifyVertexIO(ShaderGenerator *_shader)
Add your own inputs/outputs to the vertex shader.
virtual void modifyGeometryIO(ShaderGenerator *_shader)
Add your own inputs/outputs to the geometry shader.
virtual void modifyFragmentEndCode(QStringList *_code)
Append code the the fragment shader.
ShaderGenerator::DefaultIODesc ioDesc_
default IO descriptor for the vertex shader
QString toString() const
convert ShaderGenDesc to string format for debugging
virtual void modifyTessEvalIO(ShaderGenerator *_shader)
Add your own inputs/outputs to the tessellation evaluation shader.
unsigned int getID()
Returns the modifier ID.
bool passPosVS_
default attributes that should be passed down from vertex shader
virtual void modifyFragmentBeginCode(QStringList *_code)
Append code the the fragment shader.
QString vertexColorsInterpolator
interpolation qualifier for input vertex colors: "flat", "smooth", "noperspective" ...
bool inputTexCoord_
default attributes that should be imported in vertex shader
void clearTextures()
disables texture support and removes all texture types
void addInput(const QString &_type, const QString &_varname)
Add one GLSL input specifier.
bool outputArrays_
outputs of shader are arrays (tess-control)
virtual void modifyLightingCode(QStringList *_code, int _lightId, ShaderGenLightType _lightType)
Modify the default lighting code of the shader generator.
bool inputArrays_
inputs of shader are arrays (tess-control, tess-eval, geometry)
std::map< size_t, TextureType > textureTypes_
holds the texture types (second) and the stage id (first). if empty, shader does not support textures...
bool renormalizeLighting_
renormalize before lighting, default false, can be activated via "#define SG_REQUEST_RENORMALIZE" ...
virtual void modifyVertexEndCode(QStringList *_code)
Append code the the vertex shader.
QStringList rawIO_
io block as glsl code
QString inputPrefix_
prefix of inputs to this shader, same as prefix of ouputs of previous stage
QStringList imports_
glsl code imports (with #include )
virtual void modifyFragmentIO(ShaderGenerator *_shader)
Add your own inputs/outputs to the fragment shader.
QString vertexShaderFile_
path + filename to shader templates