Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ iPhone OpenGL ES 2.0“渲染到纹理”(render to texture)将颜色值相乘,而不是相加_C++_Ios_Iphone_Opengl Es_Opengl Es 2.0 - Fatal编程技术网

C++ iPhone OpenGL ES 2.0“渲染到纹理”(render to texture)将颜色值相乘,而不是相加

C++ iPhone OpenGL ES 2.0“渲染到纹理”(render to texture)将颜色值相乘,而不是相加,c++,ios,iphone,opengl-es,opengl-es-2.0,C++,Ios,Iphone,Opengl Es,Opengl Es 2.0,我正在尝试使用openGL ES 2.0在iPhone上渲染纹理 为了测试,我渲染了一个多边形(只是一个简单的圆)。圆应该渲染为完全红色(rgba颜色为(1,0,0,1)),但出于某种原因,它的结果颜色将是它的颜色与我初始化渲染纹理的颜色的乘积 例如: Texture init color : (1, 1, 1, 1) Circle color : (1, 0, 0, 1) Resulting circle color : (1, 0,

我正在尝试使用openGL ES 2.0在iPhone上渲染纹理

为了测试,我渲染了一个多边形(只是一个简单的圆)。圆应该渲染为完全红色(rgba颜色为(1,0,0,1)),但出于某种原因,它的结果颜色将是它的颜色与我初始化渲染纹理的颜色的乘积

例如:

Texture init color :     (1,   1,   1,   1)
Circle color :           (1,   0,   0,   1)
Resulting circle color : (1,   0,   0,   1) //This is as expected

Texture init color :     (0.5, 0.5, 0.5, 1) 
Circle color :           (1,   0,   0,   1) 
Resulting circle color : (0.5, 0,   0,   1) //This is unexpected.  
背景色根本不应该考虑在内

如果我用深红色(0.5,0,0,1)渲染圆,它将颜色相乘而不是相加的事实更为明显

Texture init color :     (1,   1,   1,   1)
Circle color :           (0.5, 0,   0,   1)
Resulting circle color : (0.5, 0,   0,   1) //This is as expected

Texture init color :     (0.5,  0.5, 0.5, 1)
Circle color :           (0.5,  0,   0,   1)
Resulting circle color : (0.25, 0,   0,   1) //This is unexpected.  
我的直觉是这是混合模式的问题,但即使我将glBlendFunc设置为(GL_ONE,GL_ZERO),问题仍然存在

这也可能是因为渲染使用了预乘值,但我在这里使用的是非常简单的值和完整的alpha值,这应该忽略这个问题,但我可能错了

问题:为什么颜色与背景色相乘


我的渲染器设置:

glBindTexture(GL_TEXTURE_2D, 0);

glGenFramebuffers(1, &m_frameBuffer);
glGenTextures(1, &m_textureId);

glBindFramebuffer(GL_FRAMEBUFFER, m_frameBuffer);
glBindTexture(GL_TEXTURE_2D, m_textureId);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, MathUtil::NextPowerOf2(m_width), MathUtil::NextPowerOf2(m_height), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_data);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureId, 0);

glViewport(0, 0, m_width, m_height);
渲染对象时,我调用以下命令:

glEnable(GL_BLEND);
glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO);

glDrawElements(GL_TRIANGLES, (GLsizei)(m_offset / 4) * 6, GL_UNSIGNED_SHORT, (void*)0);
最后,我使用glReadPixels从渲染纹理中读取值(我相信iPhone的openGL ES 2.0不提供glGetTexImage)以确认这些值

glReadPixels(0, 0, MathUtil::NextPowerOf2(m_width), MathUtil::NextPowerOf2(m_height), GL_RGBA, GL_UNSIGNED_BYTE, m_data);

经过大量测试后,发现出现问题的原因是openGL设置在将渲染目标从屏幕设置到纹理之间没有正确重置(反之亦然)

我的解决方案是在设置目标后刷新所有gl绑定和调用(如着色器程序)


不幸的是,我不知道到底是哪个电话或电话组合导致了这种奇怪的行为。

odd。如果禁用混合会发生什么情况?禁用混合不会更改生成的rgba(尽管它会更改其显示方式)。进一步测试后,我发现,如果没有其他内容渲染到屏幕上,renderToTarget方法会起作用。如果渲染其他多边形(到屏幕,而不是渲染纹理),则会导致此行为。这表明我正在使用的其他gl调用导致了该行为。然而,我甚至不知道从哪里开始得到这种效果。我将隔离代码,用这些新发现更新问题