draw string in 3d space xna

Now that we covered the basics of the graphics pipeline and some of the math that is involved, let'south come across how these concepts relate to the types exposed by XNA Game Studio and draw some triangles on the screen.

GraphicsAdapter

Employ the GraphicsAdapter class to enumerate and update graphics adapters. Virtually unmarried monitor PCs have one graphics adapter that represents their physical graphics card and the i connexion it has to the monitor. Some graphics cards provide 2 graphics adapters so yous can plug two monitors into a single graphics card. In this example, the GraphicsAdapter allows for selecting two different adapters so yous can brandish the graphics on two unlike monitors.The Xbox 360 and Windows Phone devices provide only 1 GraphicsAdapter.

The static GraphicsAdapter.DefaultAdapter property returns the default adapter in which there are multiple adapters to chose from. For example, when yous set the left monitor to be the primary display in Windows, the DefaultAdapter belongings returns the GraphicsAdapter to display graphics on the left monitor.

The static GraphicsAdapter.Adapters property returns a read-but drove of the adapters available.You lot rarely need to enumerate graphics adapters. Most commercial PC games provide a settings page that enables the user to select the adapter so he or she can alter which display to view the game. This is required when the game runs in full screen and the user does not accept the normal window controls to click and elevate.

If your game uses windowed mode, the user can click and describe the window to the other monitor.Although this changes the GraphicsAdapter used, all of this is handled for the user by XNA Game Studio past default.

Note

If yous adult games using a native graphics API before and tried to support monitor dragging and toggling betwixt windowed and full-screen modes, y'all know the problem to become it working properly.

The GraphicsAdapter provides several properties and methods that enable you lot to make up one's mind what blazon of graphics carte du jour is available and what the capabilities of the graphics card are.

The QueryBackBufferFormat and QueryRenderTargetFormat methods can exist used to determine whether a specific back buffer or render target format is supported.We discuss back buffers and render targets later in this topic.

The DeviceName, Clarification, DeviceId, VendorId, SubSystemId, and Revision backdrop are used to render data about a specific GraphicsAdapter to decide whether the graphics bill of fare is a specific model from a specific manufacturer. They too enable you lot to determine what the electric current driver version is.

Annotation

A graphics driver is a slice of software that enables the operating system to communicate with the graphics hardware. Graphics carte du jour manufacturers create the driver to instruct the operating system on how to perform specific tasks with the hardware.

Each GraphicsAdapter can support a number of display modes. Just every bit you lot can set your PC to apply different resolutions, your game tin can use different resolutions. The SupportedDisplayModes returns a collection of DisplayMode structures. Each construction contains backdrop for the Width, Height, and Format for the brandish mode.A display format is defied using the SurfaceFormat enumeration.The format defines how each pixel on the screen is represented in terms of the data that is stored at each pixel.The most common format, SurfaceFormat.Color, is a 32-bit value that stores four channels of cherry-red, dark-green, blue, and the alpha each with 8 $.25 of value.

Annotation

Alpha is a term used to express how transparent a color is.

The DisplayMode structure as well provides ii helper backdrop called AspectRatio and TitleSafeArea.The AspectRatio is the value you lot get when yous divide the width of the brandish by the elevation of the display.This is often calculated when working with 3D graphics and is sometimes calculated incorrectly. The property gives you a simple manner to call back the value without having to calculate the value each time you lot need it.

When drawing to televisions, they accept outer edges of the screen cut off. If you draw to the top left corner of the screen and display this on a number of televisions, you will notice that each television has a slightly unlike position of where the height left corner is. The TitleSafeArea defines where you should limit drawing game specific graphics such as text and the heads upward display for a character. Using the property ensures that your graphics are visible across different televisions.

GraphicsDevice

The GraphicsDevice in your game is responsible for issuing the drawing commands down to the driver, which then goes through the graphics pipeline.The GraphicsDevice is also responsible for loading graphics resources such as textures and shaders.

Every GraphicsDevice contains a back buffer.The back buffer is the data buffer where the graphics that ultimately finish upward displayed on your monitor are drawn to. It is called the dorsum buffer because it is not displayed on the screen.What you lot see displayed on the screen is actually the contents of what is called the front buffer. Drawing occurs to the back buffer, so you don't see each piece of geometry appear on the screen equally it is drawn.Later on all of the draw calls are performed in a unmarried frame, the GraphicsDevice.Present method is called, which flips the front end and back buffers to brandish the contents of the back buffer.

