Hi there, I am using a custom command in a sprite to draw my terrain with open GL using triangle strips.
This works fine however if I set the opacity it does not do a thing.
Here is my code:
Header:
#include "cocos2d.h"
class TerrainBase;
class ForegroundTerrain : public cocos2d::Sprite{
public:
static ForegroundTerrain* create(TerrainBase* terrain);
ForegroundTerrain();
~ForegroundTerrain();
void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags);
void onDraw(const cocos2d::Mat4 &transform, uint32_t flags);
void update(cocos2d::Vec2 carPosition);TerrainBase* _terrain;
private:
cocos2d::Texture2D * mTexture;
cocos2d::CustomCommand mCustomCommand;
cocos2d::Vec2 currentCarPosition;
};
Implementation:
#include "ForegroundTerrain.h"
#include "TerrainBase.h"
#include "GameModel.h"
USING_NS_CC;
ForegroundTerrain* ForegroundTerrain::create(TerrainBase *terrain){
ForegroundTerrain *foregroundTerrain = new ForegroundTerrain();
foregroundTerrain->_terrain = terrain;
return foregroundTerrain;
}
ForegroundTerrain::ForegroundTerrain(){
setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
cocos2d::Texture2D::TexParams tp1 = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
auto *textureSprite = Sprite::create("JungleBackgroundTexture.png");
textureSprite->getTexture()->setTexParameters(tp1);
mTexture = textureSprite->getTexture();
mTexture->retain();
//Does not work :-C
this->setOpacity(100);
autorelease();
}
ForegroundTerrain::~ForegroundTerrain(){
mTexture->release();
}
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);
}
}
void ForegroundTerrain::update(cocos2d::Vec2 carPosition){
}
What am I doing wrong here? The terrain draws perfectly but I am not able to set its opacity.
Best regards,
Rambazamba