Hi there,
I created a custom command to draw my terrain like this:
void ForegroundTerrain::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags){
mCustomCommand.init(_globalZOrder);
mCustomCommand.func = CC_CALLBACK_0(ForegroundTerrain::onDraw, this, transform, flags);
renderer->addCommand(&mCustomCommand);
}
void ForegroundTerrain::onDraw(const cocos2d::Mat4 &transform, uint32_t flags){
auto glProgram = getGLProgram();
glProgram->use();
glProgram->setUniformsForBuiltins(transform);
GL::bindTexture2D(mTexture->getName());
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD);
for (int i = 0; i < _terrain->mForegroundGroundTextureArray.size(); i++) {
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _terrain->mForegroundGroundTextureArray[i].values);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _terrain->mForegroundTextureArray[i].values);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_terrain->mForegroundTextureLength[i]);
CC_INCREMENT_GL_DRAWS(1);
}
}
This works fine - but now I would like to dynamically brighten//darken the Texture2D (mTexture) that I use. Is there a way of doing this within this loop? Manipulating the texture to brighten/darken it every iteration before I draw the strip. Is this possible somehow?
Best regards
Rambazamba
I will try to use Sprite instead. with sprite I can do any thing I want ( set Opacity, Color, v.v… ).
Sprite* ForegroundTerrain::getSprite()
{
// 1: Create new CCRenderTexture
RenderTexture *rt = RenderTexture::create(s.width, s.height);
// 2: Call CCRenderTexture:begin
rt->begin();
// 3: Draw into the texture
//------------------------------------------
mCustomCommand.init(_globalZOrder);
mCustomCommand.func = CC_CALLBACK_0(ForegroundTerrain::drawing, this);
auto renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_customCommand);
//------------------------------------------
// 4: Call CCRenderTexture:end
rt->end();
// 5: Create a new Sprite from the texture
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
//add the code of onDraw(...) to drawing()
void ForegroundTerrain::drawing()
{
auto glProgram = getGLProgram();
glProgram->use();
glProgram->setUniformsForBuiltins(transform);
GL::bindTexture2D(mTexture->getName());
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD);
for (int i = 0; i < _terrain->mForegroundGroundTextureArray.size(); i++) {
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _terrain->mForegroundGroundTextureArray[i].values);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _terrain->mForegroundTextureArray[i].values);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_terrain->mForegroundTextureLength[i]);
CC_INCREMENT_GL_DRAWS(1);
}
}
Sorry for the late reply(I was sick for a few days) and thank you for your answer.
I was not clear in the first post - what I wanted to do is to brighten/darken the texture for each GL_TRIANGLE_STRIP I draw. So I would have an object that is bright in the beginning and dark in the end. I am just beginning to do stuff with OpenGL so I thought there might be an easy way change the brightness in the loop.