GL3: Fix ambient

--HG--
branch : opengl3
hg/feature/opengl3
kaetemi 11 years ago
parent 75e650432f
commit 7691e22a28

@ -145,6 +145,8 @@ struct CProgramIndex
TexMatrix2, TexMatrix2,
TexMatrix3, TexMatrix3,
SelfIllumination,
Light0DirOrPos, Light0DirOrPos,
Light1DirOrPos, Light1DirOrPos,
Light2DirOrPos, Light2DirOrPos,

@ -81,15 +81,16 @@ namespace NL3D
} }
ss << std::endl; ss << std::endl;
/*if (desc->lightingEnabled()) // LIGHTING DEBUG #if 0 // LIGHTING DEBUG
if (desc->lightingEnabled())
{ {
generateInvalidPS(); generateInvalidPS();
} }
else else
{ {
generateNormalPS(); generateNormalPS();
}*/ }
#else
switch(material->getShader()) switch(material->getShader())
{ {
case CMaterial::Normal: case CMaterial::Normal:
@ -118,6 +119,7 @@ namespace NL3D
generateCloudPS(); generateCloudPS();
break; break;
} }
#endif
ps.assign(ss.str()); ps.assign(ss.str());
} }
@ -521,7 +523,7 @@ namespace NL3D
if (desc->lightingEnabled()) if (desc->lightingEnabled())
addLightsFS(); addLightsFS();
ss << "fragColor = fragColor + vec4(0.0, 0.25, 0.0, 0.0);" << std::endl; //ss << "fragColor = fragColor + vec4(0.0, 0.25, 0.0, 0.0);" << std::endl;
/*if (desc->fogEnabled()) /*if (desc->fogEnabled())
addFog(); addFog();

@ -61,6 +61,7 @@
#include "driver_opengl_states.h" #include "driver_opengl_states.h"
#include "driver_opengl_extension.h" #include "driver_opengl_extension.h"
#include "driver_opengl_shader_cache.h" #include "driver_opengl_shader_cache.h"
#include "driver_opengl_program.h"
#ifdef NL_OS_WINDOWS #ifdef NL_OS_WINDOWS
@ -276,20 +277,6 @@ public:
void setupIndexBuffer(CIndexBuffer &vb); void setupIndexBuffer(CIndexBuffer &vb);
}; };
/// Builtin vertex program description
struct CVPBuiltin
{
uint16 VertexFormat;
bool Lighting;
sint LightMode[NL_OPENGL3_MAX_LIGHT]; // -1 when disabled
bool Fog;
bool VertexColorLighted;
CVertexProgram *VertexProgram;
};
bool operator<(const CVPBuiltin &left, const CVPBuiltin &right);
class CGLSLShaderGenerator; class CGLSLShaderGenerator;
class CUsrShaderManager; class CUsrShaderManager;

@ -132,6 +132,15 @@ bool CDriverGL3::compileVertexProgram(CVertexProgram *program)
} }
return false; return false;
} }
else // debug
{
std::vector<std::string> lines;
NLMISC::explode(std::string(src->SourcePtr), std::string("\n"), lines);
for (std::vector<std::string>::size_type i = 0; i < lines.size(); ++i)
{
nldebug("GL3: %i: %s", i, lines[i].c_str());
}
}
GLenum error = glGetError(); GLenum error = glGetError();
@ -962,23 +971,27 @@ void CDriverGL3::setupUniforms(TProgram program)
int diffuseIndex = p->getUniformIndex(CProgramIndex::DiffuseColor); int diffuseIndex = p->getUniformIndex(CProgramIndex::DiffuseColor);
if (diffuseIndex != -1) if (diffuseIndex != -1)
{ {
GLfloat glCol[ 4 ]; /*GLfloat glCol[ 4 ];
CRGBA col = mat.getDiffuse(); CRGBA col = mat.getDiffuse();
glCol[ 0 ] = col.R / 255.0f; glCol[ 0 ] = col.R / 255.0f;
glCol[ 1 ] = col.G / 255.0f; glCol[ 1 ] = col.G / 255.0f;
glCol[ 2 ] = col.B / 255.0f; glCol[ 2 ] = col.B / 255.0f;
glCol[ 3 ] = col.A / 255.0f; glCol[ 3 ] = col.A / 255.0f;*/
setUniform4f(program, diffuseIndex, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]); setUniform4f(program, diffuseIndex, 1.0f, 1.0f, 1.0f, 0.0f);
} }
NLMISC::CRGBAF selfIllumination = NLMISC::CRGBAF(0.0f, 0.0f, 0.0f);
NLMISC::CRGBAF matDiffuse = NLMISC::CRGBAF(mat.getDiffuse());
NLMISC::CRGBAF matSpecular = NLMISC::CRGBAF(mat.getSpecular());
int maxLights = std::min(int(MaxLight), int(NL_OPENGL3_MAX_LIGHT)); for (int i = 0; i < NL_OPENGL3_MAX_LIGHT; ++i)
for (int i = 0; i < maxLights; i++)
{ {
if (!_UserLightEnable[ i ]) if (!_UserLightEnable[ i ])
continue; continue;
selfIllumination += NLMISC::CRGBAF(_UserLight[i].getAmbiant());
////////////////// Temporary insanity /////////////////////////////// ////////////////// Temporary insanity ///////////////////////////////
if ((_LightMode[ i ] != CLight::DirectionalLight) && (_LightMode[ i ] != CLight::PointLight)) if ((_LightMode[ i ] != CLight::DirectionalLight) && (_LightMode[ i ] != CLight::PointLight))
continue; continue;
@ -1010,25 +1023,15 @@ void CDriverGL3::setupUniforms(TProgram program)
int ldc = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ColDiff + i)); int ldc = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ColDiff + i));
if (ldc != -1) if (ldc != -1)
{ {
GLfloat glCol[ 4 ]; NLMISC::CRGBAF diffuse = NLMISC::CRGBAF(_UserLight[i].getDiffuse()) * matDiffuse;
CRGBA col = _UserLight[ i ].getDiffuse(); setUniform4f(program, ldc, diffuse.R, diffuse.G, diffuse.B, 0.0f); // 1.0f?
glCol[ 0 ] = col.R / 255.0f;
glCol[ 1 ] = col.G / 255.0f;
glCol[ 2 ] = col.B / 255.0f;
glCol[ 3 ] = col.A / 255.0f;
setUniform4f(program, ldc, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]);
} }
int lsc = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ColSpec + i)); int lsc = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ColSpec + i));
if (lsc != -1) if (lsc != -1)
{ {
GLfloat glCol[ 4 ]; NLMISC::CRGBAF specular = NLMISC::CRGBAF(_UserLight[i].getSpecular()) * matSpecular;
CRGBA col = _UserLight[ i ].getSpecular(); setUniform4f(program, lsc, specular.R, specular.G, specular.B, 0.0f); // 1.0f?
glCol[ 0 ] = col.R / 255.0f;
glCol[ 1 ] = col.G / 255.0f;
glCol[ 2 ] = col.B / 255.0f;
glCol[ 3 ] = col.A / 255.0f;
setUniform4f(program, lsc, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]);
} }
int shl = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0Shininess + i)); int shl = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0Shininess + i));
@ -1037,7 +1040,7 @@ void CDriverGL3::setupUniforms(TProgram program)
setUniform1f(program, shl, mat.getShininess()); setUniform1f(program, shl, mat.getShininess());
} }
int lac = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ColAmb + i)); /*int lac = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ColAmb + i));
if (lac != -1) if (lac != -1)
{ {
GLfloat glCol[ 4 ]; GLfloat glCol[ 4 ];
@ -1052,7 +1055,7 @@ void CDriverGL3::setupUniforms(TProgram program)
glCol[ 2 ] = col.B / 255.0f; glCol[ 2 ] = col.B / 255.0f;
glCol[ 3 ] = col.A / 255.0f; glCol[ 3 ] = col.A / 255.0f;
setUniform4f(program, lac, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]); setUniform4f(program, lac, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]);
} }*/
int lca = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ConstAttn + i)); int lca = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Light0ConstAttn + i));
if (lca != -1) if (lca != -1)
@ -1073,11 +1076,18 @@ void CDriverGL3::setupUniforms(TProgram program)
} }
} }
selfIllumination *= NLMISC::CRGBAF(mat.getAmbient());
if (mat.getShader() != CMaterial::LightMap) // Really?
selfIllumination += NLMISC::CRGBAF(mat.getEmissive());
int selfIlluminationId = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::SelfIllumination));
if (selfIlluminationId != -1)
{
setUniform4f(program, selfIlluminationId, selfIllumination.R, selfIllumination.G, selfIllumination.B, 0.0f);
}
// Lightmaps have special constants // Lightmaps have special constants
if (mat.getShader() != CMaterial::LightMap) if (mat.getShader() != CMaterial::LightMap)
{ {
for (int i = 0; i < IDRV_MAT_MAXTEXTURES; i++) for (int i = 0; i < IDRV_MAT_MAXTEXTURES; i++)
{ {
int cl = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Constant0 + i)); int cl = p->getUniformIndex(CProgramIndex::TName(CProgramIndex::Constant0 + i));
@ -1093,7 +1103,6 @@ void CDriverGL3::setupUniforms(TProgram program)
setUniform4f(program, cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]); setUniform4f(program, cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]);
} }
} }
} }
} }

@ -25,6 +25,21 @@ namespace NL3D {
namespace NLDRIVERGL3 { namespace NLDRIVERGL3 {
#endif #endif
/// Builtin vertex program description
struct CVPBuiltin
{
uint16 VertexFormat;
bool Lighting;
sint LightMode[NL_OPENGL3_MAX_LIGHT]; // -1 when disabled
bool Specular; // Reflection
bool Fog;
// bool VertexColorLighted;
CVertexProgram *VertexProgram;
};
bool operator<(const CVPBuiltin &left, const CVPBuiltin &right);
enum TAttribOffset enum TAttribOffset
{ {
Position, Position,

@ -42,10 +42,12 @@ bool operator<(const CVPBuiltin &left, const CVPBuiltin &right)
for (sint i = 0; i < NL_OPENGL3_MAX_LIGHT; ++i) for (sint i = 0; i < NL_OPENGL3_MAX_LIGHT; ++i)
if (left.LightMode[i] != right.LightMode[i]) if (left.LightMode[i] != right.LightMode[i])
return left.LightMode[i] < right.LightMode[i]; return left.LightMode[i] < right.LightMode[i];
if (left.Specular != right.Specular)
return right.Specular;
if (left.Fog != right.Fog) if (left.Fog != right.Fog)
return right.Fog; return right.Fog;
if (left.VertexColorLighted != right.VertexColorLighted) // if (left.VertexColorLighted != right.VertexColorLighted)
return right.VertexColorLighted; // return right.VertexColorLighted;
return false; return false;
} }
@ -59,14 +61,14 @@ namespace
case CLight::DirectionalLight: case CLight::DirectionalLight:
ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl; ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl;
ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; ss << "uniform vec4 light" << i << "ColDiff;" << std::endl;
ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; //ss << "uniform vec4 light" << i << "ColAmb;" << std::endl;
ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; ss << "uniform vec4 light" << i << "ColSpec;" << std::endl;
ss << "uniform float light" << i << "Shininess;" << std::endl; ss << "uniform float light" << i << "Shininess;" << std::endl;
break; break;
case CLight::PointLight: case CLight::PointLight:
ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl; ss << "uniform vec3 light" << i << "DirOrPos;" << std::endl;
ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; ss << "uniform vec4 light" << i << "ColDiff;" << std::endl;
ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; //ss << "uniform vec4 light" << i << "ColAmb;" << std::endl;
ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; ss << "uniform vec4 light" << i << "ColSpec;" << std::endl;
ss << "uniform float light" << i << "Shininess;" << std::endl; ss << "uniform float light" << i << "Shininess;" << std::endl;
ss << "uniform float light" << i << "ConstAttn;" << std::endl; ss << "uniform float light" << i << "ConstAttn;" << std::endl;
@ -108,18 +110,10 @@ namespace
ss << "normal3 = normalMatrix * normal3;" << std::endl; ss << "normal3 = normalMatrix * normal3;" << std::endl;
ss << "normal3 = normalize(normal3);" << std::endl; ss << "normal3 = normalize(normal3);" << std::endl;
//if (desc->useTextures() || (material->getShader() == CMaterial::LightMap)) ss << "vec4 lc = getIntensity" << i << "(normal3, lightDir) * light" << i << "ColDiff + ";
//{ ss << "getSpecIntensity" << i << "(normal3, lightDir) * light" << i << "ColSpec;" << std::endl;
ss << "vec4 lc = getIntensity" << i << "(normal3, lightDir) * light" << i << "ColDiff + ";
ss << "getSpecIntensity" << i << "(normal3, lightDir) * light" << i << "ColSpec + "; // ss << "return vec4(0.0, 0.0, 0.0, 0.0);" << std::endl; // DISABLE DIR LIGHT
ss << "light" << i << "ColAmb;" << std::endl;
//} // FIXME: Ambient color is not correctly implemented
/*else
{
ss << "vec4 lc = getIntensity" << num << "(normal3, lightDir) * light" << num << "ColDiff * diffuseColor + ";
ss << "getSpecIntensity" << num << "(normal3, lightDir) * light" << num << "ColSpec * specularColor + ";
ss << "light" << num << "ColAmb * ambientColor;" << std::endl;
}*/
ss << "return lc;" << std::endl; ss << "return lc;" << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
@ -156,26 +150,19 @@ namespace
ss << "float attenuation = light" << i << "ConstAttn + "; ss << "float attenuation = light" << i << "ConstAttn + ";
ss << "light" << i << "LinAttn * lightDistance +"; ss << "light" << i << "LinAttn * lightDistance +";
ss << "light" << i << "QuadAttn * lightDistance * lightDistance;" << std::endl; ss << "light" << i << "QuadAttn * lightDistance * lightDistance;" << std::endl;
// ss << "attenuation = max(attenuation, 1.0);" << std::endl; // TEST
ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl;
ss << "normal3 = normalMatrix * normal3;" << std::endl; ss << "normal3 = normalMatrix * normal3;" << std::endl;
ss << "normal3 = normalize(normal3);" << std::endl; ss << "normal3 = normalize(normal3);" << std::endl;
/*if (desc->useTextures() || (material->getShader() == CMaterial::LightMap)) ss << "vec4 lc = getIntensity" << i << "(normal3, lightDirection) * light" << i << "ColDiff + ";
{*/ ss << "getSpecIntensity" << i << "(normal3, lightDirection) * light" << i << "ColSpec;" << std::endl;
ss << "vec4 lc = getIntensity" << i << "(normal3, lightDirection) * light" << i << "ColDiff + ";
ss << "getSpecIntensity" << i << "(normal3, lightDirection) * light" << i << "ColSpec + ";
ss << "light" << i << "ColAmb;" << std::endl;
// FIXME: Ambient stuff
/*}
else
{
ss << "vec4 lc = getIntensity" << num << "(normal3, lightDirection) * light" << num << "ColDiff * diffuseColor+ ";
ss << "getSpecIntensity" << num << "(normal3, lightDirection) * light" << num << "ColSpec * specularColor + ";
ss << "light" << num << "ColAmb * ambientColor;" << std::endl;
}*/
ss << "lc = lc / attenuation;" << std::endl; ss << "lc = lc / attenuation;" << std::endl;
// ss << "return vec4(0.0, 0.0, 0.0, 0.0);" << std::endl; // DISABLE POINT LIGHT
ss << "return lc;" << std::endl; ss << "return lc;" << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
ss << std::endl; ss << std::endl;
@ -214,9 +201,12 @@ namespace
ss << std::endl; ss << std::endl;
// if (!useTextures) { // if (!useTextures) {
ss << "uniform vec4 ambientColor;" << std::endl; // FIXME: ambient color of all lights is precalculated and added with self illumination!
ss << "uniform vec4 diffuseColor;" << std::endl; // Ambient color of all lights is precalculated and added with self illumination, and multiplied with the material ambient.
ss << "uniform vec4 specularColor;" << std::endl; // } if (desc.Lighting)
ss << "uniform vec4 selfIllumination;" << std::endl;
// ss << "uniform vec4 diffuseColor;" << std::endl;
// ss << "uniform vec4 specularColor;" << std::endl; // }
if (desc.Fog || desc.Lighting) if (desc.Fog || desc.Lighting)
ss << "uniform mat4 modelView;" << std::endl; ss << "uniform mat4 modelView;" << std::endl;
@ -256,6 +246,9 @@ namespace
for (int i = 0; i < NL_OPENGL3_MAX_LIGHT; i++) for (int i = 0; i < NL_OPENGL3_MAX_LIGHT; i++)
if (desc.LightMode[i] == CLight::DirectionalLight || desc.LightMode[i] == CLight::PointLight) if (desc.LightMode[i] == CLight::DirectionalLight || desc.LightMode[i] == CLight::PointLight)
ss << "lightColor = lightColor + getLight" << i << "Color();" << std::endl; ss << "lightColor = lightColor + getLight" << i << "Color();" << std::endl;
ss << "lightColor = lightColor + selfIllumination;" << std::endl;
//ss << "lightColor = selfIllumination;" << std::endl; // DEBUG
ss << "lightColor.a = 1.0;" << std::endl; // ...
} }
for (int i = Weight; i < NumOffsets; i++) for (int i = Weight; i < NumOffsets; i++)

@ -115,6 +115,8 @@ const char *CProgramIndex::Names[NUM_UNIFORMS] =
"texMatrix2", "texMatrix2",
"texMatrix3", "texMatrix3",
"selfIllumination",
"light0DirOrPos", "light0DirOrPos",
"light1DirOrPos", "light1DirOrPos",
"light2DirOrPos", "light2DirOrPos",

Loading…
Cancel
Save