如何使用OpenGL避免透明度重叠? [英] How to avoid transparency overlap using OpenGL?

查看:85
本文介绍了如何使用OpenGL避免透明度重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iOS上的手写应用程序.我从iOS文档中找到了由OpenGL ES实现的示例项目"GLPaint",并对它进行了一些修改.

我跟踪触摸点并计算这些点之间的曲线,并单独绘制颗粒图像以使其看起来像手指经过的地方.

  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,宽度,高度,0,GL_RGBA,GL_UNSIGNED_BYTE,brushData);//burshData来自CGImage,它是//vertexBuffer是基于计算出的点生成的,它只是需要绘制图像的点序列.glVertexPointer(2,GL_FLOAT,0,vertexBuffer);glDrawArrays(GL_POINTS,0,vertexCount);glBindRenderbufferOES(GL_RENDERBUFFER_OES,viewRenderbuffer);[context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

我得到的是一条看起来不错的实线.但是现在我想绘制半透明的高光而不是实线.因此,我将粒子图像替换为透明度为50%的图像,而无需更改代码.

我使用半透明的粒子图像绘制三个点,并且相交区域应保持50%的透明度.

有什么解决方案?

解决方案

也许是两年后我回答了这个问题,但是我希望它能帮助来这里寻求解决此问题的人,就像我遇到的那样.

您将需要为每个线圈分配不同的z值.不管差异有多大或多小,我们只需要它们不严格相等即可.

首先,禁用在颜色缓冲区 glColorMask(false,false,false,false)中的书写,然后正常绘制圆圈.Z缓冲区将根据需要进行更新,但尚未绘制任何圆圈.

然后,启用写入颜色缓冲区( glColorMask(true,true,true,true)),并将depthFunc设置为LEQUAL( glDepthFunc(GL_LEQUAL)).只有最接近的圆形像素才能通过深度测试(将其设置为LEQUAL而不是EQUAL会遇到一些罕见但可能的浮点近似误差).启用混合并再次绘制它们将产生所需的图像,而不会出现透明重叠.

I am working on a handwriting application on iOS. I found the sample project "GLPaint" from iOS documentation which is implemented by OpenGL ES, and I did something modification on it.

I track the touch points and calculate the curves between the points and draw particle images alone the curve to make it looks like where the finger passby.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData); // burshData is from CGImage, it is 

// vertexBuffer is generated based on the calculated points, it's just a sequence of point where need to draw image.
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer); 
glDrawArrays(GL_POINTS, 0, vertexCount);

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

What I got is a solid line which looks quite good. But now I want to draw semi-transparent highlight instead of solid line. So I replace the particle image with a 50% transparency one without changing code.

Result of 50% transparency particle image

There is something wrong with blend.

What I need

I draw three points using the semi-transparency particle image, and the intersection area should keep 50% transparency.

What's the solution?

解决方案

Im maybe two years later answering that question, but i hope it helps somebody who comes here looking for a solution to this problem, like it happened to me.

You are going to need to assign to each cirle a different z value. It doesn't matter how big or small this difference is, we only need them to not be strictly equal.

First, you disable writing in the color buffer glColorMask(false,false,false,false) , and then draw the circles normally. The Z-buffer will be updated as desired, but no circles will be drawn yet.

Then, you enable writing in the color buffer (glColorMask(true,true,true,true) ) and set the depthFunc to LEQUAL ( glDepthFunc(GL_LEQUAL) ). Only the nearest circle pixels will pass the depth test (Setting it to LEQUAL instead of EQUAL deals with some rare but possible floating point approximation errors). Enabling blending and drawing them again will produce the image you wanted, with no transparency overlap.

这篇关于如何使用OpenGL避免透明度重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