[Solved] glDrawArrays doesn't work.

Hello,
I have tried to follow this tutorial “http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x” But I got trouble at “Adding a Gradient to the Texture” chapter. I have used this code to add gradient to my texture but that doesn’t work. I have started the game but I didn’t saw any gradient in it. I don’t know what the issue is but I think it is glDrawArrays but It can possibly be that all functions I use are decrepated because I am using cocos2d-x 3.0 final. This is my Spritewithcolor function.

Sprite* HelloWorld::spriteWithColor(Color4F bgColor, float textureWidth, float textureHeight)
{
	// 1: Create new CCRenderTexture
	RenderTexture* rt = RenderTexture::create(textureWidth, textureHeight);

	// 2: Call CCRenderTexture:begin
	rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
	
	// 3: Draw into the texture
	float gradientAlpha = 1;
	Point vertices[4];
	Color4F colors[4];
	int nVertices = 0;

	vertices[nVertices] = Point(0, 0);
	colors[nVertices++] = Color4F(0, 0, 0, 0);
	vertices[nVertices] = Point(textureWidth, 0);
	colors[nVertices++] = Color4F(0, 0, 0, 0);
	vertices[nVertices] = Point(0, textureHeight);
	colors[nVertices++] = Color4F(0, 0, 0, gradientAlpha);
	vertices[nVertices] = Point(textureWidth, textureHeight);
	colors[nVertices++] = Color4F(0, 0, 0, gradientAlpha);

	// Set the shader program for OpenGL
	this->setShaderProgram(ShaderCache::getInstance()->getProgram(kCCShader_PositionColor));
	CC_NODE_DRAW_SETUP();

	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);
	GL::blendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);


	Sprite* noise = Sprite::create("Noise.png");

	BlendFunc myBlend;
	myBlend.src = GL_DST_COLOR;
	myBlend.dst = GL_ZERO;
	noise->setBlendFunc(myBlend);

	noise->setPosition(Point(textureWidth/2, textureHeight/2));
	noise->visit();    
 
    // 4: Call CCRenderTexture:end
	rt->end();

	// 5: Create a new Sprite from the texture
	return Sprite::createWithTexture(rt->getSprite()->getTexture()); 
}

Can someone please help me because I am a bit new to texturing.

Thanks for reading.

@egordm

I will check out the tutorial later and give you my research result.

@egordm

Sprite* HelloWorld::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
    RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);
    
    rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
   
    
    // 3: Draw into the texture
    //Caution!!!  if you want to use your own opengl draw call ,you must wrap them with a custom command
    _customCommand.init(rt->getGlobalZOrder());
    _customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw,this);
    auto renderer = Director::getInstance()->getRenderer();
    renderer->addCommand(&_customCommand);
    
    //draw into texture
    _noiseSprite->setBlendFunc({GL_DST_COLOR, GL_ZERO});
    _noiseSprite->setPosition(Vec2(textureWidth/2, textureHeight/2));
    _noiseSprite->visit();

    rt->end();

    
    return Sprite::createWithTexture(rt->getSprite()->getTexture());
}


void HelloWorld::onDraw()
{
    this->setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
    this->getGLProgram()->use();
    this->getGLProgram()->setUniformsForBuiltins();
    
    
    Size winSize = Director::getInstance()->getVisibleSize();
    
    
    float gradientAlpha =  0.7;
    Vec2 vertices[4];
    Color4F colors[4];
    int nVertices = 0;
    
    vertices[nVertices] = Vec2(0, 0);
    colors[nVertices++] = (Color4F){0, 0, 0, 0 };
    vertices[nVertices] = Vec2(winSize.width, 0);
    colors[nVertices++] = (Color4F){0, 0, 0, 0};
    vertices[nVertices] = Vec2(0, winSize.height);
    colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};
    vertices[nVertices] = Vec2(winSize.width, winSize.height);
    colors[nVertices++] = (Color4F){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);

}

2 Likes

@owen
Thank you very much! My problem is solved.