2013-11-07 06:24:56 +08:00
|
|
|
//
|
|
|
|
// 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
|
2013-12-03 14:08:47 +08:00
|
|
|
RenderCommandPool<QuadCommand> QuadCommand::_commandPool;
|
2013-11-07 06:57:42 +08:00
|
|
|
|
2013-12-03 11:11:46 +08:00
|
|
|
QuadCommand::QuadCommand()
|
2013-11-07 06:57:42 +08:00
|
|
|
:RenderCommand()
|
2013-12-03 11:11:46 +08:00
|
|
|
,_viewport(0)
|
|
|
|
,_depth(0)
|
|
|
|
,_textureID(0)
|
|
|
|
,_blendType(BlendFunc::DISABLE)
|
|
|
|
,_quadCount(0)
|
2013-12-06 11:04:01 +08:00
|
|
|
,_capacity(0)
|
2013-11-07 06:57:42 +08:00
|
|
|
{
|
|
|
|
_type = QUAD_COMMAND;
|
2013-12-03 11:11:46 +08:00
|
|
|
_shader = nullptr;
|
|
|
|
_quad = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-06 11:04:01 +08:00
|
|
|
void QuadCommand::init(int viewport, int32_t depth, GLuint textureID, GLProgram* shader, BlendFunc blendType, V3F_C4B_T2F_Quad* quad, int quadCount, const kmMat4 &mv)
|
2013-12-03 11:11:46 +08:00
|
|
|
{
|
|
|
|
_viewport = viewport;
|
|
|
|
_depth = depth;
|
|
|
|
_textureID = textureID;
|
|
|
|
_blendType = blendType;
|
|
|
|
_quadCount = quadCount;
|
2013-11-08 07:48:37 +08:00
|
|
|
_shader = shader;
|
2013-12-06 11:04:01 +08:00
|
|
|
|
|
|
|
if(quadCount > _capacity ) {
|
2013-12-07 03:41:24 +08:00
|
|
|
//TODO find a better way to manage quads, current way will result in memory be wasted
|
2013-12-06 11:04:01 +08:00
|
|
|
// _quad = (V3F_C4B_T2F_Quad*)malloc(sizeof(V3F_C4B_T2F_Quad) * quadCount);
|
|
|
|
_quad = (V3F_C4B_T2F_Quad*) realloc(_quad, sizeof(*quad) * quadCount );
|
|
|
|
_capacity = quadCount;
|
|
|
|
}
|
|
|
|
|
2013-12-06 13:24:59 +08:00
|
|
|
kmMat4 p, mvp;
|
|
|
|
kmGLGetMatrix(KM_GL_PROJECTION, &p);
|
|
|
|
|
|
|
|
kmMat4Multiply(&mvp, &p, &mv);
|
|
|
|
|
|
|
|
|
2013-12-06 11:04:01 +08:00
|
|
|
_quadCount = quadCount;
|
2013-11-13 03:14:34 +08:00
|
|
|
memcpy(_quad, quad, sizeof(V3F_C4B_T2F_Quad) * quadCount);
|
2013-12-06 11:04:01 +08:00
|
|
|
|
|
|
|
for(int i=0; i<quadCount; ++i) {
|
|
|
|
V3F_C4B_T2F_Quad *q = &_quad[i];
|
|
|
|
|
|
|
|
kmVec3 vec1, out1;
|
|
|
|
vec1.x = q->bl.vertices.x;
|
|
|
|
vec1.y = q->bl.vertices.y;
|
|
|
|
vec1.z = q->bl.vertices.z;
|
2013-12-06 13:24:59 +08:00
|
|
|
kmVec3TransformCoord(&out1, &vec1, &mvp);
|
2013-12-06 11:04:01 +08:00
|
|
|
q->bl.vertices.x = out1.x;
|
|
|
|
q->bl.vertices.y = out1.y;
|
|
|
|
q->bl.vertices.z = out1.z;
|
|
|
|
|
|
|
|
kmVec3 vec2, out2;
|
|
|
|
vec2.x = q->br.vertices.x;
|
|
|
|
vec2.y = q->br.vertices.y;
|
|
|
|
vec2.z = q->br.vertices.z;
|
2013-12-06 13:24:59 +08:00
|
|
|
kmVec3TransformCoord(&out2, &vec2, &mvp);
|
2013-12-06 11:04:01 +08:00
|
|
|
q->br.vertices.x = out2.x;
|
|
|
|
q->br.vertices.y = out2.y;
|
|
|
|
q->br.vertices.z = out2.z;
|
|
|
|
|
|
|
|
kmVec3 vec3, out3;
|
|
|
|
vec3.x = q->tr.vertices.x;
|
|
|
|
vec3.y = q->tr.vertices.y;
|
|
|
|
vec3.z = q->tr.vertices.z;
|
2013-12-06 13:24:59 +08:00
|
|
|
kmVec3TransformCoord(&out3, &vec3, &mvp);
|
2013-12-06 11:04:01 +08:00
|
|
|
q->tr.vertices.x = out3.x;
|
|
|
|
q->tr.vertices.y = out3.y;
|
|
|
|
q->tr.vertices.z = out3.z;
|
|
|
|
|
|
|
|
kmVec3 vec4, out4;
|
|
|
|
vec4.x = q->tl.vertices.x;
|
|
|
|
vec4.y = q->tl.vertices.y;
|
|
|
|
vec4.z = q->tl.vertices.z;
|
2013-12-06 13:24:59 +08:00
|
|
|
kmVec3TransformCoord(&out4, &vec4, &mvp);
|
2013-12-06 11:04:01 +08:00
|
|
|
q->tl.vertices.x = out4.x;
|
|
|
|
q->tl.vertices.y = out4.y;
|
|
|
|
q->tl.vertices.z = out4.z;
|
|
|
|
}
|
2013-11-07 06:57:42 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 02:09:53 +08:00
|
|
|
QuadCommand::~QuadCommand()
|
|
|
|
{
|
2013-11-13 03:14:34 +08:00
|
|
|
free(_quad);
|
2013-11-08 02:09:53 +08:00
|
|
|
}
|
|
|
|
|
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-22 08:36:19 +08:00
|
|
|
|
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-12-03 14:08:47 +08:00
|
|
|
void QuadCommand::releaseToCommandPool()
|
|
|
|
{
|
|
|
|
getCommandPool().pushBackCommand(this);
|
|
|
|
}
|
|
|
|
|
2013-11-07 06:57:42 +08:00
|
|
|
NS_CC_END
|