2013-12-18 10:12:15 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-18 10:12:15 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-11-07 06:24:56 +08:00
|
|
|
|
|
|
|
|
2013-12-26 16:36:03 +08:00
|
|
|
#include "renderer/CCQuadCommand.h"
|
2013-11-08 07:48:37 +08:00
|
|
|
#include "ccGLStateCache.h"
|
2013-11-07 06:57:42 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-03 11:11:46 +08:00
|
|
|
QuadCommand::QuadCommand()
|
2014-01-18 08:08:29 +08:00
|
|
|
:_textureID(0)
|
2013-12-03 11:11:46 +08:00
|
|
|
,_blendType(BlendFunc::DISABLE)
|
2014-01-16 06:35:26 +08:00
|
|
|
,_quadsCount(0)
|
2013-11-07 06:57:42 +08:00
|
|
|
{
|
2013-12-18 10:02:11 +08:00
|
|
|
_type = RenderCommand::Type::QUAD_COMMAND;
|
2013-12-03 11:11:46 +08:00
|
|
|
_shader = nullptr;
|
2014-01-16 06:35:26 +08:00
|
|
|
_quads = nullptr;
|
2013-12-03 11:11:46 +08:00
|
|
|
}
|
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
void QuadCommand::init(float globalOrder, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, ssize_t quadCount, const kmMat4 &mv)
|
2013-12-03 11:11:46 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_globalOrder = globalOrder;
|
2013-12-03 11:11:46 +08:00
|
|
|
_textureID = textureID;
|
|
|
|
_blendType = blendType;
|
2013-11-08 07:48:37 +08:00
|
|
|
_shader = shader;
|
2013-12-06 11:04:01 +08:00
|
|
|
|
2014-01-16 06:35:26 +08:00
|
|
|
_quadsCount = quadCount;
|
|
|
|
_quads = quad;
|
2013-12-06 11:04:01 +08:00
|
|
|
|
2014-01-16 06:35:26 +08:00
|
|
|
_mv = mv;
|
2014-01-18 08:08:29 +08:00
|
|
|
|
|
|
|
generateMaterialID();
|
2013-11-07 06:57:42 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 02:09:53 +08:00
|
|
|
QuadCommand::~QuadCommand()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-01-18 08:08:29 +08:00
|
|
|
void QuadCommand::generateMaterialID()
|
2013-11-07 06:57:42 +08:00
|
|
|
{
|
|
|
|
//Generate Material ID
|
|
|
|
//TODO fix shader ID generation
|
2014-03-26 10:07:50 +08:00
|
|
|
CCASSERT(_shader->getMaterialProgramID() < GLProgram::_maxMaterialIDNumber, "ShaderID is greater than Id limitation");
|
2013-11-07 06:57:42 +08:00
|
|
|
|
|
|
|
//TODO fix blend id generation
|
|
|
|
int blendID = 0;
|
|
|
|
if(_blendType == BlendFunc::DISABLE)
|
|
|
|
{
|
|
|
|
blendID = 0;
|
|
|
|
}
|
|
|
|
else if(_blendType == BlendFunc::ALPHA_PREMULTIPLIED)
|
|
|
|
{
|
|
|
|
blendID = 1;
|
|
|
|
}
|
|
|
|
else if(_blendType == BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
|
|
|
{
|
|
|
|
blendID = 2;
|
|
|
|
}
|
|
|
|
else if(_blendType == BlendFunc::ADDITIVE)
|
|
|
|
{
|
|
|
|
blendID = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blendID = 4;
|
|
|
|
}
|
|
|
|
|
2014-01-07 15:41:01 +08:00
|
|
|
//TODO Material ID should be part of the ID
|
|
|
|
//
|
|
|
|
// Temporal hack (later, these 32-bits should be packed in 24-bits
|
|
|
|
//
|
2014-02-26 15:01:45 +08:00
|
|
|
// +---------------------+-------------------+----------------------------------------+
|
|
|
|
// | Shader ID (10 bits) | Blend ID (4 bits) | empty (18bits) | Texture ID (32 bits) |
|
|
|
|
// +---------------------+-------------------+----------------------------------------+
|
2014-01-07 15:41:01 +08:00
|
|
|
|
2014-03-25 16:54:29 +08:00
|
|
|
_materialID = (uint64_t)_shader->getMaterialProgramID() << 54
|
2014-02-27 09:27:33 +08:00
|
|
|
| (uint64_t)blendID << 50
|
2014-02-26 15:01:45 +08:00
|
|
|
| (uint64_t)_textureID << 0;
|
2013-11-07 06:57:42 +08:00
|
|
|
}
|
|
|
|
|
2014-01-18 15:10:04 +08:00
|
|
|
void QuadCommand::useMaterial() const
|
2013-11-08 07:48:37 +08:00
|
|
|
{
|
|
|
|
_shader->use();
|
2014-03-06 10:50:09 +08:00
|
|
|
_shader->setUniformsForBuiltins(_mv);
|
2013-11-09 05:57:21 +08:00
|
|
|
|
|
|
|
//Set texture
|
|
|
|
GL::bindTexture2D(_textureID);
|
2013-12-03 04:13:05 +08:00
|
|
|
|
|
|
|
//set blend mode
|
|
|
|
GL::blendFunc(_blendType.src, _blendType.dst);
|
2013-11-08 07:48:37 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
NS_CC_END
|