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) , _displayColor(1.0f, 1.0f, 1.0f, 1.0f)
, _matrixPalette(nullptr) , _matrixPalette(nullptr)
, _matrixPaletteSize(0) , _matrixPaletteSize(0)
, _materialID(0)
{ {
_type = RenderCommand::Type::MESH_COMMAND; _type = RenderCommand::Type::MESH_COMMAND;
} }
@ -169,27 +170,49 @@ void MeshCommand::preDraw()
GL::bindTexture2D(_textureID); GL::bindTexture2D(_textureID);
GL::blendFunc(_blendType.src, _blendType.dst); GL::blendFunc(_blendType.src, _blendType.dst);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); //
_glProgramState->apply(_mv); _glProgramState->apply(_mv);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
} }
void MeshCommand::draw() void MeshCommand::draw()
{ {
auto glProgram = _glProgramState->getGLProgram(); if (_matrixPalette)
auto uniform = glProgram->getUniform("u_color");
if (uniform)
{ {
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) if (_matrixPaletteSize && _matrixPalette)
{ {
uniform = glProgram->getUniform("u_matrixPalette"); _glProgramState->setUniformCallback("u_matrixPalette", CC_CALLBACK_2(MeshCommand::MatrixPalleteCallBack, this));
if (uniform)
glProgram->setUniformLocationWith4fv(uniform->location, (const float*)_matrixPalette, _matrixPaletteSize);
} }
//_glProgramState->apply(_mv);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
// Draw // Draw
glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0); glDrawElements(_primitive, (GLsizei)_indexCount, _indexFormat, 0);

View File

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

View File

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