Custom shader for cocos2d-x v3.8 to reflect changes later in setUniformLocation

Asked 2 years ago, Updated 2 years ago, 133 views

I'm trying to use a custom shader with cocos2d-x v3.8.

Every time I press the button and menuCallback is called, the _sprite gets brighter.
I'd like to make it shine white in the end, but
The sprite does not light up due to an error and remains black all the time.

Error message immediately following boot
libpng warning:iCCP:known incorrectsRGB profile

Messages that appear when you press the button
OpenGL error 0x0502 in/Users/username/shadertest/cocos2d/cocos/render/CCFrameBuffer.cpp applyFBO444

How can I change the brightness?

HelloWorld.cpp

bool HelloWorld::init()
{
    if(!Layer::init())
    {
        return false;
    }

    Size visibleSize=Director::getInstance()->getVisibleSize();
    Vec2origin=Director::getInstance()->getVisibleOrigin();

    /* Abbreviated*/

    _sprite=Sprite::createWithSpriteFrameName("RRGPlayer/walk/south/0001.png");
    _sprite->setPosition(visibleSize.width/2+origin.x,
                       visibleSize.height/2+origin.y);
    addChild(_sprite,10);

    _glProgram=GLProgram::createWithFilenames("myshader.vsh", "myshader.fsh");
    CC_SAFE_RETAIN(_glProgram);
    _glProgram-> bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    _glProgram-> bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
    _glProgram-> bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
    _glProgram->link();
    menuCallback(nullptr);
    _sprite->setGLProgram(_glProgram);

    return true;
}


void HelloWorld::menuCallback(Ref*pSender)
{
    static float brightness = 0.0f;
    _glProgram->setUniformLocationWith1f(_glProgram->getUniformLocationForName("u_brightness"),brightness);
    _glProgram->updateUniforms();

    brightness+=.1f;
    if(brightness>2){
        brightness = 0;
    }
}

myshader.fsh

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

uniform sampler2Du_texture;
uniform float u_brightness;

void main()
{
    vec4 v_orColor=v_fragmentColor*texture2D(CC_Texture0, v_texCoord);
    gl_FragColor=vec4(v_orColor.r*u_brightness,
                        v_orColor.g*u_brightness,
                        v_orColor.b*u_brightness,
                        v_orColor.a);
}

myshader.vsh

attribute vec4a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

void main()
{
    gl_Position=CC_PMatrix*a_position;
    v_fragmentColor=a_color;
    v_texCoord = a_texCoord;
}

cocos2d-x opengl-es opengl

2022-09-30 21:10

1 Answers

I was able to use GLProgramState

bool HelloWorld::init()
{
    if(!Layer::init())
    {
        return false;
    }

    /* Abbreviated*/

    _sprite=Sprite::createWithSpriteFrameName("RRGPlayer/walk/south/0001.png");
    _sprite->setPosition(visibleSize.width/2+origin.x,
                       visibleSize.height/2+origin.y);
    addChild(_sprite,10);

    GLProgram*glProgram=GLProgram::createWithFilenames("myshader.vsh", "myshader.fsh");
    GLProgramState*state=GLProgramState::getOrCreateWithGLProgram (glProgram);
    _sprite->setGLProgramState(state);

    return true;
}


void HelloWorld::menuCloseCallback (Ref*pSender)
{
    static float brightness = 0.0f;
    GLProgramState*state=_sprite->getGLProgramState();
    state->setUniformFloat("u_brightness", brightness);

    brightness+=.1f;
    if(brightness>2){
        brightness = 0;
    }
}


2022-09-30 21:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.