From c03ac80700e2ec049138336a5d7175ef9bf70d40 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 23 Jun 2014 23:58:45 +0800 Subject: [PATCH] Sprite3D render batch, not finished --- cocos/renderer/CCMeshCommand.cpp | 45 ++++++++++++++++++++++++-------- cocos/renderer/CCRenderer.cpp | 36 +++++++++++++++++++------ cocos/renderer/CCRenderer.h | 4 +++ 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/cocos/renderer/CCMeshCommand.cpp b/cocos/renderer/CCMeshCommand.cpp index 2513df159e..51208939b0 100644 --- a/cocos/renderer/CCMeshCommand.cpp +++ b/cocos/renderer/CCMeshCommand.cpp @@ -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); diff --git a/cocos/renderer/CCRenderer.cpp b/cocos/renderer/CCRenderer.cpp index f49252e3dd..a399d2f964 100644 --- a/cocos/renderer/CCRenderer.cpp +++ b/cocos/renderer/CCRenderer.cpp @@ -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(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(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) diff --git a/cocos/renderer/CCRenderer.h b/cocos/renderer/CCRenderer.h index e7e5a041bf..7a1fd14253 100644 --- a/cocos/renderer/CCRenderer.h +++ b/cocos/renderer/CCRenderer.h @@ -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 _batchedQuadCommands; V3F_C4B_T2F_Quad _quads[VBO_SIZE];