Along with a width and height, the dorsum buffer too has a SurfaceFormat, which defines the scrap layout and utilize for each pixel in the dorsum buffer. As nosotros discussed, the GraphicsAdapter can be used to make up one's mind what formats are bachelor.The almost common is the Color format.

If a back buffer resolution that is smaller than the display is requested on the Xbox 360 or on a Windows Phone, the final image is up scaled to fit the screen. If the aspect ratio is unlike, then the brandish black bars are displayed on the top and bottom of the screen called letterboxing or on the sides of the screen called pillarboxing to make full out the actress space.

When calling the Nowadays method, you specify a PresentInterval.The present interval is used to determine when the flip of the front end and dorsum buffers occur. Displays on computer monitors, televisions, and phone screens all have a refresh rate.The refresh rate is the speed that the displays update their physical screen commonly between 30Hrz to 60Hrz.When displays update, they exercise and so by updating line by line until the unabridged screen is updated. At that time, the display performs a vertical retrace sometimes referred to every bit a vblank.This is when the screen is moving from the end of where it is currently updating to the first again. If the forepart and dorsum buffers are flipped in the middle of the display, updating a graphical artifact called tearing can occur.Violent is where part of the displayed screen shows 1 frame while another portion displays another.This occurs if the flip is not in sync with the vertical retrace because you change what is fatigued on the screen in the middle of the display updating the display. To prevent this, the flip should occur during the vertical retrace.

When you lot set the PresentInterval, you have iii options of when the flip between the front and dorsum buffers occurs. If PresentInterval.One is used, the buffers wait to flip until the display is in the vertical retrace phase. This means the fastest your graphics can update is the same speed as your display's refresh charge per unit. PresentInterval.2 is used only to flip the buffers every other vertical retrace. PresentInterval.Immediate can be used to perform the flip as shortly equally possible and not wait for the vertical retrace. Although your game tin endure from violent artifacts, this way is helpful when performing performance analysis because it does non limit the speed at which your game can draw.

Along with the dorsum buffer, a GraphicsDevice tin besides incorporate some other buffer called the depth buffer.The depth buffer is used to shop the depth values of a triangle when the colour of that triangle is written to the back buffer.The values are used in the depth test position of the graphics pipeline that we discussed previously. A depth buffer can have three types of formats specified by the DepthFormat enumeration.The beginning is Depth16, which stores a 16-bit floating point value at each pixel. Depth24 provides a 24-bit floating signal number to shop depth at each pixel. Finally, the Depth24Stencil8 format provides the same 24 $.25 for depth but also enables some other 8 $.25 to exist used by the stencil buffer. The stencil buffer is used in the stencil examination portion of the graphics pipeline.

Creating the GraphicsDevice

Although it is possible to create the graphics device yourself using the GraphicsDevice constructor, this is not needed when using Game form because the default template creates a GraphicsDeviceManager, which creates the GraphicsDevice.

If yous want to change whatever of the default values that the GraphicsDeviceManager uses to create the graphics device, you tin can use code similar to the following in your Game class constructor.

tmpD-74_thumb

The GraphicsDeviceManager.SupportedOrientations can be used to define which orientations y'all desire your game to back up. This is useful for Windows Phone games where the user tin can flip the physical telephone device and might do so because of the requirements of the game. If yous elect to support an orientation when the user rotates the phone, the screen flips to support the new orientation. For example, if you lot want to support a mural game where the user can play the game by rotating the telephone to the left or correct, utilise the following lines of lawmaking:

tmpD-75_thumb

Now when the user rotates the telephone from 1 horizontal position to the other, the display automatically flips and your game appears correctly for the new orientation.

Reference Devices

There is a special type of graphics device called a reference device besides known as ref device.A reference device does not employ the graphics hardware and implements all of the graphics functionally in software on the computer's CPU. A reference device can be used when the graphics hardware does not support some specific graphics feature. The downside is that the reference device is tremendously slow compared to graphics hardware.

Cartoon with Primitives

If you have never adult 3D graphics applications, this is where you become a graphics developer for the first time.All of the geometry that we describe in futurity topics builds upon the following archaic drawing.

Archaic Types

XNA Game Studio supports four types of primitives defined by the PrimitiveType enumeration: TriangleList, TriangleStrip, LineList, and LineStrip.A TriangleList is a list of triangles merely like its name implies. Every iii vertices are treated as a new triangle meaning that each triangle requires three vertices to be stored. In most cases, this is overkill because most triangles are direct next to some other triangle.With a TriangleStrip, the last 3 vertices are used to draw the next triangle. For example, you lot can draw two triangles with only four vertices. The first three vertices make up the commencement triangle and the last vertex is used in conjunction with the last two vertices from the first triangle to form the second triangle. Using triangle strips helps reduce the amount of memory required to store the vertices of triangles.The TriangleStrip is by far the about common PrimitiveType used.

