config max support light number in shader

This commit is contained in:
yangxiao 2014-09-25 15:29:32 +08:00
parent 56313aa895
commit 567c632d98
8 changed files with 106 additions and 53 deletions

View File

@ -92,8 +92,6 @@ DirectionLight3D::~DirectionLight3D()
}
unsigned int DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM = 1;
//////////////////////////////////////////////////////////////////
PointLight3D* PointLight3D::create(const Vec3 &position, const Color3B &color, float range)
{
@ -114,8 +112,6 @@ PointLight3D::~PointLight3D()
}
unsigned int PointLight3D::MAX_POINT_LIGHT_NUM = 1;
//////////////////////////////////////////////////////////////
SpotLight3D* SpotLight3D::create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range)
{
@ -173,8 +169,6 @@ SpotLight3D::~SpotLight3D()
}
unsigned int SpotLight3D::MAX_SPOT_LIGHT_NUM = 1;
/////////////////////////////////////////////////////////////
AmbientLight3D* AmbientLight3D::create( const Color3B &color )

View File

@ -22,8 +22,8 @@
THE SOFTWARE.
****************************************************************************/
#ifndef __CCLIGHT_H__
#define __CCLIGHT_H__
#ifndef __CCLIGHT3D_H__
#define __CCLIGHT3D_H__
#include "2d/CCNode.h"
#include "3d/3dExport.h"
@ -126,10 +126,6 @@ public:
* Returns direction in world.
*/
const Vec3& getDirectionInWorld() const;
public:
static unsigned int MAX_DIRECTIONAL_LIGHT_NUM;
CC_CONSTRUCTOR_ACCESS:
DirectionLight3D();
@ -155,10 +151,6 @@ public:
/** get or set range */
float getRange() const { return _range; }
void setRange(float range) { _range = range; }
public:
static unsigned int MAX_POINT_LIGHT_NUM;
CC_CONSTRUCTOR_ACCESS:
PointLight3D();
@ -245,10 +237,6 @@ public:
/** get cos outAngle */
float getCosOuterAngle() const { return _cosInnerAngle; }
public:
static unsigned int MAX_SPOT_LIGHT_NUM;
CC_CONSTRUCTOR_ACCESS:
SpotLight3D();

View File

@ -47,6 +47,9 @@ Configuration::Configuration()
, _maxSamplesAllowed(0)
, _maxTextureUnits(0)
, _glExtensions(nullptr)
, _maxDirLightInShader(1)
, _maxPointLightInShader(1)
, _maxSpotLightInShader(1)
{
}
@ -244,6 +247,21 @@ bool Configuration::supportsShareableVAO() const
#endif
}
int Configuration::getMaxSupportDirLightInShader() const
{
return _maxDirLightInShader;
}
int Configuration::getMaxSupportPointLightInShader() const
{
return _maxPointLightInShader;
}
int Configuration::getMaxSupportSpotLightInShader() const
{
return _maxSpotLightInShader;
}
//
// generic getters for properties
//
@ -313,6 +331,25 @@ void Configuration::loadConfigFile(const std::string& filename)
else
CCLOG("Key already present. Ignoring '%s'",dataMapIter->first.c_str());
}
//light info
std::string name = "cocos2d.x.3d.max_dir_light_in_shader";
if (_valueDict.find(name) != dataMap.end())
_maxDirLightInShader = _valueDict[name].asInt();
else
_valueDict[name] = Value(_maxDirLightInShader);
name = "cocos2d.x.3d.max_point_light_in_shader";
if (_valueDict.find(name) != dataMap.end())
_maxPointLightInShader = _valueDict[name].asInt();
else
_valueDict[name] = Value(_maxPointLightInShader);
name = "cocos2d.x.3d.max_spot_light_in_shader";
if (_valueDict.find(name) != dataMap.end())
_maxSpotLightInShader = _valueDict[name].asInt();
else
_valueDict[name] = Value(_maxSpotLightInShader);
}
NS_CC_END

View File

@ -110,6 +110,21 @@ public:
@since v2.0.0
*/
bool supportsShareableVAO() const;
/** Max support directional light in shader, for Sprite3D
@since v3.3
*/
int getMaxSupportDirLightInShader() const;
/** Max support point light in shader, for Sprite3D
*since v3.3
*/
int getMaxSupportPointLightInShader() const;
/** Max support spot light in shader, for Sprite3D
*since v3.3
*/
int getMaxSupportSpotLightInShader() const;
/** returns whether or not an OpenGL is supported */
bool checkForGLExtension(const std::string &searchName) const;
@ -150,6 +165,9 @@ protected:
GLint _maxSamplesAllowed;
GLint _maxTextureUnits;
char * _glExtensions;
int _maxDirLightInShader; //max support directional light in shader
int _maxPointLightInShader; // max support point light in shader
int _maxSpotLightInShader; // max support spot light in shader
ValueMap _valueDict;
};

View File

