support ambient light without normal & optimaze code

This commit is contained in:
yangxiao 2014-09-25 18:32:33 +08:00
parent 8f3f37c2bb
commit ca9eb3d203
3 changed files with 58 additions and 25 deletions

View File

@ -2800,6 +2800,9 @@
B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = "<group>"; }; B3AF019F1842FBA400A98B85 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = "<group>"; };
B60C5BD219AC68B10056FBDE /* CCBillBoard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCBillBoard.cpp; sourceTree = "<group>"; }; B60C5BD219AC68B10056FBDE /* CCBillBoard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCBillBoard.cpp; sourceTree = "<group>"; };
B60C5BD319AC68B10056FBDE /* CCBillBoard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCBillBoard.h; sourceTree = "<group>"; }; B60C5BD319AC68B10056FBDE /* CCBillBoard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCBillBoard.h; sourceTree = "<group>"; };
B67C624319D4186F00F11FC6 /* ccShader_3D_ColorNormal.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_ColorNormal.frag; sourceTree = "<group>"; };
B67C624419D4186F00F11FC6 /* ccShader_3D_ColorNormalTex.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_ColorNormalTex.frag; sourceTree = "<group>"; };
B67C624519D4186F00F11FC6 /* ccShader_3D_PositionNormalTex.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_PositionNormalTex.vert; sourceTree = "<group>"; };
B6C039DB19C95E03007207DC /* CCLight3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLight3D.cpp; sourceTree = "<group>"; }; B6C039DB19C95E03007207DC /* CCLight3D.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLight3D.cpp; sourceTree = "<group>"; };
B6C039DC19C95E03007207DC /* CCLight3D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLight3D.h; sourceTree = "<group>"; }; B6C039DC19C95E03007207DC /* CCLight3D.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLight3D.h; sourceTree = "<group>"; };
ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
@ -4484,6 +4487,9 @@
5034CA5D191D591900CE6051 /* shaders */ = { 5034CA5D191D591900CE6051 /* shaders */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
B67C624319D4186F00F11FC6 /* ccShader_3D_ColorNormal.frag */,
B67C624419D4186F00F11FC6 /* ccShader_3D_ColorNormalTex.frag */,
B67C624519D4186F00F11FC6 /* ccShader_3D_PositionNormalTex.vert */,
B29594AF1926D5D9003EEF37 /* ccShader_3D_Color.frag */, B29594AF1926D5D9003EEF37 /* ccShader_3D_Color.frag */,
B29594B01926D5D9003EEF37 /* ccShader_3D_ColorTex.frag */, B29594B01926D5D9003EEF37 /* ccShader_3D_ColorTex.frag */,
B29594B11926D5D9003EEF37 /* ccShader_3D_PositionTex.vert */, B29594B11926D5D9003EEF37 /* ccShader_3D_PositionTex.vert */,

View File

@ -526,10 +526,12 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
//check light and determine the shader used //check light and determine the shader used
const auto& lights = Director::getInstance()->getRunningScene()->getLights(); const auto& lights = Director::getInstance()->getRunningScene()->getLights();
bool usingLight = false; bool usingLight = false;
Color4F ambient(0.f, 0.f, 0.f, 1.f);
for (const auto light : lights) { for (const auto light : lights) {
usingLight = ((unsigned short)light->getLightFlag() & _lightMask) > 0; if (((unsigned short)light->getLightFlag() & _lightMask) > 0)
if (usingLight) {
break; usingLight = true;
}
} }
if (usingLight != _shaderUsingLight) if (usingLight != _shaderUsingLight)
genGLProgramState(); genGLProgramState();

View File

@ -252,6 +252,7 @@ void MeshCommand::batchDraw()
} }
if (Director::getInstance()->getRunningScene()->getLights().size() > 0)
setLightUniforms(); setLightUniforms();
_glProgramState->applyGLProgram(_mv); _glProgramState->applyGLProgram(_mv);
@ -294,6 +295,7 @@ void MeshCommand::execute()
} }
if (Director::getInstance()->getRunningScene()->getLights().size() > 0)
setLightUniforms(); setLightUniforms();
_glProgramState->apply(_mv); _glProgramState->apply(_mv);
@ -351,18 +353,16 @@ void MeshCommand::setLightUniforms()
int maxDirLight = conf->getMaxSupportDirLightInShader(); int maxDirLight = conf->getMaxSupportDirLightInShader();
int maxPointLight = conf->getMaxSupportPointLightInShader(); int maxPointLight = conf->getMaxSupportPointLightInShader();
int maxSpotLight = conf->getMaxSupportSpotLightInShader(); int maxSpotLight = conf->getMaxSupportSpotLightInShader();
if (scene)
{
auto &lights = scene->getLights(); auto &lights = scene->getLights();
if (_glProgramState->getVertexAttribsFlags() & GLProgram::VERTEX_ATTRIB_NORMAL)
{
GLint enabledDirLightNum = 0; GLint enabledDirLightNum = 0;
GLint enabledPointLightNum = 0; GLint enabledPointLightNum = 0;
GLint enabledSpotLightNum = 0; GLint enabledSpotLightNum = 0;
Vec3 ambientColor; Vec3 ambientColor;
for (unsigned int i = 0; i < lights.size(); ++i) for (const auto& light : lights)
{ {
BaseLight3D *light = lights[i]; bool useLight = light->isEnabled() && ((unsigned short)light->getLightFlag() & _lightMask);
bool useLight = light->isEnabled();
useLight = useLight && ((unsigned short)light->getLightFlag() & _lightMask);
if (useLight) if (useLight)
{ {
float intensity = light->getIntensity(); float intensity = light->getIntensity();
@ -447,6 +447,31 @@ void MeshCommand::setLightUniforms()
_glProgramState->setUniformVec3("u_AmbientLightSourceColor", ambientColor); _glProgramState->setUniformVec3("u_AmbientLightSourceColor", ambientColor);
} }
else // normal does not exist
{
Vec3 ambient(0.0f, 0.0f, 0.0f);
bool hasAmbient;
for (const auto& light : lights)
{
if (light->getLightType() == LightType::AMBIENT)
{
bool useLight = light->isEnabled() && ((unsigned short)light->getLightFlag() & _lightMask);
if (useLight)
{
hasAmbient = true;
const Color3B &col = light->getDisplayedColor();
ambient.x += col.r * light->getIntensity();
ambient.y += col.g * light->getIntensity();
ambient.z += col.b * light->getIntensity();
}
}
}
if (hasAmbient)
{
ambient.x /= 255.f; ambient.y /= 255.f; ambient.z /= 255.f;
}
_glProgramState->setUniformVec4("u_color", Vec4(_displayColor.x * ambient.x, _displayColor.y * ambient.y, _displayColor.z * ambient.z, _displayColor.w));
}
} }
void MeshCommand::setLightUniformNames() void MeshCommand::setLightUniformNames()