add clear feature when before draw scene with a camera

This commit is contained in:
Huabing.Xu 2015-05-07 15:09:06 +08:00
parent c785b7a021
commit 67ec758d15
11 changed files with 140 additions and 5 deletions

View File

@ -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 = "<group>"; };
B230ED7019B417AE00364AA8 /* CCTrianglesCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTrianglesCommand.h; sourceTree = "<group>"; };
B241A6E21AFB0BE700C5623C /* ccShader_CameraClear.frag */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_CameraClear.frag; sourceTree = "<group>"; };
B241A6E31AFB0BE700C5623C /* ccShader_CameraClear.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_CameraClear.vert; sourceTree = "<group>"; };
B24AA981195A675C007B4522 /* CCFastTMXLayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXLayer.cpp; sourceTree = "<group>"; };
B24AA982195A675C007B4522 /* CCFastTMXLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCFastTMXLayer.h; sourceTree = "<group>"; };
B24AA983195A675C007B4522 /* CCFastTMXTiledMap.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCFastTMXTiledMap.cpp; sourceTree = "<group>"; };
@ -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 */,

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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

View File

@ -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.
@}

View File

@ -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);

View File

@ -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);
}
);

View File

@ -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;
}
);

View File

@ -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

View File

@ -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