OpenGL Alpha 混合问题,背面可见

发布于 2024-10-11 16:35:02 字数 2501 浏览 4 评论 0原文

我试图显示“透明”表面(不是封闭的体积),正面和背面都是可见的(未剔除)。

例如,显示圆锥体或圆柱体,其中两侧都应用透明度。 有一些可见的伪影,表面的某些部分似乎没有正确处理 alpha 值。

问题似乎是当我(opengl)尝试将 alpha 从表面的正面应用到表面的背面时。 (当表面的内部/外部都可见时)。

void init()
{
    glMatrixMode(GL_PROJECTION);
    gluPerspective( /* field of view in degree */ 40.0,
        /* aspect ratio */ 1.0,
        /* Z near */ 1.0, /* Z far */ 10.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
        0.0, 0.0, 0.0,      /* center is at (0,0,0) */
        0.0, 1.0, 0.);      /* up is in positive Y direction */
    glTranslatef(0.0, 0.6, -1.0);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
    glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
    glLightfv(GL_LIGHT2, GL_POSITION, light2_position);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);
    glFrontFace( GL_CW );

    glShadeModel(GL_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}
void draw ()
{
    static GLfloat amb[] = {0.4f, 0.4f, 0.4f, 0.0f};
    static GLfloat dif[] = {1.0f, 1.0f, 1.0f, 0.0f};
    static GLfloat back_amb[] = {0.4f, 0.4f, 0.4f, 1.0f};
    static GLfloat back_dif[] = {1.0f, 1.0f, 1.0f, 1.0f};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_LIGHT1);
    glDisable(GL_LIGHT2);
    amb[3] = dif[3] = 0.5f;// cos(s) / 2.0f + 0.5f;
    glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
    glMaterialfv(GL_BACK, GL_AMBIENT, back_amb);
    glMaterialfv(GL_BACK, GL_DIFFUSE, back_dif);
    glPushMatrix();
    glTranslatef(-0.3f, -0.3f, 0.0f);
    glRotatef(angle1, 1.0f, 5.0f, 0.0f);
    glutSolidCone(1.0, 1.0, 50, 2 ); 
    glPopMatrix();

    ///...
    SwapBuffers(wglGetCurrentDC()); // glutSwapBuffers();

}

该代码基于: http://www .sgi.com/products/software/opengl/examples/glut/examples/source/blender.c

tinyurled 链接到 flickr 上显示问题的 2 个图像(但来自生产代码,不是上面的代码,而是两者有同样的问题): http://flic.kr/p/99soxyhttp://flic.kr/p/99pg18

谢谢。 最大限度。

I'm trying to display "transparent" surfaces (not closed volumes) with both the front face and back face are visible (not culled).

For example displaying a cone or cylinder where the transparency is applied on both sides.
There are some visible artifacts where some part of the surface does not seems to be handling the alpha values correctly.

The issue it seems is when I (opengl) is trying to apply the alpha from the front side of the surface to the backside of the surface. (when both the inside/outside of the surface is visible).

void init()
{
    glMatrixMode(GL_PROJECTION);
    gluPerspective( /* field of view in degree */ 40.0,
        /* aspect ratio */ 1.0,
        /* Z near */ 1.0, /* Z far */ 10.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
        0.0, 0.0, 0.0,      /* center is at (0,0,0) */
        0.0, 1.0, 0.);      /* up is in positive Y direction */
    glTranslatef(0.0, 0.6, -1.0);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
    glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
    glLightfv(GL_LIGHT2, GL_POSITION, light2_position);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    //glEnable(GL_CULL_FACE);
    glFrontFace( GL_CW );

    glShadeModel(GL_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}
void draw ()
{
    static GLfloat amb[] = {0.4f, 0.4f, 0.4f, 0.0f};
    static GLfloat dif[] = {1.0f, 1.0f, 1.0f, 0.0f};
    static GLfloat back_amb[] = {0.4f, 0.4f, 0.4f, 1.0f};
    static GLfloat back_dif[] = {1.0f, 1.0f, 1.0f, 1.0f};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_LIGHT1);
    glDisable(GL_LIGHT2);
    amb[3] = dif[3] = 0.5f;// cos(s) / 2.0f + 0.5f;
    glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
    glMaterialfv(GL_BACK, GL_AMBIENT, back_amb);
    glMaterialfv(GL_BACK, GL_DIFFUSE, back_dif);
    glPushMatrix();
    glTranslatef(-0.3f, -0.3f, 0.0f);
    glRotatef(angle1, 1.0f, 5.0f, 0.0f);
    glutSolidCone(1.0, 1.0, 50, 2 ); 
    glPopMatrix();

    ///...
    SwapBuffers(wglGetCurrentDC()); // glutSwapBuffers();

}

The code is based on : http://www.sgi.com/products/software/opengl/examples/glut/examples/source/blender.c

tinyurled links to 2 images on flickr showing the issue (but from out production code, not the above code, but both have the same kind of problems):
http://flic.kr/p/99soxy and http://flic.kr/p/99pg18

Thanks.
Max.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2024-10-18 16:35:03

您的问题可能是两个问题的混合体。

  • 当深度测试已启用,并且所有多边形都写入深度缓冲区时,如果较远的多边形是在最近的多边形之后绘制的,则它们无法对像素颜色产生影响。您应该尝试禁用深度测试。

  • alphablending 是一种不可交换的运算,因此三角形的绘制顺序很重要,并且会改变结果。为了获得一致的结果,您必须以一致的顺序绘制三角形。在您的代码中,此顺序可能会有所不同,具体取决于观点。由于您的对象是凸面的,您可以分两次绘制对象,两次都启用剔除,第一遍绘制背面三角形(距相机最远),然后绘制正面三角形(最近)

更一般地说,正确混合任意几何体是一个难题,如果您想更进一步,您可能需要查看顺序无关透明度算法(深度剥离等)。

Your problem is probably a mix of two problems.

  • as your depth test is enabled, and all polygons write to depth buffer, the polygons that are further away cannot contribute to the pixel color if they are drawn after the nearest polygons. You should try to disable your depth test.

  • alphablending is a non-commutative operation, so the order is which the triangles are drawn is important and changes the result. To have a consistent result, you'd have to draw the triangles in consistent order. In your code, this order may vary, depending on the viewpoint for instance. As your objects are convex, you could draw your object in two passes, both with culling enabled, the first pass drawing the backfacing triangles (that are the most distant to the camera), then the frontfacing triangles (that are nearest)

More generally, correct blending for arbitrary geometry is a hard problem, you may want to look at Order Independent Transparency algorithms (depth peeling, ...) if you want to go further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文