DirectX:如何将效果应用到使用 ID3DXSprite.Draw(..) 绘制的纹理
我想为DirectX程序编写一个非常简单的效果,它使用ID3DXSprite接口来绘制2D-Hud >。在 XNA 中,我简单地调用了
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
effect.Begin();
effect.CurrentTechnique.Passes[0].Begin();
spriteBatch.Draw(texture, new Rectangle(0, 0, 300, 300), Color.White);
effect.CurrentTechnique.Passes[0].End();
effect.End();
spriteBatch.End();
“但在 C++ 中,几乎相同的代码不起作用”
pSprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_DONOTSAVESTATE | D3DXSPRITE_SORT_TEXTURE);
anEffect->SetTechnique(technique);
anEffect->Begin(&passes, 0);
anEffect->BeginPass(0);
pSprite->Draw(pTexture, NULL, NULL, &position, 0xFFFFFFFF);
anEffect->EndPass();
anEffect->End();
pSprite->End();
注意:效果已正确加载!
I want to write a very simple Effect for a DirectX program which uses the ID3DXSprite interface to draw a 2D-Hud. In XNA I simply called
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
effect.Begin();
effect.CurrentTechnique.Passes[0].Begin();
spriteBatch.Draw(texture, new Rectangle(0, 0, 300, 300), Color.White);
effect.CurrentTechnique.Passes[0].End();
effect.End();
spriteBatch.End();
But in C++, nearly the same code doesnt work
pSprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_DONOTSAVESTATE | D3DXSPRITE_SORT_TEXTURE);
anEffect->SetTechnique(technique);
anEffect->Begin(&passes, 0);
anEffect->BeginPass(0);
pSprite->Draw(pTexture, NULL, NULL, &position, 0xFFFFFFFF);
anEffect->EndPass();
anEffect->End();
pSprite->End();
NOTE: The effect is loaded correctly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先,您拥有的 XNA 代码适用于 XNA 3.1,并且它是错误的。 这篇博文解释了如何为 XNA 3.1 和 4.0 执行此操作(API 更改)。
在 XNA 3.1 中,当使用
SpriteSortMode.Immediate
时,SpriteBatch
将在Begin
调用中设置其着色器和其他设备状态,而不是在结束
通话。这使您有机会在实际绘制之前替换部分设备状态(在Draw
或End
中,具体取决于它何时刷新)。然后你应该结束
你的效果在你结束
精灵批次(所以所有东西都会先绘制) 。现在,在 DirectX 中,我认为
End
调用的相同错误顺序也是罪魁祸首。具体请参阅文档的这一部分对于ID3DXEffect::Begin
的第二个参数结果是,当您
End
效果时,会将设备重置回正常的精灵绘制,在对ID3DXSprite
调用End
之前,这实际上是发送要绘制的精灵批次。我猜想,您的错误排序代码在 XNA 上运行的原因是,当开始效果时,XNA 可能会在后台执行相当于传递
D3DXFX_DONOTSAVESTATE
的操作。Well, first of all the XNA code you have is for XNA 3.1, and it's wrong. This blog post explains how to do it for both XNA 3.1 and 4.0 (the API changes in between).
In XNA 3.1, when using
SpriteSortMode.Immediate
,SpriteBatch
will set up its shaders and other device state in theBegin
call, instead of in theEnd
call. This gives you the opportunity to replace parts of the device state before drawing actually takes place (inDraw
orEnd
, depending on when it flushes). And then you are supposed toEnd
your effect after youEnd
the sprite batch (so everything gets drawn first).Now, in DirectX, I would suggest that the same incorrect ordering of your
End
calls is to blame. Specifically refer to this part of the documentation for the second parameter toID3DXEffect::Begin
The upshot is that, when you
End
the effect, it is resetting the device back to normal sprite drawing, before you callEnd
on theID3DXSprite
, which is what is actually sending your sprite batch to be drawn.I would guess that the reason your incorrectly-ordered code works on XNA is that XNA is probably doing the equivalent of passing
D3DXFX_DONOTSAVESTATE
, when beginning the effect, under the hood.Sprite 与 HLSL 效果的使用:(适用于 C++ 游戏开发人员)
下面是示例代码,解释了 sprite 绘制如何与 HLSL 效果文件一起使用
伪代码:< /em>
Usage of Sprite with HLSL Effect: (for C++ Game Developers)
Below is the sample code which explains how sprite draw can work with HLSL effect files
Pseudo Code: