mirror of https://github.com/axmolengine/axmol.git
Finished Point Light
Finished Directional Light Finished Spot Light
This commit is contained in:
parent
9b3a02f350
commit
10afb9b9af
|
@ -4,8 +4,7 @@
|
|||
NS_CC_BEGIN
|
||||
|
||||
Light3D::Light3D()
|
||||
: _isEnabled(false)
|
||||
, _range(0.0f)
|
||||
: _range(0.0f)
|
||||
, _innerAngle(0.0f)
|
||||
, _outerAngle(0.0f)
|
||||
{
|
||||
|
@ -35,17 +34,6 @@ Light3D::LightType Light3D::getLightType()
|
|||
return _lightType;
|
||||
}
|
||||
|
||||
void Light3D::setEnabled( bool isEnabled )
|
||||
{
|
||||
_isEnabled = isEnabled;
|
||||
}
|
||||
|
||||
bool Light3D::getEnabled()
|
||||
{
|
||||
return _isEnabled;
|
||||
}
|
||||
|
||||
|
||||
void Light3D::setRange( float range )
|
||||
{
|
||||
_range = range;
|
||||
|
@ -91,7 +79,7 @@ void Light3D::onEnter()
|
|||
auto scene = getScene();
|
||||
if (scene)
|
||||
{
|
||||
auto lights = scene->_lights;
|
||||
auto &lights = scene->_lights;
|
||||
auto iter = std::find(lights.begin(), lights.end(), this);
|
||||
if (iter == lights.end())
|
||||
lights.push_back(this);
|
||||
|
@ -103,7 +91,7 @@ void Light3D::onExit()
|
|||
auto scene = getScene();
|
||||
if (scene)
|
||||
{
|
||||
auto lights = scene->_lights;
|
||||
auto &lights = scene->_lights;
|
||||
auto iter = std::find(lights.begin(), lights.end(), this);
|
||||
if (iter != lights.end())
|
||||
lights.erase(iter);
|
||||
|
|
|
@ -59,16 +59,6 @@ public:
|
|||
*/
|
||||
LightType getLightType();
|
||||
|
||||
/**
|
||||
* Sets light enabled.
|
||||
*/
|
||||
void setEnabled(bool isEnabled);
|
||||
|
||||
/**
|
||||
* Gets light enabled.
|
||||
*/
|
||||
bool getEnabled();
|
||||
|
||||
/**
|
||||
* Sets the range of point or spot light.
|
||||
*
|
||||
|
@ -133,7 +123,6 @@ CC_CONSTRUCTOR_ACCESS:
|
|||
protected:
|
||||
|
||||
LightType _lightType;
|
||||
bool _isEnabled;
|
||||
Vec3 _dir;
|
||||
float _range;
|
||||
float _innerAngle;
|
||||
|
|
|
@ -80,7 +80,8 @@ const char* GLProgram::SHADER_3D_SKINPOSITION_TEXTURE = "Shader3DSkinPositionTex
|
|||
|
||||
|
||||
// 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_MV_MATRIX = "CC_MVMatrix";
|
||||
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"
|
||||
"struct LightSource \n"
|
||||
"{ \n"
|
||||
" vec4 color; \n"
|
||||
" vec3 position; \n"
|
||||
" vec3 direction; \n"
|
||||
" float type; \n"
|
||||
" float range; \n"
|
||||
" float range2; \n"
|
||||
" float innerAngle; \n"
|
||||
" float outerAngle; \n"
|
||||
" float type; \n"
|
||||
" float use; \n"
|
||||
" vec4 color; \n"
|
||||
" vec3 position; \n"
|
||||
" vec3 direction; \n"
|
||||
"}; \n"
|
||||
"uniform LightSource CC_LightSource[CC_MAX_LIGHT];\n"
|
||||
"uniform int CC_EnabledLightNum; \n"
|
||||
"uniform vec4 CC_AmbientColor; \n"
|
||||
};
|
||||
|
||||
const GLchar *sources[] = {
|
||||
|
@ -498,7 +500,8 @@ void GLProgram::bindAttribLocation(const std::string &attributeName, GLuint inde
|
|||
|
||||
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_MV_MATRIX] = glGetUniformLocation(_program, UNIFORM_NAME_MV_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_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.usesMV = _builtInUniforms[UNIFORM_MV_MATRIX] != -1;
|
||||
_flags.usesMVP = _builtInUniforms[UNIFORM_MVP_MATRIX] != -1;
|
||||
|
@ -931,31 +934,38 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV)
|
|||
auto scene = director->getRunningScene();
|
||||
if (scene)
|
||||
{
|
||||
auto lights = scene->getLights();
|
||||
auto &lights = scene->getLights();
|
||||
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;
|
||||
for (auto iter : lights)
|
||||
char str[32];
|
||||
for (unsigned int i = 0; i < lights.size(); ++i)
|
||||
{
|
||||
const Color3B &col = iter->getColor();
|
||||
const Vec3 &pos = iter->getPosition3D();
|
||||
const Vec3 &dir = iter->getDirection();
|
||||
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;
|
||||
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;
|
||||
lightSources[10 + idx] = iter->getRange();
|
||||
lightSources[11 + idx] = iter->getRange() * iter->getRange();
|
||||
lightSources[12 + idx] = iter->getInnerAngle();
|
||||
lightSources[13 + idx] = iter->getOuterAngle();
|
||||
lightSources[14 + idx] = static_cast<float>(iter->getLightType());
|
||||
lightSources[15 + idx] = static_cast<float>(iter->getEnabled());
|
||||
idx += 16;
|
||||
Light3D *light = lights[i];
|
||||
const Color3B &col = light->getColor();
|
||||
const Vec3 &pos = light->getPosition3D();
|
||||
const Vec3 &dir = light->getDirection();
|
||||
|
||||
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "type");
|
||||
setUniformLocationWith1f(glGetUniformLocation(_program, str), static_cast<float>(light->getLightType()));
|
||||
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "range");
|
||||
setUniformLocationWith1f(glGetUniformLocation(_program, str), light->getRange());
|
||||
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "range2");
|
||||
setUniformLocationWith1f(glGetUniformLocation(_program, str), light->getRange() * light->getRange());
|
||||
sprintf_s(str, 32, "CC_LightSource[%d].%s", i, "innerAngle");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -96,7 +96,8 @@ public:
|
|||
|
||||
enum
|
||||
{
|
||||
UNIFORM_LIGHT_SOURCE,
|
||||
UNIFORM_ENABLED_LIGHT_NUM,
|
||||
UNIFORM_AMBIENT_COLOR,
|
||||
UNIFORM_P_MATRIX,
|
||||
UNIFORM_MV_MATRIX,
|
||||
UNIFORM_MVP_MATRIX,
|
||||
|
@ -137,7 +138,8 @@ public:
|
|||
static const char* SHADER_3D_SKINPOSITION_TEXTURE;
|
||||
|
||||
// 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_MV_MATRIX;
|
||||
static const char* UNIFORM_NAME_MVP_MATRIX;
|
||||
|
|
|
@ -7,9 +7,59 @@ varying lowp vec4 DestinationColor;
|
|||
varying vec4 DestinationColor;
|
||||
\n#endif\n
|
||||
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)
|
||||
{
|
||||
gl_FragColor = u_color;
|
||||
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;
|
||||
else
|
||||
gl_FragColor = u_color * (CC_AmbientColor + intensity);
|
||||
}
|
||||
);
|
||||
|
|
|
@ -3,18 +3,63 @@ const char* cc3D_ColorTex_frag = STRINGIFY(
|
|||
|
||||
\n#ifdef GL_ES\n
|
||||
varying mediump vec2 TextureCoordOut;
|
||||
varying mediump vec4 LightIntensityOut;
|
||||
\n#else\n
|
||||
varying vec2 TextureCoordOut;
|
||||
varying vec4 LightIntensityOut;
|
||||
\n#endif\n
|
||||
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)
|
||||
{
|
||||
if (LightIntensityOut.w == 0.0)
|
||||
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color;
|
||||
else
|
||||
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color * LightIntensityOut;
|
||||
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;
|
||||
else
|
||||
gl_FragColor = texture2D(CC_Texture0, TextureCoordOut) * u_color * (CC_AmbientColor + intensity);
|
||||
}
|
||||
);
|
||||
|
|
|
@ -5,37 +5,16 @@ attribute vec4 a_position;
|
|||
attribute vec2 a_texCoord;
|
||||
attribute vec3 a_normal;
|
||||
varying vec2 TextureCoordOut;
|
||||
varying vec4 LightIntensityOut;
|
||||
|
||||
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;
|
||||
}
|
||||
varying vec4 ePosition;
|
||||
varying vec3 eNormal;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
vec4 ePosition = CC_MVMatrix * a_position;
|
||||
vec3 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;
|
||||
|
||||
ePosition = CC_MVMatrix * a_position;
|
||||
eNormal = CC_NormalMatrix * a_normal;
|
||||
TextureCoordOut = a_texCoord;
|
||||
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
|
||||
varying vec2 TextureCoordOut;
|
||||
varying vec4 LightIntensityOut;
|
||||
|
||||
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;
|
||||
}
|
||||
varying vec4 ePosition;
|
||||
varying vec3 eNormal;
|
||||
|
||||
void getPositionAndNormal(out vec4 position, out vec3 normal)
|
||||
{
|
||||
|
@ -117,8 +83,8 @@ void getPositionAndNormal(out vec4 position, out vec3 normal)
|
|||
position.z = dot(p, matrixPalette3);
|
||||
position.w = p.w;
|
||||
|
||||
vec4 n = vec4(a_normal, 0.0);
|
||||
normal.x = dot(n, matrixPalette1);
|
||||
vec4 n = vec4(a_normal, 0.0);
|
||||
normal.x = dot(n, matrixPalette1);
|
||||
normal.y = dot(n, matrixPalette2);
|
||||
normal.z = dot(n, matrixPalette3);
|
||||
}
|
||||
|
@ -126,21 +92,14 @@ void getPositionAndNormal(out vec4 position, out vec3 normal)
|
|||
void main()
|
||||
{
|
||||
vec4 position;
|
||||
vec3 normal;
|
||||
getPositionAndNormal(position, normal);
|
||||
|
||||
vec4 ePosition = CC_MVMatrix * position;
|
||||
vec3 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;
|
||||
vec3 normal;
|
||||
getPositionAndNormal(position, normal);
|
||||
|
||||
ePosition = CC_MVMatrix * position;
|
||||
eNormal = CC_NormalMatrix * normal;
|
||||
TextureCoordOut = a_texCoord;
|
||||
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
|
||||
gl_Position = CC_PMatrix * ePosition;
|
||||
gl_Position = CC_PMatrix * ePosition;
|
||||
}
|
||||
|
||||
);
|
|
@ -6,41 +6,41 @@ static int sceneIdx = -1;
|
|||
|
||||
static std::function<Layer*()> createFunctions[] =
|
||||
{
|
||||
CL(LightTestDemo)
|
||||
CL(DirectionalLightTestDemo),
|
||||
CL(PointLightTestDemo),
|
||||
CL(SpotLightTestDemo)
|
||||
};
|
||||
|
||||
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
||||
|
||||
static Layer* nextSpriteTestAction()
|
||||
{
|
||||
sceneIdx++;
|
||||
sceneIdx = sceneIdx % MAX_LAYER;
|
||||
sceneIdx++;
|
||||
sceneIdx = sceneIdx % MAX_LAYER;
|
||||
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
}
|
||||
|
||||
static Layer* backSpriteTestAction()
|
||||
{
|
||||
sceneIdx--;
|
||||
int total = MAX_LAYER;
|
||||
if( sceneIdx < 0 )
|
||||
sceneIdx += total;
|
||||
sceneIdx--;
|
||||
int total = MAX_LAYER;
|
||||
if( sceneIdx < 0 )
|
||||
sceneIdx += total;
|
||||
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
}
|
||||
|
||||
static Layer* restartSpriteTestAction()
|
||||
{
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
auto layer = (createFunctions[sceneIdx])();
|
||||
return layer;
|
||||
}
|
||||
|
||||
LightTestDemo::LightTestDemo()
|
||||
{
|
||||
addSprite();
|
||||
addLights();
|
||||
}
|
||||
|
||||
|
||||
|
@ -50,85 +50,218 @@ LightTestDemo::~LightTestDemo()
|
|||
|
||||
std::string LightTestDemo::title() const
|
||||
{
|
||||
return "Point Light";
|
||||
return "Point Light";
|
||||
}
|
||||
|
||||
std::string LightTestDemo::subtitle() const
|
||||
{
|
||||
return "";
|
||||
return "";
|
||||
}
|
||||
|
||||
void LightTestDemo::restartCallback( Ref* sender )
|
||||
{
|
||||
auto s = new LightTestScene();
|
||||
s->addChild(restartSpriteTestAction());
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
auto s = new LightTestScene();
|
||||
s->addChild(restartSpriteTestAction());
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
}
|
||||
|
||||
void LightTestDemo::nextCallback( Ref* sender )
|
||||
{
|
||||
auto s = new LightTestScene();
|
||||
s->addChild( nextSpriteTestAction() );
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
auto s = new LightTestScene();
|
||||
s->addChild( nextSpriteTestAction() );
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
}
|
||||
|
||||
void LightTestDemo::backCallback( Ref* sender )
|
||||
{
|
||||
auto s = new LightTestScene();
|
||||
s->addChild( backSpriteTestAction() );
|
||||
Director::getInstance()->replaceScene(s);
|
||||
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));
|
||||
}
|
||||
auto s = new LightTestScene();
|
||||
s->addChild( backSpriteTestAction() );
|
||||
Director::getInstance()->replaceScene(s);
|
||||
s->release();
|
||||
}
|
||||
|
||||
void LightTestDemo::onEnter()
|
||||
{
|
||||
BaseTest::onEnter();
|
||||
BaseTest::onEnter();
|
||||
}
|
||||
|
||||
void LightTestDemo::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);
|
||||
BaseTest::onExit();
|
||||
}
|
||||
|
||||
void LightTestScene::runThisTest()
|
||||
{
|
||||
auto layer = nextSpriteTestAction();
|
||||
addChild(layer);
|
||||
auto layer = nextSpriteTestAction();
|
||||
addChild(layer);
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -31,31 +31,86 @@
|
|||
class LightTestDemo : public BaseTest
|
||||
{
|
||||
public:
|
||||
CREATE_FUNC(LightTestDemo);
|
||||
LightTestDemo();
|
||||
virtual ~LightTestDemo();
|
||||
LightTestDemo();
|
||||
virtual ~LightTestDemo();
|
||||
|
||||
virtual std::string title() const;
|
||||
virtual std::string subtitle() const;
|
||||
virtual std::string title() const;
|
||||
virtual std::string subtitle() const;
|
||||
|
||||
virtual void restartCallback(Ref* sender);
|
||||
virtual void nextCallback(Ref* sender);
|
||||
virtual void backCallback(Ref* sender);
|
||||
virtual void restartCallback(Ref* sender);
|
||||
virtual void nextCallback(Ref* sender);
|
||||
virtual void backCallback(Ref* sender);
|
||||
|
||||
virtual void onEnter() override;
|
||||
virtual void onExit() override;
|
||||
virtual void onEnter() 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:
|
||||
|
||||
void addSprite();
|
||||
void addLights();
|
||||
void addSprite();
|
||||
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
|
||||
{
|
||||
public:
|
||||
virtual void runThisTest();
|
||||
LightTestScene();
|
||||
virtual void runThisTest();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
Loading…
Reference in New Issue