打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# – 在绘制文本时XNA奇怪的3D绘画

我正在使用XNA 3.1开发3D Spaceship游戏

我试图将我的绘画代码与游戏逻辑分开(altought XNA几乎就是这样).

我正在使用一个特殊的静态类,在屏幕上绘制我的模型……主要的Game类在绘画中使用此代码:

protected override void Draw(GameTime gameTime)    {        graphics.GraphicsDevice.Clear(Color.Black);        // Draws the stage (the skybox and objects like rocks)        stageManager.Draw(gameTime, GraphicsDevice, camera);        // Draws each player and each Enemy on stage given the camera        foreach (Player p in players)            p.Draw(camera);        foreach(Enemy e in enemies)            e.Draw(camera);        if(Configuration.Debug)            col.renderColBoundings(GraphicsDevice, camera);        GraphicHelper.drawOverlayText("50", "10"); // "Error" line...        base.Draw(gameTime);    }

但是当我画文字时,会出现一些奇怪的东西……这是一张图片(original):


正如你所看到的,一切看起来都是重叠的(但在适当的位置)……就像飞船涡轮机一样.

GraphicHelper.drawOverlayText中的代码是这样的:

public static void drawOverlayText(String p1Hp, String currEnemies)    {        string text1 = "Player1: "   p1Hp;        string text2 = "Enemies: "   currEnemies   "/10";        string text3 = "Space Hogs Beta";        spriteBatch.Begin();        // Draw the string twice to create a drop shadow, first colored black        // and offset one pixel to the bottom right, then again in white at the        // intended position. This makes text easier to read over the background.        spriteBatch.DrawString(font, text1, new Vector2(651, 11), Color.Gray);        spriteBatch.DrawString(font, text1, new Vector2(650, 10), Color.White);        spriteBatch.DrawString(font, text2, new Vector2(851, 11), Color.Gray);        spriteBatch.DrawString(font, text2, new Vector2(850, 10), Color.White);        spriteBatch.DrawString(font, text3, new Vector2(741, 611), Color.Gray);        spriteBatch.DrawString(font, text3, new Vector2(740, 610), Color.White);        spriteBatch.End();    }

而这个静态类具有以下属性:

static ContentManager content;    static GraphicsDevice graphics;    static Camera camera;    static Dictionary<String, Model> models = new Dictionary<string, Model>();    static SpriteBatch spriteBatch;    static SpriteFont font;    public static void initHelper(ContentManager c, GraphicsDevice g, Camera cam)    {        content = c;        graphics = g;        camera = cam;        spriteBatch = new SpriteBatch(g);        font = c.Load<SpriteFont>("Fonts/main");    }

希望你能帮我 :)

解决方法:

您看到的奇怪渲染是因为深度缓冲区已关闭.使用SpriteBatch时会关闭它.这是XNA 3.1 API的一个奇怪之处. XNA 4.0至少使这种情况更加明显.

Here is an explanation of what render states are changed by SpriteBatch in XNA 3.1.和here is the same thing for XNA 4.0.

解决方案是在使用SpriteBatch之后,基本上将渲染状态设置回您想要的状态.在这种情况下,至少,设置:

GraphicsDevice.RenderState.DepthBufferEnable = true;

(在那里你可能还有其他一些州要改变.)

来源:http://www.icode9.com/content-1-236801.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
xna 游戏源码
java柱状图和饼状图的动态实现
vue 遇到的bug
Silverlight Toolkit 的3D功能
05713DirectX.doc
Java常用类库介绍
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服