如何使用 XNA 绘制线条?
我读过很多涉及 XNA 的教程(它有各种版本),但我对绘制基元仍然有点困惑。 一切似乎真的很复杂。
有人可以使用代码向我展示在屏幕上绘制一两条线的最简单的 XNA 实现吗? 也许有一个简短的解释(包括样板)?
我不是游戏程序员,也没有什么 XNA 经验。 我的最终目标是在屏幕上画一些线,我最终将通过旋转等(手动)对其进行变换。 然而,对于第一步......我需要简单地画线! 我记得在我古老的 OpenGL 时代,通过一些方法调用来画一条线是相当简单的。 我应该简单地恢复使用非托管 directx 调用吗?
I've read a bunch of tutorials involving XNA (and it's various versions) and I still am a little confused on drawing primitives. Everything seems to be really convoluted.
Can someone show me, using code, the simplest XNA implementation of drawing one or two lines on to the screen? Perhaps with a brief explanation (including the boilerplate)?
I'm not a games programmer and I have little XNA experience. My ultimate goal is to draw some lines onto the screen which I will eventually transform with rotations, etc (by hand). However, for this first step.. I need to simply draw the lines! I remember back in my ancient OpenGL days it was fairly straightforward when drawing a line with a few method calls. Should I simply revert to using unmanaged directx calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用 XNA 时,所有内容(甚至 2d 图元)都必须以 3d 卡可以理解的方式表达,这意味着一条线只是一组顶点。
MSDN 这里有一个非常好的演练:
http://msdn.microsoft.com /en-us/library/bb196414.aspx#ID2EEF
您会发现渲染原始线需要更多代码,而不是仅设置纹理四边形并旋转它,因为本质上,您所做的渲染一条线时也是如此。
When working with XNA, everything (even 2d primitives) have to be expressed in a way that a 3d card can understand, which means that a line is just a set of vertices.
MSDN has a pretty good walkthrough here:
http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF
You'll find that it takes more code to render a primitive line than it would take to just setup a textured quad and rotate that, since in essence, your doing the same thing when rendering a line.
按照 NoHayProblema 的回答(我还不能发表评论)。
这个答案虽然对于这个老问题来说是正确的,但并不完整。 Texture2D 构造函数返回一个未初始化的纹理,该纹理永远不会绘制在屏幕上。
为了使用这种方法,您需要像这样设置纹理的数据:
考虑到将数据写入像素的方式必须与纹理的 SurfaceFormat 一致。 该示例之所以有效,是因为纹理被格式化为 RGB。
旋转可以在 spriteBatch.Draw 中应用,如下所示:
Following NoHayProblema's answer (I cannot comment yet).
That answer, although the correct one for this old question, is incomplete. Texture2D constructor returns an uninitialized texture, which is never painted on screen.
In order to use that approach, you need to set the texture's data like this:
Take into account that the way you write the data into pixel must be consistent with the texture's SurfaceFormat. The example works because the texture is being formatted as RGB.
Rotations can be applied in spriteBatch.Draw like this:
找到了一个教程
http://www.bit-101.com/blog/?p=2832< /a>
它使用BasicEffect(着色器)
以及 XNA 4.0 中内置的绘制用户原语,
我发现一些有用的代码示例:
加载内容方法
绘制方法
found a tutorial for that
http://www.bit-101.com/blog/?p=2832
its using a BasicEffect (shader)
and the built in draw user primitive in XNA 4.0
some code samples i find helpful:
load content method
draw method
好吧,你可以用一种非常简单的方式做到这一点,而无需陷入可怕的 3D 矢量内容。
只需创建一个快速纹理,例如:
Texture2D SimpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
然后使用该纹理绘制一条线:
this. spriteBatch.Draw(SimpleTexture, new Rectangle(100, 100, 100, 1), Color.Blue);
我希望这有帮助
Well, you can do it in a very simple way without getting into the 3D horrible vector stuff.
Just create a quick texture, for example:
Texture2D SimpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
And then just draw a line using that texture:
this.spriteBatch.Draw(SimpleTexture, new Rectangle(100, 100, 100, 1), Color.Blue);
I hope this helps
我认为,最简单的最好方法是获取一个白色像素的图像,然后将该像素拉伸到一个矩形中,使其看起来像一条线,
我制作了一个 Line 类,
希望这有帮助
The simplest best way, I think, is to get the image of just a white pixel then stretch that pixel in a rectangle to look like a line
I made a Line class,
Hope this helps
还有“manders”在 CodePlex 上发布的“round line”代码:
这是关于它的博客文章:
There is also the "round line" code that "manders" has released on CodePlex:
Here is the blog post about it:
只需拉伸一个白色像素即可。
Just stretch a white pixel.
我想绘制光线,以便可以调试爆炸产生的光线以及它们与物体相交的位置。 这将在两点之间绘制一条单像素细线。 这就是我所做的:
存储一些简单光线数据的类。 XNA 默认光线类可以工作,但它不存储光线到交点的长度。
用于存储要绘制的光线的列表:
创建一个 BasicEffect 并在 LoadContent 方法中向其传递具有所需分辨率的“Matrix.CreateOrthographicOffCenter”投影。
然后在绘制方法中运行它:
所以当我的游戏中发生爆炸时,它会执行以下操作(伪代码):
它非常简单(它可能看起来比实际情况复杂得多)并且很容易将其放入单独的类中您永远不必再考虑。 它还可以让您一次绘制大量线条。
I wanted to draw rays so that I could debug rays created by explosions and where they intersect objects. This will draw a single pixel thin line between two points. This is what I did:
Class to store some simple ray data. The XNA default ray class could work, but it doesn't store the length of the ray to intersection.
A list to store the rays that are to be drawn:
Create a BasicEffect and pass it a "Matrix.CreateOrthographicOffCenter" projection with your desired resolution in the LoadContent method.
Then run this in the draw method:
So when an explosion happens in my game it does this (Psuedocode):
It's pretty simple (It possibly looks way more complicated than it is) and it'd be easy to put it into a separate class that you never have to think about again. It also lets you draw a whole lot of lines at once.
我自己遇到了这个问题,并决定创建一个名为 LineBatch 的类。
LineBatch 将绘制线条,而不需要 spriteBatch 或点。
班级如下。
I encountered this problem my self and decided to make a class called LineBatch.
LineBatch will draw lines without needing a spriteBatch or dots.
The class is below.
这是我用来通过指定起始坐标、结束坐标、宽度和颜色来制作线条的简单方法:
注意:您必须将名为“dot”的文件添加到内容目录中(该线条将被制作出来)这些)。
Here is a simple way that I use to make lines by specifying a start coordinate, an end coordinate, width, and color of them:
NOTE: you must add a file named "dot" to the content directory (the line will be made out of these).