The ListList is similar to the triangle list except that only two points are needed to form a single line. Every ii points creates a new line segment. The LineStrip is like to a triangle strip except that merely the last point from the previous line segment is needed to form another line.

Vertex Types

Each vertex that makes upwards the triangle contains the position information for where the triangle is in infinite, simply information technology tin as well comprise other data such as the colour that the triangle should exist, texture coordinates for textured triangles, or normal data for lighting calculations. XNA Game Studio provides several ways to build in vertex types that can be used when defining your triangles or lines.You lot can also create your own vertex types for more than complex rendering scenarios.

Drawing Primitives

There are means to render geometric primitives in XNA Game Studio. All four methods are provided past the GraphicsDevice form.The first ii methods—DrawUserPrimitives and DrawUserIndexedPrimitives—both work by specifying an array of vertices and a archaic type to the GraphicsDevice, which, in plough, draws the primitives.

The second ii methods—DrawPrimitives and DrawIndexedPrimitives—use vertices that take been stored on the graphics hardware. Storing vertex data directly on the graphics hardware lowers drawing latency because the information needed to describe the geometry is already in memory in the graphics hardware most where the computations are occurring.

Along with vertex data, the ii indexed methods—DrawUserIndexedPrimitives and DrawIndexedPrimitives—also use index values that are used to index into the vertex data. Instead of using the vertex data directly to draw the primitive, indexed vertices are divers using an offset into the vertex information. This saves infinite when multiple primitives employ the aforementioned vertex.

DrawUserPrimitives

The easiest way to draw a primitive on the screen is to declare an array of vertices and pass these to the DrawUserPrimitive method.Along with the vertices, nosotros create an case of BasicEffect.For now, you know that the outcome is needed so the graphics hardware knows what to do in the vertex shader and pixel shader portions of the graphics pipeline.

Define the post-obit member variables in your Game grade:

tmpD-76_thumb

Next, ascertain the vertices and create a new instance of BasicEffect and set some of the properties.Add the following lines of lawmaking in your Game class LoadContent method:

tmpD-77_thumb

In the previous lawmaking, y'all define three vertices of type VertexPositionColor.This vertex type holds a position and colour for each vertex.You define the position and color for the top, correct, and left vertices. The order you specify these is important because social club is not culled by default in XNA Game Studio.

After you create your vertices, y'all create an instance of BasicEffect.The Earth, View, and Projection properties are used in the vertex shader portion of the graphics pipeline to transform the input vertices to draw on the screen. For at present, don't worry about the specific values for these matrices.The VertexColorEnabled property is set to true to tell the BasicEffect to use the color from each vertex as the output value of the pixel shader.

The last step is to issue the draw call using the DrawUserPrimitives method.Add the following lines of code to your Game class Draw method:

tmpD-78_thumb

Earlier you tin can call DrawUserPrimitives, tell the GraphicsDevice to employ the BasicEffect.This is washed by calling Use on the EffectPass you use.

Next, call DrawUserPrimitives.This method is generic and expects the vertex type as the generic T type parameter. In this case, pass VertexPositionColor as the vertex blazon. The first parameter defines what PrimitiveType you want to describe. Specify TriangleList to draw a list of triangles.The second parameter is an array of vertices of the aforementioned blazon used for the generic T parameter. Laissez passer the userPrimitives array you created previously. The third parameter is used to specify an offset into the array of vertices. If your assortment contains multiple primitive vertices and you desire to draw only a subset of those, you tin specify an offset into the source array. For your purpose, you don't want any get-go, and so ready the value to 0.The final parameter DrawUserPrimitives takes the total number of primitives to describe. If your array defines multiple primitives, specify that number to draw with this parameter. For this example, y'all draw only a single triangle, and then specify a value of 1.

If you build and run the previous example, you should see a unmarried triangle like that in Figure 4.22. Notice that each vertex has its own color. and the color of the middle sections of the triangle change across the triangle to each vertex.This is called interpolation and occurs on values that are specified per vertex. Considering the color is specified per vertex, the dissimilar vertex colors have to interpolate over the pixels between the ii vertices.

Now allow's update the example to draw multiple primitives using the LineStrip archaic. First, update where the vertices are created with the following lines of lawmaking:

tmpD-79_thumb

tmpD-80_thumb

Using DrawUserPrimitives to draw a single triangle

