Hello to evereybody.
so i decided to play with this example http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x
My version of cocos framework is 3-alpha.
So my problem- I can’t do gradient in my texture. Here is my code
#pragma once
#include "cocos2d.h"
USING_NS_CC;
class DynamicTextureScene : public cocos2d::Layer
{
private:
CCSprite *_background=nullptr;
ccColor4F randomBrightColor();
EventListenerTouchOneByOne *listenerTouchOneByOne;
public:
static cocos2d::Scene* createScene();
virtual bool init();
void generateBgByTouch();
void genBackground();
CCSprite* spriteWithColor(ccColor4F bgColor, float textureWidth, float textureHeight);
void initListeners();
CREATE_FUNC(DynamicTextureScene);
};
cpp file
Sprite* DynamicTextureScene::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
rt->end();
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
Everything is fine i see texture with random color. So i decided to add gradient, after it my function became like this
Sprite* DynamicTextureScene::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
this->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
CC_NODE_DRAW_SETUP();
// 3: Draw into the texture
float gradientAlpha = 0.7f;
CCPoint vertices[4];
ccColor4F colors[4];
int nVertices = 0;
vertices[nVertices] = CCPointMake(0, 0);
colors[nVertices++] = ccc4f(0, 0, 0, 0);
vertices[nVertices] = CCPointMake(textureWidth, 0);
colors[nVertices++] = ccc4f(0, 0, 0, 0);
vertices[nVertices] = CCPointMake(0, textureHeight);
colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);
vertices[nVertices] = CCPointMake(textureWidth, textureHeight);
colors[nVertices++] = ccc4f(0, 0, 0, gradientAlpha);
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
rt->end();
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
As result there is no gradient. After it i googled a little and found this solution.
Header file
#include "cocos2d.h"
USING_NS_CC;
class DynamicTextureScene : public cocos2d::Layer
{
private:
CCSprite *_background=nullptr;
ccColor4F randomBrightColor();
EventListenerTouchOneByOne *listenerTouchOneByOne;
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
virtual bool init();
void generateBgByTouch();
void genBackground();
CCSprite* spriteWithColor(ccColor4F bgColor, float textureWidth, float textureHeight);
void initListeners();
void onDraw();
CREATE_FUNC(DynamicTextureScene);
};
Added method onDraw.
Cpp file.
Sprite* DynamicTextureScene::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
CustomCommand _customCommand;
_customCommand.init(rt->getGlobalZOrder());
_customCommand.func = CC_CALLBACK_0(DynamicTextureScene::onDraw, this);
auto renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_customCommand);
rt->end();
return Sprite::createWithTexture(rt->getSprite()->getTexture());
}
void DynamicTextureScene::onDraw()
{
this->setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
this->getShaderProgram()->use();
this->getShaderProgram()->setUniformsForBuiltins();
Size winSize = Director::getInstance()->getVisibleSize();
float gradientAlpha = 0.7;
Vertex2F vertices[4];
Color4F colors[4];
int nVertices = 0;
float width = 512;
vertices[nVertices] = { 0, 0 };
colors[nVertices++] = { 0, 0, 0, 0 };
vertices[nVertices] = { width, 0 };
colors[nVertices++] = { 0, 0, 0, 0 };
vertices[nVertices] = { 0, width };
colors[nVertices++] = { 0, 0, 0, gradientAlpha };
vertices[nVertices] = { 2, width };
colors[nVertices++] = { 0, 0, 0, gradientAlpha };
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_COLOR | GL::VERTEX_ATTRIB_FLAG_POSITION);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
}
So in debug i never go to onDraw method, can anybody show workable example for c0c0s2d-x 3 version or find bug in my code? Because this solution [Solved] glDrawArrays doesn't work. is not workable. Pls show me workable header and cpp file!