|
| 1 | +--- |
| 2 | +title: What is 3D Rendering? |
| 3 | +description: The basics of the 3D rendering pipeline for MonoGame! |
| 4 | +requireMSLicense: true |
| 5 | +--- |
| 6 | + |
| 7 | +The 3D graphics pipeline uses a `graphics device` (the current display device rendering to the screen) to load resources and render a 3D scene using an [Effect](xref:Microsoft.Xna.Framework.Graphics.Effect). |
| 8 | + |
| 9 | +In general, the 3D pipeline requires the following state information for initialization: |
| 10 | + |
| 11 | +* World, view, and projection matrices to transform 3D vertices into a 2D space. |
| 12 | +* A vertex buffer which contains the geometry to render. |
| 13 | +* An effect that sets the render state necessary for drawing the geometry. |
| 14 | + |
| 15 | +> [!NOTE] |
| 16 | +> For a more detailed explanation of World, View and Projection matrices , check out this [GameFromScratch - beginning 3D](https://gamefromscratch.com/monogame-tutorial-beginning-3d-programming/) article. |
| 17 | +
|
| 18 | +As you become comfortable with these ideas, you may want to learn more about the following: |
| 19 | + |
| 20 | +* Manipulating vertices |
| 21 | +* Creating your own effects |
| 22 | +* Applying textures |
| 23 | +* Improving performance by using index buffers. |
| 24 | + |
| 25 | +The MonoGame Framework uses a shader-driven programmable pipeline and requires a graphics card capable of at least Shader Model 2.0. A class called [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect) encapsulates most of these common operations. |
| 26 | + |
| 27 | +> ![IMPORTANT] |
| 28 | +> Shader requirements depend on the platform being targeted, the majority of MonoGame titles these days require Shader Model 3.0 as a minimum. You can use [this guide](https://www.lifewire.com/determine-directx-version-and-shader-model-812997) as a reference. |
| 29 | +> MonoGame automatically converts all shaders to their appropriate platform based on the "shader level" defined. |
| 30 | +
|
| 31 | +## The Graphics Device |
| 32 | + |
| 33 | +When you create a game with MonoGame, the framework initializes a graphics device for you. |
| 34 | + |
| 35 | +The [GraphicsDeviceManager](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) initializes the [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice). |
| 36 | + |
| 37 | +Before [Initialize](xref:Microsoft.Xna.Framework.Game.Initialize) is called, there are three ways to change the [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice) settings: |
| 38 | + |
| 39 | +1. Set the appropriate properties such as the [PreferredBackBufferHeight](xref:Microsoft.Xna.Framework.GraphicsDeviceManager.PreferredBackBufferHeight) and [PreferredBackBufferWidth](xref:Microsoft.Xna.Framework.GraphicsDeviceManager.PreferredBackBufferWidth) on the [GraphicsDeviceManager](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) in your game's constructor. |
| 40 | + |
| 41 | + ```csharp |
| 42 | + _graphics.PreferredBackBufferHeight = 768; |
| 43 | + _graphics.PreferredBackBufferWidth = 1024; |
| 44 | + _graphics.ApplyChanges(); |
| 45 | + ``` |
| 46 | + |
| 47 | +1. Handle the `PreparingDeviceSettings` event on the [GraphicsDeviceManager](xref:Microsoft.Xna.Framework.GraphicsDeviceManager), and change the [PreparingDeviceSettingsEventArgs.GraphicsDeviceInformation.PresentationParameters](xref:Microsoft.Xna.Framework.Graphics.PresentationParameters) member properties. |
| 48 | + |
| 49 | + ```csharp |
| 50 | + _graphics.PreparingDeviceSettings += OnPreparingDeviceSettings; |
| 51 | + private void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e) |
| 52 | + { |
| 53 | + e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = 1024; |
| 54 | + e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = 768; |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | + > [!WARNING] |
| 59 | + > Any changes made to the [PreparingDeviceSettingsEventArgs](xref:Microsoft.Xna.Framework.PreparingDeviceSettingsEventArgs) will override the [GraphicsDeviceManager](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) preferred settings. |
| 60 | + |
| 61 | +1. Handle the `DeviceCreated` event on the [GraphicsDeviceManager](xref:Microsoft.Xna.Framework.GraphicsDeviceManager), and change the [PresentationParameters](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice.PresentationParameters) of the [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice) directly. |
| 62 | + |
| 63 | + ```csharp |
| 64 | + IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService) |
| 65 | + Game.Services.GetService(typeof(IGraphicsDeviceService)); |
| 66 | + |
| 67 | + if (graphicsDeviceService != null) |
| 68 | + { |
| 69 | + graphicsDeviceService.DeviceCreated += OnDeviceCreated; |
| 70 | + } |
| 71 | + |
| 72 | + private void OnDeviceCreated(object sender, EventArgs e) |
| 73 | + { |
| 74 | + // Handle updating Graphics Device Presentation Parameters |
| 75 | + } |
| 76 | + ``` |
| 77 | + |
| 78 | +When you call [Game.Initialize](xref:Microsoft.Xna.Framework.Game.Initialize) the [GraphicsDeviceManager](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) creates and configures [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice). You can then safely access [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice) settings such as the backbuffer, depth/stencil buffer, viewport, and render states in the [Initialize](xref:Microsoft.Xna.Framework.Game.Initialize) method. |
| 79 | + |
| 80 | +> [!IMPORTANT] |
| 81 | +> After you call [Game.Initialize](xref:Microsoft.Xna.Framework.Game.Initialize), changes to the [PresentationParameters](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice.PresentationParameters) of the [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice) will not take effect until you call [GraphicsDeviceManager.ApplyChanges](xref:Microsoft.Xna.Framework.GraphicsDeviceManager.ApplyChanges). Other changes, such as render states, will take effect immediately. |
| 82 | + |
| 83 | +## Resources |
| 84 | + |
| 85 | +A graphics resource is a collection of data stored in memory that can be accessed by either the CPU or GPU. The types of resources that an application might use include: |
| 86 | + |
| 87 | +* [Render targets](./WhatIs_Render_Target.md) |
| 88 | +* [Vertex buffers](xref:Microsoft.Xna.Framework.Graphics.VertexBuffer) |
| 89 | +* [Index buffers](xref:Microsoft.Xna.Framework.Graphics.IndexBuffer) |
| 90 | +* [Textures](xref:Microsoft.Xna.Framework.Graphics.Texture) |
| 91 | + |
| 92 | +> ![NOTE] |
| 93 | +> FOr a more detailed understanding in the use of these terms, check out [Riemers 3D series](https://github.com/simondarksidej/XNAGameStudio/wiki/Riemers3DXNA1Terrainoverview) on the XNAGameStudio Archive. |
| 94 | +
|
| 95 | +Based on the resource management mode that was used when a resource is created, it should be reloaded when the device is reset. For more information, see [Loading Resources](../../howto/content_pipeline/HowTo_LoadContentLibrary.md). |
| 96 | + |
| 97 | +### Vertex and Index Buffers |
| 98 | + |
| 99 | +A vertex buffer contains a list of 3D vertices to be streamed to the graphics device. Each vertex in a vertex buffer may contain data about not only the 3D coordinate of the vertex but also other information describing the vertex, such as the vertex normal, color, or texture coordinate. Which you should use will depend on your usage and the needs of the vertex information you are drawing with, [for example](https://stackoverflow.com/questions/4702150/difference-between-using-vertexpositionnormaltexture-and-vertexpositiontexture.) |
| 100 | +
|
| 101 | +The MonoGame Framework contains several classes to describe common vertex declaration types, such as: |
| 102 | + |
| 103 | +* [VertexPositionColor](xref:Microsoft.Xna.Framework.Graphics.VertexPositionColor) |
| 104 | + |
| 105 | + Vertex Declaration containing Position and Color of a vertex. |
| 106 | + |
| 107 | +* [VertexPositionColorTexture](xref:Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture) |
| 108 | + |
| 109 | + Vertex Declaration containing Position, Color and Texture of a vertex. |
| 110 | + |
| 111 | +* [VertexPositionNormalTexture](xref:Microsoft.Xna.Framework.Graphics.VertexPositionNormalTexture) |
| 112 | + |
| 113 | + Vertex Declaration containing Position, Normal and Texture of a vertex. |
| 114 | + |
| 115 | +* [VertexPositionTexture](xref:Microsoft.Xna.Framework.Graphics.VertexPositionTexture). |
| 116 | + |
| 117 | + Vertex Declaration containing Position and Texture of a vertex. |
| 118 | + |
| 119 | +> [!NOTE] |
| 120 | +> Use the [VertexElement](xref:Microsoft.Xna.Framework.Graphics.VertexElement) class to compose custom vertex types. |
| 121 | + |
| 122 | +Vertex buffers can contain indexed or non-indexed vertex data. |
| 123 | + |
| 124 | +> [!NOTE] |
| 125 | +> If a **Vertex Buffer** is not indexed, all of the vertices are placed in the vertex buffer in the order they are to be rendered. Because 3D line lists or triangle lists often reference the same vertices multiple times, this can result in a large amount of redundant data. |
| 126 | +> |
| 127 | +> **Index buffers** allow you to list each vertex only once in the vertex buffer. An index buffer is a list of indices into the vertex buffer, given in the order that you want the vertices to render. |
| 128 | + |
| 129 | +### Render a non-indexed vertex buffer |
| 130 | + |
| 131 | +To render a non-indexed vertex buffer, call the [GraphicsDevice.DrawPrimitives](/api/Microsoft.Xna.Framework.Graphics.GraphicsDevice.html#Microsoft_Xna_Framework_Graphics_GraphicsDevice_DrawPrimitives_Microsoft_Xna_Framework_Graphics_PrimitiveType_System_Int32_System_Int32_) or [GraphicsDevice.DrawUserPrimitives](/api/Microsoft.Xna.Framework.Graphics.GraphicsDevice.html#Microsoft_Xna_Framework_Graphics_GraphicsDevice_DrawUserPrimitives__1_Microsoft_Xna_Framework_Graphics_PrimitiveType___0___System_Int32_System_Int32_) Methods. |
| 132 | + |
| 133 | +### Render an indexed vertex buffer |
| 134 | + |
| 135 | +To render an indexed buffer, call the [GraphicsDevice.DrawIndexedPrimitives](/api/Microsoft.Xna.Framework.Graphics.GraphicsDevice.html#Microsoft_Xna_Framework_Graphics_GraphicsDevice_DrawIndexedPrimitives_Microsoft_Xna_Framework_Graphics_PrimitiveType_System_Int32_System_Int32_System_Int32_) or [GraphicsDevice.DrawUserIndexedPrimitives](/api/Microsoft.Xna.Framework.Graphics.GraphicsDevice.html#Microsoft_Xna_Framework_Graphics_GraphicsDevice_DrawUserIndexedPrimitives__1_Microsoft_Xna_Framework_Graphics_PrimitiveType___0___System_Int32_System_Int32_System_Int16___System_Int32_System_Int32_) Methods. |
| 136 | + |
| 137 | +### Textures |
| 138 | + |
| 139 | +A texture resource is a structured collection of texture data. The data in a texture resource is made up of one or more sub-resources that are organized into arrays and mipmap chains. Textures are filtered by a texture sampler as they are read. The type of texture influences how the texture is filtered. |
| 140 | + |
| 141 | +You can apply textures by using the [Texture](xref:Microsoft.Xna.Framework.Graphics.BasicEffect.Texture) property of the [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect) class, or choose to write your own effect methods to apply textures. |
0 commit comments