Finished Point Light

Finished Directional Light
Finished Spot Light
This commit is contained in:
songchengjiang 2014-08-18 17:10:07 +08:00
parent 9b3a02f350
commit 10afb9b9af
11 changed files with 429 additions and 198 deletions

View File

@ -4,8 +4,7 @@
NS_CC_BEGIN NS_CC_BEGIN
Light3D::Light3D() Light3D::Light3D()
: _isEnabled(false) : _range(0.0f)
, _range(0.0f)
, _innerAngle(0.0f) , _innerAngle(0.0f)
, _outerAngle(0.0f) , _outerAngle(0.0f)
{ {
@ -35,17 +34,6 @@ Light3D::LightType Light3D::getLightType()
return _lightType; return _lightType;
} }
void Light3D::setEnabled( bool isEnabled )
{
_isEnabled = isEnabled;
}
bool Light3D::getEnabled()
{
return _isEnabled;
}
void Light3D::setRange( float range ) void Light3D::setRange( float range )
{ {
_range = range; _range = range;
@ -91,7 +79,7 @@ void Light3D::onEnter()
auto scene = getScene(); auto scene = getScene();
if (scene) if (scene)
{ {
auto lights = scene->_lights; auto &lights = scene->_lights;
auto iter = std::find(lights.begin(), lights.end(), this); auto iter = std::find(lights.begin(), lights.end(), this);
if (iter == lights.end()) if (iter == lights.end())
lights.push_back(this); lights.push_back(this);
@ -103,7 +91,7 @@ void Light3D::onExit()
auto scene = getScene(); auto scene = getScene();
if (scene) if (scene)
{ {
auto lights = scene->_lights; auto &lights = scene->_lights;
auto iter = std::find(lights.begin(), lights.end(), this); auto iter = std::find(lights.begin(), lights.end(), this);
if (iter != lights.end()) if (iter != lights.end())
lights.erase(iter); lights.erase(iter);

View File

@ -59,16 +59,6 @@ public:
*/ */
LightType getLightType(); LightType getLightType();
/**
* Sets light enabled.
*/
void setEnabled(bool isEnabled);
/**
* Gets light enabled.
*/
bool getEnabled();
/** /**
* Sets the range of point or spot light. * Sets the range of point or spot light.
* *
@ -133,7 +123,6 @@ CC_CONSTRUCTOR_ACCESS:
protected: protected:
LightType _lightType; LightType _lightType;
bool _isEnabled;
Vec3 _dir; Vec3 _dir;
float _range; float _range;
float _innerAngle; float _innerAngle;

View File

@ -80,7 +80,8 @@ const char* GLProgram::SHADER_3D_SKINPOSITION_TEXTURE = "Shader3DSkinPositionTex
// uniform names // uniform names
const char* GLProgram::UNIFORM_NAME_LIGHT_SOURCE = "CC_LightSource"; const char* GLProgram::UNIFORM_NAME_ENABLED_LIGHT_NUM = "CC_EnabledLightNum";
const char* GLProgram::UNIFORM_NAME_AMBIENT_COLOR = "CC_AmbientColor";
const char* GLProgram::UNIFORM_NAME_P_MATRIX = "CC_PMatrix"; const char* GLProgram::UNIFORM_NAME_P_MATRIX = "CC_PMatrix";
const char* GLProgram::UNIFORM_NAME_MV_MATRIX = "CC_MVMatrix"; const char* GLProgram::UNIFORM_NAME_MV_MATRIX = "CC_MVMatrix";
const char* GLProgram::UNIFORM_NAME_MVP_MATRIX = "CC_MVPMatrix"; const char* GLProgram::UNIFORM_NAME_MVP_MATRIX = "CC_MVPMatrix";
@ -417,17 +418,18 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source
"#define CC_MAX_LIGHT 6 \n" "#define CC_MAX_LIGHT 6 \n"
"struct LightSource \n" "struct LightSource \n"
"{ \n" "{ \n"
" vec4 color; \n" " float type; \n"
" vec3 position; \n"
" vec3 direction; \n"
" float range; \n" " float range; \n"
" float range2; \n" " float range2; \n"
" float innerAngle; \n" " float innerAngle; \n"
" float outerAngle; \n" " float outerAngle; \n"
" float type; \n" " vec4 color; \n"
" float use; \n" " vec3 position; \n"
" vec3 direction; \n"
"}; \n" "}; \n"
"uniform LightSource CC_LightSource[CC_MAX_LIGHT];\n" "uniform LightSource CC_LightSource[CC_MAX_LIGHT];\n"
"uniform int CC_EnabledLightNum; \n"
"uniform vec4 CC_AmbientColor; \n"
}; };
const GLchar *sources[] = { const GLchar *sources[] = {
@ -498,7 +500,8 @@ void GLProgram::bindAttribLocation(const std::string &attributeName, GLuint inde
void GLProgram::updateUniforms() void GLProgram::updateUniforms()
{ {
_builtInUniforms[UNIFORM_LIGHT_SOURCE] = glGetUniformLocation(_program, UNIFORM_NAME_LIGHT_SOURCE); _builtInUniforms[UNIFORM_ENABLED_LIGHT_NUM] = glGetUniformLocation(_program, UNIFORM_NAME_ENABLED_LIGHT_NUM);
_builtInUniforms[UNIFORM_AMBIENT_COLOR] = glGetUniformLocation(_program, UNIFORM_NAME_AMBIENT_COLOR);
_builtInUniforms[UNIFORM_P_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_P_MATRIX); _builtInUniforms[UNIFORM_P_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_P_MATRIX);
_builtInUniforms[UNIFORM_MV_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MV_MATRIX); _builtInUniforms[UNIFORM_MV_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MV_MATRIX);
_builtInUniforms[UNIFORM_MVP_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MVP_MATRIX); _builtInUniforms[UNIFORM_MVP_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MVP_MATRIX);
@ -515,7 +518,7 @@ void GLProgram::updateUniforms()
_builtInUniforms[UNIFORM_SAMPLER2] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER2); _builtInUniforms[UNIFORM_SAMPLER2] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER2);
_builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3); _builtInUniforms[UNIFORM_SAMPLER3] = glGetUniformLocation(_program, UNIFORM_NAME_SAMPLER3);
_flags.usesLights = _builtInUniforms[UNIFORM_LIGHT_SOURCE] != -1; _flags.usesLights = _builtInUniforms[UNIFORM_ENABLED_LIGHT_NUM] != -1;
_flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1; _flags.usesP = _builtInUniforms[UNIFORM_P_MATRIX] != -1;
_flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1; _flags.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1;
_flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1; _flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1;
@ -931,31 +934,38 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
auto scene = director->getRunningScene(); auto scene = director->getRunningScene();
if (scene) if (scene)
{ {
auto lights = scene->getLights(); auto &lights = scene->getLights();
CCASSERT(lights.size() < CC_MAX_LIGHT_NUM, ""); CCASSERT(lights.size() < CC_MAX_LIGHT_NUM, "");
GLfloat lightSources[16 * CC_MAX_LIGHT_NUM];
unsigned int sz = sizeof(lightSources);
memset(lightSources, 0, sizeof(lightSources));
unsigned int idx = 0; unsigned int idx = 0;
for (auto iter : lights) char str[32];
for (unsigned int i = 0; i < lights.size(); ++i)
{ {
const Color3B &col = iter->getColor(); Light3D *light = lights[i];
const Vec3 &pos = iter->getPosition3D(); const Color3B &col = light->getColor();
const Vec3 &dir = iter->getDirection(); const Vec3 &pos = light->getPosition3D();
lightSources[0 + idx] = col.r / 255.0f;lightSources[1 + idx] = col.g / 255.0f;lightSources[2 + idx] = col.b / 255.0f;lightSources[3 + idx] = 1.0f; const Vec3 &dir = light->getDirection();
lightSources[4 + idx] = pos.x;lightSources[5 + idx] = pos.y;lightSources[6 + idx] = pos.z;
lightSources[7 + idx] = dir.x;lightSources[8 + idx] = dir.y;lightSources[9 + idx] = dir.z; sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "type");
lightSources[10 + idx] = iter->getRange(); setUniformLocationWith1f(glGetUniformLocation(_program, str), static_cast<float>(light->getLightType()));
lightSources[11 + idx] = iter->getRange() * iter->getRange(); sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "range");
lightSources[12 + idx] = iter->getInnerAngle(); setUniformLocationWith1f(glGetUniformLocation(_program, str), light->getRange());
lightSources[13 + idx] = iter->getOuterAngle(); sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "range2");
lightSources[14 + idx] = static_cast<float>(iter->getLightType()); setUniformLocationWith1f(glGetUniformLocation(_program, str), light->getRange() * light->getRange());
lightSources[15 + idx] = static_cast<float>(iter->getEnabled()); sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "innerAngle");
idx += 16; setUniformLocationWith1f(glGetUniformLocation(_program, str), light->getInnerAngle());
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "outerAngle");
setUniformLocationWith1f(glGetUniformLocation(_program, str), light->getOuterAngle());
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "color");
setUniformLocationWith4f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f, 1.0f);
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "position");
setUniformLocationWith3f(glGetUniformLocation(_program, str), pos.x, pos.y, pos.z);
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "direction");
setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z);
idx += 8;
} }
setUniformLocationWith1fv(_builtInUniforms[GLProgram::UNIFORM_LIGHT_SOURCE], lightSources, 16 * CC_MAX_LIGHT_NUM); setUniformLocationWith1i(_builtInUniforms[GLProgram::UNIFORM_ENABLED_LIGHT_NUM], (GLint)lights.size());
setUniformLocationWith4f(_builtInUniforms[GLProgram::UNIFORM_AMBIENT_COLOR], scene->getAmbientColor().r, scene->getAmbientColor().g, scene->getAmbientColor().b, scene->getAmbientColor().a);
} }
} }
} }

View File

@ -96,7 +96,8 @@ public:
enum enum
{ {
UNIFORM_LIGHT_SOURCE, UNIFORM_ENABLED_LIGHT_NUM,
UNIFORM_AMBIENT_COLOR,
UNIFORM_P_MATRIX, UNIFORM_P_MATRIX,
UNIFORM_MV_MATRIX, UNIFORM_MV_MATRIX,
UNIFORM_MVP_MATRIX, UNIFORM_MVP_MATRIX,
@ -137,7 +138,8 @@ public:
static const char* SHADER_3D_SKINPOSITION_TEXTURE; static const char* SHADER_3D_SKINPOSITION_TEXTURE;
// uniform names // uniform names
static const char* UNIFORM_NAME_LIGHT_SOURCE; static const char* UNIFORM_NAME_ENABLED_LIGHT_NUM;
static const char* UNIFORM_NAME_AMBIENT_COLOR;
static const char* UNIFORM_NAME_P_MATRIX; static const char* UNIFORM_NAME_P_MATRIX;
static const char* UNIFORM_NAME_MV_MATRIX; static const char* UNIFORM_NAME_MV_MATRIX;
static const char* UNIFORM_NAME_MVP_MATRIX; static const char* UNIFORM_NAME_MVP_MATRIX;

View File

@ -7,9 +7,59 @@ varying lowp vec4 DestinationColor;
varying vec4 DestinationColor; varying vec4 DestinationColor;
\n#endif\n \n#endif\n
uniform vec4 u_color; uniform vec4 u_color;
varying vec4 ePosition;
varying vec3 eNormal;
void PointLight(int n, vec4 ePosition, vec3 eNormal, out vec4 intensity)
{
if (distance(CC_LightSource[n].position, ePosition.xyz) < CC_LightSource[n].range)
{
vec4 p = vec4(CC_LightSource[n].position, 1.0);
p -= ePosition;
intensity.xyz += CC_LightSource[n].color * max(0.0, dot(normalize(p.xyz), eNormal));
}
intensity.w = 1.0;
}
void DirectionalLight(int n, vec3 eNormal, out vec4 intensity)
{
intensity.xyz += CC_LightSource[n].color * max(0.0, dot(normalize(-CC_LightSource[n].direction), eNormal));
intensity.w = 1.0;
}
void SpotLight(int n, vec4 ePosition, vec3 eNormal, out vec4 intensity)
{
if (distance(CC_LightSource[n].position, ePosition.xyz) < CC_LightSource[n].range)
{
vec3 lightDir = CC_LightSource[n].position - ePosition.xyz;
lightDir = normalize(lightDir);
float spotDot = dot(lightDir, normalize(-CC_LightSource[n].direction));
float innerCos = cos(CC_LightSource[n].innerAngle);
float outerCos = cos(CC_LightSource[n].outerAngle);
float factor = smoothstep(outerCos, innerCos, spotDot);
intensity.xyz += CC_LightSource[n].color * max(0.0, dot(lightDir, eNormal)) * factor;
}
intensity.w = 1.0;
}
void main(void) void main(void)
{ {
vec3 normal = normalize(eNormal);
vec4 intensity = vec4(0.0);
for (int i = 0; i < CC_EnabledLightNum; ++i)
{
if (CC_LightSource[i].type == 0.0f)
DirectionalLight(i, normal, intensity);
else
if (CC_LightSource[i].type == 1.0f)
PointLight(i, ePosition, normal, intensity);
else
SpotLight(i, ePosition, normal, intensity);
}
if (intensity.w == 0.0)
gl_FragColor = u_color; gl_FragColor = u_color;
else
gl_FragColor = u_color * (CC_AmbientColor + intensity);
} }
); );

View File

@ -3,18 +3,63 @@ const char* cc3D_ColorTex_frag = STRINGIFY(
\n#ifdef GL_ES\n \n#ifdef GL_ES\n
varying mediump vec2 TextureCoordOut; varying mediump vec2 TextureCoordOut;
varying mediump vec4 LightIntensityOut;
\n#else\n \n#else\n
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
varying vec4 LightIntensityOut;
\n#endif\n \n#endif\n
uniform vec4 u_color; uniform vec4 u_color;
varying vec4 ePosition;
varying vec3 eNormal;
void PointLight(int n, vec4 ePosition, vec3 eNormal, out vec4 intensity)
{
if (distance(CC_LightSource[n].position, ePosition.xyz) < CC_LightSource[n].range)
{
vec4 p = vec4(CC_LightSource[n].position, 1.0);
p -= ePosition;
intensity.xyz += CC_LightSource[n].color * max(0.0, dot(normalize(p.xyz), eNormal));
}
intensity.w = 1.0;
}
void DirectionalLight(int n, vec3 eNormal, out vec4 intensity)
{
intensity.xyz += CC_LightSource[n].color * max(0.0, dot(normalize(-CC_LightSource[n].direction), eNormal));
intensity.w = 1.0;
}
void SpotLight(int n, vec4 ePosition, vec3 eNormal, out vec4 intensity)
{
if (distance(CC_LightSource[n].position, ePosition.xyz) < CC_LightSource[n].range)
{
vec3 lightDir = CC_LightSource[n].position - ePosition.xyz;
lightDir = normalize(lightDir);
float spotDot = dot(lightDir, normalize(-CC_LightSource[n].direction));
float innerCos = cos(CC_LightSource[n].innerAngle);
float outerCos = cos(CC_LightSource[n].outerAngle);
float factor = smoothstep(outerCos, innerCos, spotDot);
intensity.xyz += CC_LightSource[n].color * max(0.0, dot(lightDir, eNormal)) * factor;
}
intensity.w = 1.0;
}
void main(void) void main(void)
{ {
if (LightIntensityOut.w == 0.0) vec3 normal = normalize(eNormal);
vec4 intensity = vec4(0.0);
for (int i = 0; i < CC_EnabledLightNum; ++i)
{
if (CC_LightSource[i].type == 0.0f)
DirectionalLight(i, normal, intensity);
else
if (CC_LightSource[i].type == 1.0f)
PointLight(i, ePosition, normal, intensity);
else
SpotLight(i, ePosition, normal, intensity);
}
if (intensity.w == 0.0)
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color; gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color;
else else
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color * LightIntensityOut; gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color * (CC_AmbientColor + intensity);
} }
); );

View File

@ -5,34 +5,13 @@ attribute vec4 a_position;
attribute vec2 a_texCoord; attribute vec2 a_texCoord;
attribute vec3 a_normal; attribute vec3 a_normal;
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
varying vec4 LightIntensityOut; varying vec4 ePosition;
varying vec3 eNormal;
vec4 PointLight(int n, vec4 ePosition, vec3 eNormal)
{
vec4 intensity = vec4(0.0);
if (0.0 < CC_LightSource[n].use)
{
vec4 p = vec4(CC_LightSource[n].position, 1.0) * CC_MVMatrix;
p -= ePosition;
intensity.xyz = CC_LightSource[n].color * max(0.0, dot(normalize(p.xyz), eNormal));
intensity.w = 1.0;
}
return intensity;
}
void main(void) void main(void)
{ {
vec4 ePosition = CC_MVMatrix * a_position; ePosition = CC_MVMatrix * a_position;
vec3 eNormal = CC_NormalMatrix * a_normal; eNormal = CC_NormalMatrix * a_normal;
vec4 intensity = vec4(0.0);
for (int i = 0; i < CC_MAX_LIGHT; ++i)
{
intensity += PointLight(i, ePosition, eNormal);
}
LightIntensityOut = intensity;
TextureCoordOut = a_texCoord; TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y; TextureCoordOut.y = 1.0 - TextureCoordOut.y;
gl_Position = CC_PMatrix * ePosition; gl_Position = CC_PMatrix * ePosition;
@ -55,21 +34,8 @@ uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
// Varyings // Varyings
varying vec2 TextureCoordOut; varying vec2 TextureCoordOut;
varying vec4 LightIntensityOut; varying vec4 ePosition;
varying vec3 eNormal;
vec4 PointLight(int n, vec4 ePosition, vec3 eNormal)
{
vec4 intensity = vec4(0.0);
if (0.0 < CC_LightSource[n].use)
{
vec4 p = vec4(CC_LightSource[n].position, 1.0) * CC_MVMatrix;
p -= ePosition;
intensity.xyz = CC_LightSource[n].color * max(0.0, dot(normalize(p.xyz), eNormal));
intensity.w = 1.0;
}
return intensity;
}
void getPositionAndNormal(out vec4 position, out vec3 normal) void getPositionAndNormal(out vec4 position, out vec3 normal)
{ {
@ -129,15 +95,8 @@ void main()
vec3 normal; vec3 normal;
getPositionAndNormal(position, normal); getPositionAndNormal(position, normal);
vec4 ePosition = CC_MVMatrix * position; ePosition = CC_MVMatrix * position;
vec3 eNormal = CC_NormalMatrix * normal; eNormal = CC_NormalMatrix * normal;
vec4 intensity = vec4(0.0);
for (int i = 0; i < CC_MAX_LIGHT; ++i)
{
intensity += PointLight(i, ePosition, eNormal);
}
LightIntensityOut = intensity;
TextureCoordOut = a_texCoord; TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y; TextureCoordOut.y = 1.0 - TextureCoordOut.y;
gl_Position = CC_PMatrix * ePosition; gl_Position = CC_PMatrix * ePosition;

View File

@ -6,7 +6,9 @@ static int sceneIdx = -1;
static std::function<Layer*()> createFunctions[] = static std::function<Layer*()> createFunctions[] =
{ {
CL(LightTestDemo) CL(DirectionalLightTestDemo),
CL(PointLightTestDemo),
CL(SpotLightTestDemo)
}; };
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) #define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
@ -39,8 +41,6 @@ static Layer* restartSpriteTestAction()
LightTestDemo::LightTestDemo() LightTestDemo::LightTestDemo()
{ {
addSprite();
addLights();
} }
@ -82,28 +82,6 @@ void LightTestDemo::backCallback( Ref* sender )
s->release(); s->release();
} }
void LightTestDemo::addSprite()
{
auto s = Director::getInstance()->getWinSize();
std::string fileName = "Sprite3DTest/orc.c3b";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(5.0f);
sprite->setRotation3D(Vec3(0,180,0));
sprite->setPosition(Vec2(s.width/2, s.height/2));
addChild(sprite);
auto sp = Sprite3D::create("Sprite3DTest/axe.c3b");
sprite->getAttachNode("Bip001 R Hand")->addChild(sp);
auto animation = Animation3D::create(fileName);
if (animation)
{
auto animate = Animate3D::create(animation);
sprite->runAction(RepeatForever::create(animate));
}
}
void LightTestDemo::onEnter() void LightTestDemo::onEnter()
{ {
BaseTest::onEnter(); BaseTest::onEnter();
@ -114,17 +92,6 @@ void LightTestDemo::onExit()
BaseTest::onExit(); BaseTest::onExit();
} }
void LightTestDemo::addLights()
{
auto s = Director::getInstance()->getWinSize();
auto light1 = Light3D::Create(Light3D::POINT);
light1->setEnabled(true);
light1->setPosition(Vec2(s.width/2, s.height));
light1->setColor(Color3B(255, 255, 255));
addChild(light1);
}
void LightTestScene::runThisTest() void LightTestScene::runThisTest()
{ {
auto layer = nextSpriteTestAction(); auto layer = nextSpriteTestAction();
@ -132,3 +99,169 @@ void LightTestScene::runThisTest()
Director::getInstance()->replaceScene(this); Director::getInstance()->replaceScene(this);
} }
LightTestScene::LightTestScene()
{
setAmbientColor(Color4F(0.2f, 0.2f, 0.2f, 1.0f));
}
PointLightTestDemo::PointLightTestDemo()
: _pointLight(nullptr)
{
addSprite();
addLights();
scheduleUpdate();
}
PointLightTestDemo::~PointLightTestDemo()
{
if (_pointLight)
_pointLight->release();
}
void PointLightTestDemo::addSprite()
{
auto s = Director::getInstance()->getWinSize();
std::string fileName = "Sprite3DTest/sphere.c3b";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(5.0f);
sprite->setPosition(Vec2(s.width/2, s.height/2));
addChild(sprite);
}
void PointLightTestDemo::addLights()
{
auto s = Director::getInstance()->getWinSize();
_pointLight = Light3D::Create(Light3D::POINT);
_pointLight->setPosition(Vec2(s.width/2, s.height/2));
_pointLight->setColor(Color3B(128, 128, 128));
_pointLight->setRange(10000.0f);
_pointLight->retain();
addChild(_pointLight);
}
void PointLightTestDemo::update( float delta )
{
auto s = Director::getInstance()->getWinSize();
static float angleDelta = 0.0;
_pointLight->setPositionX(s.width / 2 + s.width / 4 * cosf(angleDelta));
_pointLight->setPositionY(s.height / 2);
_pointLight->setPositionZ(s.width / 4 * sinf(angleDelta));
angleDelta += delta;
BaseTest::update(delta);
}
std::string PointLightTestDemo::title() const
{
return "Point Light";
}
DirectionalLightTestDemo::DirectionalLightTestDemo()
: _directionalLight(nullptr)
{
addSprite();
addLights();
scheduleUpdate();
}
DirectionalLightTestDemo::~DirectionalLightTestDemo()
{
if (_directionalLight)
_directionalLight->release();
}
std::string DirectionalLightTestDemo::title() const
{
return "Directional Light";
}
void DirectionalLightTestDemo::update( float delta )
{
static float angleDelta = 0.0;
_directionalLight->setDirection(-Vec3(cosf(angleDelta), 0.0f, sinf(angleDelta)));
angleDelta += delta;
BaseTest::update(delta);
}
void DirectionalLightTestDemo::addSprite()
{
auto s = Director::getInstance()->getWinSize();
std::string fileName = "Sprite3DTest/sphere.c3b";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(5.0f);
sprite->setRotation3D(Vec3(0,180,0));
sprite->setPosition(Vec2(s.width/2, s.height/2));
addChild(sprite);
}
void DirectionalLightTestDemo::addLights()
{
auto s = Director::getInstance()->getWinSize();
_directionalLight = Light3D::Create(Light3D::DIRECTIONAL);
_directionalLight->setDirection(Vec3(-1.0f, 0.0f, 0.0f));
_directionalLight->setColor(Color3B(128, 128, 128));
_directionalLight->retain();
addChild(_directionalLight);
}
std::string SpotLightTestDemo::title() const
{
return "Spot Light";
}
void SpotLightTestDemo::addSprite()
{
auto s = Director::getInstance()->getWinSize();
std::string fileName = "Sprite3DTest/sphere.c3b";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(5.0f);
sprite->setPosition(Vec2(s.width/2, s.height/2));
addChild(sprite);
}
void SpotLightTestDemo::addLights()
{
auto s = Director::getInstance()->getWinSize();
_spotLight = Light3D::Create(Light3D::SPOT);
_spotLight->setDirection(Vec3(0.0f, 0.0f, -1.0f));
_spotLight->setColor(Color3B(128, 128, 128));
_spotLight->setPosition(Vec2(s.width/2, s.height/2));
_spotLight->setPositionZ(s.width/2);
_spotLight->setInnerAngle(0.0f);
_spotLight->setOuterAngle(0.4f);
_spotLight->setRange(10000.0f);
_spotLight->retain();
addChild(_spotLight);
}
SpotLightTestDemo::SpotLightTestDemo()
: _spotLight(nullptr)
{
addSprite();
addLights();
scheduleUpdate();
}
SpotLightTestDemo::~SpotLightTestDemo()
{
if (_spotLight)
_spotLight->release();
}
void SpotLightTestDemo::update( float delta )
{
auto s = Director::getInstance()->getWinSize();
static float angleDelta = 0.0;
_spotLight->setDirection(-Vec3(cosf(angleDelta), 0.0f, sinf(angleDelta)));
_spotLight->setPositionX(s.width / 2 + s.width / 4 * cosf(angleDelta));
_spotLight->setPositionY(s.height / 2);
_spotLight->setPositionZ(s.width / 4 * sinf(angleDelta));
angleDelta += delta;
BaseTest::update(delta);
}

View File

@ -31,7 +31,6 @@
class LightTestDemo : public BaseTest class LightTestDemo : public BaseTest
{ {
public: public:
CREATE_FUNC(LightTestDemo);
LightTestDemo(); LightTestDemo();
virtual ~LightTestDemo(); virtual ~LightTestDemo();
@ -44,17 +43,73 @@ public:
virtual void onEnter() override; virtual void onEnter() override;
virtual void onExit() override; virtual void onExit() override;
};
class PointLightTestDemo : public LightTestDemo
{
public:
CREATE_FUNC(PointLightTestDemo);
PointLightTestDemo();
virtual ~PointLightTestDemo();
virtual std::string title() const;
virtual void update(float delta);
private: private:
void addSprite(); void addSprite();
void addLights(); void addLights();
private:
Light3D *_pointLight;
};
class DirectionalLightTestDemo : public LightTestDemo
{
public:
CREATE_FUNC(DirectionalLightTestDemo);
DirectionalLightTestDemo();
virtual ~DirectionalLightTestDemo();
virtual std::string title() const;
virtual void update(float delta);
private:
void addSprite();
void addLights();
private:
Light3D *_directionalLight;
};
class SpotLightTestDemo : public LightTestDemo
{
public:
CREATE_FUNC(SpotLightTestDemo);
SpotLightTestDemo();
virtual ~SpotLightTestDemo();
virtual std::string title() const;
virtual void update(float delta);
private:
void addSprite();
void addLights();
private:
Light3D *_spotLight;
}; };
class LightTestScene : public TestScene class LightTestScene : public TestScene
{ {
public: public:
LightTestScene();
virtual void runThisTest(); virtual void runThisTest();
}; };

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB