Direct3D 9 阴影贴图混合模式

发布于 2024-11-05 08:55:00 字数 890 浏览 5 评论 0原文

我目前正在尝试在 direct3d 9 中的场景上绘制阴影。我正在尝试进行一些多通道渲染,但无法理解如何使用/设置混合模式。

我已经完成了填充深度缓冲区的深度通道,然后我有一个循环遍历场景中的所有灯光。在该循环中,我有 2 个循环,它们都循环遍历场景中的所有形状,

我在 pix 中进行了这种设置

for(number of shapes)
{
  //render from camera position and fill depth buffer
}
for(number of lights)
{
  for(number of shapes)
  {
    //render to shadow map
  }
  for(number of shapes)
  {
    //render to screen
  }
}

,我可以看到它循环遍历每个灯光,但当我运行它时,仅显示灯光阵列中的最后一个灯光。我认为这与混合模式有关。

我研究了混合模式并找到了有关源和目标混合的信息。这是我需要的/有人可以帮忙解释一下吗?

提前致谢,

马克

[编辑] 我使用以下代码使两个灯光都可见。

hr = device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
hr = device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
hr = device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

阴影看起来不正确,但我越来越接近所需的结果。

任何更多的建议都会很好,

谢谢,

马克

I am currently trying to draw shadows over a scene in direct3d 9. I am trying to do some multi pass rendering and am having trouble understanding how to use/set the blend mode.

I have done a depth pass which fills the depth buffer and I then have a loop which loops through all the lights in the scene. Within that loop I have 2 loops which both loop through all the shapes in the scene

I have this sort of set up

for(number of shapes)
{
  //render from camera position and fill depth buffer
}
for(number of lights)
{
  for(number of shapes)
  {
    //render to shadow map
  }
  for(number of shapes)
  {
    //render to screen
  }
}

In pix I can see that it loops through each light but when I run it only the last light in the light array is displayed. I think it is something to do with the blend mode.

I have looked into the blend mode and found information about source and destination blend. Is this what I need/could someone help explain it please?

Thanks in advance,

Mark

[EDIT]
I got both lights visible using the following code

hr = device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
hr = device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
hr = device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

The shadows do not look correct but I am getting closer to the desired result.

Any more advice would be great,

Thanks,

Mark

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

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

发布评论

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

评论(1

喵星人汪星人 2024-11-12 08:55:00

我还认为您设置了错误的 alpha 混合状态。我假设您进行了 SRC 混合,并用新图像替换旧图像,并且不进行任何 alpha 混合。
你需要考虑一下你想要什么。我假设您想要 SRC_OVER 混合。
Porter Duff 规则可以给你一个提示,而且很容易理解在 directX 中实现。有关示例,请查看此处
[编辑]我应该更仔细地阅读。

Alpha 混合和像素着色器是独立的。因此,您当然可以使用像素着色器来更改源的颜色值。也许添加一些特殊效果或任何你想尝试的东西。
但是,一旦您想要将源像素混合到目标像素上,并且不想用新像素替换所有像素,则需要启用 Alpha 混合。例如,像这样

hr = device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
hr = device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
hr = device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

此代码将所有源像素与其 alpha 值相乘,并将所有目标像素与 1-source_alpha 相乘。并将它们组合到新的目的地。

I also think that you have wrong alpha blend states set. I assume that you do a SRC blend and replacing your old image with the new one and not doing any alpha blending.
You need to think about what you want. I assume that you want a SRC_OVER blending.
The Porter Duff rules can give you a hint and they are very easy to implement in directX. For an example look here.
[Edit] I should read more carefully.

Alpha blending and pixel shaders are independent. So you can of course use the pixel shader to change the color value of your source. Maybe to add some special effect or what ever you want to try.
But as soon as you want to blend your source over a destination and you don't want to replace all the pixels with new ones, you need to enable alpha blending. E.g. like this

hr = device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
hr = device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
hr = device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

This code will multiply all source pixels with their alpha value and all destination pixels with 1-source_alpha. And combine them to the new destination.

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