From c364323bbc24730d43ad239a8af7040ffd8ee902 Mon Sep 17 00:00:00 2001 From: Nite Luo Date: Tue, 27 Jan 2015 16:26:31 -0800 Subject: [PATCH] Not using static value to save previous render state since other commands like custom command could change them --- cocos/renderer/CCMeshCommand.cpp | 50 +++++++++++++++----------------- cocos/renderer/CCMeshCommand.h | 4 +++ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cocos/renderer/CCMeshCommand.cpp b/cocos/renderer/CCMeshCommand.cpp index 630e6c1294..66d9625690 100644 --- a/cocos/renderer/CCMeshCommand.cpp +++ b/cocos/renderer/CCMeshCommand.cpp @@ -42,12 +42,6 @@ NS_CC_BEGIN -//render state -static bool s_cullFaceEnabled = false; -static GLenum s_cullFace = 0; -static bool s_depthTestEnabled = false; -static bool s_depthWriteEnabled = false; - static const char *s_dirLightUniformColorName = "u_DirLightSourceColor"; static std::vector s_dirLightUniformColorValues; static const char *s_dirLightUniformDirName = "u_DirLightSourceDirection"; @@ -185,46 +179,48 @@ MeshCommand::~MeshCommand() void MeshCommand::applyRenderState() { - if (_cullFaceEnabled && !s_cullFaceEnabled) + _renderStateCullFace = glIsEnabled(GL_CULL_FACE); + _renderStateDepthTest = glIsEnabled(GL_DEPTH_TEST); + glGetBooleanv(GL_DEPTH_WRITEMASK, &_renderStateDepthWrite); + + if (_cullFaceEnabled && !_renderStateCullFace) { glEnable(GL_CULL_FACE); - s_cullFaceEnabled = true; } - if (s_cullFace != _cullFace) - { - glCullFace(_cullFace); - s_cullFace = _cullFace; - } - if (_depthTestEnabled && !s_depthTestEnabled) + + glCullFace(_cullFace); + + if (_depthTestEnabled && !_renderStateDepthTest) { glEnable(GL_DEPTH_TEST); - s_depthTestEnabled = true; } - if (_depthWriteEnabled && !s_depthWriteEnabled) + if (_depthWriteEnabled && !_renderStateDepthWrite) { glDepthMask(GL_TRUE); - s_depthWriteEnabled = true; } } void MeshCommand::restoreRenderState() { - if (s_cullFaceEnabled) + if (_renderStateCullFace) + { + glEnable(GL_CULL_FACE); + } + else { glDisable(GL_CULL_FACE); - s_cullFaceEnabled = false; } - if (s_depthTestEnabled) + + if (_renderStateDepthTest) + { + glEnable(GL_DEPTH_TEST); + } + else { glDisable(GL_DEPTH_TEST); - s_depthTestEnabled = false; } - if (s_depthWriteEnabled) - { - glDepthMask(GL_FALSE); - s_depthWriteEnabled = false; - } - s_cullFace = 0; + + glDepthMask(_renderStateDepthTest); } void MeshCommand::genMaterialID(GLuint texID, void* glProgramState, GLuint vertexBuffer, GLuint indexBuffer, const BlendFunc& blend) diff --git a/cocos/renderer/CCMeshCommand.h b/cocos/renderer/CCMeshCommand.h index 3dfc6937ed..867da76d2a 100644 --- a/cocos/renderer/CCMeshCommand.h +++ b/cocos/renderer/CCMeshCommand.h @@ -127,6 +127,10 @@ protected: GLenum _cullFace; bool _depthTestEnabled; bool _depthWriteEnabled; + + bool _renderStateCullFace; + bool _renderStateDepthTest; + GLboolean _renderStateDepthWrite; // ModelView transform Mat4 _mv;