2014-05-19 05:49:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 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-22 13:52:03 +08:00
|
|
|
#include "3d/CCMesh.h"
|
|
|
|
#include "3d/CCMeshSkin.h"
|
2014-08-28 17:32:23 +08:00
|
|
|
#include "3d/CCSkeleton3D.h"
|
2014-08-22 13:52:03 +08:00
|
|
|
#include "3d/CCMeshVertexIndexData.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "2d/CCLight.h"
|
|
|
|
#include "2d/CCScene.h"
|
2014-06-16 20:58:13 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "base/CCDirector.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
2014-08-22 13:52:03 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-08-28 17:32:23 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "renderer/CCMaterial.h"
|
|
|
|
#include "renderer/CCTechnique.h"
|
|
|
|
#include "renderer/CCPass.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2015-05-13 10:58:09 +08:00
|
|
|
#include "renderer/CCVertexAttribBinding.h"
|
2015-05-06 04:07:32 +08:00
|
|
|
#include "math/Mat4.h"
|
|
|
|
|
2014-05-19 05:49:16 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
// Helpers
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
static void resetLightUniformValues()
|
|
|
|
{
|
|
|
|
const auto& conf = Configuration::getInstance();
|
|
|
|
int maxDirLight = conf->getMaxSupportDirLightInShader();
|
|
|
|
int maxPointLight = conf->getMaxSupportPointLightInShader();
|
|
|
|
int maxSpotLight = conf->getMaxSupportSpotLightInShader();
|
|
|
|
|
|
|
|
s_dirLightUniformColorValues.assign(maxDirLight, Vec3::ZERO);
|
|
|
|
s_dirLightUniformDirValues.assign(maxDirLight, Vec3::ZERO);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Generate a dummy texture when the texture file is missing
|
|
|
|
static Texture2D * getDummyTexture()
|
|
|
|
{
|
|
|
|
auto texture = Director::getInstance()->getTextureCache()->getTextureForKey("/dummyTexture");
|
|
|
|
if(!texture)
|
|
|
|
{
|
|
|
|
#ifdef NDEBUG
|
|
|
|
unsigned char data[] ={0,0,0,0};//1*1 transparent picture
|
|
|
|
#else
|
|
|
|
unsigned char data[] ={255,0,0,255};//1*1 red picture
|
|
|
|
#endif
|
|
|
|
Image * image =new (std::nothrow) Image();
|
|
|
|
image->initWithRawData(data,sizeof(data),1,1,sizeof(unsigned char));
|
|
|
|
texture=Director::getInstance()->getTextureCache()->addImage(image,"/dummyTexture");
|
|
|
|
image->release();
|
|
|
|
}
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
Mesh::Mesh()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _texture(nullptr)
|
2014-08-22 13:52:03 +08:00
|
|
|
, _skin(nullptr)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _visible(true)
|
|
|
|
, _isTransparent(false)
|
2014-08-22 13:52:03 +08:00
|
|
|
, _meshIndexData(nullptr)
|
2015-05-06 04:07:32 +08:00
|
|
|
, _material(nullptr)
|
2014-08-22 13:52:03 +08:00
|
|
|
, _glProgramState(nullptr)
|
|
|
|
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _visibleChanged(nullptr)
|
2015-05-06 04:07:32 +08:00
|
|
|
, _blendDirty(true)
|
2015-07-01 14:24:22 +08:00
|
|
|
, _force2DQueue(false)
|
2014-08-22 13:52:03 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
Mesh::~Mesh()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
CC_SAFE_RELEASE(_skin);
|
|
|
|
CC_SAFE_RELEASE(_meshIndexData);
|
2015-05-06 04:07:32 +08:00
|
|
|
CC_SAFE_RELEASE(_material);
|
2014-08-22 13:52:03 +08:00
|
|
|
CC_SAFE_RELEASE(_glProgramState);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint Mesh::getVertexBuffer() const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getVertexBuffer()->getVBO();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mesh::hasVertexAttrib(int attrib) const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getMeshVertexData()->hasVertexAttrib(attrib);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Mesh::getMeshVertexAttribCount() const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getMeshVertexData()->getMeshVertexAttribCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
const MeshVertexAttrib& Mesh::getMeshVertexAttribute(int idx)
|
|
|
|
{
|
|
|
|
return _meshIndexData->getMeshVertexData()->getMeshVertexAttrib(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Mesh::getVertexSizeInBytes() const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getVertexBuffer()->getSizePerVertex();
|
|
|
|
}
|
|
|
|
|
|
|
|
Mesh* Mesh::create(const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<float>& texs, const IndexArray& indices)
|
|
|
|
{
|
|
|
|
int perVertexSizeInFloat = 0;
|
|
|
|
std::vector<float> vertices;
|
|
|
|
std::vector<MeshVertexAttrib> attribs;
|
|
|
|
MeshVertexAttrib att;
|
|
|
|
att.size = 3;
|
|
|
|
att.type = GL_FLOAT;
|
|
|
|
att.attribSizeBytes = att.size * sizeof(float);
|
|
|
|
|
|
|
|
if (positions.size())
|
|
|
|
{
|
|
|
|
perVertexSizeInFloat += 3;
|
|
|
|
att.vertexAttrib = GLProgram::VERTEX_ATTRIB_POSITION;
|
|
|
|
attribs.push_back(att);
|
|
|
|
}
|
|
|
|
if (normals.size())
|
|
|
|
{
|
|
|
|
perVertexSizeInFloat += 3;
|
|
|
|
att.vertexAttrib = GLProgram::VERTEX_ATTRIB_NORMAL;
|
|
|
|
attribs.push_back(att);
|
|
|
|
}
|
|
|
|
if (texs.size())
|
|
|
|
{
|
|
|
|
perVertexSizeInFloat += 2;
|
|
|
|
att.vertexAttrib = GLProgram::VERTEX_ATTRIB_TEX_COORD;
|
|
|
|
att.size = 2;
|
|
|
|
att.attribSizeBytes = att.size * sizeof(float);
|
|
|
|
attribs.push_back(att);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasNormal = (normals.size() != 0);
|
|
|
|
bool hasTexCoord = (texs.size() != 0);
|
|
|
|
//position, normal, texCoordinate into _vertexs
|
|
|
|
size_t vertexNum = positions.size() / 3;
|
|
|
|
for(size_t i = 0; i < vertexNum; i++)
|
|
|
|
{
|
|
|
|
vertices.push_back(positions[i * 3]);
|
|
|
|
vertices.push_back(positions[i * 3 + 1]);
|
|
|
|
vertices.push_back(positions[i * 3 + 2]);
|
|
|
|
|
|
|
|
if (hasNormal)
|
|
|
|
{
|
|
|
|
vertices.push_back(normals[i * 3]);
|
|
|
|
vertices.push_back(normals[i * 3 + 1]);
|
|
|
|
vertices.push_back(normals[i * 3 + 2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasTexCoord)
|
|
|
|
{
|
|
|
|
vertices.push_back(texs[i * 2]);
|
|
|
|
vertices.push_back(texs[i * 2 + 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return create(vertices, perVertexSizeInFloat, indices, attribs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Mesh* Mesh::create(const std::vector<float>& vertices, int perVertexSizeInFloat, const IndexArray& indices, const std::vector<MeshVertexAttrib>& attribs)
|
|
|
|
{
|
|
|
|
MeshData meshdata;
|
|
|
|
meshdata.attribs = attribs;
|
|
|
|
meshdata.vertex = vertices;
|
|
|
|
meshdata.subMeshIndices.push_back(indices);
|
|
|
|
meshdata.subMeshIds.push_back("");
|
|
|
|
auto meshvertexdata = MeshVertexData::create(meshdata);
|
2014-09-04 14:26:23 +08:00
|
|
|
auto indexData = meshvertexdata->getMeshIndexDataByIndex(0);
|
2014-08-22 13:52:03 +08:00
|
|
|
|
|
|
|
return create("", indexData);
|
|
|
|
}
|
|
|
|
|
|
|
|
Mesh* Mesh::create(const std::string& name, MeshIndexData* indexData, MeshSkin* skin)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto state = new (std::nothrow) Mesh();
|
2014-08-22 13:52:03 +08:00
|
|
|
state->autorelease();
|
|
|
|
state->bindMeshCommand();
|
|
|
|
state->_name = name;
|
|
|
|
state->setMeshIndexData(indexData);
|
|
|
|
state->setSkin(skin);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
if (_visible != visible)
|
|
|
|
{
|
|
|
|
_visible = visible;
|
|
|
|
if (_visibleChanged)
|
|
|
|
_visibleChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
bool Mesh::isVisible() const
|
|
|
|
{
|
|
|
|
return _visible;
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
void Mesh::setTexture(const std::string& texPath)
|
|
|
|
{
|
|
|
|
auto tex = Director::getInstance()->getTextureCache()->addImage(texPath);
|
|
|
|
setTexture(tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setTexture(Texture2D* tex)
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
// Texture must be saved for future use
|
|
|
|
// it doesn't matter if the material is already set or not
|
|
|
|
// This functionality is added for compatibility issues
|
2015-07-08 17:00:01 +08:00
|
|
|
if (tex == nullptr)
|
|
|
|
tex = getDummyTexture();
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
if (tex != _texture)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(tex);
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
_texture = tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_material) {
|
|
|
|
auto technique = _material->_currentTechnique;
|
|
|
|
for(auto& pass: technique->_passes)
|
|
|
|
{
|
2015-06-06 05:53:30 +08:00
|
|
|
// FIXME: Ideally it should use glProgramState->setUniformTexture()
|
|
|
|
// and set CC_Texture0 that way. But trying to it, will trigger
|
|
|
|
// another bug
|
2015-05-06 04:07:32 +08:00
|
|
|
pass->setTexture(tex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bindMeshCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* Mesh::getTexture() const
|
|
|
|
{
|
|
|
|
return _texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setMaterial(Material* material)
|
|
|
|
{
|
|
|
|
if (_material != material) {
|
|
|
|
CC_SAFE_RELEASE(_material);
|
|
|
|
_material = material;
|
|
|
|
CC_SAFE_RETAIN(_material);
|
|
|
|
}
|
2015-05-13 10:58:09 +08:00
|
|
|
|
|
|
|
if (_material)
|
|
|
|
{
|
|
|
|
for (auto technique: _material->getTechniques())
|
|
|
|
{
|
|
|
|
for (auto pass: technique->getPasses())
|
|
|
|
{
|
|
|
|
auto vertexAttribBinding = VertexAttribBinding::create(_meshIndexData, pass->getGLProgramState());
|
|
|
|
pass->setVertexAttribBinding(vertexAttribBinding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Material* Mesh::getMaterial() const
|
|
|
|
{
|
|
|
|
return _material;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::draw(Renderer* renderer, float globalZOrder, const Mat4& transform, uint32_t flags, unsigned int lightMask, const Vec4& color, bool forceDepthWrite)
|
|
|
|
{
|
|
|
|
if (! isVisible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool isTransparent = (_isTransparent || color.w < 1.f);
|
|
|
|
float globalZ = isTransparent ? 0 : globalZOrder;
|
|
|
|
if (isTransparent)
|
|
|
|
flags |= Node::FLAGS_RENDER_AS_3D;
|
|
|
|
|
|
|
|
|
|
|
|
_meshCommand.init(globalZ,
|
|
|
|
_material,
|
|
|
|
getVertexBuffer(),
|
|
|
|
getIndexBuffer(),
|
|
|
|
getPrimitiveType(),
|
|
|
|
getIndexFormat(),
|
|
|
|
getIndexCount(),
|
|
|
|
transform,
|
|
|
|
flags);
|
|
|
|
|
|
|
|
|
|
|
|
if (isTransparent && !forceDepthWrite)
|
|
|
|
_material->getStateBlock()->setDepthWrite(false);
|
|
|
|
else
|
|
|
|
_material->getStateBlock()->setDepthWrite(true);
|
|
|
|
|
|
|
|
|
|
|
|
_meshCommand.setSkipBatching(isTransparent);
|
2015-06-05 16:25:09 +08:00
|
|
|
_meshCommand.setTransparent(isTransparent);
|
2015-07-01 14:24:22 +08:00
|
|
|
_meshCommand.set3D(!_force2DQueue);
|
2015-08-28 23:00:45 +08:00
|
|
|
_material->getStateBlock()->setBlend(_force2DQueue || isTransparent);
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
// set default uniforms for Mesh
|
|
|
|
// 'u_color' and others
|
2015-05-14 08:39:39 +08:00
|
|
|
const auto scene = Director::getInstance()->getRunningScene();
|
2015-05-06 04:07:32 +08:00
|
|
|
auto technique = _material->_currentTechnique;
|
2015-05-14 08:39:39 +08:00
|
|
|
for(const auto pass : technique->_passes)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto programState = pass->getGLProgramState();
|
|
|
|
programState->setUniformVec4("u_color", color);
|
|
|
|
|
|
|
|
if (_skin)
|
2015-05-13 13:24:52 +08:00
|
|
|
programState->setUniformVec4v("u_matrixPalette", (GLsizei)_skin->getMatrixPaletteSize(), _skin->getMatrixPalette());
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
if (scene && scene->getLights().size() > 0)
|
2015-05-13 10:58:09 +08:00
|
|
|
setLightUniforms(pass, scene, color, lightMask);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderer->addCommand(&_meshCommand);
|
2014-08-22 13:52:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setSkin(MeshSkin* skin)
|
|
|
|
{
|
|
|
|
if (_skin != skin)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(skin);
|
|
|
|
CC_SAFE_RELEASE(_skin);
|
|
|
|
_skin = skin;
|
2015-03-20 17:20:22 +08:00
|
|
|
calculateAABB();
|
2014-08-22 13:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setMeshIndexData(MeshIndexData* subMesh)
|
|
|
|
{
|
|
|
|
if (_meshIndexData != subMesh)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(subMesh);
|
|
|
|
CC_SAFE_RELEASE(_meshIndexData);
|
|
|
|
_meshIndexData = subMesh;
|
2015-03-20 17:20:22 +08:00
|
|
|
calculateAABB();
|
2014-08-22 13:52:03 +08:00
|
|
|
bindMeshCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setGLProgramState(GLProgramState* glProgramState)
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
// XXX create dummy texture
|
|
|
|
auto material = Material::createWithGLStateProgram(glProgramState);
|
|
|
|
setMaterial(material);
|
|
|
|
|
|
|
|
// Was the texture set before teh GLProgramState ? Set it
|
|
|
|
if (_texture)
|
|
|
|
setTexture(_texture);
|
|
|
|
|
|
|
|
if (_blendDirty)
|
|
|
|
setBlendFunc(_blend);
|
|
|
|
|
|
|
|
bindMeshCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLProgramState* Mesh::getGLProgramState() const
|
|
|
|
{
|
2015-05-06 06:59:41 +08:00
|
|
|
return _material ?
|
|
|
|
_material->_currentTechnique->_passes.at(0)->getGLProgramState()
|
|
|
|
: nullptr;
|
2014-08-22 13:52:03 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 17:20:22 +08:00
|
|
|
void Mesh::calculateAABB()
|
2014-08-22 13:52:03 +08:00
|
|
|
{
|
|
|
|
if (_meshIndexData)
|
|
|
|
{
|
|
|
|
_aabb = _meshIndexData->getAABB();
|
|
|
|
if (_skin)
|
|
|
|
{
|
2014-10-20 22:53:24 +08:00
|
|
|
//get skin root
|
|
|
|
Bone3D* root = nullptr;
|
|
|
|
Mat4 invBindPose;
|
|
|
|
if (_skin->_skinBones.size())
|
|
|
|
{
|
|
|
|
root = _skin->_skinBones.at(0);
|
|
|
|
while (root) {
|
|
|
|
auto parent = root->getParentBone();
|
|
|
|
bool parentInSkinBone = false;
|
|
|
|
for (const auto& bone : _skin->_skinBones) {
|
|
|
|
if (bone == parent)
|
|
|
|
{
|
|
|
|
parentInSkinBone = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!parentInSkinBone)
|
|
|
|
break;
|
|
|
|
root = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
if (root)
|
|
|
|
{
|
2014-10-20 22:53:24 +08:00
|
|
|
_aabb.transform(root->getWorldMat() * _skin->getInvBindPose(root));
|
2014-08-22 13:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::bindMeshCommand()
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
if (_material && _meshIndexData)
|
|
|
|
{
|
|
|
|
auto pass = _material->_currentTechnique->_passes.at(0);
|
|
|
|
auto glprogramstate = pass->getGLProgramState();
|
|
|
|
auto texture = pass->getTexture();
|
|
|
|
auto textureid = texture ? texture->getName() : 0;
|
|
|
|
// XXX
|
|
|
|
// auto blend = pass->getStateBlock()->getBlendFunc();
|
|
|
|
auto blend = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
|
|
|
|
_meshCommand.genMaterialID(textureid, glprogramstate, _meshIndexData->getVertexBuffer()->getVBO(), _meshIndexData->getIndexBuffer()->getVBO(), blend);
|
|
|
|
_material->getStateBlock()->setCullFace(true);
|
|
|
|
_material->getStateBlock()->setDepthTest(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 10:58:09 +08:00
|
|
|
void Mesh::setLightUniforms(Pass* pass, Scene* scene, const Vec4& color, unsigned int lightmask)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
2015-05-13 10:58:09 +08:00
|
|
|
CCASSERT(pass, "Invalid Pass");
|
2015-05-06 04:07:32 +08:00
|
|
|
CCASSERT(scene, "Invalid scene");
|
|
|
|
|
|
|
|
const auto& conf = Configuration::getInstance();
|
|
|
|
int maxDirLight = conf->getMaxSupportDirLightInShader();
|
|
|
|
int maxPointLight = conf->getMaxSupportPointLightInShader();
|
|
|
|
int maxSpotLight = conf->getMaxSupportSpotLightInShader();
|
|
|
|
auto &lights = scene->getLights();
|
|
|
|
|
2015-05-13 10:58:09 +08:00
|
|
|
auto glProgramState = pass->getGLProgramState();
|
|
|
|
auto attributes = pass->getVertexAttributeBinding()->getVertexAttribsFlags();
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2015-05-13 10:58:09 +08:00
|
|
|
if (attributes & (1 << GLProgram::VERTEX_ATTRIB_NORMAL))
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
resetLightUniformValues();
|
|
|
|
|
|
|
|
GLint enabledDirLightNum = 0;
|
|
|
|
GLint enabledPointLightNum = 0;
|
|
|
|
GLint enabledSpotLightNum = 0;
|
|
|
|
Vec3 ambientColor;
|
|
|
|
for (const auto& light : lights)
|
|
|
|
{
|
|
|
|
bool useLight = light->isEnabled() && ((unsigned int)light->getLightFlag() & lightmask);
|
|
|
|
if (useLight)
|
|
|
|
{
|
|
|
|
float intensity = light->getIntensity();
|
|
|
|
switch (light->getLightType())
|
|
|
|
{
|
|
|
|
case LightType::DIRECTIONAL:
|
|
|
|
{
|
|
|
|
if(enabledDirLightNum < maxDirLight)
|
|
|
|
{
|
|
|
|
auto dirLight = static_cast<DirectionLight *>(light);
|
|
|
|
Vec3 dir = dirLight->getDirectionInWorld();
|
|
|
|
dir.normalize();
|
|
|
|
const Color3B &col = dirLight->getDisplayedColor();
|
|
|
|
s_dirLightUniformColorValues[enabledDirLightNum].set(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
s_dirLightUniformDirValues[enabledDirLightNum] = dir;
|
|
|
|
++enabledDirLightNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LightType::POINT:
|
|
|
|
{
|
|
|
|
if(enabledPointLightNum < maxPointLight)
|
|
|
|
{
|
|
|
|
auto pointLight = static_cast<PointLight *>(light);
|
|
|
|
Mat4 mat= pointLight->getNodeToWorldTransform();
|
|
|
|
const Color3B &col = pointLight->getDisplayedColor();
|
|
|
|
s_pointLightUniformColorValues[enabledPointLightNum].set(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
s_pointLightUniformPositionValues[enabledPointLightNum].set(mat.m[12], mat.m[13], mat.m[14]);
|
|
|
|
s_pointLightUniformRangeInverseValues[enabledPointLightNum] = 1.0f / pointLight->getRange();
|
|
|
|
++enabledPointLightNum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LightType::SPOT:
|
|
|
|
{
|
|
|
|
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].set(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
s_spotLightUniformPositionValues[enabledSpotLightNum].set(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LightType::AMBIENT:
|
|
|
|
{
|
|
|
|
auto ambLight = static_cast<AmbientLight *>(light);
|
|
|
|
const Color3B &col = ambLight->getDisplayedColor();
|
|
|
|
ambientColor.add(col.r / 255.0f * intensity, col.g / 255.0f * intensity, col.b / 255.0f * intensity);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 < maxDirLight)
|
|
|
|
{
|
2015-05-13 13:24:52 +08:00
|
|
|
glProgramState->setUniformVec3v(s_dirLightUniformColorName, s_dirLightUniformColorValues.size(), &s_dirLightUniformColorValues[0]);
|
|
|
|
glProgramState->setUniformVec3v(s_dirLightUniformDirName, s_dirLightUniformDirValues.size(), &s_dirLightUniformDirValues[0]);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 < maxPointLight)
|
|
|
|
{
|
2015-05-13 13:24:52 +08:00
|
|
|
glProgramState->setUniformVec3v(s_pointLightUniformColorName, s_pointLightUniformColorValues.size(), &s_pointLightUniformColorValues[0]);
|
|
|
|
glProgramState->setUniformVec3v(s_pointLightUniformPositionName, s_pointLightUniformPositionValues.size(), &s_pointLightUniformPositionValues[0]);
|
|
|
|
glProgramState->setUniformFloatv(s_pointLightUniformRangeInverseName, s_pointLightUniformRangeInverseValues.size(), &s_pointLightUniformRangeInverseValues[0]);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 < maxSpotLight)
|
|
|
|
{
|
2015-05-13 13:24:52 +08:00
|
|
|
glProgramState->setUniformVec3v(s_spotLightUniformColorName, s_spotLightUniformColorValues.size(), &s_spotLightUniformColorValues[0]);
|
|
|
|
glProgramState->setUniformVec3v(s_spotLightUniformPositionName, s_spotLightUniformPositionValues.size(), &s_spotLightUniformPositionValues[0]);
|
|
|
|
glProgramState->setUniformVec3v(s_spotLightUniformDirName, s_spotLightUniformDirValues.size(), &s_spotLightUniformDirValues[0]);
|
|
|
|
glProgramState->setUniformFloatv(s_spotLightUniformInnerAngleCosName, s_spotLightUniformInnerAngleCosValues.size(), &s_spotLightUniformInnerAngleCosValues[0]);
|
|
|
|
glProgramState->setUniformFloatv(s_spotLightUniformOuterAngleCosName, s_spotLightUniformOuterAngleCosValues.size(), &s_spotLightUniformOuterAngleCosValues[0]);
|
|
|
|
glProgramState->setUniformFloatv(s_spotLightUniformRangeInverseName, s_spotLightUniformRangeInverseValues.size(), &s_spotLightUniformRangeInverseValues[0]);
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:31:33 +08:00
|
|
|
glProgramState->setUniformVec3(s_ambientLightUniformColorName, Vec3(ambientColor.x, ambientColor.y, ambientColor.z));
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
else // normal does not exist
|
|
|
|
{
|
|
|
|
Vec3 ambient(0.0f, 0.0f, 0.0f);
|
|
|
|
bool hasAmbient = false;
|
|
|
|
for (const auto& light : lights)
|
|
|
|
{
|
|
|
|
if (light->getLightType() == LightType::AMBIENT)
|
|
|
|
{
|
|
|
|
bool useLight = light->isEnabled() && ((unsigned int)light->getLightFlag() & lightmask);
|
|
|
|
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;
|
2015-06-17 16:32:08 +08:00
|
|
|
//override the uniform value of u_color using the calculated color
|
2015-06-17 11:17:46 +08:00
|
|
|
glProgramState->setUniformVec4("u_color", Vec4(color.x * ambient.x, color.y * ambient.y, color.z * ambient.z, color.w));
|
2015-05-06 04:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-22 13:52:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::setBlendFunc(const BlendFunc &blendFunc)
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
// Blend must be saved for future use
|
|
|
|
// it doesn't matter if the material is already set or not
|
|
|
|
// This functionality is added for compatibility issues
|
|
|
|
if(_blend != blendFunc)
|
|
|
|
{
|
|
|
|
_blendDirty = true;
|
|
|
|
_blend = blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_material) {
|
|
|
|
_material->getStateBlock()->setBlendFunc(blendFunc);
|
|
|
|
bindMeshCommand();
|
|
|
|
}
|
2014-08-22 13:52:03 +08:00
|
|
|
}
|
2015-05-06 06:59:41 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
const BlendFunc& Mesh::getBlendFunc() const
|
2014-08-22 13:52:03 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
// return _material->_currentTechnique->_passes.at(0)->getBlendFunc();
|
2014-08-22 13:52:03 +08:00
|
|
|
return _blend;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum Mesh::getPrimitiveType() const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getPrimitiveType();
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t Mesh::getIndexCount() const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getIndexBuffer()->getIndexNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum Mesh::getIndexFormat() const
|
|
|
|
{
|
|
|
|
return GL_UNSIGNED_SHORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint Mesh::getIndexBuffer() const
|
|
|
|
{
|
|
|
|
return _meshIndexData->getIndexBuffer()->getVBO();
|
|
|
|
}
|
2014-05-19 05:49:16 +08:00
|
|
|
NS_CC_END
|