简体   繁体   English

Cocos2dx CCRendertexture和framebuffer对象问题。 我的新纹理看起来不一样。 Alpha值不正确

[英]Cocos2dx CCRendertexture and framebuffer object problems. My new texture doesn't look the same. Alpha values are incorrect

I'm having a bit of a problem trying to use the CCRenderTexture class from Cocos2Dx for the iPhone (which, as I understand it, is just a FBO class). 尝试将Cocos2Dx中的CCRenderTexture类用于iPhone(据我了解,它只是一个FBO类),我遇到了一些问题。

I have a class that derives from CCSprite. 我有一个从CCSprite派生的类。 Within it, I'm trying to draw a texture to a CCrenderTexture and then save it for later. 在其中,我尝试将纹理绘制到CCrenderTexture,然后将其保存以供以后使用。 However, I noticed that the alpha values in my new texture seem to have been intensified. 但是,我注意到新纹理中的alpha值似乎已经增强。 My new texture appears a bit more faded than the original, it only seems to effect pixels with an alpha less then 1.0. 我的新纹理看起来比原始纹理更加褪色,似乎只影响alpha小于1.0的像素。

Here is the code I am using: 这是我正在使用的代码:

CCRenderTexture* myRenderTexture = CCRenderTexture::create((int)getContentSize().width, (int)getContentSize().height);   //set RenderTexture to same size as sprite

myRenderTexture->begin();

CC_NODE_DRAW_SETUP();

ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);

ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);      //Is this where my problem is?  

glActiveTexture(GL_TEXTURE0);


glBindTexture( GL_TEXTURE_2D, finalTexture->getName());     //finalTexture is created before this code is run
glUniform1i(textureLocation, 0);


long offset = (long)&m_sQuad;

// vertex
int diff = offsetof( ccV3F_C4B_T2F, vertices);
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
// texCoods
diff = offsetof( ccV3F_C4B_T2F, texCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
// color
diff = offsetof( ccV3F_C4B_T2F, colors);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glActiveTexture(GL_TEXTURE0);


myRenderTexture->end();
finalTexture = myRenderTexture->getSprite()->getTexture();   //Replace Final Texture with my new texture (Should be exact same)

finalTexture->retain();
myRenderTexture->release();

As a test, I ran the above code 4 times in succession. 作为测试,我连续运行了上述代码4次。 Each time, the texture seemed to fade a bit more. 每次,纹理似乎都会褪色一些。 But what I expect is it to look the same each time. 但是我希望它每次看起来都一样。

The next step I took was to replace ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) It seemed to solve my problem, as the resulting texture looked the same no matter how many times I ran it. 我下一步是将ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)替换为ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)这似乎解决了我的问题,因为无论运行多少次,生成的纹理看起来都一样。 However, I am also running a custom fragment shader on this texture (shader not pictured and not running during this test) and it requires ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) to work correctly. 但是,我也在此纹理上运行自定义片段着色器(该着色器未显示且在此测试期间未运行),并且需要ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)才能正常工作。

After some research, I tried replacing ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA) . 经过一些研究,我尝试用glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA)替换ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA) I hoped this would maintain my alpha levels, while changing the color values. 我希望这可以保持我的Alpha水平,同时更改颜色值。 Unfortunately, this had the opposite effect and seemed to actually dull the alpha, causing the texture to darken. 不幸的是,这产生了相反的效果,并且似乎实际上使Alpha变暗,导致纹理变暗。

Here is a picture with my three tests. 这是我的三个测试的照片。 This bikini bottom asset makes it easy to see the effect ;) 这种比基尼泳裤的底色很容易看到效果;)

在此处输入图片说明

Can someone tell me what I am doing doing wrong and what is causing these effects? 有人可以告诉我我在做什么错,是什么导致了这些后果? Is it my BlendFunc ? 是我的BlendFunc吗? I need to be able to draw to a CCRenderTexture (or a FBO) several times in succession and I don't want any undesired effects on the image. 我需要能够连续多次绘制CCRenderTexture (或FBO),并且我不希望对图像产生任何不良影响。 I also need to use my shaders… so ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) doesn't work... 我还需要使用我的着色器...所以ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)不起作用...

Any help is appreciated! 任何帮助表示赞赏! Thanks in advance! 提前致谢!

I was able to find a solution that worked for me. 我找到了适合我的解决方案。 I'm not sure if it's ideal or if there is a better way, but it seems to work. 我不确定这是理想的还是有更好的方法,但是似乎可行。

As I said before, using ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) allowed me to use a CCRenderTexture as many times as I wanted, but my shaders didn't work correctly. 如我之前所说,使用ccGLBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)允许我多次使用CCRenderTexture ,但是我的着色器无法正常工作。

Changing to glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA) allowed the shaders, but the texture darkened each time I rendered it to the CCRenderTexture . 更改为glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA)允许着色器,但每次将其渲染到CCRenderTexture时,纹理都会变暗。

After comparing the textures in Photoshop, it appeared that glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA) would multiply my color values by my alpha value. 在Photoshop中比较纹理之后,看来glBlendFuncSeparate(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA,GL_ONE, GL_SRC_ALPHA)会将我的颜色值乘以我的alpha值。 Which makes sense I suppose, because I am setting the color blend to GL_ONE_MINUS_SRC_ALPHA . 我想这很有意义,因为我将颜色混合设置为GL_ONE_MINUS_SRC_ALPHA

So my solution was to reverse this effect by then dividing my color values by the alpha value within the shader. 因此,我的解决方案是通过将我的颜色值除以着色器中的alpha值来反转此效果。 I added these lines to the end of my shader. 我将这些行添加到了着色器的末尾。

texColor.r = texColor.r / texColor.a;      \n\
texColor.b = texColor.b / texColor.a;      \n\
texColor.g = texColor.g / texColor.a;      \n\ 

It seems a little silly, I would prefer to find a way that doesn't make these changes to the color at all, because this step is wasteful. 似乎有点愚蠢,我宁愿找到一种根本不对颜色进行这些更改的方法,因为此步骤很浪费。 But it works! 但这有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM