完全透明的OpenGL模型 [英] Fully transparent OpenGL model

查看:97
本文介绍了完全透明的OpenGL模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我我在做什么错.我已将3d模型加载到自编码的opengl渲染(v 3.3)中,并尝试使用顶点着色器使其像X射线效果一样透明:

Explain me what i'm doing wrong. I have loaded 3d model to self-coded opengl render (v 3.3) and try to make it transparent like xray effect using vertex shader:

#version 330

attribute vec3 coord3d;
attribute vec2 texcoord;
varying vec2 f_texcoord;

uniform mat4 projectionMatrix; 
uniform mat4 modelViewMatrix; 

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec4 inColor;

smooth out vec4 theColor;

void main()
{
    gl_Position = projectionMatrix*modelViewMatrix*vec4(inPosition, 1.0);
    theColor = vec4(0.0,0.2,0.4,0.4);
    f_texcoord = texcoord;

}

在编辑器中对模型进行了三角剖分,并使用以下图形绘制:

The model was triangulated in editor and draw with:

glDrawArrays(GL_TRIANGLE_FAN, 0, (vertices.at(i)->size()/3));

如果我使用

glEnable(GL_ALPHA_TEST);
glEnable( GL_BLEND );
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0); 

我看到一些不需要的三角形或线条:

I see some unwanted triangles or lines:

如果我没有进行深度测试就发现里面有多个三角形(这是我不想要的):

If i do it without depth test i see multiple triangless inside faces (which i don't want):

我如何摆脱不必要的影响并实现像Google Sketchup这样的X射线效果

How can i rid of unwanted effects and implement x-ray effect like Google Sketchup did it

如果我希望所有模型都透明,我应该实施深度排序吗?

Should i implement depth sorting if i want all model transparent?

我该如何实现:

推荐答案

问题1:

首先,不要禁用深度测试.深度测试在混合之前 进行,以确保正确地丢弃片段,即使启用了混合也是如此.有关每个片段操作及其执行顺序的详细信息,请参阅核心OpenGL 4.4规范的第17.3节.

First of all, don't disable the depth test. The depth test is performed before blending to ensure fragments are correctly dropped, even when blending is enabled. For more information on per-fragment operations and the order in which they are performed, see section 17.3 of the core OpenGL 4.4 specification.

当禁用深度测试时,您将得到意想不到的结果,除非绘制调用的顺序是绝对正确的,否则将无法达到深度缓冲的目的.而且,即使您获得正确的命令,也不能指望所有副作用都消失了,仅凭最简单的程序就无法按照您需要的方式对每个命令进行排序.

When you disable depth testing, you will get unexpected results unless the order of draw calls is absolutely correct, thus defying the very purpose of depth buffering. And even if you get the order right you cannot expect all side effects to be gone and trying to order every command just the way you need it doesn't work for anything but the simplest programs.

考虑以下非常简单的示例:

Consider the following, very simple example:

您可以看到3个四边形,其中最远的是通常的tex坐标空间(s,t E [0,1]),一个蓝色四边形与后者交融,还有一个不透明的红色四边形,其假定为在最前面.显然,红色四边形不是在它们的前面,尽管深度值表明不是这样.同样明显的是,目的是将蓝色四边形与红色四边形混合.在蓝色四边形之后渲染红色四边形似乎可以解决该问题,但是如果您绘制另一个应该位于蓝色和红色四边形的 之后的四边形,则只需在禁用深度测试的情况下显示在顶部.都错了.

You can see 3 quads, the farthest visualizing the usual tex coordinate space (s,t E [0, 1]), a blue quad which blends with the latter, and an opaque red quad which is supposed to be in the front. Obviously, the red quad isn't in front, although the depth values indicate otherwise. Also obvious, the intent is to not blend the blue quad with the red quad. Rendering the red quad after the blue quad seemingly fixes the problem, but if you draw another quad which is supposed to be behind both the blue and the red quad, it would simply appear on top with disabled depth testing. This is all wrong.

黄色四边形遮挡了所有东西,尽管它应该位于红色和蓝色四边形的后面.正确的图像如下,这完全是通过启用深度测试获得的:

The yellow quad obscures everything, although it's supposed to go behind the red and blue quad. The correct image is the following, which is obtained purely by enabling depth testing:

我的一般建议是:除非您有充分的理由禁用深度测试,例如在渲染叠加层并且不想麻烦深度缓冲区时,否则请启用深度测试.

My general suggestion would be: unless you have a very good reason to disable the depth test, for instance when rendering overlays and not wanting to hassle with the depth buffer, leave depth testing enabled.

注意:深度缓冲无法解决通常必须将非不透明几何图形并排放置才能获得正确结果的问题!如果要在尝试模拟透明度时完全不依赖于顺序,请在有关顺序独立透明度的许多论文中寻找通常的可疑对象.

Note: Depth buffering will not solve your problem of usually having to arrange non-opaque geometry back-to-front to get a correct result! If you want to be completely independent of ordering when trying to simulate transparency, look for the usual suspects among the many papers on order independent transparency.

问题2:

从外观上看,我假设您使用以下混合函数:

From the looks of it, I assume your using the following blend function:

gl::BlendFunc (gl::ONE, gl::ONE);

或导致稳定,连续值累积的任何事物

or anythin that leads to a steady, successive accumulation of values like

gl::BlendFunc (gl::SRC_ALPHA, gl::ONE);

以及RGB和alpha分量的默认混合等式:

and the default blend-equation for RGB and alpha components:

gl::BlendEquation (gl::FUNC_ADD);

如果不是这种情况,请添加注释以说明实际值,以便我可以重新检查并可能编辑建议.以下内容仍然适用,因为颜色不会说谎. ;)

