在XNA使用CreateOrthographicOffCenterXNA、CreateOrthographicOffCenter

2023-09-07 22:41:17 作者:不懂你的多愁善感

我试图找出如何绘制图形的XNA,和其他人提出这一点。 但在此之前我尝试使用此...

I'm trying to figure out how to draw graphics in XNA, and someone else suggested this. But before I attempt to use this...

如果我创建和使用这台相机,并设置左,上为0,WIDTH = 256 HEIGHT = 240,什么我呈现在屏幕将使用这些坐标?因此,宽度和高度1一箱,如果设置为0,0会占用空间,从0,0到1,1?

If I create and use this camera, and set LEFT,TOP to 0 and WIDTH=256 and HEIGHT=240, anything I render to the screen will use these coordinates? So a box with a width and height of 1, if set to 0,0 will take up space from 0,0 to 1,1?

推荐答案

你指的功能是: Matrix.CreateOrthographicOffCenter(左,右,下,前,zNearPlane,zFarPlane)

此返回可用于转化在世界空间的一个点的点在投影空间中的投影矩阵

This returns a projection matrix that can be used to transform a point in world space to a point in projection space.

投影空间变为从(-1,-1)在视口的右上角左下角为(1,1)。这是坐标空间,该GPU的光栅化时,在实际工作。

Projection space goes from (-1,-1) in the bottom left corner of the viewport to (1,1) in the top right corner. This is the coordinate space that the GPU actually works in when rasterising.

世界空间是任何你希望它是。

World space is whatever you want it to be.

因此​​,让我们说你创建 Matrix.CreateOrthographicOffCenter(0,256,240,0,-10,10),而你使用的矩阵作为投影矩阵矩阵BasicEffect绘制一个立方体的模型。比方说,立方体的模型为中心原点,是尺寸1(长,宽,高)。

So let's say you create a matrix with Matrix.CreateOrthographicOffCenter(0, 256, 240, 0, -10, 10), and you used that matrix as your projection matrix with BasicEffect to draw a model of a cube. Let's say the model of the cube is centered at the origin and is of size 1 (length, width and height).

除了 basicEffect.Projection ,您将设置 basicEffect.View = Matrix.Identity (因为我们不'吨要一个额外的摄像头变换)和 basicEffect.World = Matrix.CreateTranslation(0.5F,0.5F,0),这样它的存在从(0翻译模型, 0)至(1,1)在世界空间。然后使用BasicEffect绘制的模型。

As well as basicEffect.Projection, you would set basicEffect.View = Matrix.Identity (because we don't want an additional camera transformation) and basicEffect.World = Matrix.CreateTranslation(0.5f, 0.5f, 0) to translate your model so that it exists from (0,0) to (1,1) in world space. Then draw your model using that BasicEffect.

您立方体的顶面(正投影意味着没有透视)在视口的左上角将绘制。这将需要多达1/256的宽度和1/240的视口的高度(参见 GraphicsDevice.Viewport )。

The top face of your cube (orthographic projection means that there is no perspective) will be drawn at the top left corner of the viewport. It will take up 1/256th of the width and 1/240th of the height of the viewport (see also GraphicsDevice.Viewport).

(PS:我不记得背面剔除是如何影响这种投影如果你什么也看不到试图将其关闭或切换缠绕顺序)

(PS: I can't remember how backface culling is affected by this kind of projection. If you see nothing try turning it off or switching the winding order.)

现在,这是说 - 我得到一个感觉从你的其他问题(和你想的正交矩阵的事实),你想要做的2D精灵的工作。 BasicEffect主要是做3D工作(但如果你让自己的顶点着色器,不建议精灵,您需要一个投影矩阵)设计。

Now, this being said - I get a sense from your other questions (and the fact you want to make an orthographic matrix) that you want to do 2D sprite work. BasicEffect is designed primarily for doing 3D work (although if you make your own vertex shader, not recommended for sprites, you will need a projection matrix).

您可能需要使用XNA的SpriteBatch - 并非最不重要的是,因为它的重的绘制精灵优化。 SpriteBatch.Begin 将采取矩阵transformMatrix 作为参数。这相当于世界和视图矩阵,上面的没有的投影矩阵。

You probably want to use XNA's SpriteBatch - not least of all because it's heavily optimised for drawing sprites. SpriteBatch.Begin will take a Matrix transformMatrix as an argument. This is equivalent to the World and View matrix, above, not the Projection matrix.

SpriteBatch假设你的世界的空间是相同的客户端的空间(左上角为(0,0),宽度和高度与视口的大小),并处理投影为您服务。 (其实这是比这更先进的 - 它会申请一个适合你的偏移,使精灵像素排队与屏幕像素)

SpriteBatch assumes your world space is the same as client space (top left is (0,0), width and height are the size of the viewport) and handles the projection for you. (Actually it is more advanced than this - it will apply an offset for you so that sprite pixels line up with screen pixels.)

如果你想画精灵使世界出现256个单位宽和240个单位高,在视口中,你可以传递这样一个矩阵来 SpriteBatch.Begin

If you want to draw sprites so that world appears 256 units wide and 240 units high in the viewport, you could pass a matrix like this to SpriteBatch.Begin:

Matrix.CreateScale(viewport.Width / 256f,viewport.Height / 240F,1F)

值得注意的是,在新的XNA 4.0您可以use SpriteBatch绘制自定义顶点着色器,所以你可以使用任意的世界观项目矩阵。

It is worth noting that in the new XNA 4.0 you can use SpriteBatch to draw with custom vertex shaders and so you may use arbitrary world-view-project matrices.