axmol/cocos/2d/renderer/QuadCommand.cpp

100 lines
2.0 KiB
C++
Raw Normal View History

//
// Created by NiTe Luo on 11/6/13.
//
#include "QuadCommand.h"
2013-11-08 07:48:37 +08:00
#include "ccGLStateCache.h"
2013-11-07 06:57:42 +08:00
NS_CC_BEGIN
QuadCommand::QuadCommand()
2013-11-07 06:57:42 +08:00
:RenderCommand()
,_viewport(0)
,_depth(0)
,_textureID(0)
,_blendType(BlendFunc::DISABLE)
,_quadCount(0)
2013-11-07 06:57:42 +08:00
{
_type = QUAD_COMMAND;
_shader = nullptr;
_quad = nullptr;
}
void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount)
{
_viewport = viewport;
_depth = depth;
_textureID = textureID;
_blendType = blendType;
_quadCount = quadCount;
2013-11-08 07:48:37 +08:00
_shader = shader;
free(_quad);
_quad = (V3F_C4B_T2F_Quad*)malloc(sizeof(V3F_C4B_T2F_Quad) * quadCount);
memcpy(_quad, quad, sizeof(V3F_C4B_T2F_Quad) * quadCount);
2013-11-07 06:57:42 +08:00
}
QuadCommand::~QuadCommand()
{
free(_quad);
}
2013-11-07 06:57:42 +08:00
int64_t QuadCommand::generateID()
{
_id = 0;
//Generate Material ID
//TODO fix shader ID generation
2013-11-08 07:48:37 +08:00
CCASSERT(_shader->getProgram() < 64, "ShaderID is greater than 64");
2013-11-07 06:57:42 +08:00
//TODO fix texture ID generation
CCASSERT(_textureID < 1024, "TextureID is greater than 1024");
//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;
}
2013-11-08 07:48:37 +08:00
_materialID = (int32_t)_shader->getProgram() << 28
2013-11-07 06:57:42 +08:00
| (int32_t)blendID << 24
| (int32_t)_textureID << 14;
//Generate RenderCommandID
_id = (int64_t)_viewport << 61
| (int64_t)1 << 60 //translucent
2013-11-11 16:14:29 +08:00
| (int64_t)_depth << 36;
2013-11-07 06:57:42 +08:00
return _id;
}
2013-11-08 07:48:37 +08:00
void QuadCommand::useMaterial()
{
_shader->use();
2013-11-08 07:48:37 +08:00
_shader->setUniformsForBuiltins();
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