mirror of https://github.com/axmolengine/axmol.git
refactor light compile
This commit is contained in:
parent
41caef878b
commit
8c77130ae0
|
@ -124,16 +124,6 @@ void Scene::onProjectionChanged(EventCustom* event)
|
|||
}
|
||||
}
|
||||
|
||||
void Scene::setAmbientColor( const Color4F &color )
|
||||
{
|
||||
_ambientColor = color;
|
||||
}
|
||||
|
||||
const Color4F& Scene::getAmbientColor() const
|
||||
{
|
||||
return _ambientColor;
|
||||
}
|
||||
|
||||
#if CC_USE_PHYSICS
|
||||
void Scene::addChild(Node* child, int zOrder, int tag)
|
||||
{
|
||||
|
|
|
@ -34,7 +34,7 @@ THE SOFTWARE.
|
|||
NS_CC_BEGIN
|
||||
|
||||
class Camera;
|
||||
class Light3D;
|
||||
class BaseLight3D;
|
||||
class EventListenerCustom;
|
||||
class EventCustom;
|
||||
#if CC_USE_PHYSICS
|
||||
|
@ -73,21 +73,7 @@ public:
|
|||
/** get all cameras */
|
||||
const std::vector<Camera*>& getCameras() const { return _cameras; }
|
||||
|
||||
const std::vector<Light3D*>& getLights() const { return _lights; }
|
||||
|
||||
/**
|
||||
* Sets the ambient color of scene.
|
||||
*
|
||||
* @param color The ambient color of scene.
|
||||
*/
|
||||
void setAmbientColor(const Color4F &color);
|
||||
|
||||
/**
|
||||
* Returns the ambient color of scene.
|
||||
*
|
||||
* @return The ambient color of scene.
|
||||
*/
|
||||
const Color4F& getAmbientColor() const;
|
||||
const std::vector<BaseLight3D*>& getLights() const { return _lights; }
|
||||
|
||||
CC_CONSTRUCTOR_ACCESS:
|
||||
Scene();
|
||||
|
@ -104,15 +90,14 @@ protected:
|
|||
friend class SpriteBatchNode;
|
||||
friend class Camera;
|
||||
friend class Director;
|
||||
friend class Light3D;
|
||||
friend class BaseLight3D;
|
||||
friend class Renderer;
|
||||
|
||||
std::vector<Camera*> _cameras; //weak ref to Camera
|
||||
Camera* _defaultCamera; //weak ref, default camera created by scene, _cameras[0], Caution that the default camera can not be added to _cameras before onEnter is called
|
||||
EventListenerCustom* _event;
|
||||
|
||||
std::vector<Light3D *> _lights;
|
||||
Color4F _ambientColor;
|
||||
std::vector<BaseLight3D *> _lights;
|
||||
|
||||
private:
|
||||
CC_DISALLOW_COPY_AND_ASSIGN(Scene);
|
||||
|
|
|
@ -34,6 +34,14 @@ void BaseLight3D::onExit()
|
|||
Node::onExit();
|
||||
}
|
||||
|
||||
void BaseLight3D::setRotationFromDirection( 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));
|
||||
}
|
||||
|
||||
BaseLight3D::BaseLight3D()
|
||||
: _intensity(1.0f)
|
||||
, _enabled(true)
|
||||
|
@ -50,12 +58,16 @@ BaseLight3D::~BaseLight3D()
|
|||
////////////////////////////////////////////////////////////////////
|
||||
DirectionLight3D* DirectionLight3D::create(const Vec3 &direction, const Color3B &color)
|
||||
{
|
||||
|
||||
auto light = new (std::nothrow) DirectionLight3D();
|
||||
light->setRotationFromDirection(direction);
|
||||
light->setColor(color);
|
||||
light->autorelease();
|
||||
return light;
|
||||
}
|
||||
|
||||
void DirectionLight3D::setDirection(const Vec3 &dir)
|
||||
{
|
||||
|
||||
setRotationFromDirection(dir);
|
||||
}
|
||||
const Vec3& DirectionLight3D::getDirection() const
|
||||
{
|
||||
|
@ -83,7 +95,12 @@ DirectionLight3D::~DirectionLight3D()
|
|||
//////////////////////////////////////////////////////////////////
|
||||
PointLight3D* PointLight3D::create(const Vec3 &position, const Color3B &color, float range)
|
||||
{
|
||||
|
||||
auto light = new (std::nothrow) PointLight3D();
|
||||
light->setPosition3D(position);
|
||||
light->setColor(color);
|
||||
light->_range = range;
|
||||
light->autorelease();
|
||||
return light;
|
||||
}
|
||||
|
||||
PointLight3D::PointLight3D()
|
||||
|
@ -98,52 +115,48 @@ PointLight3D::~PointLight3D()
|
|||
//////////////////////////////////////////////////////////////
|
||||
SpotLight3D* SpotLight3D::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
|
||||
{
|
||||
|
||||
auto light = new (std::nothrow) SpotLight3D();
|
||||
light->setRotationFromDirection(direction);
|
||||
light->setPosition3D(position);
|
||||
light->setColor(color);
|
||||
light->setInnerAngle(innerAngle);
|
||||
light->setOuterAngle(outerAngle);
|
||||
light->_range = range;
|
||||
light->autorelease();
|
||||
return light;
|
||||
}
|
||||
|
||||
void SpotLight3D::setDirection(const Vec3 &dir)
|
||||
{
|
||||
|
||||
setRotationFromDirection(dir);
|
||||
}
|
||||
|
||||
const Vec3& SpotLight3D::getDirection() const
|
||||
{
|
||||
|
||||
static Vec3 dir;
|
||||
Mat4 mat = getNodeToParentTransform();
|
||||
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
||||
return dir;
|
||||
}
|
||||
|
||||
const Vec3& SpotLight3D::getDirectionInWorld() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SpotLight3D::setRange(float range)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float SpotLight3D::getRange()
|
||||
{
|
||||
|
||||
static Vec3 dir;
|
||||
Mat4 mat = getNodeToWorldTransform();
|
||||
dir.set(-mat.m[8], -mat.m[9], -mat.m[10]);
|
||||
return dir;
|
||||
}
|
||||
|
||||
void SpotLight3D::setInnerAngle(float angle)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float SpotLight3D::getInnerAngle()
|
||||
{
|
||||
|
||||
_innerAngle = angle;
|
||||
_cosInnerAngle = cosf(angle);
|
||||
}
|
||||
|
||||
void SpotLight3D::setOuterAngle(float angle)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float SpotLight3D::getOuterAngle()
|
||||
{
|
||||
|
||||
_outerAngle = angle;
|
||||
_cosInnerAngle = cosf(angle);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
@ -239,28 +252,28 @@ float Light3D::getOuterAngle()
|
|||
|
||||
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();
|
||||
// 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();
|
||||
// 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 )
|
||||
|
|
|
@ -87,6 +87,9 @@ CC_CONSTRUCTOR_ACCESS:
|
|||
BaseLight3D();
|
||||
virtual ~BaseLight3D();
|
||||
|
||||
protected:
|
||||
void setRotationFromDirection( const Vec3 &direction );
|
||||
|
||||
protected:
|
||||
float _intensity;
|
||||
LightFlag _lightFlag;
|
||||
|
@ -145,9 +148,16 @@ public:
|
|||
*/
|
||||
static PointLight3D* create(const Vec3 &position, const Color3B &color, float range);
|
||||
|
||||
/** get or set range */
|
||||
float getRange() const { return _range; }
|
||||
void setRange(float range) { _range = range; }
|
||||
|
||||
CC_CONSTRUCTOR_ACCESS:
|
||||
PointLight3D();
|
||||
virtual ~PointLight3D();
|
||||
|
||||
protected:
|
||||
float _range;
|
||||
};
|
||||
|
||||
class CC_3D_DLL SpotLight3D : public BaseLight3D
|
||||
|
|
|
@ -32,7 +32,7 @@ THE SOFTWARE.
|
|||
#include <alloca.h>
|
||||
#endif
|
||||
|
||||
#include "3d/CCLight.h"
|
||||
#include "3d/CCLight3D.h"
|
||||
#include "base/CCDirector.h"
|
||||
#include "base/uthash.h"
|
||||
#include "renderer/ccGLStateCache.h"
|
||||
|
@ -969,79 +969,79 @@ 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;
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void GLProgram::reset()
|
||||
|
|
Loading…
Reference in New Issue