[英]How to use Sprite class in Microsoft.DirectX to draw text and allow the mouse to rotate the screen?
我正在使用Microsoft.DirectX.Direct3D
和Microsoft.DirectX
渲染3D数据。
现在,我想绘制文本以指示空间内的高度值。 如下图所示:
现在,我面临两个主要问题:
问题1:如您所见,图形文字被颠倒了。 我该怎么做才能使其正确显示?
问题2:尝试旋转屏幕时出现Direct3DXException
错误。 错误消息如下:
下面是我使用Sprite
类的DrawText()
代码
Microsoft.DirectX.Direct3D Device = new Device(0, DeviceType.Hardware, this.panel1,
CreateFlags.HardwareVertexProcessing, presentationParameters);
private void Render()
{
device.BeginScene();
if(checkBox1.checked)
{
DrawText();
}
device.EndScene();
device.SetTexture(0, null);
device.Present();
}
private void DrawText()
{
Sprite fontsprite = new Sprite(device);
fontsprite.Begin(SpriteFlags.AlphaBlend | SpriteFlags.ObjectSpace);
_font = new Microsoft.DirectX.Direct3D.Font(device, 10, 10, FontWeight.Bold, 1, false, CharacterSet.Ansi, Precision.Default, FontQuality.ClearType, PitchAndFamily.DefaultPitch, "face1");
_font.DrawText(fontsprite, "1.5 Km", 120, 15, Color.Black);
_font.DrawText(fontsprite, "2.5 Km", 120, 25, Color.Black);
_font.DrawText(fontsprite, "3.5 Km", 120, 35, Color.Black);
fontsprite.Transform.Translate(120, 100, 100);
fontsprite.End();
}
在解决问题1和问题2时需要一些帮助。谢谢。
更新:问题1已由我自己解决。 现在只剩下问题2。
解决问题的代码1:
fontsprite.Transform = Matrix.RotationZ((float)Math.PI);
fontsprite.Transform *= Matrix.Translation(heightBoxSize, heightValue, heightBoxSize);
fontsprite.Transform.Translate(100, 100, 100);
我认为这是因为高内存消耗,因为您要在每个帧中创建新的Sprite。 (每秒60帧?)
发生此问题的地方是,使用鼠标事件旋转屏幕会不断更新frontsprite中的设备结果。
因此,我建议您将部分代码移出。
将声明部分移动到全局,然后将初始化移动到Initialization()部分。
还将fontsprite.End()移到Final()部分。
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.