From fcf4d79b065aabeeb0768979725855aeff0cf2b2 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Wed, 20 Aug 2014 19:01:04 +0800 Subject: [PATCH] setGLProgramState support --- cocos/2d/CCNode.h | 4 ++-- cocos/3d/CCSprite3D.cpp | 23 ++++++++++++++++++++--- cocos/3d/CCSprite3D.h | 6 ++++++ cocos/3d/CCSubMeshState.cpp | 2 ++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 3d4d7500b4..186d8b3fca 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -961,7 +961,7 @@ public: CC_DEPRECATED_ATTRIBUTE GLProgram* getShaderProgram() const { return getGLProgram(); } GLProgramState *getGLProgramState() const; - void setGLProgramState(GLProgramState *glProgramState); + virtual void setGLProgramState(GLProgramState *glProgramState); /** * Sets the shader program for this node @@ -974,7 +974,7 @@ public: * * @param shaderProgram The shader program */ - void setGLProgram(GLProgram *glprogram); + virtual void setGLProgram(GLProgram *glprogram); CC_DEPRECATED_ATTRIBUTE void setShaderProgram(GLProgram *glprogram) { setGLProgram(glprogram); } /// @} end of Shader Program diff --git a/cocos/3d/CCSprite3D.cpp b/cocos/3d/CCSprite3D.cpp index db52c4ff7a..93d9828ae5 100644 --- a/cocos/3d/CCSprite3D.cpp +++ b/cocos/3d/CCSprite3D.cpp @@ -414,7 +414,9 @@ void Sprite3D::setTexture(const std::string& texFile) void Sprite3D::setTexture(Texture2D* texture) { - _subMeshStates.at(0)->setTexture(texture); + for (auto& state : _subMeshStates) { + state->setTexture(texture); + } } AttachNode* Sprite3D::getAttachNode(const std::string& boneName) { @@ -474,8 +476,6 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) auto submesh = submeshstate->getSubMesh(); meshCommand.init(_globalZOrder, textureID, programstate, _blend, submesh->getMesh()->getVertexBuffer(), submesh->getIndexBuffer(), (GLenum)submesh->getPrimitiveType(), (GLenum)submesh->getIndexFormat(), submesh->getIndexCount(), transform); - meshCommand.setCullFaceEnabled(true); - meshCommand.setDepthTestEnabled(true); auto skin = submeshstate->getSkin(); if (skin) { @@ -488,11 +488,28 @@ void Sprite3D::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) } } +void Sprite3D::setGLProgramState(GLProgramState *glProgramState) +{ + Node::setGLProgramState(glProgramState); + for (auto& state : _subMeshStates) { + state->setGLProgramState(glProgramState); + } +} +void Sprite3D::setGLProgram(GLProgram *glprogram) +{ + Node::setGLProgram(glprogram); + setGLProgramState(getGLProgramState()); +} + void Sprite3D::setBlendFunc(const BlendFunc &blendFunc) { if(_blend.src != blendFunc.src || _blend.dst != blendFunc.dst) { _blend = blendFunc; + for(auto& state : _subMeshStates) + { + state->setBlendFunc(blendFunc); + } } } diff --git a/cocos/3d/CCSprite3D.h b/cocos/3d/CCSprite3D.h index 6ffff020c9..e547e551d4 100644 --- a/cocos/3d/CCSprite3D.h +++ b/cocos/3d/CCSprite3D.h @@ -91,6 +91,12 @@ public: virtual void setBlendFunc(const BlendFunc &blendFunc) override; virtual const BlendFunc &getBlendFunc() const override; + // overrides + /** set GLProgramState, you should bind attributes by yourself */ + virtual void setGLProgramState(GLProgramState *glProgramState) override; + /** just rember bind attributes */ + virtual void setGLProgram(GLProgram *glprogram) override; + /* * Get AABB * If the sprite has animation, it can't be calculated accuratly, diff --git a/cocos/3d/CCSubMeshState.cpp b/cocos/3d/CCSubMeshState.cpp index c89e8885c2..99819279d5 100644 --- a/cocos/3d/CCSubMeshState.cpp +++ b/cocos/3d/CCSubMeshState.cpp @@ -169,6 +169,8 @@ void SubMeshState::bindMeshCommand() { GLuint texID = _texture ? _texture->getName() : 0; _meshCommand.genMaterialID(texID, _glProgramState, _subMesh->getMesh()->getVertexBuffer(), _subMesh->getIndexBuffer(), _blend); + _meshCommand.setCullFaceEnabled(true); + _meshCommand.setDepthTestEnabled(true); } }