Commit 9cdc9130 authored by Jan Möbius's avatar Jan Möbius
Browse files

Shader generator extension to replace lightingcode.

Some debugging extensions to IRenderer
Support shininess in lighting code

refs #1315




git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@18195 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 77055d28
...@@ -79,9 +79,15 @@ void IRenderer::addRenderObject(ACG::RenderObject* _renderObject) ...@@ -79,9 +79,15 @@ void IRenderer::addRenderObject(ACG::RenderObject* _renderObject)
{ {
// do some more checks for error detection // do some more checks for error detection
if (!_renderObject->vertexDecl) if (!_renderObject->vertexDecl)
std::cout << "error: missing vertex declaration" << std::endl; std::cout << "error: missing vertex declaration in renderobject: " << _renderObject->debugName << std::endl;
else else
{ {
if (!_renderObject->depthWrite &&
!_renderObject->colorWriteMask[0] && !_renderObject->colorWriteMask[1] &&
!_renderObject->colorWriteMask[2] && !_renderObject->colorWriteMask[3])
std::cout << "warning: depth write and color write disabled in renderobject: " << _renderObject->debugName << std::endl;
renderObjects_.push_back(*_renderObject); renderObjects_.push_back(*_renderObject);
......
...@@ -122,7 +122,16 @@ void ScreenQuad::init () ...@@ -122,7 +122,16 @@ void ScreenQuad::init ()
if (!texDrawProg_) if (!texDrawProg_)
{
// save active program
GLint curProg = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &curProg);
texDrawProg_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "ScreenQuad/tex2D.glsl"); texDrawProg_ = GLSL::loadProgram("ScreenQuad/screenquad.glsl", "ScreenQuad/tex2D.glsl");
// restore active program
glUseProgram(curProg);
}
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
......
...@@ -351,15 +351,15 @@ void ShaderGenerator::buildShaderCode(QStringList* _pMainCode) ...@@ -351,15 +351,15 @@ void ShaderGenerator::buildShaderCode(QStringList* _pMainCode)
foreach(it, genDefines_) foreach(it, genDefines_)
code_.push_back(it); code_.push_back(it);
// provide imports
foreach(it, imports_)
code_.push_back(it);
// IO // IO
addIOToCode(inputs_); addIOToCode(inputs_);
addIOToCode(outputs_); addIOToCode(outputs_);
addIOToCode(uniforms_); addIOToCode(uniforms_);
// provide imports
foreach(it, imports_)
code_.push_back(it);
// main function // main function
foreach(it, (*_pMainCode)) foreach(it, (*_pMainCode))
code_.push_back(it); code_.push_back(it);
...@@ -917,6 +917,7 @@ void ShaderProgGenerator::addFragmentBeginCode(QStringList* _code) ...@@ -917,6 +917,7 @@ void ShaderProgGenerator::addFragmentBeginCode(QStringList* _code)
inputShader = "Geometry"; inputShader = "Geometry";
// support for projective texture mapping // support for projective texture mapping
_code->push_back("vec4 sg_vPosCS = out" + inputShader + "PosCS;");
_code->push_back("vec2 sg_vScreenPos = out" + inputShader + "PosCS.xy / out" + inputShader + "PosCS.w * 0.5 + vec2(0.5, 0.5);"); _code->push_back("vec2 sg_vScreenPos = out" + inputShader + "PosCS.xy / out" + inputShader + "PosCS.w * 0.5 + vec2(0.5, 0.5);");
_code->push_back("vec4 sg_cColor = vec4(g_cEmissive, ALPHA);"); _code->push_back("vec4 sg_cColor = vec4(g_cEmissive, ALPHA);");
...@@ -934,6 +935,7 @@ void ShaderProgGenerator::addFragmentBeginCode(QStringList* _code) ...@@ -934,6 +935,7 @@ void ShaderProgGenerator::addFragmentBeginCode(QStringList* _code)
_code->push_back("vec4 sg_vPosVS = out" + inputShader + "PosVS;"); _code->push_back("vec4 sg_vPosVS = out" + inputShader + "PosVS;");
_code->push_back("vec3 sg_vNormalVS = out" + inputShader + "Normal;"); _code->push_back("vec3 sg_vNormalVS = out" + inputShader + "Normal;");
addLightingCode(_code); addLightingCode(_code);
} }
...@@ -979,35 +981,83 @@ void ShaderProgGenerator::addFragmentEndCode(QStringList* _code) ...@@ -979,35 +981,83 @@ void ShaderProgGenerator::addFragmentEndCode(QStringList* _code)
void ShaderProgGenerator::addLightingCode(QStringList* _code) void ShaderProgGenerator::addLightingCode(QStringList* _code)
{ {
QString buf;
ShaderModifier* lightingModifier = 0;
for (int i = 0; i < desc_.numLights; ++i)
// check if any modifier replaces the default lighting function
for (int i = 0; i < numModifiers_ && !lightingModifier; ++i)
{ {
ShaderGenLightType lgt = desc_.lightTypes[i]; if (usage_ & (1 << i))
if (modifiers_[i]->replaceDefaultLightingCode())
lightingModifier = modifiers_[i];
}
if (!lightingModifier)
{
// default lighting code:
QString buf;
switch (lgt) for (int i = 0; i < desc_.numLights; ++i)
{ {
case SG_LIGHT_DIRECTIONAL: ShaderGenLightType lgt = desc_.lightTypes[i];
buf.sprintf("sg_cColor.xyz += LitDirLight(sg_vPosVS.xyz, sg_vNormalVS, g_vLightDir_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d);", i, i, i, i);
break;
case SG_LIGHT_POINT: switch (lgt)
buf.sprintf("sg_cColor.xyz += LitPointLight(sg_vPosVS.xyz, sg_vNormalVS, g_vLightPos_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_vLightAtten_%d);", i, i, i, i, i); {
break; case SG_LIGHT_DIRECTIONAL:
buf.sprintf("sg_cColor.xyz += LitDirLight(sg_vPosVS.xyz, sg_vNormalVS, g_vLightDir_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d);", i, i, i, i);
break;
case SG_LIGHT_POINT:
buf.sprintf("sg_cColor.xyz += LitPointLight(sg_vPosVS.xyz, sg_vNormalVS, g_vLightPos_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_vLightAtten_%d);", i, i, i, i, i);
break;
case SG_LIGHT_SPOT: case SG_LIGHT_SPOT:
buf.sprintf("sg_cColor.xyz += LitSpotLight(sg_vPosVS.xyz, sg_vNormalVS, g_vLightPos_%d, g_vLightDir_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_vLightAtten_%d, g_vLightAngleExp_%d);", i, i, i, i, i, i, i); buf.sprintf("sg_cColor.xyz += LitSpotLight(sg_vPosVS.xyz, sg_vNormalVS, g_vLightPos_%d, g_vLightDir_%d, g_cLightAmbient_%d, g_cLightDiffuse_%d, g_cLightSpecular_%d, g_vLightAtten_%d, g_vLightAngleExp_%d);", i, i, i, i, i, i, i);
break; break;
default: break;
}
default: break; _code->push_back(buf);
} }
_code->push_back(buf); // modify lighting color afterwards
for (int i = 0; i < numModifiers_; ++i)
{
if (usage_ & (1 << i))
modifyLightingCode(_code, modifiers_[i]);
}
}
else
{
// there exists a lighting modifier that completely replaces the default lighting shader
modifyLightingCode(_code, lightingModifier);
// apply remaining modifiers that do not replace the complete lighting code
for (int i = 0; i < numModifiers_; ++i)
{
if (usage_ & (1 << i) && lightingModifier != modifiers_[i])
modifyLightingCode(_code, modifiers_[i]);
}
} }
} }
void ShaderProgGenerator::modifyLightingCode( QStringList* _code, ShaderModifier* _modifier )
{
if (!_modifier) return;
for (int i = 0; i < desc_.numLights; ++i)
{
ShaderGenLightType lgt = desc_.lightTypes[i];
_modifier->modifyLightingCode(_code, i, lgt);
}
}
void ShaderProgGenerator::addLightingFunctions(QStringList* _code) void ShaderProgGenerator::addLightingFunctions(QStringList* _code)
...@@ -1110,6 +1160,35 @@ unsigned int ShaderProgGenerator::registerModifier( ShaderModifier* _modifier ) ...@@ -1110,6 +1160,35 @@ unsigned int ShaderProgGenerator::registerModifier( ShaderModifier* _modifier )
return _modifier->modifierID_; return _modifier->modifierID_;
} }
ShaderModifier* ShaderProgGenerator::getActiveModifier( int _i )
{
// search active modifiers
int counter = 0;
for (int i = 0; i < numModifiers_; ++i)
{
if (usage_ & (1 << i))
{
if (counter++ == _i)
return modifiers_[i];
}
}
// invalid _i
return 0;
}
int ShaderProgGenerator::getNumActiveModifiers() const
{
// count modifiers
int numActive = 0;
for (int i = 0; i < numModifiers_; ++i)
{
if (usage_ & (1 << i))
++numActive;
}
return numActive;
}
//============================================================================= //=============================================================================
......
...@@ -647,6 +647,27 @@ public: ...@@ -647,6 +647,27 @@ public:
*/ */
virtual void modifyFragmentEndCode(QStringList* _code) {} virtual void modifyFragmentEndCode(QStringList* _code) {}
/** \brief Modify the default lighting code of the shader generator.
*
* Refer to the generation structure (\ref ShaderGenerator_page )
* to see where your code is added and which variables you can modify.
* Use
* \code
* _code->push_back("...");
* \endcode
* to insert your code here.
* @param _code string list of shader code.
* @param _lightId light index, use g_vLightDir_{_lightId}, etc. in shader code to use light parameters
* @param _lightType light type: point, spot or directional light
*/
virtual void modifyLightingCode(QStringList* _code, int _lightId, ShaderGenLightType _lightType) {}
/** \brief Specify whether this modifier replaces or extends the default lighting code.
*
* @return return false if this modifier modifies an alredy computed lighting color, return true if this modifier replaces the default lighting color
*/
virtual bool replaceDefaultLightingCode() {return false;}
/** \brief Returns the modifier ID /** \brief Returns the modifier ID
* *
* @return Id of the modifier * @return Id of the modifier
...@@ -702,8 +723,17 @@ public: ...@@ -702,8 +723,17 @@ public:
*/ */
const QStringList& getFragmentShaderCode(); const QStringList& getFragmentShaderCode();
/** \brief Get the number of active modifiers.
*/
int getNumActiveModifiers() const;
/** \brief Get active modfiers for this program.
*/
ShaderModifier* getActiveModifier(int _i);
/** \brief Shader modifiers has to be registered before it can be used. /** \brief Shader modifiers have to be registered before they can be used.
They also must remain allocated for the rest of the applications runtime. They also must remain allocated for the rest of the applications runtime.
Use a combination of modifier-IDs in the constructor of ShaderProgGen to active them. Use a combination of modifier-IDs in the constructor of ShaderProgGen to active them.
@param _modifier address of a modifier implementation @param _modifier address of a modifier implementation
...@@ -743,6 +773,10 @@ private: ...@@ -743,6 +773,10 @@ private:
*/ */
void addLightingCode(QStringList* _code); void addLightingCode(QStringList* _code);
/** \brief Calls lighting modifier for each light
*/
void modifyLightingCode(QStringList* _code, ShaderModifier* _modifier);
/// returns path to _strFileName without last slash /// returns path to _strFileName without last slash
QString getPathName(QString _strFileName); QString getPathName(QString _strFileName);
......
#ifndef SHININESS
#define SHININESS g_vMaterial.x #define SHININESS g_vMaterial.x
#endif
#ifndef TRANSPARENCY
#define TRANSPARENCY g_vMaterial.y #define TRANSPARENCY g_vMaterial.y
#endif
#ifndef ALPHA
#define ALPHA g_vMaterial.y #define ALPHA g_vMaterial.y
#endif
vec3 LitPointLight(vec3 vPosition, vec3 LitPointLight(vec3 vPosition,
vec3 vNormal, vec3 vNormal,
......
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