透明多边形使底层表面也透明

发布于 2024-10-05 15:37:10 字数 797 浏览 5 评论 0原文

我正在编写一个简单的赛车游戏。当尝试通过绘制黑色透明多边形在场景中添加阴影时,我发现自己陷入了一个奇怪的问题。

当在水平表面上绘制阴影时,当我从远处看它们时,我可以透过表面看到它们,就好像它们是透明的一样。

void PolyShadow::Draw(){
 glColor4f(0,0,0,0.5f);
  glEnable (GL_BLEND);
  glBlendFunc (GL_DST_COLOR,GL_ONE_MINUS_SRC_ALPHA);

  this->drawShadow();
 glDisable (GL_BLEND);
   shadow_initialized = true;
}

为了避免冲突,我为任何投影阴影的对象分配了不同的shadow_offset,并在绘制阴影之前进行此调用

glEnable(GL_POLYGON_OFFSET_FILL);

//call to object.drawShadows()

glDisable(GL_POLYGON_OFFSET_FILL);

void Object::drawShadows(){
glPolygonOffset(-1.0-shadow_offset,-1.0-shadow_offset);
//Draw shadow
}

我也尝试过,

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

但没有任何改变。

提前致谢

I'm coding a simple car racing game. While trying to add shadows in my scene, by drawing black transparent polygons, i found myself stuck in a strange problem.

When shadows are drawn on horizontal surfaces, when i look at them from far away, i can see through the surfaces as if they were transparent.

void PolyShadow::Draw(){
 glColor4f(0,0,0,0.5f);
  glEnable (GL_BLEND);
  glBlendFunc (GL_DST_COLOR,GL_ONE_MINUS_SRC_ALPHA);

  this->drawShadow();
 glDisable (GL_BLEND);
   shadow_initialized = true;
}

To avoid conflicts i assign to any object projecting shadows a different shadow_offset and make this call before drawing the shadow

glEnable(GL_POLYGON_OFFSET_FILL);

//call to object.drawShadows()

glDisable(GL_POLYGON_OFFSET_FILL);

And

void Object::drawShadows(){
glPolygonOffset(-1.0-shadow_offset,-1.0-shadow_offset);
//Draw shadow
}

I also tried with

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

but nothing changed.

Thanks in advance

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

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

发布评论

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

评论(1

白云不回头 2024-10-12 15:37:10

当您使用混合时,您需要从后到前渲染。因此,您需要对模型甚至三角形进行 z 排序。
通常,您首先以任意顺序渲染所有不透明的内容,然后从后到前混合内容。

由于所有阴影都具有相同的颜色,这对您来说不是很糟糕吗?只需在渲染几何图形后渲染阴影即可。
还有其他渲染阴影的方法看起来更好并且不存在此问题(例如 标记模板缓冲区中的几何图形)。

有一些技术可以渲染复杂的 alpha 混合场景(例如“深度剥离”),这些技术可以避免排序问题,甚至适用于相交几何体,但它们相当昂贵。

When you use blending you need to render back-to-front. So you need to z-order your models or even triangles.
Typically you first render all opaque stuff in arbitrary order, and then blended stuff back-to-front.

Since all shadows have the same color, isn't that bad for you. Just render the shadows after rendering the geometry.
There are other methods for rendering shadows which look better and don't have this problem(for example marking the shadowed parts of the geometry in the stencil buffer).

There are some techniques to render complicated alpha-blended scenes (for example "Depth peeling") which avoid the ordering problem and even work for intersecting geometry, but they are rather expensive.

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