From 15adaf2877052541ac6f14286c7a38cc89fdd201 Mon Sep 17 00:00:00 2001 From: Nite Luo Date: Mon, 26 Jan 2015 16:37:11 -0800 Subject: [PATCH] save/restore render state --- cocos/renderer/CCRenderer.cpp | 41 ++++++++++++++++++++++++++++++++++- cocos/renderer/CCRenderer.h | 10 ++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/cocos/renderer/CCRenderer.cpp b/cocos/renderer/CCRenderer.cpp index dc718d284f..e4b087dbc0 100644 --- a/cocos/renderer/CCRenderer.cpp +++ b/cocos/renderer/CCRenderer.cpp @@ -131,6 +131,41 @@ void RenderQueue::clear() _queuePosZ.clear(); } +void RenderQueue::saveRenderState() +{ + _isDepthEnabled = glIsEnabled(GL_DEPTH_TEST); + _isCullEnabled = glIsEnabled(GL_CULL_FACE); + glGetBooleanv(GL_DEPTH_WRITEMASK, &_isDepthWrite); + + CHECK_GL_ERROR_DEBUG(); +} + +void RenderQueue::restoreRenderState() +{ + if (_isCullEnabled) + { + glEnable(GL_CULL_FACE); + } + else + { + glDisable(GL_CULL_FACE); + } + + + if (_isDepthEnabled) + { + glEnable(GL_DEPTH_TEST); + } + else + { + glDisable(GL_DEPTH_TEST); + } + + glDepthMask(_isDepthWrite); + + CHECK_GL_ERROR_DEBUG(); +} + // // // @@ -465,10 +500,12 @@ void Renderer::processRenderCommand(RenderCommand* command) } } -void Renderer::visitRenderQueue(const RenderQueue& queue) +void Renderer::visitRenderQueue(RenderQueue& queue) { ssize_t size = queue.size(); + queue.saveRenderState(); + //Process Opaque Object const std::vector& opaqueQueue = queue.getOpaqueCommands(); if (opaqueQueue.size() > 0) @@ -507,6 +544,8 @@ void Renderer::visitRenderQueue(const RenderQueue& queue) processRenderCommand(queue[index]); } flush(); + + queue.restoreRenderState(); } void Renderer::render() diff --git a/cocos/renderer/CCRenderer.h b/cocos/renderer/CCRenderer.h index 70392c0be4..b0c206b167 100644 --- a/cocos/renderer/CCRenderer.h +++ b/cocos/renderer/CCRenderer.h @@ -57,12 +57,20 @@ public: ssize_t getOpaqueQueueSize() const { return _queue3DOpaque.size(); } const std::vector& getOpaqueCommands() const { return _queue3DOpaque; } + void saveRenderState(); + void restoreRenderState(); + protected: std::vector _queue3DOpaque; std::vector _queue3DTransparent; std::vector _queueNegZ; std::vector _queue0; std::vector _queuePosZ; + + //Render State related + bool _isCullEnabled; + bool _isDepthEnabled; + GLboolean _isDepthWrite; }; struct RenderStackElement @@ -162,7 +170,7 @@ protected: void flushTriangles(); void processRenderCommand(RenderCommand* command); - void visitRenderQueue(const RenderQueue& queue); + void visitRenderQueue(RenderQueue& queue); void fillVerticesAndIndices(const TrianglesCommand* cmd); void fillQuads(const QuadCommand* cmd);