2014-05-19 05:49:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "renderer/CCMeshCommand.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-06-24 19:45:51 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-06-24 19:45:51 +08:00
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventType.h"
|
2014-09-25 15:29:32 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2014-10-20 16:25:24 +08:00
|
|
|
#include "2d/CCLight.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCTextureAtlas.h"
|
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-06-11 14:05:35 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-06-23 19:08:26 +08:00
|
|
|
#include "xxhash.h"
|
2014-05-19 05:49:16 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-10-13 15:17:22 +08:00
|
|
|
static const char *s_dirLightUniformColorName = "u_DirLightSourceColor";
|
|
|
|
static std::vector<Vec3> s_dirLightUniformColorValues;
|
|
|
|
static const char *s_dirLightUniformDirName = "u_DirLightSourceDirection";
|
|
|
|
static std::vector<Vec3> s_dirLightUniformDirValues;
|
|
|
|
|
|
|
|
static const char *s_pointLightUniformColorName = "u_PointLightSourceColor";
|
|
|
|
static std::vector<Vec3> s_pointLightUniformColorValues;
|
|
|
|
static const char *s_pointLightUniformPositionName = "u_PointLightSourcePosition";
|
|
|
|
static std::vector<Vec3> s_pointLightUniformPositionValues;
|
|
|
|
static const char *s_pointLightUniformRangeInverseName = "u_PointLightSourceRangeInverse";
|
|
|
|
static std::vector<float> s_pointLightUniformRangeInverseValues;
|
|
|
|
|
|
|
|
static const char *s_spotLightUniformColorName = "u_SpotLightSourceColor";
|
|
|
|
static std::vector<Vec3> s_spotLightUniformColorValues;
|
|
|
|
static const char *s_spotLightUniformPositionName = "u_SpotLightSourcePosition";
|
|
|
|
static std::vector<Vec3> s_spotLightUniformPositionValues;
|
|
|
|
static const char *s_spotLightUniformDirName = "u_SpotLightSourceDirection";
|
|
|
|
static std::vector<Vec3> s_spotLightUniformDirValues;
|
|
|
|
static const char *s_spotLightUniformInnerAngleCosName = "u_SpotLightSourceInnerAngleCos";
|
|
|
|
static std::vector<float> s_spotLightUniformInnerAngleCosValues;
|
|
|
|
static const char *s_spotLightUniformOuterAngleCosName = "u_SpotLightSourceOuterAngleCos";
|
|
|
|
static std::vector<float> s_spotLightUniformOuterAngleCosValues;
|
|
|
|
static const char *s_spotLightUniformRangeInverseName = "u_SpotLightSourceRangeInverse";
|
|
|
|
static std::vector<float> s_spotLightUniformRangeInverseValues;
|
|
|
|
|
|
|
|
static const char *s_ambientLightUniformColorName = "u_AmbientLightSourceColor";
|
2014-09-25 16:18:55 +08:00
|
|
|
|
2014-09-23 17:08:28 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
MeshCommand::MeshCommand()
|
|
|
|
: _textureID(0)
|
|
|
|
, _glProgramState(nullptr)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _blendType(BlendFunc::DISABLE)
|
2014-05-19 05:49:16 +08:00
|
|
|
, _displayColor(1.0f, 1.0f, 1.0f, 1.0f)
|
2014-06-05 16:36:01 +08:00
|
|
|
, _matrixPalette(nullptr)
|
|
|
|
, _matrixPaletteSize(0)
|
2014-06-23 23:58:45 +08:00
|
|
|
, _materialID(0)
|
2014-06-24 19:45:51 +08:00
|
|
|
, _vao(0)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _cullFaceEnabled(false)
|
|
|
|
, _cullFace(GL_BACK)
|
|
|
|
, _depthTestEnabled(false)
|
|
|
|
, _depthWriteEnabled(false)
|
2014-09-22 18:08:58 +08:00
|
|
|
, _lightMask(-1)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
_type = RenderCommand::Type::MESH_COMMAND;
|
2014-11-22 02:00:34 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-07-09 23:03:04 +08:00
|
|
|
// listen the event that renderer was recreated on Android/WP8
|
|
|
|
_rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(MeshCommand::listenRendererRecreated, this));
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_rendererRecreatedListener, -1);
|
2014-06-24 19:45:51 +08:00
|
|
|
#endif
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 06:00:49 +08:00
|
|
|
void MeshCommand::init(float globalZOrder,
|
2014-05-19 05:49:16 +08:00
|
|
|
GLuint textureID,
|
2015-01-16 06:00:49 +08:00
|
|
|
cocos2d::GLProgramState *glProgramState,
|
|
|
|
cocos2d::BlendFunc blendType,
|
2014-05-19 05:49:16 +08:00
|
|
|
GLuint vertexBuffer,
|
|
|
|
GLuint indexBuffer,
|
|
|
|
GLenum primitive,
|
|
|
|
GLenum indexFormat,
|
|
|
|
ssize_t indexCount,
|
2015-01-16 06:00:49 +08:00
|
|
|
const cocos2d::Mat4 &mv,
|
|
|
|
uint32_t flags)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
CCASSERT(glProgramState, "GLProgramState cannot be nill");
|
|
|
|
|
2015-01-16 06:00:49 +08:00
|
|
|
_globalOrder = globalZOrder;
|
2014-05-19 05:49:16 +08:00
|
|
|
_textureID = textureID;
|
|
|
|
_blendType = blendType;
|
|
|
|
_glProgramState = glProgramState;
|
2015-01-16 06:00:49 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
_vertexBuffer = vertexBuffer;
|
|
|
|
_indexBuffer = indexBuffer;
|
|
|
|
_primitive = primitive;
|
|
|
|
_indexFormat = indexFormat;
|
|
|
|
_indexCount = indexCount;
|
2014-08-13 17:38:16 +08:00
|
|
|
_mv.set(mv);
|
2015-01-16 06:00:49 +08:00
|
|
|
|
2015-01-12 14:20:41 +08:00
|
|
|
_is3D = true;
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2015-01-16 06:00:49 +08:00
|
|
|
void MeshCommand::init(float globalOrder,
|
|
|
|
GLuint textureID,
|
|
|
|
GLProgramState* glProgramState,
|
|
|
|
BlendFunc blendType,
|
|
|
|
GLuint vertexBuffer,
|
|
|
|
GLuint indexBuffer,
|
|
|
|
GLenum primitive,
|
|
|
|
GLenum indexFormat,
|
|
|
|
ssize_t indexCount,
|
|
|
|
const Mat4 &mv)
|
|
|
|
{
|
|
|
|
init(globalOrder, textureID, glProgramState, blendType, vertexBuffer, indexBuffer, primitive, indexFormat, indexCount, mv, 0);
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
void MeshCommand::setCullFaceEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_cullFaceEnabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::setCullFace(GLenum cullFace)
|
|
|
|
{
|
|
|
|
_cullFace = cullFace;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::setDepthTestEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_depthTestEnabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::setDepthWriteEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_depthWriteEnabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::setDisplayColor(const Vec4& color)
|
|
|
|
{
|
|
|
|
_displayColor = color;
|
|
|
|
}
|
|
|
|
|
2015-01-08 08:35:08 +08:00
|
|
|
void MeshCommand::setTransparent(bool value)
|
|
|
|
{
|
2015-01-10 06:06:21 +08:00
|
|
|
_isTransparent = value;
|
|
|
|
//Skip batching for transparent mesh
|
|
|
|
_skipBatching = value;
|
2015-01-08 08:35:08 +08:00
|
|
|
setDepthWriteEnabled(!_isTransparent);
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
MeshCommand::~MeshCommand()
|
|
|
|
{
|
2014-06-24 19:45:51 +08:00
|
|
|
releaseVAO();
|
2014-11-22 02:00:34 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-07-09 23:03:04 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_rendererRecreatedListener);
|
2014-06-24 19:45:51 +08:00
|
|
|
#endif
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::applyRenderState()
|
|
|
|
{
|
2015-01-28 08:26:31 +08:00
|
|
|
_renderStateCullFace = glIsEnabled(GL_CULL_FACE);
|
|
|
|
_renderStateDepthTest = glIsEnabled(GL_DEPTH_TEST);
|
|
|
|
glGetBooleanv(GL_DEPTH_WRITEMASK, &_renderStateDepthWrite);
|
|
|
|
|
|
|
|
if (_cullFaceEnabled && !_renderStateCullFace)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
glEnable(GL_CULL_FACE);
|
2014-12-11 14:24:26 +08:00
|
|
|
}
|
2015-01-28 08:26:31 +08:00
|
|
|
|
|
|
|
glCullFace(_cullFace);
|
|
|
|
|
|
|
|
if (_depthTestEnabled && !_renderStateDepthTest)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
}
|
2015-01-28 08:26:31 +08:00
|
|
|
if (_depthWriteEnabled && !_renderStateDepthWrite)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::restoreRenderState()
|
|
|
|
{
|
2015-01-28 08:26:31 +08:00
|
|
|
if (_renderStateCullFace)
|
|
|
|
{
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
}
|
|
|
|
else
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
}
|
2015-01-28 08:26:31 +08:00
|
|
|
|
|
|
|
if (_renderStateDepthTest)
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2015-01-28 08:26:31 +08:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2015-01-28 08:26:31 +08:00
|
|
|
else
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2015-01-28 08:26:31 +08:00
|
|
|
glDisable(GL_DEPTH_TEST);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
2015-01-28 08:26:31 +08:00
|
|
|
|
|
|
|
glDepthMask(_renderStateDepthTest);
|
2014-05-19 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 18:20:19 +08:00
|
|
|
void MeshCommand::genMaterialID(GLuint texID, void* glProgramState, GLuint vertexBuffer, GLuint indexBuffer, const BlendFunc& blend)
|
2014-06-23 19:08:26 +08:00
|
|
|
{
|
2014-09-12 15:43:10 +08:00
|
|
|
int intArray[7] = {0};
|
|
|
|
intArray[0] = (int)texID;
|
|
|
|
*(int**)&intArray[1] = (int*) glProgramState;
|
|
|
|
intArray[3] = (int) vertexBuffer;
|
|
|
|
intArray[4] = (int) indexBuffer;
|
|
|
|
intArray[5] = (int) blend.src;
|
|
|
|
intArray[6] = (int) blend.dst;
|
2014-06-23 19:08:26 +08:00
|
|
|
_materialID = XXH32((const void*)intArray, sizeof(intArray), 0);
|
|
|
|
}
|
|
|
|
|
2014-06-11 14:05:35 +08:00
|
|
|
void MeshCommand::MatrixPalleteCallBack( GLProgram* glProgram, Uniform* uniform)
|
|
|
|
{
|
2014-07-04 16:10:43 +08:00
|
|
|
glUniform4fv( uniform->location, (GLsizei)_matrixPaletteSize, (const float*)_matrixPalette );
|
2014-06-11 14:05:35 +08:00
|
|
|
}
|
|
|
|
|
2014-06-24 19:45:51 +08:00
|
|
|
void MeshCommand::preBatchDraw()
|
2014-05-19 05:49:16 +08:00
|
|
|
{
|
2014-06-23 19:08:26 +08:00
|
|
|
// Set material
|
|
|
|
GL::bindTexture2D(_textureID);
|
|
|
|
GL::blendFunc(_blendType.src, _blendType.dst);
|
2014-06-25 17:36:55 +08:00
|
|
|
|
2014-07-04 10:42:02 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO() && _vao == 0)
|
2014-06-24 19:45:51 +08:00
|
|
|
buildVAO();
|
|
|
|
if (_vao)
|
|
|
|
{
|
|
|
|
GL::bindVAO(_vao);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
_glProgramState->applyAttributes();
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
}
|
2014-06-23 19:08:26 +08:00
|
|
|
}
|
2014-06-24 19:45:51 +08:00
|
|
|
void MeshCommand::batchDraw()
|
2014-06-23 19:08:26 +08:00
|
|
|
{
|
2014-08-13 17:38:16 +08:00
|
|
|
// set render state
|
|
|
|
applyRenderState();
|
|
|
|
|
2014-06-23 23:58:45 +08:00
|
|
|
_glProgramState->setUniformVec4("u_color", _displayColor);
|
2014-05-19 05:49:16 +08:00
|
|
|
|
2014-06-23 19:08:26 +08:00
|
|
|
if (_matrixPaletteSize && _matrixPalette)
|
|
|
|
{
|
2014-06-23 23:58:45 +08:00
|
|
|
_glProgramState->setUniformCallback("u_matrixPalette", CC_CALLBACK_2(MeshCommand::MatrixPalleteCallBack, this));
|
|
|
|
|
2014-06-23 19:08:26 +08:00
|
|
|
}
|
2014-06-23 23:58:45 +08:00
|
|
|
|
2014-06-24 19:45:51 +08:00
|
|
|
_glProgramState->applyGLProgram(_mv);
|
|
|
|
_glProgramState->applyUniforms();
|
2014-10-13 15:17:22 +08:00
|
|
|
|
|
|
|
if (Director::getInstance()->getRunningScene()->getLights().size() > 0)
|
|
|
|
setLightUniforms();
|
2014-06-23 23:58:45 +08:00
|
|
|
|
2014-06-23 19:08:26 +08:00
|
|
|
// Draw
|
|
|
|
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
|
|
|
|
}
|
2014-06-24 19:45:51 +08:00
|
|
|
void MeshCommand::postBatchDraw()
|
2014-06-23 19:08:26 +08:00
|
|
|
{
|
|
|
|
//restore render state
|
|
|
|
restoreRenderState();
|
2014-06-24 19:45:51 +08:00
|
|
|
if (_vao)
|
|
|
|
{
|
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
2014-06-23 19:08:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshCommand::execute()
|
|
|
|
{
|
|
|
|
// set render state
|
|
|
|
applyRenderState();
|
2014-05-19 05:49:16 +08:00
|
|
|
// Set material
|
|
|
|
GL::bindTexture2D(_textureID);
|
|
|
|
GL::blendFunc(_blendType.src, _blendType.dst);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
_glProgramState->setUniformVec4("u_color", _displayColor);
|
2014-06-11 14:05:35 +08:00
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
if (_matrixPaletteSize && _matrixPalette)
|
|
|
|
{
|
2014-06-11 14:05:35 +08:00
|
|
|
_glProgramState->setUniformCallback("u_matrixPalette", CC_CALLBACK_2(MeshCommand::MatrixPalleteCallBack, this));
|
|
|
|
|
2014-06-05 16:36:01 +08:00
|
|
|
}
|
2014-10-13 15:17:22 +08:00
|
|
|
|
|
|
|
_glProgramState->apply(_mv);
|
2014-09-23 17:08:28 +08:00
|
|
|
|
2014-09-25 18:32:33 +08:00
|
|
|
if (Director::getInstance()->getRunningScene()->getLights().size() > 0)
|
|
|
|
setLightUniforms();
|
2014-06-05 16:36:01 +08:00
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
|
|
|
|
|
|
|
|
//restore render state
|
|
|
|
restoreRenderState();
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
2014-06-24 19:45:51 +08:00
|
|
|
void MeshCommand::buildVAO()
|
|
|
|
{
|
|
|
|
releaseVAO();
|
2014-07-04 16:10:43 +08:00
|
|
|
glGenVertexArrays(1, &_vao);
|
|
|
|
GL::bindVAO(_vao);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
auto flags = _glProgramState->getVertexAttribsFlags();
|
|
|
|
for (int i = 0; flags > 0; i++) {
|
|
|
|
int flag = 1 << i;
|
|
|
|
if (flag & flags)
|
|
|
|
glEnableVertexAttribArray(i);
|
|
|
|
flags &= ~flag;
|
2014-06-24 19:45:51 +08:00
|
|
|
}
|
2014-07-04 16:10:43 +08:00
|
|
|
_glProgramState->applyAttributes(false);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
|
|
|
|
GL::bindVAO(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2014-06-24 19:45:51 +08:00
|
|
|
}
|
|
|
|
void MeshCommand::releaseVAO()
|
|
|
|
{
|
2014-07-04 10:42:02 +08:00
|
|
|
if (_vao)
|
2014-06-24 19:45:51 +08:00
|
|
|
{
|
|
|
|
glDeleteVertexArrays(1, &_vao);
|
2014-06-25 17:36:55 +08:00
|
|
|
_vao = 0;
|
2014-06-24 19:45:51 +08:00
|
|
|
GL::bindVAO(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-19 10:54:07 +08:00
|
|
|
|
2014-09-23 17:08:28 +08:00
|
|
|
void MeshCommand::setLightUniforms()
|
2014-09-19 10:54:07 +08:00
|
|
|
{
|
|
|
|
Director *director = Director::getInstance();
|
|
|
|
auto scene = director->getRunningScene();
|
2014-09-25 15:29:32 +08:00
|
|
|
const auto& conf = Configuration::getInstance();
|
|
|
|
int maxDirLight = conf->getMaxSupportDirLightInShader();
|
|
|
|
int maxPointLight = conf->getMaxSupportPointLightInShader();
|
|
|
|
int maxSpotLight = conf->getMaxSupportSpotLightInShader();
|
2014-09-25 18:32:33 +08:00
|
|
|
auto &lights = scene->getLights();
|
2014-10-13 15:17:22 +08:00
|
|
|
auto glProgram = _glProgramState->getGLProgram();
|
2014-09-26 16:46:15 +08:00
|
|
|
if (_glProgramState->getVertexAttribsFlags() & (1 << GLProgram::VERTEX_ATTRIB_NORMAL))
|
2014-09-19 10:54:07 +08:00
|
|
|
{
|
2014-10-13 15:17:22 +08:00
|
|
|
resetLightUniformValues();
|
|
|
|
|
2014-09-19 10:54:07 +08:00
|
|
|
GLint enabledDirLightNum = 0;
|
|
|
|
GLint enabledPointLightNum = 0;
|
|
|
|
GLint enabledSpotLightNum = 0;
|
2014-09-23 17:08:28 +08:00
|
|
|
Vec3 ambientColor;
|
2014-09-25 18:32:33 +08:00
|
|
|
for (const auto& light : lights)
|
2014-09-19 10:54:07 +08:00
|
|
|
{
|
2014-09-26 14:13:26 +08:00
|
|
|
bool useLight = light->isEnabled() && ((unsigned int)light->getLightFlag() & _lightMask);
|
2014-09-23 17:08:28 +08:00
|
|
|
if (useLight)
|
2014-09-19 10:54:07 +08:00
|
|
|
{
|
2014-09-23 17:08:28 +08:00
|
|
|
float intensity = light->getIntensity();
|
|
|
|
switch (light->getLightType())
|
2014-09-19 10:54:07 +08:00
|
|
|
{
|
2014-09-25 18:32:33 +08:00
|
|
|
case LightType::DIRECTIONAL:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-10-13 15:17:22 +08:00
|
|
|
if(enabledDirLightNum < maxDirLight)
|
|
|
|
{
|
|
|
|
auto dirLight = static_cast<DirectionLight *>(light);
|
|
|
|
Vec3 dir = dirLight->getDirectionInWorld();
|
|
|
|
dir.normalize();
|
|
|
|
const Color3B &col = dirLight->getDisplayedColor();
|
|
|
|
s_dirLightUniformColorValues[enabledDirLightNum] = Vec3(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
s_dirLightUniformDirValues[enabledDirLightNum] = dir;
|
|
|
|
++enabledDirLightNum;
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-09-25 18:32:33 +08:00
|
|
|
break;
|
|
|
|
case LightType::POINT:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-10-13 15:17:22 +08:00
|
|
|
if(enabledPointLightNum < maxPointLight)
|
|
|
|
{
|
|
|
|
auto pointLight = static_cast<PointLight *>(light);
|
|
|
|
Mat4 mat= pointLight->getNodeToWorldTransform();
|
|
|
|
const Color3B &col = pointLight->getDisplayedColor();
|
|
|
|
s_pointLightUniformColorValues[enabledPointLightNum] = Vec3(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
s_pointLightUniformPositionValues[enabledPointLightNum] = Vec3(mat.m[12], mat.m[13], mat.m[14]);
|
|
|
|
s_pointLightUniformRangeInverseValues[enabledPointLightNum] = 1.0f / pointLight->getRange();
|
|
|
|
++enabledPointLightNum;
|
|
|
|
}
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-09-25 18:32:33 +08:00
|
|
|
break;
|
|
|
|
case LightType::SPOT:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-10-13 15:17:22 +08:00
|
|
|
if(enabledSpotLightNum < maxSpotLight)
|
|
|
|
{
|
|
|
|
auto spotLight = static_cast<SpotLight *>(light);
|
|
|
|
Vec3 dir = spotLight->getDirectionInWorld();
|
|
|
|
dir.normalize();
|
|
|
|
Mat4 mat= light->getNodeToWorldTransform();
|
|
|
|
const Color3B &col = spotLight->getDisplayedColor();
|
|
|
|
s_spotLightUniformColorValues[enabledSpotLightNum] = Vec3(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
s_spotLightUniformPositionValues[enabledSpotLightNum] = Vec3(mat.m[12], mat.m[13], mat.m[14]);
|
|
|
|
s_spotLightUniformDirValues[enabledSpotLightNum] = dir;
|
|
|
|
s_spotLightUniformInnerAngleCosValues[enabledSpotLightNum] = spotLight->getCosInnerAngle();
|
|
|
|
s_spotLightUniformOuterAngleCosValues[enabledSpotLightNum] = spotLight->getCosOuterAngle();
|
|
|
|
s_spotLightUniformRangeInverseValues[enabledSpotLightNum] = 1.0f / spotLight->getRange();
|
|
|
|
++enabledSpotLightNum;
|
|
|
|
}
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-09-25 18:32:33 +08:00
|
|
|
break;
|
|
|
|
case LightType::AMBIENT:
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-09-28 16:02:12 +08:00
|
|
|
auto ambLight = static_cast<AmbientLight *>(light);
|
2014-09-23 17:08:28 +08:00
|
|
|
const Color3B &col = ambLight->getDisplayedColor();
|
|
|
|
ambientColor += Vec3(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
}
|
2014-09-25 18:32:33 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-09-19 10:54:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 18:32:33 +08:00
|
|
|
|
2014-10-13 15:17:22 +08:00
|
|
|
if (0 < maxDirLight)
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-10-17 11:58:12 +08:00
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_dirLightUniformColorName), (GLfloat*)(&s_dirLightUniformColorValues[0]), (unsigned int)s_dirLightUniformColorValues.size());
|
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_dirLightUniformDirName), (GLfloat*)(&s_dirLightUniformDirValues[0]), (unsigned int)s_dirLightUniformDirValues.size());
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-10-13 15:17:22 +08:00
|
|
|
|
|
|
|
if (0 < maxPointLight)
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-10-17 11:58:12 +08:00
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_pointLightUniformColorName), (GLfloat*)(&s_pointLightUniformColorValues[0]), (unsigned int)s_pointLightUniformColorValues.size());
|
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_pointLightUniformPositionName), (GLfloat*)(&s_pointLightUniformPositionValues[0]), (unsigned int)s_pointLightUniformPositionValues.size());
|
|
|
|
glProgram->setUniformLocationWith1fv((GLint)glProgram->getUniformLocationForName(s_pointLightUniformRangeInverseName), (GLfloat*)(&s_pointLightUniformRangeInverseValues[0]), (unsigned int)s_pointLightUniformRangeInverseValues.size());
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-10-13 15:17:22 +08:00
|
|
|
|
|
|
|
if (0 < maxSpotLight)
|
2014-09-23 17:08:28 +08:00
|
|
|
{
|
2014-10-17 11:58:12 +08:00
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_spotLightUniformColorName), (GLfloat*)(&s_spotLightUniformColorValues[0]), (unsigned int)s_spotLightUniformColorValues.size());
|
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_spotLightUniformPositionName), (GLfloat*)(&s_spotLightUniformPositionValues[0]), (unsigned int)s_spotLightUniformPositionValues.size());
|
|
|
|
glProgram->setUniformLocationWith3fv((GLint)glProgram->getUniformLocationForName(s_spotLightUniformDirName), (GLfloat*)(&s_spotLightUniformDirValues[0]), (unsigned int)s_spotLightUniformDirValues.size());
|
|
|
|
glProgram->setUniformLocationWith1fv((GLint)glProgram->getUniformLocationForName(s_spotLightUniformInnerAngleCosName), (GLfloat*)(&s_spotLightUniformInnerAngleCosValues[0]), (unsigned int)s_spotLightUniformInnerAngleCosValues.size());
|
|
|
|
glProgram->setUniformLocationWith1fv((GLint)glProgram->getUniformLocationForName(s_spotLightUniformOuterAngleCosName), (GLfloat*)(&s_spotLightUniformOuterAngleCosValues[0]), (unsigned int)s_spotLightUniformOuterAngleCosValues.size());
|
|
|
|
glProgram->setUniformLocationWith1fv((GLint)glProgram->getUniformLocationForName(s_spotLightUniformRangeInverseName), (GLfloat*)(&s_spotLightUniformRangeInverseValues[0]), (unsigned int)s_spotLightUniformRangeInverseValues.size());
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-10-13 15:17:22 +08:00
|
|
|
|
|
|
|
glProgram->setUniformLocationWith3f(glProgram->getUniformLocationForName(s_ambientLightUniformColorName), ambientColor.x, ambientColor.y, ambientColor.z);
|
2014-09-19 10:54:07 +08:00
|
|
|
}
|
2014-09-25 18:32:33 +08:00
|
|
|
else // normal does not exist
|
|
|
|
{
|
|
|
|
Vec3 ambient(0.0f, 0.0f, 0.0f);
|
2014-12-03 16:00:52 +08:00
|
|
|
bool hasAmbient = false;
|
2014-09-25 18:32:33 +08:00
|
|
|
for (const auto& light : lights)
|
|
|
|
{
|
|
|
|
if (light->getLightType() == LightType::AMBIENT)
|
|
|
|
{
|
2014-09-26 14:13:26 +08:00
|
|
|
bool useLight = light->isEnabled() && ((unsigned int)light->getLightFlag() & _lightMask);
|
2014-09-25 18:32:33 +08:00
|
|
|
if (useLight)
|
|
|
|
{
|
|
|
|
hasAmbient = true;
|
|
|
|
const Color3B &col = light->getDisplayedColor();
|
|
|
|
ambient.x += col.r * light->getIntensity();
|
|
|
|
ambient.y += col.g * light->getIntensity();
|
|
|
|
ambient.z += col.b * light->getIntensity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasAmbient)
|
|
|
|
{
|
|
|
|
ambient.x /= 255.f; ambient.y /= 255.f; ambient.z /= 255.f;
|
|
|
|
}
|
2014-10-13 15:17:22 +08:00
|
|
|
glProgram->setUniformLocationWith4f(glProgram->getUniformLocationForName("u_color"), _displayColor.x * ambient.x, _displayColor.y * ambient.y, _displayColor.z * ambient.z, _displayColor.w);
|
2014-09-25 18:32:33 +08:00
|
|
|
}
|
2014-09-19 10:54:07 +08:00
|
|
|
}
|
|
|
|
|
2014-10-13 15:17:22 +08:00
|
|
|
void MeshCommand::resetLightUniformValues()
|
2014-09-22 18:08:58 +08:00
|
|
|
{
|
2014-09-25 15:29:32 +08:00
|
|
|
const auto& conf = Configuration::getInstance();
|
|
|
|
int maxDirLight = conf->getMaxSupportDirLightInShader();
|
|
|
|
int maxPointLight = conf->getMaxSupportPointLightInShader();
|
|
|
|
int maxSpotLight = conf->getMaxSupportSpotLightInShader();
|
2014-09-22 18:08:58 +08:00
|
|
|
|
2014-10-13 15:17:22 +08:00
|
|
|
s_dirLightUniformColorValues.assign(maxDirLight, Vec3::ZERO);
|
|
|
|
s_dirLightUniformDirValues.assign(maxDirLight, Vec3::ZERO);
|
2014-09-22 18:08:58 +08:00
|
|
|
|
2014-10-13 15:17:22 +08:00
|
|
|
s_pointLightUniformColorValues.assign(maxPointLight, Vec3::ZERO);
|
|
|
|
s_pointLightUniformPositionValues.assign(maxPointLight, Vec3::ZERO);
|
|
|
|
s_pointLightUniformRangeInverseValues.assign(maxPointLight, 0.0f);
|
|
|
|
|
|
|
|
s_spotLightUniformColorValues.assign(maxSpotLight, Vec3::ZERO);
|
|
|
|
s_spotLightUniformPositionValues.assign(maxSpotLight, Vec3::ZERO);
|
|
|
|
s_spotLightUniformDirValues.assign(maxSpotLight, Vec3::ZERO);
|
|
|
|
s_spotLightUniformInnerAngleCosValues.assign(maxSpotLight, 0.0f);
|
|
|
|
s_spotLightUniformOuterAngleCosValues.assign(maxSpotLight, 0.0f);
|
|
|
|
s_spotLightUniformRangeInverseValues.assign(maxSpotLight, 0.0f);
|
2014-09-23 17:08:28 +08:00
|
|
|
}
|
2014-09-22 18:08:58 +08:00
|
|
|
|
2014-11-22 02:00:34 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-07-09 23:03:04 +08:00
|
|
|
void MeshCommand::listenRendererRecreated(EventCustom* event)
|
2014-06-24 19:45:51 +08:00
|
|
|
{
|
2014-07-04 16:10:43 +08:00
|
|
|
_vao = 0;
|
2014-06-24 19:45:51 +08:00
|
|
|
}
|
2014-09-19 10:54:07 +08:00
|
|
|
|
2014-06-24 19:45:51 +08:00
|
|
|
#endif
|
|
|
|
|
2014-06-19 19:45:24 +08:00
|
|
|
NS_CC_END
|