From 3c67515378d38563d52265cf02779f12196083d6 Mon Sep 17 00:00:00 2001 From: songchengjiang Date: Thu, 18 Sep 2014 16:38:35 +0800 Subject: [PATCH 1/4] Finished light reconstruction --- cocos/2d/libcocos2d.vcxproj | 4 +- cocos/2d/libcocos2d.vcxproj.filters | 12 +- cocos/3d/CCLight3D.cpp | 169 ++------------- cocos/3d/CCLight3D.h | 127 +---------- cocos/renderer/CCGLProgram.cpp | 198 ++++++++++-------- cocos/renderer/CCGLProgram.h | 6 - cocos/renderer/ccShader_3D_ColorNormal.frag | 19 +- .../renderer/ccShader_3D_ColorNormalTex.frag | 16 +- .../cpp-tests/Classes/LightTest/LightTest.cpp | 83 ++++++-- tests/cpp-tests/Classes/LightTest/LightTest.h | 12 +- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 7 + .../Classes/Sprite3DTest/Sprite3DTest.h | 1 + tests/cpp-tests/proj.win32/cpp-tests.vcxproj | 8 +- .../proj.win32/cpp-tests.vcxproj.filters | 14 +- 14 files changed, 243 insertions(+), 433 deletions(-) diff --git a/cocos/2d/libcocos2d.vcxproj b/cocos/2d/libcocos2d.vcxproj index f6fd450c7d..0c61f8b281 100644 --- a/cocos/2d/libcocos2d.vcxproj +++ b/cocos/2d/libcocos2d.vcxproj @@ -208,7 +208,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* - + @@ -546,7 +546,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.* - + diff --git a/cocos/2d/libcocos2d.vcxproj.filters b/cocos/2d/libcocos2d.vcxproj.filters index a5f72ceebd..1631bf1e70 100644 --- a/cocos/2d/libcocos2d.vcxproj.filters +++ b/cocos/2d/libcocos2d.vcxproj.filters @@ -1039,9 +1039,6 @@ 3d - - 3d - cocosbuilder\Source Files @@ -1171,6 +1168,9 @@ ui\UIWidgets\EditBox + + 3d + @@ -2149,9 +2149,6 @@ 3d - - 3d - cocosbuilder\Header Files @@ -2302,6 +2299,9 @@ ui\UIWidgets\EditBox + + 3d + diff --git a/cocos/3d/CCLight3D.cpp b/cocos/3d/CCLight3D.cpp index 911c350e61..beb6e38cd1 100644 --- a/cocos/3d/CCLight3D.cpp +++ b/cocos/3d/CCLight3D.cpp @@ -159,173 +159,34 @@ void SpotLight3D::setOuterAngle(float angle) _cosInnerAngle = cosf(angle); } +SpotLight3D::SpotLight3D() +{ + +} + +SpotLight3D::~SpotLight3D() +{ + +} + ///////////////////////////////////////////////////////////// -Light3D::Light3D() - : _isEnabled(true) - , _range(0.0) - , _innerAngle(0.0) - , _outerAngle(0.0) +AmbientLight3D* AmbientLight3D::create( const Color3B &color ) { -} - -Light3D::~Light3D() -{ -} - -Light3D* Light3D::createDirectionalLight( const Vec3 &direction, const Color3B &color ) -{ - Light3D *light = new Light3D; - light->_lightType = Light3D::LightType::DIRECTIONAL; - light->calculateRotation(direction); + auto light = new (std::nothrow) AmbientLight3D(); light->setColor(color); light->autorelease(); return light; } -Light3D* Light3D::createPointLight( const Vec3 &position, const Color3B &color, float range ) +AmbientLight3D::AmbientLight3D() { - Light3D *light = new Light3D; - light->_lightType = Light3D::LightType::POINT; - light->setPosition3D(position); - light->setColor(color); - light->_range = range; - light->autorelease(); - return light; + } -Light3D* Light3D::createSpotLight( const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range ) +AmbientLight3D::~AmbientLight3D() { - Light3D *light = new Light3D; - light->_lightType = Light3D::LightType::SPOT; - light->calculateRotation(direction); - light->setPosition3D(position); - light->setColor(color); - light->_innerAngle = innerAngle; - light->_outerAngle = outerAngle; - light->_range = range; - light->autorelease(); - return light; -} - -void Light3D::setLightType( LightType lightType ) -{ - _lightType = lightType; -} - - -Light3D::LightType Light3D::getLightType() -{ - return _lightType; -} - -void Light3D::setRange( float range ) -{ - _range = range; -} - -float Light3D::getRange() -{ - return _range; -} - -void Light3D::setInnerAngle( float angle ) -{ - _innerAngle = angle; -} - -float Light3D::getInnerAngle() -{ - return _innerAngle; -} - -void Light3D::setOuterAngle( float angle ) -{ - _outerAngle = angle; -} - -float Light3D::getOuterAngle() -{ - return _outerAngle; -} - -void Light3D::onEnter() -{ -// auto scene = getScene(); -// if (scene) -// { -// auto &lights = scene->_lights; -// auto iter = std::find(lights.begin(), lights.end(), this); -// if (iter == lights.end()) -// lights.push_back(this); -// } -// Node::onEnter(); -} - -void Light3D::onExit() -{ -// auto scene = getScene(); -// if (scene) -// { -// auto &lights = scene->_lights; -// auto iter = std::find(lights.begin(), lights.end(), this); -// if (iter != lights.end()) -// lights.erase(iter); -// } -// Node::onExit(); -} - -void Light3D::setEnabled( bool enabled ) -{ - _isEnabled = enabled; -} - -bool Light3D::getEnabled() -{ - return _isEnabled; -} - -void Light3D::calculateRotation( const Vec3 &direction ) -{ - float projLen = sqrt(direction.x * direction.x + direction.z * direction.z); - float rotY = CC_RADIANS_TO_DEGREES(atan2f(-direction.x, -direction.z)); - float rotX = -CC_RADIANS_TO_DEGREES(atan2f(-direction.y, projLen)); - setRotation3D(Vec3(rotX, rotY, 0.0f)); -} - -void Light3D::setDirection( const Vec3 &dir ) -{ - calculateRotation(dir); -} - -Vec3 Light3D::getDirection() const -{ - Mat4 mat = getNodeToParentTransform(); -// mat.m[12] = mat.m[13] = mat.m[14] = 0.0f; -// mat.inverse(); -// mat.transpose(); -// -// Vec3 dir; -// mat.transformVector(0.0f, 0.0f, -1.0f, 0.0f, &dir); -// -// return dir; - - return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]); -} - -Vec3 Light3D::getWorldDirection() const -{ - Mat4 mat = getNodeToWorldTransform(); -// mat.m[12] = mat.m[13] = mat.m[14] = 0.0f; -// mat.inverse(); -// mat.transpose(); -// -// Vec3 dir; -// mat.transformVector(0.0f, 0.0f, -1.0f, 0.0f, &dir); -// -// return dir; - return Vec3(-mat.m[8], -mat.m[9], -mat.m[10]); } NS_CC_END \ No newline at end of file diff --git a/cocos/3d/CCLight3D.h b/cocos/3d/CCLight3D.h index 9e0521757a..3486d59fc6 100644 --- a/cocos/3d/CCLight3D.h +++ b/cocos/3d/CCLight3D.h @@ -263,131 +263,8 @@ public: static AmbientLight3D* create(const Color3B &color); CC_CONSTRUCTOR_ACCESS: - AmbientLight3D() {} - virtual ~AmbientLight3D() {} -}; - -class CC_3D_DLL Light3D : public Node -{ -public: - - enum class LightType - { - DIRECTIONAL = 0, - POINT = 1, - SPOT = 2 - }; - - /** - * Create light according to type. - */ - static Light3D* createDirectionalLight(const Vec3 &direction, const Color3B &color); - static Light3D* createPointLight(const Vec3 &position, const Color3B &color, float range); - static Light3D* createSpotLight(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range); - - //override - virtual void onEnter() override; - virtual void onExit() override; - - /** - * Gets light type. - */ - LightType getLightType(); - - /** - * Sets Light Enabled. - */ - void setEnabled(bool enabled); - - /** - * Gets Light Enabled. - */ - bool getEnabled(); - - - /** - * Sets the range of point or spot light. - * - * @param range The range of point or spot light. - */ - void setRange(float range); - - /** - * Returns the range of point or spot light. - * - * @return The range of the point or spot light. - */ - float getRange(); - - /** - * Sets the Direction of Directional light or Spot light. - * - * @param dir The Direction of the Directional light or Spot light. - */ - void setDirection(const Vec3 &dir); - - /** - * Returns the Direction of Directional light or Spot light. - * - * @return dir Direction of the Directional light or Spot light. - */ - Vec3 getDirection() const; - - /** - * Returns the World coordinate Direction of Directional light or Spot light. - * - * @return dir the World coordinate Direction of the Directional light or Spot light. - */ - Vec3 getWorldDirection() const; - - /** - * Sets the inner angle of a spot light (in radians). - * - * @param angle The angle of spot light (in radians). - */ - void setInnerAngle(float angle); - - /** - * Returns the inner angle the spot light (in radians). - * - * @return The inner angle of the spot light (in radians). - */ - float getInnerAngle(); - - /** - * Sets the outer angle of a spot light (in radians). - * - * @param outerAngle The angle of spot light (in radians). - */ - void setOuterAngle(float angle); - - /** - * Returns the outer angle of the spot light (in radians). - * - * @return The outer angle of the spot light (in radians). - */ - float getOuterAngle(); - -CC_CONSTRUCTOR_ACCESS: - - Light3D(); - virtual ~Light3D(); - -protected: - - /** - * Sets light type. - */ - void setLightType(LightType lightType); - void calculateRotation(const Vec3 &direction); - -protected: - - LightType _lightType; - bool _isEnabled; - float _range; - float _innerAngle; - float _outerAngle; + AmbientLight3D(); + virtual ~AmbientLight3D(); }; NS_CC_END diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index 80a1ff711b..def48059de 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -47,6 +47,7 @@ THE SOFTWARE. #define CC_MAX_DIRECTIONAL_LIGHT_NUM 1 #define CC_MAX_POINT_LIGHT_NUM 1 #define CC_MAX_SPOT_LIGHT_NUM 1 +#define CC_MAX_AMBIENT_LIGHT_NUM 1 NS_CC_BEGIN @@ -83,9 +84,6 @@ const char* GLProgram::SHADER_3D_SKINPOSITION_NORMAL_TEXTURE = "Shader3DSkinPosi // uniform names -const char* GLProgram::UNIFORM_NAME_ENABLED_DIRECTIONAL_LIGHT_NUM = "CC_EnabledDirLightNum"; -const char* GLProgram::UNIFORM_NAME_ENABLED_POINT_LIGHT_NUM = "CC_EnabledPointLightNum"; -const char* GLProgram::UNIFORM_NAME_ENABLED_SPOT_LIGHT_NUM= "CC_EnabledSpotLightNum"; const char* GLProgram::UNIFORM_NAME_AMBIENT_COLOR = "CC_AmbientColor"; const char* GLProgram::UNIFORM_NAME_P_MATRIX = "CC_PMatrix"; const char* GLProgram::UNIFORM_NAME_MV_MATRIX = "CC_MVMatrix"; @@ -434,22 +432,25 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source } - GLchar def[128]; + GLchar def[256]; sprintf(def , "#define CC_MAX_DIRECTIONAL_LIGHT_NUM %d \n" "#define CC_MAX_POINT_LIGHT_NUM %d \n" "#define CC_MAX_SPOT_LIGHT_NUM %d \n" - , CC_MAX_DIRECTIONAL_LIGHT_NUM, CC_MAX_POINT_LIGHT_NUM, CC_MAX_SPOT_LIGHT_NUM); + "#define CC_MAX_AMBIENT_LIGHT_NUM %d \n" + , CC_MAX_DIRECTIONAL_LIGHT_NUM, CC_MAX_POINT_LIGHT_NUM, CC_MAX_SPOT_LIGHT_NUM, CC_MAX_AMBIENT_LIGHT_NUM); GLchar lightStruct[] = { "#if CC_MAX_DIRECTIONAL_LIGHT_NUM \n" "uniform vec3 CC_DirLightSourceColor[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n" "uniform vec3 CC_DirLightSourceDirection[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n" + "uniform float CC_DirLightSourceIntensity[CC_MAX_DIRECTIONAL_LIGHT_NUM]; \n" "#endif \n" "#if CC_MAX_POINT_LIGHT_NUM \n" "uniform vec3 CC_PointLightSourceColor[CC_MAX_POINT_LIGHT_NUM]; \n" "uniform vec3 CC_PointLightSourcePosition[CC_MAX_POINT_LIGHT_NUM]; \n" "uniform float CC_PointLightSourceRangeInverse[CC_MAX_POINT_LIGHT_NUM]; \n" + "uniform float CC_PointLightSourceIntensity[CC_MAX_POINT_LIGHT_NUM]; \n" "#endif \n" "#if CC_MAX_SPOT_LIGHT_NUM \n" "uniform vec3 CC_SpotLightSourceColor[CC_MAX_SPOT_LIGHT_NUM]; \n" @@ -458,8 +459,12 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source "uniform float CC_SpotLightSourceInnerAngleCos[CC_MAX_SPOT_LIGHT_NUM]; \n" "uniform float CC_SpotLightSourceOuterAngleCos[CC_MAX_SPOT_LIGHT_NUM]; \n" "uniform float CC_SpotLightSourceRangeInverse[CC_MAX_SPOT_LIGHT_NUM]; \n" + "uniform float CC_SpotLightSourceIntensity[CC_MAX_SPOT_LIGHT_NUM]; \n" "#endif \n" - "uniform vec4 CC_AmbientColor; \n" + //"#if CC_MAX_AMBIENT_LIGHT_NUM \n" + "uniform vec3 CC_AmbientLightSourceColor[CC_MAX_AMBIENT_LIGHT_NUM]; \n" + "uniform float CC_AmbientLightSourceIntensity[CC_MAX_AMBIENT_LIGHT_NUM]; \n" + //"#endif \n" }; const GLchar *sources[] = { @@ -531,9 +536,6 @@ void GLProgram::bindAttribLocation(const std::string &attributeName, GLuint inde void GLProgram::updateUniforms() { - //_builtInUniforms[UNIFORM_ENABLED_DIRECTIONAL_LIGHT_NUM] = glGetUniformLocation(_program, UNIFORM_NAME_ENABLED_DIRECTIONAL_LIGHT_NUM); - //_builtInUniforms[UNIFORM_ENABLED_POINT_LIGHT_NUM] = glGetUniformLocation(_program, UNIFORM_NAME_ENABLED_POINT_LIGHT_NUM); - //_builtInUniforms[UNIFORM_ENABLED_SPOT_LIGHT_NUM] = glGetUniformLocation(_program, UNIFORM_NAME_ENABLED_SPOT_LIGHT_NUM); _builtInUniforms[UNIFORM_AMBIENT_COLOR] = glGetUniformLocation(_program, UNIFORM_NAME_AMBIENT_COLOR); _builtInUniforms[UNIFORM_P_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_P_MATRIX); _builtInUniforms[UNIFORM_MV_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MV_MATRIX); @@ -551,15 +553,10 @@ void GLProgram::updateUniforms() _builtInUniforms[UNIFORM_SAMPLER2] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER2); _builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3); - //_flags.usesLights = ( - // _builtInUniforms[UNIFORM_ENABLED_DIRECTIONAL_LIGHT_NUM] != -1 || - // _builtInUniforms[UNIFORM_ENABLED_POINT_LIGHT_NUM] != -1 || - // _builtInUniforms[UNIFORM_ENABLED_SPOT_LIGHT_NUM] != -1 - // ); - _flags.usesLights = (0 < CC_MAX_DIRECTIONAL_LIGHT_NUM) || - (0 < CC_MAX_POINT_LIGHT_NUM) || - (0 < CC_MAX_SPOT_LIGHT_NUM); + (0 < CC_MAX_POINT_LIGHT_NUM) || + (0 < CC_MAX_SPOT_LIGHT_NUM) || + (0 < CC_MAX_AMBIENT_LIGHT_NUM); _flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1; _flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1; _flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1; @@ -969,79 +966,100 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) if(_flags.usesRandom) setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_RANDOM01], CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1()); -// if (_flags.usesLights) -// { -// Director *director = Director::getInstance(); -// auto scene = director->getRunningScene(); -// if (scene) -// { -// auto &lights = scene->getLights(); -// -// char str[64]; -// GLint enabledDirLightNum = 0; -// GLint enabledPointLightNum = 0; -// GLint enabledSpotLightNum = 0; -// for (unsigned int i = 0; i < lights.size(); ++i) -// { -// Light3D *light = lights[i]; -// Color3B col = light->getDisplayedColor(); -// if (!light->getEnabled()) -// { -// col = Color3B::BLACK; -// } -// if (light->getLightType() == Light3D::LightType::DIRECTIONAL) -// { -// CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, ""); -// Vec3 dir = light->getWorldDirection(); -// dir.normalize(); -// sprintf(str, "CC_DirLightSourceColor[%d]", enabledDirLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); -// sprintf(str, "CC_DirLightSourceDirection[%d]", enabledDirLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); -// ++enabledDirLightNum; -// } -// else if (light->getLightType() == Light3D::LightType::POINT) -// { -// CCASSERT(enabledPointLightNum < CC_MAX_POINT_LIGHT_NUM, ""); -// Mat4 mat= light->getNodeToWorldTransform(); -// sprintf(str, "CC_PointLightSourceColor[%d]", enabledPointLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); -// sprintf(str, "CC_PointLightSourcePosition[%d]", enabledPointLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]); -// sprintf(str, "CC_PointLightSourceRangeInverse[%d]", enabledPointLightNum); -// setUniformLocationWith1f(glGetUniformLocation(_program, str), 1.0f / light->getRange()); -// ++enabledPointLightNum; -// } -// else -// { -// CCASSERT(enabledSpotLightNum < CC_MAX_SPOT_LIGHT_NUM, ""); -// Vec3 dir = light->getWorldDirection(); -// dir.normalize(); -// Mat4 mat= light->getNodeToWorldTransform(); -// sprintf(str, "CC_SpotLightSourceColor[%d]", enabledSpotLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); -// sprintf(str, "CC_SpotLightSourcePosition[%d]", enabledSpotLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]); -// sprintf(str, "CC_SpotLightSourceDirection[%d]", enabledSpotLightNum); -// setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); -// sprintf(str, "CC_SpotLightSourceInnerAngleCos[%d]", enabledSpotLightNum); -// setUniformLocationWith1f(glGetUniformLocation(_program, str), cosf(light->getInnerAngle())); -// sprintf(str, "CC_SpotLightSourceOuterAngleCos[%d]", enabledSpotLightNum); -// setUniformLocationWith1f(glGetUniformLocation(_program, str), cosf(light->getOuterAngle())); -// sprintf(str, "CC_SpotLightSourceRangeInverse[%d]", enabledSpotLightNum); -// setUniformLocationWith1f(glGetUniformLocation(_program, str), 1.0f / light->getRange()); -// ++enabledSpotLightNum; -// } -// } -// -// -// //setUniformLocationWith1i(_builtInUniforms[GLProgram::UNIFORM_ENABLED_DIRECTIONAL_LIGHT_NUM], enabledDirLightNum); -// //setUniformLocationWith1i(_builtInUniforms[GLProgram::UNIFORM_ENABLED_POINT_LIGHT_NUM], enabledPointLightNum); -// //setUniformLocationWith1i(_builtInUniforms[GLProgram::UNIFORM_ENABLED_SPOT_LIGHT_NUM], enabledSpotLightNum); -// const auto& ambientColor = scene->getAmbientColor(); -// setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_AMBIENT_COLOR], ambientColor.r, ambientColor.g, ambientColor.b, ambientColor.a); -// } -// } + if (_flags.usesLights) + { + Director *director = Director::getInstance(); + auto scene = director->getRunningScene(); + if (scene) + { + auto &lights = scene->getLights(); + char str[64]; + GLint enabledDirLightNum = 0; + GLint enabledPointLightNum = 0; + GLint enabledSpotLightNum = 0; + GLint enabledAmbientLightNum = 0; + for (unsigned int i = 0; i < lights.size(); ++i) + { + BaseLight3D *light = lights[i]; + float intensity = light->isEnabled() == true? light->getIntensity() : 0.0f; + switch (light->getLightType()) + { + case LightType::DIRECTIONAL: + { + CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, ""); + DirectionLight3D *dirLight = static_cast(light); + Vec3 dir = dirLight->getDirectionInWorld(); + dir.normalize(); + const Color3B &col = dirLight->getDisplayedColor(); + sprintf(str, "CC_DirLightSourceColor[%d]", enabledDirLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_DirLightSourceDirection[%d]", enabledDirLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); + sprintf(str, "CC_DirLightSourceIntensity[%d]", enabledDirLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); + ++enabledDirLightNum; + } + break; + case LightType::POINT: + { + CCASSERT(enabledPointLightNum < CC_MAX_POINT_LIGHT_NUM, ""); + PointLight3D *pointLight = static_cast(light); + Mat4 mat= pointLight->getNodeToWorldTransform(); + const Color3B &col = pointLight->getDisplayedColor(); + sprintf(str, "CC_PointLightSourceColor[%d]", enabledPointLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_PointLightSourcePosition[%d]", enabledPointLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]); + sprintf(str, "CC_PointLightSourceRangeInverse[%d]", enabledPointLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), 1.0f / pointLight->getRange()); + sprintf(str, "CC_PointLightSourceIntensity[%d]", enabledPointLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); + ++enabledPointLightNum; + } + break; + case LightType::SPOT: + { + CCASSERT(enabledSpotLightNum < CC_MAX_SPOT_LIGHT_NUM, ""); + SpotLight3D *spotLight = static_cast(light); + Vec3 dir = spotLight->getDirectionInWorld(); + dir.normalize(); + Mat4 mat= light->getNodeToWorldTransform(); + const Color3B &col = spotLight->getDisplayedColor(); + sprintf(str, "CC_SpotLightSourceColor[%d]", enabledSpotLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_SpotLightSourcePosition[%d]", enabledSpotLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]); + sprintf(str, "CC_SpotLightSourceDirection[%d]", enabledSpotLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); + sprintf(str, "CC_SpotLightSourceInnerAngleCos[%d]", enabledSpotLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), spotLight->getCosInnerAngle()); + sprintf(str, "CC_SpotLightSourceOuterAngleCos[%d]", enabledSpotLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), spotLight->getCosOuterAngle()); + sprintf(str, "CC_SpotLightSourceRangeInverse[%d]", enabledSpotLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), 1.0f / spotLight->getRange()); + sprintf(str, "CC_SpotLightSourceIntensity[%d]", enabledSpotLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); + ++enabledSpotLightNum; + } + break; + case LightType::AMBIENT: + { + CCASSERT(enabledAmbientLightNum < CC_MAX_AMBIENT_LIGHT_NUM, ""); + AmbientLight3D *ambLight = static_cast(light); + const Color3B &col = ambLight->getDisplayedColor(); + sprintf(str, "CC_AmbientLightSourceColor[%d]", enabledAmbientLightNum); + setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_AmbientLightSourceIntensity[%d]", enabledAmbientLightNum); + setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); + ++enabledAmbientLightNum; + } + break; + default: + break; + } + } + } + } } void GLProgram::reset() diff --git a/cocos/renderer/CCGLProgram.h b/cocos/renderer/CCGLProgram.h index 10448a72f9..b56736af81 100644 --- a/cocos/renderer/CCGLProgram.h +++ b/cocos/renderer/CCGLProgram.h @@ -103,9 +103,6 @@ public: enum { - //UNIFORM_ENABLED_DIRECTIONAL_LIGHT_NUM, - //UNIFORM_ENABLED_POINT_LIGHT_NUM, - //UNIFORM_ENABLED_SPOT_LIGHT_NUM, UNIFORM_AMBIENT_COLOR, UNIFORM_P_MATRIX, UNIFORM_MV_MATRIX, @@ -150,9 +147,6 @@ public: static const char* SHADER_3D_SKINPOSITION_NORMAL_TEXTURE; // uniform names - static const char* UNIFORM_NAME_ENABLED_DIRECTIONAL_LIGHT_NUM; - static const char* UNIFORM_NAME_ENABLED_POINT_LIGHT_NUM; - static const char* UNIFORM_NAME_ENABLED_SPOT_LIGHT_NUM; static const char* UNIFORM_NAME_AMBIENT_COLOR; static const char* UNIFORM_NAME_P_MATRIX; static const char* UNIFORM_NAME_MV_MATRIX; diff --git a/cocos/renderer/ccShader_3D_ColorNormal.frag b/cocos/renderer/ccShader_3D_ColorNormal.frag index abc40d5904..edc21b7707 100644 --- a/cocos/renderer/ccShader_3D_ColorNormal.frag +++ b/cocos/renderer/ccShader_3D_ColorNormal.frag @@ -45,14 +45,14 @@ void main(void) vec3 normal = normalize(v_normal); \n#endif\n - vec4 combinedColor = CC_AmbientColor; + vec4 combinedColor = vec4(0.0, 0.0, 0.0, 1.0); // Directional light contribution \n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM > 0)\n for (int i = 0; i < CC_MAX_DIRECTIONAL_LIGHT_NUM; ++i) { vec3 lightDirection = normalize(CC_DirLightSourceDirection[i] * 2.0); - combinedColor.xyz += computeLighting(normal, -lightDirection, CC_DirLightSourceColor[i], 1.0); + combinedColor.xyz += computeLighting(normal, -lightDirection, CC_DirLightSourceColor[i] * CC_DirLightSourceIntensity[i], 1.0); } \n#endif\n @@ -62,7 +62,7 @@ void main(void) { vec3 ldir = v_vertexToPointLightDirection[i] * CC_PointLightSourceRangeInverse[i]; float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0); - combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), CC_PointLightSourceColor[i], attenuation); + combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), CC_PointLightSourceColor[i] * CC_PointLightSourceIntensity[i], attenuation); } \n#endif\n @@ -82,14 +82,21 @@ void main(void) // Apply spot attenuation attenuation *= smoothstep(CC_SpotLightSourceOuterAngleCos[i], CC_SpotLightSourceInnerAngleCos[i], spotCurrentAngleCos); - combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, CC_SpotLightSourceColor[i], attenuation); + combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, CC_SpotLightSourceColor[i] * CC_SpotLightSourceIntensity[i], attenuation); } \n#endif\n -\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n +\n#if (CC_MAX_AMBIENT_LIGHT_NUM > 0)\n + for (int i = 0; i < CC_MAX_AMBIENT_LIGHT_NUM; ++i) + { + combinedColor.xyz += CC_AmbientLightSourceColor[i] * CC_AmbientLightSourceIntensity[i]; + } +\n#endif\n + +\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0) || (CC_MAX_AMBIENT_LIGHT_NUM > 0))\n gl_FragColor = u_color * combinedColor; \n#else\n - gl_FragColor = u_color; + gl_FragColor = u_color; \n#endif\n } diff --git a/cocos/renderer/ccShader_3D_ColorNormalTex.frag b/cocos/renderer/ccShader_3D_ColorNormalTex.frag index 80010b35b2..47a660bf57 100644 --- a/cocos/renderer/ccShader_3D_ColorNormalTex.frag +++ b/cocos/renderer/ccShader_3D_ColorNormalTex.frag @@ -45,14 +45,14 @@ void main(void) vec3 normal = normalize(v_normal); \n#endif\n - vec4 combinedColor = CC_AmbientColor; + vec4 combinedColor = vec4(0.0, 0.0, 0.0, 1.0); // Directional light contribution \n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM > 0)\n for (int i = 0; i < CC_MAX_DIRECTIONAL_LIGHT_NUM; ++i) { vec3 lightDirection = normalize(CC_DirLightSourceDirection[i] * 2.0); - combinedColor.xyz += computeLighting(normal, -lightDirection, CC_DirLightSourceColor[i], 1.0); + combinedColor.xyz += computeLighting(normal, -lightDirection, CC_DirLightSourceColor[i] * CC_DirLightSourceIntensity[i], 1.0); } \n#endif\n @@ -62,7 +62,7 @@ void main(void) { vec3 ldir = v_vertexToPointLightDirection[i] * CC_PointLightSourceRangeInverse[i]; float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0); - combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), CC_PointLightSourceColor[i], attenuation); + combinedColor.xyz += computeLighting(normal, normalize(v_vertexToPointLightDirection[i]), CC_PointLightSourceColor[i] * CC_PointLightSourceIntensity[i], attenuation); } \n#endif\n @@ -82,12 +82,18 @@ void main(void) // Apply spot attenuation attenuation *= smoothstep(CC_SpotLightSourceOuterAngleCos[i], CC_SpotLightSourceInnerAngleCos[i], spotCurrentAngleCos); - combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, CC_SpotLightSourceColor[i], attenuation); + combinedColor.xyz += computeLighting(normal, vertexToSpotLightDirection, CC_SpotLightSourceColor[i] * CC_SpotLightSourceIntensity[i], attenuation); } \n#endif\n +\n#if (CC_MAX_AMBIENT_LIGHT_NUM > 0)\n + for (int i = 0; i < CC_MAX_AMBIENT_LIGHT_NUM; ++i) + { + combinedColor.xyz += CC_AmbientLightSourceColor[i] * CC_AmbientLightSourceIntensity[i]; + } +\n#endif\n -\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0))\n +\n#if ((CC_MAX_DIRECTIONAL_LIGHT_NUM > 0) || (CC_MAX_POINT_LIGHT_NUM > 0) || (CC_MAX_SPOT_LIGHT_NUM > 0) || (CC_MAX_AMBIENT_LIGHT_NUM > 0))\n gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color * combinedColor; \n#else\n gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color; diff --git a/tests/cpp-tests/Classes/LightTest/LightTest.cpp b/tests/cpp-tests/Classes/LightTest/LightTest.cpp index 0083128295..79e81e4ea9 100644 --- a/tests/cpp-tests/Classes/LightTest/LightTest.cpp +++ b/tests/cpp-tests/Classes/LightTest/LightTest.cpp @@ -53,23 +53,28 @@ LightTest::LightTest() addChild(camera); TTFConfig ttfConfig("fonts/arial.ttf", 15); - _directionalLightLabel = Label::createWithTTF(ttfConfig,"Directional Light ON"); + _ambientLightLabel = Label::createWithTTF(ttfConfig,"Ambient Light ON"); + _ambientLightLabel->retain(); + auto menuItem0 = MenuItemLabel::create(_ambientLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::AMBIENT)); + _directionalLightLabel = Label::createWithTTF(ttfConfig,"Directional Light OFF"); _directionalLightLabel->retain(); - auto menuItem1 = MenuItemLabel::create(_directionalLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,Light3D::LightType::DIRECTIONAL)); + auto menuItem1 = MenuItemLabel::create(_directionalLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::DIRECTIONAL)); _pointLightLabel = Label::createWithTTF(ttfConfig,"Point Light OFF"); _pointLightLabel->retain(); - auto menuItem2 = MenuItemLabel::create(_pointLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,Light3D::LightType::POINT)); + auto menuItem2 = MenuItemLabel::create(_pointLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::POINT)); _spotLightLabel = Label::createWithTTF(ttfConfig,"Spot Light OFF"); _spotLightLabel->retain(); - auto menuItem3 = MenuItemLabel::create(_spotLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,Light3D::LightType::SPOT)); - auto menu = Menu::create(menuItem1,menuItem2,menuItem3,NULL); + auto menuItem3 = MenuItemLabel::create(_spotLightLabel, CC_CALLBACK_1(LightTest::SwitchLight,this,LightType::SPOT)); + auto menu = Menu::create(menuItem0, menuItem1,menuItem2,menuItem3,NULL); menu->setPosition(Vec2::ZERO); + menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); + menuItem0->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-50) ); menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); - menuItem1->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-50) ); + menuItem1->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y-100) ); menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); - menuItem2->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -100)); + menuItem2->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -150)); menuItem3->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); - menuItem3->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -150)); + menuItem3->setPosition( Vec2(VisibleRect::left().x, VisibleRect::top().y -200)); addChild(menu); } @@ -85,14 +90,17 @@ LightTest::~LightTest() if (_directionalLightLabel) _directionalLightLabel->release(); - if (_directionalLight) - _directionalLight->release(); + if (_spotLight) + _spotLight->release(); if (_pointLight) _pointLight->release(); - if (_spotLight) - _spotLight->release(); + if (_directionalLight) + _directionalLight->release(); + + if (_ambientLight) + _ambientLight->release(); } std::string LightTest::title() const @@ -202,23 +210,39 @@ void LightTest::addSprite() void LightTest::addLights() { auto s = Director::getInstance()->getWinSize(); - _directionalLight = Light3D::createDirectionalLight(Vec3(-1.0f, -1.0f, 0.0f), Color3B(200, 200, 200)); + _ambientLight = AmbientLight3D::create(Color3B(200, 200, 200)); + _ambientLight->retain(); + _ambientLight->setEnabled(true); + addChild(_ambientLight); + _ambientLight->setCameraMask(2); + + _directionalLight = DirectionLight3D::create(Vec3(-1.0f, -1.0f, 0.0f), Color3B(200, 200, 200)); _directionalLight->retain(); + _directionalLight->setEnabled(false); addChild(_directionalLight); _directionalLight->setCameraMask(2); - _pointLight = Light3D::createPointLight(Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 10000.0f); + _pointLight = PointLight3D::create(Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 10000.0f); _pointLight->retain(); _pointLight->setEnabled(false); addChild(_pointLight); _pointLight->setCameraMask(2); - _spotLight = Light3D::createSpotLight(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 0.0, 0.5, 10000.0f); + _spotLight = SpotLight3D::create(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 0.0, 0.3, 10000.0f); _spotLight->retain(); _spotLight->setEnabled(false); addChild(_spotLight); _spotLight->setCameraMask(2); + { + auto tintto1 = TintTo::create(4, 0, 0, 255); + auto tintto2 = TintTo::create(4, 0, 255, 0); + auto tintto3 = TintTo::create(4, 255, 0, 0); + auto tintto4 = TintTo::create(4, 255, 255, 255); + auto seq = Sequence::create(tintto1,tintto2, tintto3, tintto4, NULL); + _ambientLight->runAction(RepeatForever::create(seq)); + } + { auto tintto1 = TintTo::create(4, 255, 0, 0); auto tintto2 = TintTo::create(4, 0, 255, 0); @@ -250,8 +274,11 @@ void LightTest::addLights() void LightTest::update( float delta ) { static float angleDelta = 0.0; + if (_directionalLight) + { _directionalLight->setRotation3D(Vec3(-45.0, -CC_RADIANS_TO_DEGREES(angleDelta), 0.0f)); + } if (_pointLight) { @@ -273,34 +300,44 @@ void LightTest::update( float delta ) BaseTest::update(delta); } -void LightTest::SwitchLight( Ref* sender,Light3D::LightType lightType ) +void LightTest::SwitchLight( Ref* sender, LightType lightType ) { switch (lightType) { - case Light3D::LightType::DIRECTIONAL: + case LightType::AMBIENT: { char str[32]; - bool isON = !_directionalLight->getEnabled(); + bool isON = !_ambientLight->isEnabled(); + sprintf(str, "Ambient Light %s", isON == true? "ON":"OFF"); + _ambientLight->setEnabled(isON); + _ambientLightLabel->setString(str); + } + break; + + case LightType::DIRECTIONAL: + { + char str[32]; + bool isON = !_directionalLight->isEnabled(); sprintf(str, "Directional Light %s", isON == true? "ON":"OFF"); _directionalLight->setEnabled(isON); _directionalLightLabel->setString(str); } break; - case Light3D::LightType::POINT: + case LightType::POINT: { char str[32]; - bool isON = !_pointLight->getEnabled(); + bool isON = !_pointLight->isEnabled(); sprintf(str, "Point Light %s", isON == true? "ON":"OFF"); _pointLight->setEnabled(isON); _pointLightLabel->setString(str); } break; - case Light3D::LightType::SPOT: + case LightType::SPOT: { char str[32]; - bool isON = !_spotLight->getEnabled(); + bool isON = !_spotLight->isEnabled(); sprintf(str, "Spot Light %s", isON == true? "ON":"OFF"); _spotLight->setEnabled(isON); _spotLightLabel->setString(str); @@ -322,5 +359,5 @@ void LightTestScene::runThisTest() LightTestScene::LightTestScene() { - setAmbientColor(Color4F(0.2f, 0.2f, 0.2f, 1.0f)); + //setAmbientColor(Color4F(0.2f, 0.2f, 0.2f, 1.0f)); } \ No newline at end of file diff --git a/tests/cpp-tests/Classes/LightTest/LightTest.h b/tests/cpp-tests/Classes/LightTest/LightTest.h index 340bc2da9c..8673fc5a0a 100644 --- a/tests/cpp-tests/Classes/LightTest/LightTest.h +++ b/tests/cpp-tests/Classes/LightTest/LightTest.h @@ -27,7 +27,7 @@ #include "../testBasic.h" #include "../BaseTest.h" -#include "3d/CCLight.h" +#include "3d/CCLight3D.h" class LightTest : public BaseTest { @@ -48,7 +48,7 @@ public: virtual void update(float delta); - void SwitchLight(Ref* sender, Light3D::LightType lightType); + void SwitchLight(Ref* sender, LightType lightType); private: @@ -57,10 +57,12 @@ private: private: - Light3D *_directionalLight; - Light3D *_pointLight; - Light3D *_spotLight; + AmbientLight3D *_ambientLight; + DirectionLight3D *_directionalLight; + PointLight3D *_pointLight; + SpotLight3D *_spotLight; + Label *_ambientLightLabel; Label *_directionalLightLabel; Label *_pointLightLabel; Label *_spotLightLabel; diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index fdafabbd82..1f26bbc8cd 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -29,6 +29,7 @@ #include "3d/CCAttachNode.h" #include "3d/CCRay.h" #include "3d/CCSprite3D.h" +#include "3d/CCLight3D.h" #include "renderer/CCVertexIndexBuffer.h" #include "DrawNode3D.h" @@ -309,6 +310,12 @@ void Sprite3DTestScene::runThisTest() Director::getInstance()->replaceScene(this); } +Sprite3DTestScene::Sprite3DTestScene() +{ + AmbientLight3D *light = AmbientLight3D::create(Color3B(255, 255, 255)); + addChild(light); +} + static int tuple_sort( const std::tuple &tuple1, const std::tuple &tuple2 ) { return std::get<0>(tuple1) < std::get<0>(tuple2); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index fcb5de6c0e..6fdd53c592 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -324,6 +324,7 @@ protected: class Sprite3DTestScene : public TestScene { public: + Sprite3DTestScene(); virtual void runThisTest(); }; diff --git a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj index 3e3bb26257..16d27ee446 100644 --- a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj +++ b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj @@ -1,4 +1,4 @@ - + @@ -168,7 +168,7 @@ - + @@ -357,7 +357,7 @@ - + @@ -571,4 +571,4 @@ - + \ No newline at end of file diff --git a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters index 3046aac1c3..7c28fbb81b 100644 --- a/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters +++ b/tests/cpp-tests/proj.win32/cpp-tests.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -861,15 +861,15 @@ Classes\Sprite3DTest - - Classes\LightTest - Classes\UITest\CocostudioGUISceneTest Classes\BillBoardTest + + Classes\LightTest + @@ -1598,14 +1598,14 @@ Classes\Sprite3DTest - - Classes\LightTest - Classes\UITest\CocostudioGUISceneTest Classes\BillBoardTest + + Classes\LightTest + \ No newline at end of file From b9ceccda74bc11fa3dd23c52c039abf901b73003 Mon Sep 17 00:00:00 2001 From: songchengjiang Date: Thu, 18 Sep 2014 18:07:20 +0800 Subject: [PATCH 2/4] Format --- cocos/renderer/CCGLProgram.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index def48059de..9d94fe0383 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -554,9 +554,9 @@ void GLProgram::updateUniforms() _builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3); _flags.usesLights = (0 < CC_MAX_DIRECTIONAL_LIGHT_NUM) || - (0 < CC_MAX_POINT_LIGHT_NUM) || - (0 < CC_MAX_SPOT_LIGHT_NUM) || - (0 < CC_MAX_AMBIENT_LIGHT_NUM); + (0 < CC_MAX_POINT_LIGHT_NUM) || + (0 < CC_MAX_SPOT_LIGHT_NUM) || + (0 < CC_MAX_AMBIENT_LIGHT_NUM); _flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1; _flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1; _flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1; From 62777e9552a1b0d1ab01921481f89a65a6feb40e Mon Sep 17 00:00:00 2001 From: songchengjiang Date: Thu, 18 Sep 2014 18:35:18 +0800 Subject: [PATCH 3/4] compile Android --- cocos/3d/Android.mk | 1 + tests/cpp-tests/Android.mk | 2 +- tests/cpp-tests/Classes/LightTest/LightTest.cpp | 3 +-- tests/cpp-tests/proj.android/jni/Android.mk | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cocos/3d/Android.mk b/cocos/3d/Android.mk index c0ce7c55f3..30a80dc5b4 100644 --- a/cocos/3d/Android.mk +++ b/cocos/3d/Android.mk @@ -15,6 +15,7 @@ CCAttachNode.cpp \ CCBillBoard.cpp \ CCBundle3D.cpp \ CCBundleReader.cpp \ +CCLight3D.cpp \ CCMesh.cpp \ CCMeshSkin.cpp \ CCMeshVertexIndexData.cpp \ diff --git a/tests/cpp-tests/Android.mk b/tests/cpp-tests/Android.mk index 46a217932e..65c6d8dc0c 100644 --- a/tests/cpp-tests/Android.mk +++ b/tests/cpp-tests/Android.mk @@ -138,7 +138,7 @@ Classes/IntervalTest/IntervalTest.cpp \ Classes/LabelTest/LabelTest.cpp \ Classes/LabelTest/LabelTestNew.cpp \ Classes/LayerTest/LayerTest.cpp \ -Classes/LightTest/LightTestDemo.cpp \ +Classes/LightTest/LightTest.cpp \ Classes/MenuTest/MenuTest.cpp \ Classes/MotionStreakTest/MotionStreakTest.cpp \ Classes/MutiTouchTest/MutiTouchTest.cpp \ diff --git a/tests/cpp-tests/Classes/LightTest/LightTest.cpp b/tests/cpp-tests/Classes/LightTest/LightTest.cpp index 79e81e4ea9..aaf2720d65 100644 --- a/tests/cpp-tests/Classes/LightTest/LightTest.cpp +++ b/tests/cpp-tests/Classes/LightTest/LightTest.cpp @@ -228,7 +228,7 @@ void LightTest::addLights() addChild(_pointLight); _pointLight->setCameraMask(2); - _spotLight = SpotLight3D::create(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 0.0, 0.3, 10000.0f); + _spotLight = SpotLight3D::create(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0.0f, 0.0f, 0.0f), Color3B(200, 200, 200), 0.0, 0.5, 10000.0f); _spotLight->retain(); _spotLight->setEnabled(false); addChild(_spotLight); @@ -359,5 +359,4 @@ void LightTestScene::runThisTest() LightTestScene::LightTestScene() { - //setAmbientColor(Color4F(0.2f, 0.2f, 0.2f, 1.0f)); } \ No newline at end of file diff --git a/tests/cpp-tests/proj.android/jni/Android.mk b/tests/cpp-tests/proj.android/jni/Android.mk index be29988826..35d516a9ff 100644 --- a/tests/cpp-tests/proj.android/jni/Android.mk +++ b/tests/cpp-tests/proj.android/jni/Android.mk @@ -139,6 +139,7 @@ LOCAL_SRC_FILES := main.cpp \ ../../Classes/LabelTest/LabelTest.cpp \ ../../Classes/LabelTest/LabelTestNew.cpp \ ../../Classes/LayerTest/LayerTest.cpp \ +../../Classes/LightTest/LightTest.cpp \ ../../Classes/MenuTest/MenuTest.cpp \ ../../Classes/MotionStreakTest/MotionStreakTest.cpp \ ../../Classes/MutiTouchTest/MutiTouchTest.cpp \ From f052e45c040e2df8b7301d75e10f745da8c0eb63 Mon Sep 17 00:00:00 2001 From: songchengjiang Date: Fri, 19 Sep 2014 10:54:07 +0800 Subject: [PATCH 4/4] Move Light Uniforms into Meshcommand --- cocos/3d/CCLight3D.h | 5 ++ cocos/renderer/CCGLProgram.cpp | 104 --------------------------- cocos/renderer/CCGLProgram.h | 1 - cocos/renderer/CCMeshCommand.cpp | 116 +++++++++++++++++++++++++++++++ cocos/renderer/CCMeshCommand.h | 4 ++ 5 files changed, 125 insertions(+), 105 deletions(-) diff --git a/cocos/3d/CCLight3D.h b/cocos/3d/CCLight3D.h index 3486d59fc6..16985b5825 100644 --- a/cocos/3d/CCLight3D.h +++ b/cocos/3d/CCLight3D.h @@ -30,6 +30,11 @@ NS_CC_BEGIN +#define CC_MAX_DIRECTIONAL_LIGHT_NUM 1 +#define CC_MAX_POINT_LIGHT_NUM 1 +#define CC_MAX_SPOT_LIGHT_NUM 1 +#define CC_MAX_AMBIENT_LIGHT_NUM 1 + enum class LightType { DIRECTIONAL = 0, diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index 9d94fe0383..0ef6c14bf2 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -44,11 +44,6 @@ THE SOFTWARE. #include "CCPrecompiledShaders.h" #endif -#define CC_MAX_DIRECTIONAL_LIGHT_NUM 1 -#define CC_MAX_POINT_LIGHT_NUM 1 -#define CC_MAX_SPOT_LIGHT_NUM 1 -#define CC_MAX_AMBIENT_LIGHT_NUM 1 - NS_CC_BEGIN typedef struct _hashUniformEntry @@ -553,10 +548,6 @@ void GLProgram::updateUniforms() _builtInUniforms[UNIFORM_SAMPLER2] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER2); _builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3); - _flags.usesLights = (0 < CC_MAX_DIRECTIONAL_LIGHT_NUM) || - (0 < CC_MAX_POINT_LIGHT_NUM) || - (0 < CC_MAX_SPOT_LIGHT_NUM) || - (0 < CC_MAX_AMBIENT_LIGHT_NUM); _flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1; _flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1; _flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1; @@ -965,101 +956,6 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) if(_flags.usesRandom) setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_RANDOM01], CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1()); - - if (_flags.usesLights) - { - Director *director = Director::getInstance(); - auto scene = director->getRunningScene(); - if (scene) - { - auto &lights = scene->getLights(); - char str[64]; - GLint enabledDirLightNum = 0; - GLint enabledPointLightNum = 0; - GLint enabledSpotLightNum = 0; - GLint enabledAmbientLightNum = 0; - for (unsigned int i = 0; i < lights.size(); ++i) - { - BaseLight3D *light = lights[i]; - float intensity = light->isEnabled() == true? light->getIntensity() : 0.0f; - switch (light->getLightType()) - { - case LightType::DIRECTIONAL: - { - CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, ""); - DirectionLight3D *dirLight = static_cast(light); - Vec3 dir = dirLight->getDirectionInWorld(); - dir.normalize(); - const Color3B &col = dirLight->getDisplayedColor(); - sprintf(str, "CC_DirLightSourceColor[%d]", enabledDirLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); - sprintf(str, "CC_DirLightSourceDirection[%d]", enabledDirLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); - sprintf(str, "CC_DirLightSourceIntensity[%d]", enabledDirLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); - ++enabledDirLightNum; - } - break; - case LightType::POINT: - { - CCASSERT(enabledPointLightNum < CC_MAX_POINT_LIGHT_NUM, ""); - PointLight3D *pointLight = static_cast(light); - Mat4 mat= pointLight->getNodeToWorldTransform(); - const Color3B &col = pointLight->getDisplayedColor(); - sprintf(str, "CC_PointLightSourceColor[%d]", enabledPointLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); - sprintf(str, "CC_PointLightSourcePosition[%d]", enabledPointLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]); - sprintf(str, "CC_PointLightSourceRangeInverse[%d]", enabledPointLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), 1.0f / pointLight->getRange()); - sprintf(str, "CC_PointLightSourceIntensity[%d]", enabledPointLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); - ++enabledPointLightNum; - } - break; - case LightType::SPOT: - { - CCASSERT(enabledSpotLightNum < CC_MAX_SPOT_LIGHT_NUM, ""); - SpotLight3D *spotLight = static_cast(light); - Vec3 dir = spotLight->getDirectionInWorld(); - dir.normalize(); - Mat4 mat= light->getNodeToWorldTransform(); - const Color3B &col = spotLight->getDisplayedColor(); - sprintf(str, "CC_SpotLightSourceColor[%d]", enabledSpotLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); - sprintf(str, "CC_SpotLightSourcePosition[%d]", enabledSpotLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), mat.m[12], mat.m[13], mat.m[14]); - sprintf(str, "CC_SpotLightSourceDirection[%d]", enabledSpotLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); - sprintf(str, "CC_SpotLightSourceInnerAngleCos[%d]", enabledSpotLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), spotLight->getCosInnerAngle()); - sprintf(str, "CC_SpotLightSourceOuterAngleCos[%d]", enabledSpotLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), spotLight->getCosOuterAngle()); - sprintf(str, "CC_SpotLightSourceRangeInverse[%d]", enabledSpotLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), 1.0f / spotLight->getRange()); - sprintf(str, "CC_SpotLightSourceIntensity[%d]", enabledSpotLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); - ++enabledSpotLightNum; - } - break; - case LightType::AMBIENT: - { - CCASSERT(enabledAmbientLightNum < CC_MAX_AMBIENT_LIGHT_NUM, ""); - AmbientLight3D *ambLight = static_cast(light); - const Color3B &col = ambLight->getDisplayedColor(); - sprintf(str, "CC_AmbientLightSourceColor[%d]", enabledAmbientLightNum); - setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); - sprintf(str, "CC_AmbientLightSourceIntensity[%d]", enabledAmbientLightNum); - setUniformLocationWith1f(glGetUniformLocation(_program, str), intensity); - ++enabledAmbientLightNum; - } - break; - default: - break; - } - } - } - } } void GLProgram::reset() diff --git a/cocos/renderer/CCGLProgram.h b/cocos/renderer/CCGLProgram.h index b56736af81..830ca8b487 100644 --- a/cocos/renderer/CCGLProgram.h +++ b/cocos/renderer/CCGLProgram.h @@ -363,7 +363,6 @@ protected: unsigned int usesMV:1; unsigned int usesP:1; unsigned int usesRandom:1; - unsigned int usesLights:1; // handy way to initialize the bitfield flag_struct() { memset(this, 0, sizeof(*this)); } } _flags; diff --git a/cocos/renderer/CCMeshCommand.cpp b/cocos/renderer/CCMeshCommand.cpp index 6873e29ff2..b4eb1a22d7 100644 --- a/cocos/renderer/CCMeshCommand.cpp +++ b/cocos/renderer/CCMeshCommand.cpp @@ -36,6 +36,7 @@ #include "renderer/CCTextureAtlas.h" #include "renderer/CCTexture2D.h" #include "renderer/ccGLStateCache.h" +#include "3d/CCLight3D.h" #include "xxhash.h" NS_CC_BEGIN @@ -59,6 +60,7 @@ MeshCommand::MeshCommand() , _matrixPaletteSize(0) , _materialID(0) , _vao(0) +, _useLights(false) { _type = RenderCommand::Type::MESH_COMMAND; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8) @@ -92,6 +94,11 @@ void MeshCommand::init(float globalOrder, _indexFormat = indexFormat; _indexCount = indexCount; _mv.set(mv); + + _useLights = (0 < CC_MAX_DIRECTIONAL_LIGHT_NUM) || + (0 < CC_MAX_POINT_LIGHT_NUM) || + (0 < CC_MAX_SPOT_LIGHT_NUM) || + (0 < CC_MAX_AMBIENT_LIGHT_NUM); } void MeshCommand::setCullFaceEnabled(bool enable) @@ -222,6 +229,11 @@ void MeshCommand::batchDraw() _glProgramState->applyGLProgram(_mv); _glProgramState->applyUniforms(); + + if (_useLights) + { + applyLightUniforms(); + } // Draw glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0); @@ -262,6 +274,11 @@ void MeshCommand::execute() _glProgramState->apply(_mv); + if (_useLights) + { + applyLightUniforms(); + } + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); // Draw @@ -306,11 +323,110 @@ void MeshCommand::releaseVAO() } } + +void MeshCommand::applyLightUniforms() +{ + Director *director = Director::getInstance(); + auto scene = director->getRunningScene(); + if (scene) + { + GLProgram *glProgram = _glProgramState->getGLProgram(); + GLint programID = glProgram->getProgram(); + auto &lights = scene->getLights(); + char str[64]; + GLint enabledDirLightNum = 0; + GLint enabledPointLightNum = 0; + GLint enabledSpotLightNum = 0; + GLint enabledAmbientLightNum = 0; + for (unsigned int i = 0; i < lights.size(); ++i) + { + BaseLight3D *light = lights[i]; + float intensity = light->isEnabled() == true? light->getIntensity() : 0.0f; + switch (light->getLightType()) + { + case LightType::DIRECTIONAL: + { + CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, ""); + DirectionLight3D *dirLight = static_cast(light); + Vec3 dir = dirLight->getDirectionInWorld(); + dir.normalize(); + const Color3B &col = dirLight->getDisplayedColor(); + sprintf(str, "CC_DirLightSourceColor[%d]", enabledDirLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_DirLightSourceDirection[%d]", enabledDirLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), dir.x, dir.y, dir.z); + sprintf(str, "CC_DirLightSourceIntensity[%d]", enabledDirLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity); + ++enabledDirLightNum; + } + break; + case LightType::POINT: + { + CCASSERT(enabledPointLightNum < CC_MAX_POINT_LIGHT_NUM, ""); + PointLight3D *pointLight = static_cast(light); + Mat4 mat= pointLight->getNodeToWorldTransform(); + const Color3B &col = pointLight->getDisplayedColor(); + sprintf(str, "CC_PointLightSourceColor[%d]", enabledPointLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_PointLightSourcePosition[%d]", enabledPointLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), mat.m[12], mat.m[13], mat.m[14]); + sprintf(str, "CC_PointLightSourceRangeInverse[%d]", enabledPointLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), 1.0f / pointLight->getRange()); + sprintf(str, "CC_PointLightSourceIntensity[%d]", enabledPointLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity); + ++enabledPointLightNum; + } + break; + case LightType::SPOT: + { + CCASSERT(enabledSpotLightNum < CC_MAX_SPOT_LIGHT_NUM, ""); + SpotLight3D *spotLight = static_cast(light); + Vec3 dir = spotLight->getDirectionInWorld(); + dir.normalize(); + Mat4 mat= light->getNodeToWorldTransform(); + const Color3B &col = spotLight->getDisplayedColor(); + sprintf(str, "CC_SpotLightSourceColor[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_SpotLightSourcePosition[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), mat.m[12], mat.m[13], mat.m[14]); + sprintf(str, "CC_SpotLightSourceDirection[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), dir.x, dir.y, dir.z); + sprintf(str, "CC_SpotLightSourceInnerAngleCos[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), spotLight->getCosInnerAngle()); + sprintf(str, "CC_SpotLightSourceOuterAngleCos[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), spotLight->getCosOuterAngle()); + sprintf(str, "CC_SpotLightSourceRangeInverse[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), 1.0f / spotLight->getRange()); + sprintf(str, "CC_SpotLightSourceIntensity[%d]", enabledSpotLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity); + ++enabledSpotLightNum; + } + break; + case LightType::AMBIENT: + { + CCASSERT(enabledAmbientLightNum < CC_MAX_AMBIENT_LIGHT_NUM, ""); + AmbientLight3D *ambLight = static_cast(light); + const Color3B &col = ambLight->getDisplayedColor(); + sprintf(str, "CC_AmbientLightSourceColor[%d]", enabledAmbientLightNum); + glProgram->setUniformLocationWith3f(glGetUniformLocation(programID, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); + sprintf(str, "CC_AmbientLightSourceIntensity[%d]", enabledAmbientLightNum); + glProgram->setUniformLocationWith1f(glGetUniformLocation(programID, str), intensity); + ++enabledAmbientLightNum; + } + break; + default: + break; + } + } + } +} + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8) void MeshCommand::listenRendererRecreated(EventCustom* event) { _vao = 0; } + #endif NS_CC_END diff --git a/cocos/renderer/CCMeshCommand.h b/cocos/renderer/CCMeshCommand.h index 094b735e6d..fedea209ae 100644 --- a/cocos/renderer/CCMeshCommand.h +++ b/cocos/renderer/CCMeshCommand.h @@ -84,6 +84,8 @@ protected: // apply renderstate void applyRenderState(); + + void applyLightUniforms(); //restore to all false void restoreRenderState(); @@ -120,6 +122,8 @@ protected: // ModelView transform Mat4 _mv; + + bool _useLights; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8) EventListenerCustom* _rendererRecreatedListener;