fix layerColor occlude sprite bug on android

This commit is contained in:
Huabing.Xu 2014-01-07 22:08:00 +08:00
parent 6c6da4cf77
commit b1d9d5e55e
5 changed files with 42 additions and 6 deletions

View File

@ -49,6 +49,7 @@ const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR = "ShaderPositionTextu
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP = "ShaderPositionTextureColor_noMVP"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP = "ShaderPositionTextureColor_noMVP";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST = "ShaderPositionTextureColorAlphaTest"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST = "ShaderPositionTextureColorAlphaTest";
const char* GLProgram::SHADER_NAME_POSITION_COLOR = "ShaderPositionColor"; const char* GLProgram::SHADER_NAME_POSITION_COLOR = "ShaderPositionColor";
const char* GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP = "ShaderPositionColor_noMVP";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE = "ShaderPositionTexture"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE = "ShaderPositionTexture";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR = "ShaderPositionTexture_uColor"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_U_COLOR = "ShaderPositionTexture_uColor";
const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR = "ShaderPositionTextureA8Color"; const char* GLProgram::SHADER_NAME_POSITION_TEXTURE_A8_COLOR = "ShaderPositionTextureA8Color";

View File

@ -82,6 +82,7 @@ public:
static const char* SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP; static const char* SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP;
static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST; static const char* SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST;
static const char* SHADER_NAME_POSITION_COLOR; static const char* SHADER_NAME_POSITION_COLOR;
static const char* SHADER_NAME_POSITION_COLOR_NO_MVP;
static const char* SHADER_NAME_POSITION_TEXTURE; static const char* SHADER_NAME_POSITION_TEXTURE;
static const char* SHADER_NAME_POSITION_TEXTURE_U_COLOR; static const char* SHADER_NAME_POSITION_TEXTURE_U_COLOR;
static const char* SHADER_NAME_POSITION_TEXTURE_A8_COLOR; static const char* SHADER_NAME_POSITION_TEXTURE_A8_COLOR;

View File

@ -512,7 +512,7 @@ bool LayerColor::initWithColor(const Color4B& color, GLfloat w, GLfloat h)
updateColor(); updateColor();
setContentSize(Size(w, h)); setContentSize(Size(w, h));
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_COLOR)); setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
return true; return true;
} }
return false; return false;
@ -567,6 +567,20 @@ void LayerColor::draw()
_customCommand.init(0, _vertexZ); _customCommand.init(0, _vertexZ);
_customCommand.func = CC_CALLBACK_0(LayerColor::onDraw, this); _customCommand.func = CC_CALLBACK_0(LayerColor::onDraw, this);
Director::getInstance()->getRenderer()->addCommand(&_customCommand); Director::getInstance()->getRenderer()->addCommand(&_customCommand);
kmMat4 p, mvp;
kmGLGetMatrix(KM_GL_PROJECTION, &p);
kmGLGetMatrix(KM_GL_MODELVIEW, &mvp);
kmMat4Multiply(&mvp, &p, &mvp);
for(int i = 0; i < 4; ++i)
{
kmVec3 pos;
pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _vertexZ;
kmVec3TransformCoord(&pos, &pos, &mvp);
_noMVPVertices[i] = Vertex3F(pos.x,pos.y,pos.z);
}
} }
void LayerColor::onDraw() void LayerColor::onDraw()
@ -574,18 +588,17 @@ void LayerColor::onDraw()
CC_NODE_DRAW_SETUP(); CC_NODE_DRAW_SETUP();
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR ); GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR );
// //
// Attributes // Attributes
// //
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
setGLBufferData(_squareVertices, 4 * sizeof(Vertex2F), 0); setGLBufferData(_noMVPVertices, 4 * sizeof(Vertex3F), 0);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
setGLBufferData(_squareColors, 4 * sizeof(Color4F), 1); setGLBufferData(_squareColors, 4 * sizeof(Color4F), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, 0);
#else #else
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _squareVertices); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
#endif // EMSCRIPTEN #endif // EMSCRIPTEN

View File

@ -36,6 +36,7 @@ THE SOFTWARE.
#include "CCEventKeyboard.h" #include "CCEventKeyboard.h"
#include "renderer/CCCustomCommand.h" #include "renderer/CCCustomCommand.h"
#include "renderer/CCQuadCommand.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -298,7 +299,7 @@ protected:
Vertex2F _squareVertices[4]; Vertex2F _squareVertices[4];
Color4F _squareColors[4]; Color4F _squareColors[4];
CustomCommand _customCommand; CustomCommand _customCommand;
Vertex3F _noMVPVertices[4];
private: private:
CC_DISALLOW_COPY_AND_ASSIGN(LayerColor); CC_DISALLOW_COPY_AND_ASSIGN(LayerColor);

View File

@ -36,6 +36,7 @@ enum {
kShaderType_PositionTextureColor_noMVP, kShaderType_PositionTextureColor_noMVP,
kShaderType_PositionTextureColorAlphaTest, kShaderType_PositionTextureColorAlphaTest,
kShaderType_PositionColor, kShaderType_PositionColor,
kShaderType_PositionColor_noMVP,
kShaderType_PositionTexture, kShaderType_PositionTexture,
kShaderType_PositionTexture_uColor, kShaderType_PositionTexture_uColor,
kShaderType_PositionTextureA8Color, kShaderType_PositionTextureA8Color,
@ -124,6 +125,13 @@ void ShaderCache::loadDefaultShaders()
loadDefaultShader(p, kShaderType_PositionColor); loadDefaultShader(p, kShaderType_PositionColor);
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_COLOR, p) ); _programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_COLOR, p) );
//
// Position, Color shader no MVP
//
p = new GLProgram();
loadDefaultShader(p, kShaderType_PositionColor_noMVP);
_programs.insert( std::make_pair(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP, p) );
// //
// Position Texture shader // Position Texture shader
// //
@ -202,6 +210,12 @@ void ShaderCache::reloadDefaultShaders()
p->reset(); p->reset();
loadDefaultShader(p, kShaderType_PositionColor); loadDefaultShader(p, kShaderType_PositionColor);
//
// Position, Color shader no MVP
//
p = getProgram(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP);
loadDefaultShader(p, kShaderType_PositionColor_noMVP);
// //
// Position Texture shader // Position Texture shader
// //
@ -288,6 +302,12 @@ void ShaderCache::loadDefaultShader(GLProgram *p, int type)
p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION); p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR); p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
break;
case kShaderType_PositionColor_noMVP:
p->initWithVertexShaderByteArray(ccPositionTextureColor_noMVP_vert ,ccPositionColor_frag);
p->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
p->addAttribute(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
break; break;
case kShaderType_PositionTexture: case kShaderType_PositionTexture:
p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag); p->initWithVertexShaderByteArray(ccPositionTexture_vert ,ccPositionTexture_frag);