2013-02-12 60 views
0

我画使用XNA绘图在C#避免差距显卡采用浮点数C#

View = Matrix.CreateLookAt(new Vector3(0f, 100f, 0f), Vector3.Zero, Vector3.Forward); 

Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(20.0f), ((Game1)Game).graphics.GraphicsDevice.Viewport.AspectRatio, 1.0f, 10000.0f); 

我的相机永远不会发生变化,只有我的游戏OBJET移动。 我移动它们按以下方式获得平滑的运动

float speed = 0.001f; 
pos.Z = pos.Z + (gameTime.ElapsedGameTime.Milliseconds * speed); 

,但是,我有马路花砖(原语VertexPositionTexture)其他后呈现给道路的感觉驱动器上。

它的作品,但有时它是我不想要的我的道路瓷砖之间的像素间距。 我认为它与浮点坐标有关。

我该如何解决这个问题?

在此先感谢! 我希望我的解释清楚我的问题。

编辑1:如果有人想投下这篇文章,请评论为什么。 没有反馈很难开发。

编辑2:有关基元的信息。

资源上ROD1 400×50

enter image description here

using System; 
using System.Collections.Generic; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace WindowsGame1 
{ 
    class Rod 
    { 
     public static Microsoft.Xna.Framework.Graphics.VertexBuffer buffer; 
     public static Texture2D[] tex; 
     public static BasicEffect effect; 

     public Vector3 pos; 
     public int typ; 

     public const float Width = 24f; 
     public const float Height = 5; 

     public Rod(Vector3 pos,Random r) 
     { 
      this.pos = pos; 
      typ = r.Next(tex.Length); 
     } 

     static public void Initialize(GraphicsDevice g) 
     { 
      buffer = new VertexBuffer(g, VertexPositionTexture.VertexDeclaration, 6, BufferUsage.WriteOnly); 

      VertexPositionTexture[] vertices = new VertexPositionTexture[6]; 

      vertices[0].Position = new Vector3(-Width, 0f, -Height); 
      vertices[0].TextureCoordinate.X = 0; 
      vertices[0].TextureCoordinate.Y = 0; 

      vertices[1].Position = new Vector3(Width, 0f, Height); 
      vertices[1].TextureCoordinate.X = 1; 
      vertices[1].TextureCoordinate.Y = 1; 

      vertices[2].Position = new Vector3(-Width, 0f, Height); 
      vertices[2].TextureCoordinate.X = 0; 
      vertices[2].TextureCoordinate.Y = 1; 



      vertices[3].Position = new Vector3(Width, 0f, Height); 
      vertices[3].TextureCoordinate.X = 1; 
      vertices[3].TextureCoordinate.Y = 1; 

      vertices[4].Position = new Vector3(-Width, 0f, -Height); 
      vertices[4].TextureCoordinate.X = 0; 
      vertices[4].TextureCoordinate.Y = 0; 

      vertices[5].Position = new Vector3(Width, 0f, -Height); 
      vertices[5].TextureCoordinate.X = 1; 
      vertices[5].TextureCoordinate.Y = 0; 

      buffer.SetData<VertexPositionTexture>(vertices); 
      effect = new BasicEffect(g); 
      effect.TextureEnabled = true; 
     } 

     public void SetTyp(Random r) 
     { 
      typ = r.Next(tex.Length); 
     } 

     public void Update(GameTime gameTime, Double speed) 
     { 
      // pos = new Vector3(pos.X, pos.Y, pos.Z + (gameTime.ElapsedGameTime.Milliseconds * speed)); 
      pos.Z = pos.Z + (float)(gameTime.ElapsedGameTime.Milliseconds * speed); 
     } 

     public void Draw(Matrix View, Matrix Projection, GraphicsDevice g) 
     { 
      effect.World = Matrix.CreateScale(1.01f) * Matrix.CreateTranslation(pos); 
      effect.View = View; 
      effect.Projection = Projection; 
      effect.Texture = tex[typ]; 

      foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
      { 
       g.SetVertexBuffer(buffer); 
       pass.Apply(); 
       g.DrawPrimitives(PrimitiveType.TriangleList, 0, 2); 
      } 
     } 
    } 
} 
+0

尝试使用'double'或'decimal'代替 – 2013-02-12 14:49:51

+0

感谢Daniel Hilgarth! ,但不幸的是它没有效果。 – 2013-02-12 15:05:39

+0

它缺少一些重要的信息,如绘图代码。但我的建议是围绕瓷砖的位置和大小。 – dowhilefor 2013-02-12 15:17:17

回答

4

我不能完全无需更多的细节问题,但是这是很可能发生的事情:

说,例如,你正在绘制两个矩形。因此,如果您独立绘制它们中的每一个,您将得到如下所示的结果:

+--------+ 
|  | 
|  | 
+--------+ < gap greatly exaggerated 
+--------+ < 
|  | 
|  | 
+--------+ 

这可能会导致可见的间隙。

你想要什么,而不是是这样的:

+--------+ 
|  | 
|  | 
+--------+ 
|  | 
|  | 
+--------+ 

不是在没有缝隙感,但在共享顶点感。

在顶部的插图中,有8个顶点。在底部,有6个,因为顶点在矩形之间共享。这意味着即使在理论上也不可能有差距,因为两者之间只有一条边。

你需要做的是重构代码以共享矩形之间的顶点。

在图形编程中,这通常称为四角带或三角带。 Riemer在XNA上下文中解释了三角形条带page

+0

谢谢Kendall Frey:) – 2013-02-12 16:11:23

0

你可以尝试转化与Matrix.CreateScale(1.01f)纹理

此外,在您的纹理被缩放,或在绘图程序中最初创建时大小正确吗?在任何情况下通过浮动来缩放纹理都会导致边缘不准确,应尽可能避免。

+0

“你可以尝试用Matrix.CreateScale(1.01f)来转换纹理” - 工作但感觉像是一个糟糕的解决方案,还有其他选择吗? “另外,当您的绘图程序最初创建时,您的纹理是否被缩放或正确调整大小?在任何情况下通过浮动来缩放纹理都会导致边缘不准确,应尽可能避免。 - 我不确定,我会在第一篇文章中发布更多代码。 – 2013-02-12 15:12:54

+0

哦,谢谢Jason Coombes! :) – 2013-02-12 15:26:26