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-17 05:36:00 +08:00
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-13 10:12:56 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/CCGLProgramState.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
|
|
|
|
2013-12-03 11:11:46 +08:00
|
|
|
QuadCommand::QuadCommand()
|
2014-04-10 20:31:44 +08:00
|
|
|
:_materialID(0)
|
|
|
|
,_textureID(0)
|
2014-05-13 10:12:56 +08:00
|
|
|
,_glProgramState(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-05-15 01:07:09 +08:00
|
|
|
void QuadCommand::init(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, ssize_t quadCount, const Mat4 &mv)
|
2013-12-03 11:11:46 +08:00
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
CCASSERT(glProgramState, "Invalid GLProgramState");
|
|
|
|
CCASSERT(glProgramState->getVertexAttribsFlags() == 0, "No custom attributes are supported in QuadCommand");
|
|
|
|
|
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-05-13 10:12:56 +08:00
|
|
|
if( _textureID != textureID || _blendType.src != blendType.src || _blendType.dst != blendType.dst || _glProgramState != glProgramState) {
|
|
|
|
|
2014-04-10 22:56:40 +08:00
|
|
|
_textureID = textureID;
|
|
|
|
_blendType = blendType;
|
2014-05-13 10:12:56 +08:00
|
|
|
_glProgramState = glProgramState;
|
|
|
|
|
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-05-13 10:12:56 +08:00
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
if(_glProgramState->getUniformCount() > 0)
|
2014-04-02 07:13:41 +08:00
|
|
|
{
|
2014-05-13 14:51:37 +08:00
|
|
|
_materialID = QuadCommand::MATERIAL_ID_DO_NOT_BATCH;
|
2014-04-02 07:13:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
int glProgram = (int)_glProgramState->getGLProgram()->getProgram();
|
|
|
|
int intArray[4] = { glProgram, (int)_textureID, (int)_blendType.src, (int)_blendType.dst};
|
|
|
|
|
|
|
|
_materialID = XXH32((const void*)intArray, sizeof(intArray), 0);
|
2014-04-02 07:13:41 +08:00
|
|
|
}
|
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
|
|
|
{
|
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);
|
2014-05-13 10:12:56 +08:00
|
|
|
|
|
|
|
_glProgramState->apply(_mv);
|
2013-11-08 07:48:37 +08:00
|
|
|
}
|
|
|
|
|
2014-06-19 19:45:24 +08:00
|
|
|
NS_CC_END
|