Merge pull request #7008 from visiblelight/ProgramState

Fix issue 5393
This commit is contained in:
minggo 2014-06-09 11:47:57 +08:00
commit 48443b8d52
4 changed files with 43 additions and 8 deletions

View File

@ -497,9 +497,21 @@ void GLProgramState::setUniformTexture(const std::string &uniformName, GLuint te
{ {
auto v = getUniformValue(uniformName); auto v = getUniformValue(uniformName);
if (v) if (v)
v->setTexture(textureId, _textureUnitIndex++); {
if (_boundTextureUnits.find(uniformName) != _boundTextureUnits.end())
{
v->setTexture(textureId, _boundTextureUnits[uniformName]);
}
else else
{
v->setTexture(textureId, _textureUnitIndex);
_boundTextureUnits[uniformName] = _textureUnitIndex++;
}
}
else
{
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str()); CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
}
} }

View File

@ -191,6 +191,7 @@ protected:
bool _uniformAttributeValueDirty; bool _uniformAttributeValueDirty;
std::unordered_map<std::string, UniformValue> _uniforms; std::unordered_map<std::string, UniformValue> _uniforms;
std::unordered_map<std::string, VertexAttribValue> _attributes; std::unordered_map<std::string, VertexAttribValue> _attributes;
std::unordered_map<std::string, int> _boundTextureUnits;
int _textureUnitIndex; int _textureUnitIndex;
uint32_t _vertexAttribsFlags; uint32_t _vertexAttribsFlags;

View File

@ -732,7 +732,7 @@ bool ShaderGlow::init()
// //
// ShaderMultiTexture // ShaderMultiTexture
// //
ShaderMultiTexture::ShaderMultiTexture() ShaderMultiTexture::ShaderMultiTexture():_changedTextureId(0)
{ {
init(); init();
} }
@ -785,34 +785,53 @@ bool ShaderMultiTexture::init()
// Right: normal sprite // Right: normal sprite
auto right = Sprite::create("Images/grossinis_sister2.png"); auto right = Sprite::create("Images/grossinis_sister2.png");
addChild(right); addChild(right, 0, rightSpriteTag);
right->setPosition(s.width*3/4, s.height/2); right->setPosition(s.width*3/4, s.height/2);
// Center: MultiTexture // Center: MultiTexture
_sprite = Sprite::create("Images/grossinis_sister1.png"); _sprite = Sprite::createWithTexture(left->getTexture());
Texture2D *texture1 = Director::getInstance()->getTextureCache()->addImage("Images/grossinis_sister2.png");
addChild(_sprite); addChild(_sprite);
_sprite->setPosition(Vec2(s.width/2, s.height/2)); _sprite->setPosition(Vec2(s.width/2, s.height/2));
auto glprogram = GLProgram::createWithFilenames("Shaders/example_MultiTexture.vsh", "Shaders/example_MultiTexture.fsh"); auto glprogram = GLProgram::createWithFilenames("Shaders/example_MultiTexture.vsh", "Shaders/example_MultiTexture.fsh");
auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram); auto glprogramstate = GLProgramState::getOrCreateWithGLProgram(glprogram);
_sprite->setGLProgramState(glprogramstate); _sprite->setGLProgramState(glprogramstate);
glprogramstate->setUniformTexture("u_texture1", texture1); glprogramstate->setUniformTexture("u_texture1", right->getTexture());
glprogramstate->setUniformFloat("u_interpolate",0.5); glprogramstate->setUniformFloat("u_interpolate",0.5);
// slider // slider
createSliderCtl(); createSliderCtl();
// menu
auto label = Label::createWithTTF(TTFConfig("fonts/arial.ttf"), "change");
auto mi = MenuItemLabel::create(label, CC_CALLBACK_1(ShaderMultiTexture::changeTexture, this));
auto menu = Menu::create(mi, nullptr);
addChild(menu);
menu->setPosition(s.width * 7 / 8, s.height / 2);
return true; return true;
} }
return false; return false;
} }
void ShaderMultiTexture::changeTexture(Ref*)
{
static const int textureFilesCount = 3;
static const std::string textureFiles[textureFilesCount] = {
"Images/grossini.png",
"Images/grossinis_sister1.png",
"Images/grossinis_sister2.png"
};
auto textrue = Director::getInstance()->getTextureCache()->addImage(textureFiles[_changedTextureId++ % textureFilesCount]);
Sprite* right = dynamic_cast<Sprite*>(getChildByTag(rightSpriteTag));
right->setTexture(textrue);
auto programState = _sprite->getGLProgramState();
programState->setUniformTexture("u_texture1", right->getTexture());
}
///--------------------------------------- ///---------------------------------------
// //

View File

@ -166,9 +166,12 @@ public:
class ShaderMultiTexture : public ShaderTestDemo class ShaderMultiTexture : public ShaderTestDemo
{ {
static const int rightSpriteTag = 2014;
public: public:
ShaderMultiTexture(); ShaderMultiTexture();
ui::Slider* createSliderCtl(); ui::Slider* createSliderCtl();
void changeTexture(Ref*);
int _changedTextureId;
Sprite *_sprite; Sprite *_sprite;
virtual std::string title() const override; virtual std::string title() const override;