Sprite3D render batch, not finished

This commit is contained in:
yangxiao 2014-06-23 23:58:45 +08:00
parent c65e5d6872
commit c03ac80700
3 changed files with 66 additions and 19 deletions

View File

@ -47,6 +47,7 @@ MeshCommand::MeshCommand()
, _displayColor(1.0f, 1.0f, 1.0f, 1.0f)
, _matrixPalette(nullptr)
, _matrixPaletteSize(0)
, _materialID(0)
{
_type = RenderCommand::Type::MESH_COMMAND;
}
@ -169,27 +170,49 @@ void MeshCommand::preDraw()
GL::bindTexture2D(_textureID);
GL::blendFunc(_blendType.src, _blendType.dst);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
//
_glProgramState->apply(_mv);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
}
void MeshCommand::draw()
{
auto glProgram = _glProgramState->getGLProgram();
auto uniform = glProgram->getUniform("u_color");
if (uniform)
if (_matrixPalette)
{
glProgram->setUniformLocationWith4f(uniform->location, _displayColor.x, _displayColor.y, _displayColor.z, _displayColor.w);
CCLOG("skin");
}
auto glProgram = _glProgramState->getGLProgram();
//glProgram->use();
//glProgram->setUniformsForBuiltins(_mv);
// auto uniform = glProgram->getUniform("u_color");
// if (uniform)
// {
// glProgram->setUniformLocationWith4f(uniform->location, _displayColor.x, _displayColor.y, _displayColor.z, _displayColor.w);
// }
//
// if (_matrixPaletteSize && _matrixPalette)
// {
// uniform = glProgram->getUniform("u_matrixPalette");
// if (uniform)
// glProgram->setUniformLocationWith4fv(uniform->location, (const float*)_matrixPalette, _matrixPaletteSize);
// }
//
// // Draw
// glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);
//
// CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _indexCount);
// glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
_glProgramState->setUniformVec4("u_color", _displayColor);
if (_matrixPaletteSize && _matrixPalette)
{
uniform = glProgram->getUniform("u_matrixPalette");
if (uniform)
glProgram->setUniformLocationWith4fv(uniform->location, (const float*)_matrixPalette, _matrixPaletteSize);
_glProgramState->setUniformCallback("u_matrixPalette", CC_CALLBACK_2(MeshCommand::MatrixPalleteCallBack, this));
}
//_glProgramState->apply(_mv);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
// Draw
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);

View File

@ -108,6 +108,7 @@ static const int DEFAULT_RENDER_QUEUE = 0;
//
Renderer::Renderer()
:_lastMaterialID(0)
,_lastBatchedMeshCommand(nullptr)
,_numQuads(0)
,_glViewAssigned(false)
,_isRendering(false)
@ -284,6 +285,7 @@ void Renderer::visitRenderQueue(const RenderQueue& queue)
auto commandType = command->getType();
if(RenderCommand::Type::QUAD_COMMAND == commandType)
{
flush3D();
auto cmd = static_cast<QuadCommand*>(command);
//Batch quads
if(_numQuads + cmd->getQuadCount() > VBO_SIZE)
@ -322,22 +324,24 @@ void Renderer::visitRenderQueue(const RenderQueue& queue)
}
else if (RenderCommand::Type::MESH_COMMAND == commandType)
{
flush();
flush2D();
auto cmd = static_cast<MeshCommand*>(command);
// if (material3DID != cmd->getMaterialID())
// cmd->preDraw();
// cmd->draw();
// cmd->postDraw();
cmd->execute();
// if (_lastBatchedMeshCommand == nullptr || _lastBatchedMeshCommand->getMaterialID() != cmd->getMaterialID())
// {
// if (material3DID != 0)
// cmd->postDraw();
//
// flush3D();
// cmd->preDraw();
// material3DID = cmd->getMaterialID();
// cmd->draw();
// }
// else
// {
// cmd->draw();
// }
cmd->execute();
// _lastBatchedMeshCommand = cmd;
// cmd->execute();
}
else
{
@ -390,6 +394,7 @@ void Renderer::clean()
_numQuads = 0;
_lastMaterialID = 0;
_lastBatchedMeshCommand = nullptr;
}
void Renderer::convertToWorldCoordinates(V3F_C4B_T2F_Quad* quads, ssize_t quantity, const Mat4& modelView)
@ -519,11 +524,26 @@ void Renderer::drawBatchedQuads()
}
void Renderer::flush()
{
flush2D();
flush3D();
}
void Renderer::flush2D()
{
drawBatchedQuads();
_lastMaterialID = 0;
}
void Renderer::flush3D()
{
if (_lastBatchedMeshCommand)
{
_lastBatchedMeshCommand->postDraw();
_lastBatchedMeshCommand = nullptr;
}
}
// helpers
bool Renderer::checkVisibility(const Mat4 &transform, const Size &size)

View File

@ -38,6 +38,7 @@ NS_CC_BEGIN
class EventListenerCustom;
class QuadCommand;
class MeshCommand;
/** Class that knows how to sort `RenderCommand` objects.
Since the commands that have `z == 0` are "pushed back" in
@ -132,6 +133,8 @@ protected:
//Draw the previews queued quads and flush previous context
void flush();
void flush2D();
void flush3D();
void visitRenderQueue(const RenderQueue& queue);
@ -144,6 +147,7 @@ protected:
uint32_t _lastMaterialID;
MeshCommand* _lastBatchedMeshCommand;
std::vector<QuadCommand*> _batchedQuadCommands;
V3F_C4B_T2F_Quad _quads[VBO_SIZE];