托管 DirectX:指定包含纹理和文本的精灵的深度(Z 顺序)
我正在使用托管 DirectX 尝试使用 Sprite 将纹理和一段文本绘制到屏幕上。不幸的是,如果我将文本和纹理放在同一个精灵中,无论我执行绘制命令的顺序如何,纹理都会覆盖(过度绘制?)文本。
由于我最终想要散布纹理和文本,因此如何为这些精灵指定 Z 顺序。每层都必须位于单独的精灵中吗?
以下是当前代码:
m_device.BeginScene();
m_device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
m_sprite.Begin(SpriteFlags.SortTexture | SpriteFlags.AlphaBlend);
// Switching the order of following two statements doesn't change the Z-Order!
m_sprite.Draw(m_texture, Vector3.Empty, new Vector3(0, 0, 0),
Color.White.ToArgb());
m_d3dFont.DrawText(m_sprite, m_text, x, y, color);
m_sprite.End();
m_device.EndScene();
m_device.Present();
注意:使用 SpriteFlags.SortDepthBackToFront 或 SpriteFlags.SortDepthBackToFront 不会更改行为。
这可能是我的一个概念性误解,但如果代码有用,我将非常感激地接受使用 C++ 或任何语言的非托管 DirectX 中的示例。
非常感谢!
I am using managed DirectX to try and draw a texture and a piece of text to the screen using a Sprite. Unfortunately, if I place the text and the texture in the same sprite, the texture overwrites (overdraws?) the text regardless of the order I do the draw commands.
Since I will eventually want to intersperse textures and text, how do I specify a Z-order for these sprites. Does each layer have to be in a separate sprite?
Here is the current code:
m_device.BeginScene();
m_device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
m_sprite.Begin(SpriteFlags.SortTexture | SpriteFlags.AlphaBlend);
// Switching the order of following two statements doesn't change the Z-Order!
m_sprite.Draw(m_texture, Vector3.Empty, new Vector3(0, 0, 0),
Color.White.ToArgb());
m_d3dFont.DrawText(m_sprite, m_text, x, y, color);
m_sprite.End();
m_device.EndScene();
m_device.Present();
Note: Using the SpriteFlags.SortDepthBackToFront or SpriteFlags.SortDepthBackToFront does not change the behaviour.
This is probably a conceptual misunderstanding on my part, but if code is useful, I'll gratefully accept samples in unmanaged DirectX using C++ or whatever language.
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果要更改渲染的 Z 顺序,则必须在“绘制”命令中设置 Z 值。如果你将全部设置为 0,你会得到各种奇怪的结果。你更大的问题是 DrawText 不允许你设置 Z 深度,这都是垃圾。
因此,您唯一的机会是使用 ID3DXSprite::SetTransform。您只需将 Z 坐标移回相关的 z 排序位置。因此,您可以按如下方式设置变换(假设您使用的是恒等世界矩阵)(C++ 示例),
然后您继续传递 (0, 0, 0) 位置进行渲染,文本也将获得正确的 z 排序深度。
希望有帮助。
If you want to change the Z-Order of rendering then you have to set the Z value in the Draw command. If you set the all to 0 you will get all sorts of weirdness. Your bigger issue is that DrawText doesn't allow you to set a Z-Depth which is all kinds of rubbish.
Thus your only chance is to use ID3DXSprite::SetTransform. You need to shift only the Z-Coordinate back with it for the relevant z ordering position. So you can set your transforms (assuming you are using identity world matrices) as follows (C++ example)
You then carry on passing a position of (0, 0, 0) for rendering and text will also gain the correct depth for z-ordering.
Hope that helps.
这是我(发布者)的答案:
Goz 提供了解决问题所需的大部分信息。本质上,对于纹理,您可以使用第二个 Verctor3 的第三个参数指定 z 顺序。实验表明,(1) z 顺序从 0.0 到 1.0,其中 0.0 是最近的,1.0 是最远的。超出该范围的任何内容都不会出现。
对于文本,因为没有机会在调用中指定 z,所以您需要使用 Goz 建议的变换矩阵。
最后,大致是我使用的代码。
Here is the answer I (the poster) went with:
Goz provided most of the information needed to solve the problem. Essentially, for textures, you can specify the z-order using the third parameter of the second Verctor3. Experimentation has made it clear that (1) z order goes from 0.0 to 1.0, with 0.0 being closest and 1.0 being the the furthest away. Anything out of that range doesn't appear at all.
For text, because there's no opportunity to specify the z in the call, you need to use Goz's suggestion of a transform matrix.
In the end, here's roughly the code I used.