Finished light reconstruction

This commit is contained in:
songchengjiang 2014-09-18 16:38:35 +08:00
parent 8c77130ae0
commit 3c67515378
14 changed files with 243 additions and 433 deletions

View File

@ -208,7 +208,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClCompile Include="..\3d\CCBillBoard.cpp" />
<ClCompile Include="..\3d\CCBundle3D.cpp" />
<ClCompile Include="..\3d\CCBundleReader.cpp" />
<ClCompile Include="..\3d\CCLight.cpp" />
<ClCompile Include="..\3d\CCLight3D.cpp" />
<ClCompile Include="..\3d\CCMesh.cpp" />
<ClCompile Include="..\3d\CCMeshSkin.cpp" />
<ClCompile Include="..\3d\CCMeshVertexIndexData.cpp" />
@ -546,7 +546,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClInclude Include="..\3d\CCBundle3D.h" />
<ClInclude Include="..\3d\CCBundle3DData.h" />
<ClInclude Include="..\3d\CCBundleReader.h" />
<ClInclude Include="..\3d\CCLight.h" />
<ClInclude Include="..\3d\CCLight3D.h" />
<ClInclude Include="..\3d\CCMesh.h" />
<ClInclude Include="..\3d\CCMeshSkin.h" />
<ClInclude Include="..\3d\CCMeshVertexIndexData.h" />

View File

@ -1039,9 +1039,6 @@
<ClCompile Include="..\3d\CCMeshVertexIndexData.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCLight.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\editor-support\cocosbuilder\CCBAnimationManager.cpp">
<Filter>cocosbuilder\Source Files</Filter>
</ClCompile>
@ -1171,6 +1168,9 @@
<ClCompile Include="..\ui\UIEditBox\UIEditBoxImpl-win32.cpp">
<Filter>ui\UIWidgets\EditBox</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCLight3D.cpp">
<Filter>3d</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\physics\CCPhysicsBody.h">
@ -2149,9 +2149,6 @@
<ClInclude Include="..\3d\CCMeshVertexIndexData.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCLight.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\editor-support\cocosbuilder\CCBAnimationManager.h">
<Filter>cocosbuilder\Header Files</Filter>
</ClInclude>
@ -2302,6 +2299,9 @@
<ClInclude Include="..\ui\UIEditBox\UIEditBoxImpl-win32.h">
<Filter>ui\UIWidgets\EditBox</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCLight3D.h">
<Filter>3d</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\math\Mat4.inl">

View File

@ -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

View File

@ -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

View File

@ -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<DirectionLight3D *>(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<PointLight3D *>(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<SpotLight3D *>(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<AmbientLight3D *>(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()

View File

@ -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;

View File

@ -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
}

View File

@ -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;

View File

@ -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));
}

View File

@ -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;

View File

@ -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<ssize_t,Effect3D*,CustomCommand> &tuple1, const std::tuple<ssize_t,Effect3D*,CustomCommand> &tuple2 )
{
return std::get<0>(tuple1) < std::get<0>(tuple2);

View File

@ -324,6 +324,7 @@ protected:
class Sprite3DTestScene : public TestScene
{
public:
Sprite3DTestScene();
virtual void runThisTest();
};

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
@ -168,7 +168,7 @@
<ClCompile Include="..\Classes\FileUtilsTest\FileUtilsTest.cpp" />
<ClCompile Include="..\Classes\InputTest\MouseTest.cpp" />
<ClCompile Include="..\Classes\LabelTest\LabelTestNew.cpp" />
<ClCompile Include="..\Classes\LightTest\LightTestDemo.cpp" />
<ClCompile Include="..\Classes\LightTest\LightTest.cpp" />
<ClCompile Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.cpp" />
<ClCompile Include="..\Classes\NewRendererTest\NewRendererTest.cpp" />
<ClCompile Include="..\Classes\PerformanceTest\PerformanceAllocTest.cpp" />
@ -357,7 +357,7 @@
<ClInclude Include="..\Classes\FileUtilsTest\FileUtilsTest.h" />
<ClInclude Include="..\Classes\InputTest\MouseTest.h" />
<ClInclude Include="..\Classes\LabelTest\LabelTestNew.h" />
<ClInclude Include="..\Classes\LightTest\LightTestDemo.h" />
<ClInclude Include="..\Classes\LightTest\LightTest.h" />
<ClInclude Include="..\Classes\NewEventDispatcherTest\NewEventDispatcherTest.h" />
<ClInclude Include="..\Classes\NewRendererTest\NewRendererTest.h" />
<ClInclude Include="..\Classes\PerformanceTest\PerformanceAllocTest.h" />
@ -571,4 +571,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="win32">
@ -861,15 +861,15 @@
<ClCompile Include="..\Classes\Sprite3DTest\DrawNode3D.cpp">
<Filter>Classes\Sprite3DTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\LightTest\LightTestDemo.cpp">
<Filter>Classes\LightTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\UITest\CocoStudioGUITest\UIEditBoxTest.cpp">
<Filter>Classes\UITest\CocostudioGUISceneTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\BillBoardTest\BillBoardTest.cpp">
<Filter>Classes\BillBoardTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\LightTest\LightTest.cpp">
<Filter>Classes\LightTest</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
@ -1598,14 +1598,14 @@
<ClInclude Include="..\Classes\Sprite3DTest\DrawNode3D.h">
<Filter>Classes\Sprite3DTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\LightTest\LightTestDemo.h">
<Filter>Classes\LightTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\UITest\CocoStudioGUITest\UIEditBoxTest.h">
<Filter>Classes\UITest\CocostudioGUISceneTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\BillBoardTest\BillBoardTest.h">
<Filter>Classes\BillBoardTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\LightTest\LightTest.h">
<Filter>Classes\LightTest</Filter>
</ClInclude>
</ItemGroup>
</Project>