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
|
|
|
|
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCQuadCommand.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-03-31 13:47:07 +08:00
|
|
|
#include "xxhash.h"
|
2013-11-07 06:57:42 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-04-02 07:13:41 +08:00
|
|
|
|
|
|
|
static void convertIntToByteArray(int value, int* output)
|
|
|
|
{
|
|
|
|
*output = value;
|
|
|
|
}
|
|
|
|
|
2013-12-03 11:11:46 +08:00
|
|
|
QuadCommand::QuadCommand()
|
2014-04-10 20:31:44 +08:00
|
|
|
:_materialID(0)
|
|
|
|
,_textureID(0)
|
2014-03-31 13:47:07 +08:00
|
|
|
,_shader(nullptr)
|
2014-04-10 20:31:44 +08:00
|
|
|
,_blendType(BlendFunc::DISABLE)
|
2014-03-31 13:47:07 +08:00
|
|
|
,_quads(nullptr)
|
2014-04-10 20:31:44 +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
|
|
|
}
|
|
|
|
|
2014-04-09 11:53:01 +08:00
|
|
|
void QuadCommand::init(float globalOrder, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, ssize_t quadCount, const Matrix &mv)
|
2013-12-03 11:11:46 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_globalOrder = globalOrder;
|
2014-04-02 07:13:41 +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
|
|
|
|
2014-04-10 22:56:40 +08:00
|
|
|
if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst || _shader != shader) {
|
|
|
|
|
|
|
|
_textureID = textureID;
|
|
|
|
_blendType = blendType;
|
|
|
|
_shader = shader;
|
|
|
|
|
2014-04-02 07:13:41 +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
|
|
|
{
|
2014-04-02 07:13:41 +08:00
|
|
|
//TODO fix blend id generation
|
|
|
|
int blendID = 0;
|
|
|
|
if(_blendType == BlendFunc::DISABLE)
|
2013-11-07 06:57:42 +08:00
|
|
|
{
|
2014-04-02 07:13:41 +08:00
|
|
|
blendID = 0;
|
2013-11-07 06:57:42 +08:00
|
|
|
}
|
2014-04-02 07:13:41 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert program id, texture id and blend id into byte array
|
|
|
|
int intArray[3];
|
|
|
|
convertIntToByteArray(_shader->getProgram(), intArray);
|
|
|
|
convertIntToByteArray(blendID, intArray+1);
|
|
|
|
convertIntToByteArray(_textureID, intArray+2);
|
|
|
|
|
|
|
|
_materialID = XXH32((const void*)intArray, sizeof(intArray), 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
|