Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17070

cocos2d-x 3.9 DrawNode shader setUniformFloat doesn't work

$
0
0

@nalivai wrote:

Hello everybody!

I build for Android under Windows with cocos2d-x 3.9.

This question seems to be not new, but I cannot figure out what's wrong?
I use DrawNode. I wanted it to change its opacity with the following method setOpacity(GLubyte opacity);
Basically I took the same shader as given by default for DrawNode but introduced a uniform called opacity.

This is how I make my new TransNode class

TransNode* TransNode::create()
{
TransNode* ret = new (std::nothrow) TransNode();
if (ret && ret->init())
{
ret->opacity = 255;
ret->fopacity = 1.0f;
ret->autorelease();
//ALL THE SHADER CODE BELOW LOADS THE STUFF CORRECTLY! WORK WITH setOpacity
auto glp = GLProgram::createWithFilenames("ccShader_PositionColorLengthTexture.vert", "ccShader_PositionColorLengthTexture.frag");
//CC_SAFE_RETAIN(glp);
glp->retain();
glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
glp->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
glp->link();
CHECK_GL_ERROR_DEBUG();
glp->updateUniforms();
CHECK_GL_ERROR_DEBUG();

	GLProgramState* state = GLProgramState::getOrCreateWithGLProgram(glp);
	ret->setGLProgramState(state);
	int oploc = glp->getUniformLocationForName("opacity");
	CCLOG("Position of uniform %d",oploc);
	glp->setUniformLocationWith1f(oploc, ret->fopacity);
	//ret->getGLProgramState()->setUniformFloat(oploc, ret->fopacity);
	ret->setShaderProgram(glp);
	glp->use();
	//glp->setUniformLocationWith1f(oploc, 0.0);
	//
}
else
{
	CC_SAFE_DELETE(ret);
}

return ret;

}

This is how I set opacity

void TransNode::setOpacity(GLubyte opac)
{
opacity = opac;
fopacity = ((GLfloat)opac) / 255.0;
_displayedOpacity = _realOpacity = opacity;
getGLProgramState()->setUniformFloat("opacity", fopacity);
//CCLOG("Opacity %d is updated %f",oploc,fopacity);
}

This is my vertex shader where I apply opacity.

#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

uniform float opacity;

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

gl_Position = CC_MVPMatrix * a_position;

}

The code doesn't give any errors. But opacity doesn't change. But if in create() I do setUniformFloat(...) it works. But obviously only once.

Posts: 2

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17070

Trending Articles