I’m trying to add some blurring effect to some texture. For this I have a custom shader that has some uniforms with settings such as the blur radius.
The shader does get applied, however the uniforms don’t get updated somehow.
Their values are all 0, while I try to set them in code, but this seems to have no effect.
void NodeBlur::update(float dt)
{
GLuint pixelSizeLoc = glGetUniformLocation(mBlurX->getProgram(), "pixelSize");
GLuint directionLoc = glGetUniformLocation(mBlurX->getProgram(), "direction");
GLuint radiusLoc = glGetUniformLocation(mBlurX->getProgram(), "sampleRadius");
mBlurX->setUniformLocationWith2f(pixelSizeLoc, mPixelSize.width, mPixelSize.height);
mBlurX->setUniformLocationWith2f(directionLoc, 1.0f, 0.0f);
mBlurX->setUniformLocationWith1i(radiusLoc, 64);
mBlurX->updateUniforms();
mRenderTextureX->beginWithClear(1.0f, 0.0f, 0.0f, 1.0f);
mStepX->visit();
mRenderTextureX->end();
}
mBlurX is the GLProgram.
When reading the uniforms in the shader they are not set to these values somehow. The function does get called. Also trying to directly use the GL functions does not have an effect.
The shader:
`static const GLchar* blurShaderFrag = \
"#ifdef GL_ES \n"\
"precision mediump float; \n"\
"#endif \n"\
" \n"\
"varying vec4 v_fragmentColor; \n"\
"varying vec2 v_texCoord; \n"\
" \n"\
"uniform vec2 pixelSize; \n"\
"uniform vec2 direction; \n"\
"uniform int sampleRadius; \n"\
"uniform float weights[64]; \n"\
" \n"\
"void main() \n"\
"{ \n"\
" gl_FragColor = texture2D(CC_Texture0, v_texCoord) * weights[0]; \n"\
" for (int i=1; i<sampleRadius; i++) \n"\
" { \n"\
" vec2 offset = vec2(float(i)*pixelSize.x*direction.x, float(i)*pixelSize.y*direction.y); \n"\
" gl_FragColor += texture2D(CC_Texture0, v_texCoord + offset)*weights[i]; \n"\
" gl_FragColor += texture2D(CC_Texture0, v_texCoord - offset)*weights[i]; \n"\
" } \n"\
"} \n";
`
the for loop is not getting executed, as sampleRadius seems 0. If I hardcode it to say 64 it works, except that the other uniforms also are 0 values.
Am I doing something weird? I tried updating the GLProgram uniforms in both the constructor and at some update function, but both don’t work. But again, the shader does get executed, so that seems to work all fine. Just the uniforms don’t get updated.
I left out the weights uniform update, as I had to add a function to cocos2d-x to do this. But that is not the problem.
Any suggestions are welcome
I use Cocos2d-x 3.1.1 on Windows 8