diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index dfc045fb7e..0eb0e33133 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -3640,6 +3640,8 @@ B217704C1977ED8B009EE11B /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = usr/lib/libsqlite3.dylib; sourceTree = SDKROOT; }; B230ED6F19B417AE00364AA8 /* CCTrianglesCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCTrianglesCommand.cpp; sourceTree = ""; }; B230ED7019B417AE00364AA8 /* CCTrianglesCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTrianglesCommand.h; sourceTree = ""; }; + B241A6E21AFB0BE700C5623C /* ccShader_CameraClear.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_CameraClear.frag; sourceTree = ""; }; + B241A6E31AFB0BE700C5623C /* ccShader_CameraClear.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_CameraClear.vert; sourceTree = ""; }; B24AA981195A675C007B4522 /* CCFastTMXLayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXLayer.cpp; sourceTree = ""; }; B24AA982195A675C007B4522 /* CCFastTMXLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCFastTMXLayer.h; sourceTree = ""; }; B24AA983195A675C007B4522 /* CCFastTMXTiledMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXTiledMap.cpp; sourceTree = ""; }; @@ -5956,6 +5958,8 @@ 5034CA5D191D591900CE6051 /* shaders */ = { isa = PBXGroup; children = ( + B241A6E21AFB0BE700C5623C /* ccShader_CameraClear.frag */, + B241A6E31AFB0BE700C5623C /* ccShader_CameraClear.vert */, B603F1B11AC8F1FD00A9579C /* ccShader_3D_Terrain.frag */, B603F1B21AC8F1FD00A9579C /* ccShader_3D_Terrain.vert */, B6D38B941AC3B45600043997 /* ccShader_3D_Particle.frag */, diff --git a/cocos/2d/CCCamera.cpp b/cocos/2d/CCCamera.cpp index 1c86177f62..0652b6cf01 100644 --- a/cocos/2d/CCCamera.cpp +++ b/cocos/2d/CCCamera.cpp @@ -25,6 +25,9 @@ #include "base/CCDirector.h" #include "platform/CCGLView.h" #include "2d/CCScene.h" +#include "renderer/CCRenderer.h" +#include "renderer/CCQuadCommand.h" +#include "renderer/CCGLProgramCache.h" NS_CC_BEGIN @@ -377,4 +380,59 @@ void Camera::setScene(Scene* scene) } } +void Camera::onBeforeClear() +{ + glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); + glStencilMask(0); + _oldDepthTest = glIsEnabled(GL_DEPTH_TEST); + glGetIntegerv(GL_DEPTH_FUNC, &_oldDepthFunc); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_ALWAYS); +} + +void Camera::onEndClear() +{ + if(GL_FALSE == _oldDepthTest) + { + glDisable(GL_DEPTH_TEST); + } + glDepthFunc(_oldDepthFunc); + + glStencilMask(0xFFFFF); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); +} + +void Camera::clearBackground(float depth) +{ + auto renderer = Director::getInstance()->getRenderer(); + _beforeClearCommand.init(0,Mat4(),0); + _beforeClearCommand.func = CC_CALLBACK_0(Camera::onBeforeClear, this); + renderer->addCommand(&_beforeClearCommand); + + //draw + static V3F_C4B_T2F_Quad quad; + quad.bl.vertices = Vec3(-1,-1,0); + quad.br.vertices = Vec3(1,-1,0); + quad.tl.vertices = Vec3(-1,1,0); + quad.tr.vertices = Vec3(1,1,0); + + quad.bl.colors = quad.br.colors = quad.tl.colors = quad.tr.colors = Color4B(0,0,0,1); + + quad.bl.texCoords = Tex2F(0,0); + quad.br.texCoords = Tex2F(1,0); + quad.tl.texCoords = Tex2F(0,1); + quad.tr.texCoords = Tex2F(1,1); + + auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_CAMERA_CLEAR); + auto programState = GLProgramState::create(shader); + programState->setUniformFloat("depth", 1.0); + _clearCommand.init(0, 0, programState, BlendFunc::DISABLE, &quad, 1, Mat4(), 0); + renderer->addCommand(&_clearCommand); + + + _endClearCommand.init(0,Mat4(),0); + _endClearCommand.func = CC_CALLBACK_0(Camera::onEndClear, this); + renderer->addCommand(&_endClearCommand); +} + NS_CC_END diff --git a/cocos/2d/CCCamera.h b/cocos/2d/CCCamera.h index 6330b6476f..e84c7107cb 100644 --- a/cocos/2d/CCCamera.h +++ b/cocos/2d/CCCamera.h @@ -26,6 +26,8 @@ THE SOFTWARE. #include "2d/CCNode.h" #include "3d/CCFrustum.h" +#include "renderer/CCQuadCommand.h" +#include "renderer/CCCustomCommand.h" NS_CC_BEGIN @@ -183,7 +185,9 @@ public: * Get the default camera of the current running scene. */ static Camera* getDefaultCamera(); - + + void clearBackground(float depth); + CC_CONSTRUCTOR_ACCESS: Camera(); ~Camera(); @@ -200,7 +204,7 @@ CC_CONSTRUCTOR_ACCESS: bool initDefault(); bool initPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane); bool initOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane); - + protected: Scene* _scene; //Scene camera belongs to @@ -223,6 +227,15 @@ protected: static Camera* _visitingCamera; friend class Director; +protected: + //callback for clear background + void onBeforeClear(); + void onEndClear(); + GLboolean _oldDepthTest; + GLint _oldDepthFunc; + CustomCommand _beforeClearCommand; + QuadCommand _clearCommand; + CustomCommand _endClearCommand; }; NS_CC_END diff --git a/cocos/2d/CCScene.cpp b/cocos/2d/CCScene.cpp index b6e36ce5ff..33fe0dcf00 100644 --- a/cocos/2d/CCScene.cpp +++ b/cocos/2d/CCScene.cpp @@ -151,6 +151,8 @@ void Scene::render(Renderer* renderer) director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, Camera::_visitingCamera->getViewProjectionMatrix()); + //clear background with max depth + camera->clearBackground(1.0); //visit the scene visit(renderer, transform, 0); renderer->render(); diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index 69ec843d8d..c153a30101 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -74,6 +74,7 @@ const char* GLProgram::SHADER_3D_PARTICLE_COLOR = "Shader3DParticleColor"; const char* GLProgram::SHADER_3D_PARTICLE_TEXTURE = "Shader3DParticleTexture"; const char* GLProgram::SHADER_3D_SKYBOX = "Shader3DSkybox"; const char* GLProgram::SHADER_3D_TERRAIN = "Shader3DTerrain"; +const char* GLProgram::SHADER_CAMERA_CLEAR = "ShaderCameraClear"; // uniform names diff --git a/cocos/renderer/CCGLProgram.h b/cocos/renderer/CCGLProgram.h index 3d2d4c467d..78716ab2d6 100644 --- a/cocos/renderer/CCGLProgram.h +++ b/cocos/renderer/CCGLProgram.h @@ -230,7 +230,11 @@ public: Built in shader for terrain */ static const char* SHADER_3D_TERRAIN; - + + /** + Built in shader for camera clear + */ + static const char* SHADER_CAMERA_CLEAR; /** end of built shader types. @} diff --git a/cocos/renderer/CCGLProgramCache.cpp b/cocos/renderer/CCGLProgramCache.cpp index 53bfe055d6..6f5404e8da 100644 --- a/cocos/renderer/CCGLProgramCache.cpp +++ b/cocos/renderer/CCGLProgramCache.cpp @@ -66,6 +66,7 @@ enum { kShaderType_3DParticleColor, kShaderType_3DSkyBox, kShaderType_3DTerrain, + kShaderType_CameraClear, #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || defined(WP8_SHADER_COMPILER) kShaderType_PositionColor_noMVP_GrayScale, #endif @@ -260,6 +261,10 @@ void GLProgramCache::loadDefaultGLPrograms() p = new GLProgram(); loadDefaultGLProgram(p, kShaderType_3DTerrain); _programs.insert(std::make_pair(GLProgram::SHADER_3D_TERRAIN, p)); + + p = new GLProgram(); + loadDefaultGLProgram(p, kShaderType_CameraClear); + _programs.insert(std::make_pair(GLProgram::SHADER_CAMERA_CLEAR, p)); } void GLProgramCache::reloadDefaultGLPrograms() @@ -395,6 +400,10 @@ void GLProgramCache::reloadDefaultGLPrograms() p = getGLProgram(GLProgram::SHADER_3D_TERRAIN); p->reset(); loadDefaultGLProgram(p, kShaderType_3DTerrain); + + p = getGLProgram(GLProgram::SHADER_CAMERA_CLEAR); + p->reset(); + loadDefaultGLProgram(p, kShaderType_CameraClear); } void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type) @@ -496,6 +505,9 @@ void GLProgramCache::loadDefaultGLProgram(GLProgram *p, int type) case kShaderType_3DTerrain: p->initWithByteArrays(cc3D_Terrain_vert, cc3D_Terrain_frag); break; + case kShaderType_CameraClear: + p->initWithByteArrays(ccCameraClearVert, ccCameraClearFrag); + break; #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || defined(WP8_SHADER_COMPILER) case kShaderType_PositionColor_noMVP_GrayScale: p->initWithByteArrays(ccPositionTextureColor_noMVP_vert, ccUIGrayScale_frag); diff --git a/cocos/renderer/ccShader_CameraClear.frag b/cocos/renderer/ccShader_CameraClear.frag new file mode 100644 index 0000000000..f6afe83ac7 --- /dev/null +++ b/cocos/renderer/ccShader_CameraClear.frag @@ -0,0 +1,15 @@ + +const char* ccCameraClearFrag = STRINGIFY( + +\n#ifdef GL_ES\n +varying mediump vec2 v_texCoord; +varying mediump vec3 v_color; +\n#else\n +varying vec2 v_texCoord; +varying vec3 v_color; +\n#endif\n +void main() +{ + gl_FragColor = vec4(v_color, 1.0); +} +); diff --git a/cocos/renderer/ccShader_CameraClear.vert b/cocos/renderer/ccShader_CameraClear.vert new file mode 100644 index 0000000000..4c06ffb6a3 --- /dev/null +++ b/cocos/renderer/ccShader_CameraClear.vert @@ -0,0 +1,24 @@ + +const char* ccCameraClearVert = STRINGIFY( + +uniform float depth; + +attribute vec4 a_position; +attribute vec2 a_texCoord; +attribute vec3 a_color; +\n#ifdef GL_ES\n +varying mediump vec2 v_texCoord; +varying mediump vec3 v_color; +\n#else\n +varying vec2 v_texCoord; +varying vec3 v_color; +\n#endif\n +void main() +{ + gl_Position = a_position; + gl_Position.z = depth; + gl_Position.w = 1.0; + v_texCoord = a_texCoord; + v_color = a_color; +} +); diff --git a/cocos/renderer/ccShaders.cpp b/cocos/renderer/ccShaders.cpp index 68535d323d..bec5876dbf 100644 --- a/cocos/renderer/ccShaders.cpp +++ b/cocos/renderer/ccShaders.cpp @@ -97,5 +97,7 @@ NS_CC_BEGIN #include "ccShader_3D_Skybox.frag" #include "ccShader_3D_Terrain.vert" #include "ccShader_3D_Terrain.frag" +#include "ccShader_CameraClear.vert" +#include "ccShader_CameraClear.frag" NS_CC_END diff --git a/cocos/renderer/ccShaders.h b/cocos/renderer/ccShaders.h index 41ff659f1d..f83da49f1a 100644 --- a/cocos/renderer/ccShaders.h +++ b/cocos/renderer/ccShaders.h @@ -90,8 +90,8 @@ extern CC_DLL const GLchar * cc3D_Skybox_vert; extern CC_DLL const GLchar * cc3D_Skybox_frag; extern CC_DLL const GLchar * cc3D_Terrain_vert; extern CC_DLL const GLchar * cc3D_Terrain_frag; - - +extern CC_DLL const GLchar * ccCameraClearVert; +extern CC_DLL const GLchar * ccCameraClearFrag; NS_CC_END /** end of support group