@ -30,6 +30,7 @@ THE SOFTWARE.
#include "renderer/CCGLProgram.h"
#include "renderer/ccShaders.h"
#include "base/ccMacros.h"
#include "base/CCConfiguration.h"
#include "3d/CCLight3D.h"
NS_CC_BEGIN
@ -395,32 +396,20 @@ void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type)
break;
case kShaderType_3DPositionNormal:
{
GLchar def[256];
sprintf(def, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"\n#define MAX_POINT_LIGHT_NUM %d \n"
"\n#define MAX_SPOT_LIGHT_NUM %d \n"
, DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM, PointLight3D::MAX_POINT_LIGHT_NUM, SpotLight3D::MAX_SPOT_LIGHT_NUM);
p->initWithByteArrays((std::string(def) + std::string(cc3D_PositionNormalTex_vert)).c_str(), (std::string(def) + std::string(cc3D_ColorNormal_frag)).c_str());
std::string def = getShaderMacrosForLight();
p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormal_frag)).c_str());
}
break;
case kShaderType_3DPositionNormalTex:
{
GLchar def[256];
sprintf(def, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"\n#define MAX_POINT_LIGHT_NUM %d \n"
"\n#define MAX_SPOT_LIGHT_NUM %d \n"
, DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM, PointLight3D::MAX_POINT_LIGHT_NUM, SpotLight3D::MAX_SPOT_LIGHT_NUM);
p->initWithByteArrays((std::string(def) + std::string(cc3D_PositionNormalTex_vert)).c_str(), (std::string(def) + std::string(cc3D_ColorNormalTex_frag)).c_str());
std::string def = getShaderMacrosForLight();
p->initWithByteArrays((def + std::string(cc3D_PositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
}
break;
case kShaderType_3DSkinPositionNormalTex:
{
GLchar def[256];
sprintf(def, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"\n#define MAX_POINT_LIGHT_NUM %d \n"
"\n#define MAX_SPOT_LIGHT_NUM %d \n"
, DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM, PointLight3D::MAX_POINT_LIGHT_NUM, SpotLight3D::MAX_SPOT_LIGHT_NUM);
p->initWithByteArrays((std::string(def) + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (std::string(def) + std::string(cc3D_ColorNormalTex_frag)).c_str());
std::string def = getShaderMacrosForLight();
p->initWithByteArrays((def + std::string(cc3D_SkinPositionNormalTex_vert)).c_str(), (def + std::string(cc3D_ColorNormalTex_frag)).c_str());
}
break;
default:
@ -450,4 +439,14 @@ void GLProgramCache::addGLProgram(GLProgram* program, const std::string &key)
_programs[key] = program;
}
std::string GLProgramCache::getShaderMacrosForLight() const
{
GLchar def[256];
sprintf(def, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"\n#define MAX_POINT_LIGHT_NUM %d \n"
"\n#define MAX_SPOT_LIGHT_NUM %d \n"
, Configuration::getInstance()->getMaxSupportDirLightInShader(), Configuration::getInstance()->getMaxSupportPointLightInShader(), Configuration::getInstance()->getMaxSupportSpotLightInShader());
return std::string(def);
}
NS_CC_END

View File

@ -92,6 +92,8 @@ public:
private:
bool init();
void loadDefaultGLProgram(GLProgram *program, int type);
std::string getShaderMacrosForLight() const;
// Dictionary* _programs;
std::unordered_map<std::string, GLProgram*> _programs;

View File

@ -30,6 +30,7 @@
#include "base/CCEventListenerCustom.h"
#include "base/CCEventDispatcher.h"
#include "base/CCEventType.h"
#include "base/CCConfiguration.h"
#include "renderer/ccGLStateCache.h"
#include "renderer/CCGLProgramState.h"
#include "renderer/CCRenderer.h"
@ -323,6 +324,10 @@ void MeshCommand::setLightUniforms()
{
Director *director = Director::getInstance();
auto scene = director->getRunningScene();
const auto& conf = Configuration::getInstance();
int maxDirLight = conf->getMaxSupportDirLightInShader();
int maxPointLight = conf->getMaxSupportPointLightInShader();
int maxSpotLight = conf->getMaxSupportSpotLightInShader();
if (scene)
{
auto &lights = scene->getLights();
@ -342,7 +347,7 @@ void MeshCommand::setLightUniforms()
{
case LightType::DIRECTIONAL:
{
CCASSERT(enabledDirLightNum < DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM, "");
CCASSERT(enabledDirLightNum < maxDirLight, "");
DirectionLight3D *dirLight = static_cast<DirectionLight3D *>(light);
Vec3 dir = dirLight->getDirectionInWorld();
dir.normalize();
@ -354,7 +359,7 @@ void MeshCommand::setLightUniforms()
break;
case LightType::POINT:
{
CCASSERT(enabledPointLightNum < PointLight3D::MAX_POINT_LIGHT_NUM, "");
CCASSERT(enabledPointLightNum < maxPointLight, "");
PointLight3D *pointLight = static_cast<PointLight3D *>(light);
Mat4 mat= pointLight->getNodeToWorldTransform();
const Color3B &col = pointLight->getDisplayedColor();
@ -366,7 +371,7 @@ void MeshCommand::setLightUniforms()
break;
case LightType::SPOT:
{
CCASSERT(enabledSpotLightNum < SpotLight3D::MAX_SPOT_LIGHT_NUM, "");
CCASSERT(enabledSpotLightNum < maxSpotLight, "");
SpotLight3D *spotLight = static_cast<SpotLight3D *>(light);
Vec3 dir = spotLight->getDirectionInWorld();
dir.normalize();
@ -394,20 +399,20 @@ void MeshCommand::setLightUniforms()
}
}
for (unsigned short i = enabledDirLightNum; i < DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM; ++i)
for (unsigned short i = enabledDirLightNum; i < maxDirLight; ++i)
{
_glProgramState->setUniformVec3(_dirLightUniformNames[i].color, Vec3::ZERO);
_glProgramState->setUniformVec3(_dirLightUniformNames[i].dir, Vec3::ZERO);
}
for (unsigned short i = enabledPointLightNum; i < PointLight3D::MAX_POINT_LIGHT_NUM; ++i)
for (unsigned short i = enabledPointLightNum; i < maxPointLight; ++i)
{
_glProgramState->setUniformVec3(_pointLightUniformNames[i].color, Vec3::ZERO);
_glProgramState->setUniformVec3(_pointLightUniformNames[i].position, Vec3::ZERO);
_glProgramState->setUniformFloat(_pointLightUniformNames[i].rangeInverse, 0.0f);
}
for (unsigned short i = enabledSpotLightNum; i < SpotLight3D::MAX_SPOT_LIGHT_NUM; ++i)
for (unsigned short i = enabledSpotLightNum; i < maxSpotLight; ++i)
{
_glProgramState->setUniformVec3(_spotLightUniformNames[i].color, Vec3::ZERO);
_glProgramState->setUniformVec3(_spotLightUniformNames[i].position, Vec3::ZERO);
@ -423,11 +428,15 @@ void MeshCommand::setLightUniforms()
void MeshCommand::setLightUniformNames()
{
if (_dirLightUniformNames.size() != DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM)
const auto& conf = Configuration::getInstance();
int maxDirLight = conf->getMaxSupportDirLightInShader();
int maxPointLight = conf->getMaxSupportPointLightInShader();
int maxSpotLight = conf->getMaxSupportSpotLightInShader();
if (_dirLightUniformNames.size() != maxDirLight)
{
_dirLightUniformNames.resize(DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM);
_dirLightUniformNames.resize(maxDirLight);
char str[64];
for (unsigned int i = 0; i < DirectionLight3D::MAX_DIRECTIONAL_LIGHT_NUM; ++i)
for (unsigned int i = 0; i < maxDirLight; ++i)
{
sprintf(str, "u_DirLightSourceColor[%d]", i);
_dirLightUniformNames[i].color = str;
@ -436,11 +445,11 @@ void MeshCommand::setLightUniformNames()
}
}
if (_pointLightUniformNames.size() != PointLight3D::MAX_POINT_LIGHT_NUM)
if (_pointLightUniformNames.size() != maxPointLight)
{
_pointLightUniformNames.resize(PointLight3D::MAX_POINT_LIGHT_NUM);
_pointLightUniformNames.resize(maxPointLight);
char str[64];
for (unsigned int i = 0; i < PointLight3D::MAX_POINT_LIGHT_NUM; ++i)
for (unsigned int i = 0; i < maxPointLight; ++i)
{
sprintf(str, "u_PointLightSourceColor[%d]", i);
_pointLightUniformNames[i].color = str;
@ -451,11 +460,11 @@ void MeshCommand::setLightUniformNames()
}
}
if (_spotLightUniformNames.size() != SpotLight3D::MAX_SPOT_LIGHT_NUM)
if (_spotLightUniformNames.size() != maxSpotLight)
{
_spotLightUniformNames.resize(SpotLight3D::MAX_SPOT_LIGHT_NUM);
_spotLightUniformNames.resize(maxSpotLight);
char str[64];
for (unsigned int i = 0; i < SpotLight3D::MAX_SPOT_LIGHT_NUM; ++i)
for (unsigned int i = 0; i < maxSpotLight; ++i)
{
sprintf(str, "u_SpotLightSourceColor[%d]", i);
_spotLightUniformNames[i].color = str;

View File

@ -16,6 +16,12 @@
<false/>
<key>cocos2d.x.testcpp.autorun</key>
<false/>
<key>cocos2d.x.3d.max_dir_light_in_shader</key>
<integer>1</integer>
<key>cocos2d.x.3d.max_point_light_in_shader</key>
<integer>1</integer>
<key>cocos2d.x.3d.max_spot_light_in_shader</key>
<integer>1</integer>
</dict>
<key>metadata</key>
<dict>