shader help

Hi,

I am struggling to get any custom shader examples working correctly in cocos2d-x, and was hoping any experts out there could help me understand what I am doing wrong.

For example, I am trying to do something similar to this shader tutorial:

In the “basic vignette” portion, https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson3#wiki-Step1
he shows how to use the frag coordinate vector length from center displayed as a color from 0 (center, black) to 1.0 (screen edge, white)

This the basic code example to achieve that:

//texture 0
uniform sampler2D u_texture;

//our screen resolution, set from Java whenever the display is resized
uniform vec2 resolution;

//"in" attributes from our vertex shader
varying vec4 vColor;
varying vec2 vTexCoord;

void main() {
    //sample our texture
    vec4 texColor = texture2D(u_texture, vTexCoord);

    //determine origin
    vec2 position = (gl_FragCoord.xy / resolution.xy) - vec2(0.5);

    //determine the vector length of the center position
    float len = length(position);

    //show our length for debugging
    gl_FragColor = vec4( vec3(len), 1.0 );
}

The problem when I try that, is I get full white. I am using the ShaderSprite code from the TestCpp project with the following shader:

// interpolated between vertices
varying vec4 v_fragmentColor; // color result for this fragment
varying vec2 v_texCoord;

uniform sampler2D CC_Texture0;
uniform vec2 resolution; //our screen resolution

void main()
{
    //determine origin
    vec2 position = (gl_FragCoord.xy / resolution.xy) - vec2(0.5);

    //determine the vector length of the center position
    float len = length(position); 

    //show our length for debugging
    gl_FragColor = vec4( vec3(len), 1.0 );
}   

My ShaderSprite code (again, extending the ShaderSprite code found in TestCpp)

class VignetteSprite : public ShaderSprite, public ShaderSpriteCreator
{
public: 
    VignetteSprite();
protected:
    virtual void buildCustomUniforms();
    virtual void setCustomUniforms();
    GLfloat _resolution[2];
    GLuint _resolutionLoc;
};  
VignetteSprite::VignetteSprite() {
    _fragSourceFile = "Vignette.frag";
}   
void VignetteSprite::setCustomUniforms() {
    _resolutionLoc = glGetUniformLocation( getShaderProgram()->getProgram(), "resolution");
}
void VignetteSprite::buildCustomUniforms() {
    _resolution[0] = getTexture()->getContentSizeInPixels().width;
    _resolution[1] = getTexture()->getContentSizeInPixels().height;

    getShaderProgram()->setUniformLocationWith2fv(_resolutionLoc, _resolution, 1);
}   

Any help greatly appreciated. I’ve already run into other shader questions which I now think is related to this, and help you can provide will likely go a long way towards my understanding what I am missing

Thanks

How do you create an instance of VignetteSprite? ShaderSpriteCreator::createSprite(shader_file_name) should be used. This line of code

_fragSourceFile = "Vignette.frag";

in VignetteSprite constructor confuses me.

It is from the ShaderTest2.cpp test from the TestCpp sample (cocos2d-x/samples/Cpp/TestCpp/Classes/ShaderTest/ShaderTest2.cpp)

template 
class ShaderSpriteCreator
{
public:
    static spriteType* createSprite(const char* pszFileName) {
        spriteType* pRet = new spriteType();
        bool success = false;
        if (pRet) {
            if(strlen(pszFileName) > 0) {
                success = pRet->initWithFile(pszFileName);
            }
            else {
                success = pRet->init();
            }
        }

        if(success) { 
            pRet->autorelease();
            return pRet;
        }
        CC_SAFE_DELETE(pRet);
        return NULL;
    }
};  

class ShaderSprite : public Sprite
{
public:
    ShaderSprite();
    ~ShaderSprite();

    bool initWithTexture(Texture2D* texture, const Rect&  rect);
    void draw();
    void initProgram();
    void listenBackToForeground(Object *obj);

protected:
    virtual void buildCustomUniforms() = 0;
    virtual void setCustomUniforms() = 0;
protected:
    std::string _fragSourceFile;

};