Change color of polygon in DrawNode

Hi!

I’m making a game which is based on polygons changing colors, fading between them.
Is there a way to have a variable color for a polygon, instead of redrawing the polygon for each change in color, or is there another approach to this?

possibly shaders?
but what do you mean instead of redrawing? everything gets redrawn on every frame right?
are you having slow frame rate or there’s something wrong with your current implementation?

Yeah, i mean when you go drawNode->drawPolygon(…) that polygon “persists” between draws, I only have to do it at initialization.
Sure, OpenGL redraws the poly every frame, but I would guess that some vertex buffers are used here, and not upload new vertices every frame, which will be the case if i clear and re-create the poly each frame.

I’ll have a look at shaders :slight_smile:

I see. :slight_smile:
yah, in that case using VBO/IBO with static mode is perfect solution.

So I’m having some problems using a custom shader :confused:

I do this in the constructor of my DrawNode subclass:

GLProgram * program = new GLProgram();
program->initWithVertexShaderFilename("CRVertShader.glsl", "CRFragShader.glsl");
program->link();

this->setShaderProgram(program);

I use a basic shader but overriding fragColor just to se if it sticks, but it doesn’t:

varying vec4 v_fragmentColor;

void main()
{
    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); // should make everything green
}

Is there something else I need to do to make the DrawNode use my shader program?

To be honest with you i still don’t know how to use cocos2dx-3 and just a little bit of 2.0
But on openGL perspective, i can see that you have your shaders compiled and linked but you still have to call glUseProgram.
I just scan quickly on the documentation on the GLProgram and it has a GLProgram.use method so try that and see the result.
I notice a ShaderCache class under shaders but i don’t know what its use, well maybe to cache a shader program or load a default one (silly me :;qst ), just basing on the methods available.
Let me know if it works.

Tried ->use() like this, still no cigarr :frowning:

GLProgram * program = new GLProgram();
program->autorelease();
program->initWithVertexShaderFilename("CRVertShader.glsl", "CRFragShader.glsl");
program->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
program->link();
program->use();
this->setShaderProgram(program);

Thanks for your input though, golden! :smiley:

Ah, I found the problem!
I needed to do this in the init() method, not in the constructor.
Now I have a very handsome green circle!

yes perfect :;ok

Hi could you please share the code of your shaders?

I tried this code:

GLProgram * program = new GLProgram();
program->autorelease();
program->initWithVertexShaderFilename("shaders/test.vsh", "shaders/test.fsh");
program->addAttribute(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
program->link();
program->use();
node->setShaderProgram(program);
node->drawCircle(Vec2(0, 0), 50, 360, 360, false, Color4F::BLUE);
addChild(node);

and this sets of shaders:

#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
varying mediump vec2 v_screenY;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
varying float v_screenY;
#endif

void main()
{

    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}



#ifdef GL_ES
attribute mediump vec4 a_position;
attribute mediump vec2 a_texcoord;
attribute mediump vec4 a_color;

varying mediump vec4 v_color;
varying mediump vec2 v_texcoord;

#else

attribute vec4 a_position;
attribute vec2 a_texcoord;
attribute vec4 a_color;

varying vec4 v_color;
varying vec2 v_texcoord;

#endif

void main()
{
    v_color = vec4(a_color.rgb * a_color.a, a_color.a);
    v_texcoord = a_texcoord;

    gl_Position = CC_MVPMatrix * a_position;
}

And I still see blue circle istead of green one. What Do I do wrong?