If this isn't the case, please add a comment stating the actual values so I can re-recheck and possibly edit my advice. The following still applies because colors just don't lie. ;)

您的第二个问题是由于投影下的z角战斗.一些片段被正确丢弃,而其他则没有.由于您显然拥有在某些位置完全重合的重复面,因此对于一个片段,可能会为第一个面生成略高的深度值.渲染第二张面时,会生成一个稍低的深度值,并且片段会通过,从而导致实际应该没有的深度透支.

Your second problem is due to z-fighting under projection. Some fragments are correctly discarded, others aren't. Since you obviously have duplicate faces that exactly coincide in some places, it may be possible that for one fragment a slightly higher depth value is generated for the first face. When the second face is rendered, a slightly lower depth value is generated and the fragment passes, leading to overdraw where there should actually be none.

第一个片段已经完成了所有的魔术工作,并且将一个混合值写入了帧缓冲区,并且将深度值写入了深度缓冲区,即,您的脸部一个片段通过了所有测试并与背景.现在是第二个片段,由于深度值稍低,它被 丢弃,并再次与帧缓冲区中已经混合的颜色混合.由于添加了添加剂,您可以获得观察到的结果.在下面的示例中,我使用与您使用的相同的颜色和alpha值重现了这种情况:

The first fragment has already done all the magic and a blended value was written into the frame buffer and the depth value was written to the depth buffer, i.e. you have one fragment of the face that passed all tests and has blended with the background. Now comes the second fragment which is not discarded because the depth value is slightly lower and again blends with the already blended color in the framebuffer. Due to additive blending, you get the results you observed. I reproduced this case in the following example with the same color and alpha values you used:

问题3::您的模型似乎有很多重复的面孔,并且通常来说,三角剖分看起来很可怕.您应该真的重做.理解和使用混合已经足够困难了.不要将其与次优数据复杂化.

Problem 3: Your model seems to have a lot of duplicate faces and in general, the triangulation looks horrible. You should really redo it. Understanding and using blending is hard enough as it is. Don't complicate it with sub-optimal data.

这篇关于完全透明的OpenGL模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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