Figure 4.22 Using DrawUserPrimitives to draw a single triangle

Note

The lines are set to Color.White to help make them more visible. Y'all can apply any color just like you lot would for triangles.

And then, update your DrawUserPrimitives telephone call to specify the LineStrip primitive type and the new number of primitives:

tmpD-82_thumb

If you run the example now, it should await like Figure 4.23.

Using DrawUserPrimitives to draw a LineStrip DrawUserIndexedPrimitives

Effigy four.23 Using DrawUserPrimitives to draw a LineStrip DrawUserIndexedPrimitives

If your primitives commonly use the same vertices for a number of dissimilar primitives, it tin can save you retentiveness to use indexed primitives instead. To depict a user indexed primitive, you demand to specify an array of indexes to use when drawing the triangles.These index values are used to expect up each specific vertex to use when rendering the primitive.

To draw a user indexed primitive, update the electric current example.Add together an array of alphabetize value. These are integer values that represent offsets into the vertex assortment you lot also pass into the DrawUserIndexedPrimitives method. Update the member variables for your Game class to have the additional index array like the following code:

tmpD-84_thumb

Use a 16-bit brusque array to shop the index values. Because each index is represented by a curt, the index values can be between merely 0 and 65535, which is the maximum positive number a brusque can represent.You lot can besides employ an assortment of 32-bit int values, which can shop positive values of more than four billion but this has 2 repercussions. The first is that storing index values as int values ways that each index takes upward two more than bytes than when using shorts thus doubling the memory usage for your index val-ues.The other is that 32-bit int indexes are supported only in the HiDef GraphicsProfile, so your game must usea HiDef GraphicsDevice.

Next, create the vertices and index values for your primitives. In this example, draw a square, often called a quad, that is fabricated of ii triangles. Update the existing example'south LoadContent method to comprise the following code:

tmpD-85_thumb

Like the previous example, create an assortment of VertexPositionColor to represent the vertices in your primitives.The code creates iv vertices: one for each corner of the quad. A brusque array is then created to specify which vertex to use for each primitive. The first triangle uses the elevation left, top correct, and lesser left vertices making sure to ascertain them in clockwise lodge. The 2nd triangle uses the top right, bottom right, and bottom left.

Finally, update your Game class Draw method to use the DrawUserIndexPrimitives method:

tmpD-86_thumb

Like the previous example with DrawUserPrimitives, the DrawUserIndexedPrimitives phone call is generic and yous pass the VertexPositionColor every bit the generic T type parameter. The first parameter is once again the primitive type. The 2nd parameter is again the assortment of vertices.The tertiary parameter is the vertex kickoff that should be used. For index primitives, this showtime is added to each alphabetize value to obtain the final alphabetize to utilize in the vertex array. The 4th parameter is the number of vertices that is used in the draw call. In this case, use iv vertices.This parameter is often the size of the vertex assortment itself unless you lot use offsets.The fifth parameter is the array used to store the index values. For the example, that is the userPrimitivesIndices variable.The 6th parameter is the commencement into the index array that should be used. In this case, you don't need to outset, so the value is 0.The terminal parameter is over again the number of primitives to describe. For the example, yous try two triangles, so a value of ii is specified.

If you build and run the case lawmaking, you should run into an image like that in Figure 4.24 where a 4-color quad is describe on the screen.

Using DrawUserIndexedPrimitives to draw a quad

Figure four.24 Using DrawUserIndexedPrimitives to draw a quad

Let's lower the retentiveness used past the index assortment past updating the example to just use 4 index values and employ the TriangleStrip PrimitiveType. Update where you create the index array to await like the post-obit:

tmpD-88_thumb

The second triangle is formed by using the final two alphabetize values plus the new additional value. In this case, the triangle is formed by userPrimitivesIndices[3] , userPrimitivesIndices[two] , and userPrimitivesIndices[1] .This second triangle saves 32 $.25 of data by not using two additional alphabetize values.

The DrawUserIndexedPrimitives method call needs to be updated to employ only PrimitiveType.TriangleStrip and non TriangleList. If you run the example lawmaking at present, yous will not see whatsoever visible difference yet you are saving valuable memory space. It is not always possible to salvage space and apply triangle strips; in these cases, you should apply TriangleLists.

fraserlonly1967.blogspot.com

Source: http://what-when-how.com/xna-game-studio-4-0-programmingdeveloping-for-windows-phone-7-and-xbox-360/let-the-3d-rendering-start-xna-game-studio-4-0-programming-part-1/

0 Response to "draw string in 3d space xna"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel