What is 3D Rendering?
The basics of the 3D rendering pipeline for MonoGame!
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.
In general, the 3D pipeline requires the following state information for initialization:
- World, view, and projection matrices to transform 3D vertices into a 2D space.
- A vertex buffer which contains the geometry to render.
- An effect that sets the render state necessary for drawing the geometry.
Note
For a more detailed explanation of World, View and Projection matrices , check out this GameFromScratch - beginning 3D article.
As you become comfortable with these ideas, you may want to learn more about the following:
- Manipulating vertices
- Creating your own effects
- Applying textures
- Improving performance by using index buffers.
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 encapsulates most of these common operations.
![IMPORTANT] 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 as a reference. MonoGame automatically converts all shaders to their appropriate platform based on the "shader level" defined.
The Graphics Device
When you create a game with MonoGame, the framework initializes a graphics device for you.
The GraphicsDeviceManager initializes the GraphicsDevice.
Before Initialize is called, there are three ways to change the GraphicsDevice settings:
Set the appropriate properties such as the PreferredBackBufferHeight and PreferredBackBufferWidth on the GraphicsDeviceManager in your game's constructor.
_graphics.PreferredBackBufferHeight = 768; _graphics.PreferredBackBufferWidth = 1024; _graphics.ApplyChanges();
Handle the
PreparingDeviceSettings
event on the GraphicsDeviceManager, and change the PreparingDeviceSettingsEventArgs.GraphicsDeviceInformation.PresentationParameters member properties._graphics.PreparingDeviceSettings += OnPreparingDeviceSettings; private void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e) { e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = 1024; e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = 768; }
Warning
Any changes made to the PreparingDeviceSettingsEventArgs will override the GraphicsDeviceManager preferred settings.
Handle the
DeviceCreated
event on the GraphicsDeviceManager, and change the PresentationParameters of the GraphicsDevice directly.IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService) Game.Services.GetService(typeof(IGraphicsDeviceService)); if (graphicsDeviceService != null) { graphicsDeviceService.DeviceCreated += OnDeviceCreated; } private void OnDeviceCreated(object sender, EventArgs e) { // Handle updating Graphics Device Presentation Parameters }
When you call Game.Initialize the GraphicsDeviceManager creates and configures GraphicsDevice. You can then safely access GraphicsDevice settings such as the backbuffer, depth/stencil buffer, viewport, and render states in the Initialize method.
Important
After you call Game.Initialize, changes to the PresentationParameters of the GraphicsDevice will not take effect until you call GraphicsDeviceManager.ApplyChanges. Other changes, such as render states, will take effect immediately.
Resources
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:
![NOTE] FOr a more detailed understanding in the use of these terms, check out Riemers 3D series on the XNAGameStudio Archive.
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.
Vertex and Index Buffers
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
The MonoGame Framework contains several classes to describe common vertex declaration types, such as:
-
Vertex Declaration containing Position and Color of a vertex.
-
Vertex Declaration containing Position, Color and Texture of a vertex.
-
Vertex Declaration containing Position, Normal and Texture of a vertex.
-
Vertex Declaration containing Position and Texture of a vertex.
Note
Use the VertexElement class to compose custom vertex types.
Vertex buffers can contain indexed or non-indexed vertex data.
Note
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.
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.
Render a non-indexed vertex buffer
To render a non-indexed vertex buffer, call the GraphicsDevice.DrawPrimitives or GraphicsDevice.DrawUserPrimitives Methods.
Render an indexed vertex buffer
To render an indexed buffer, call the GraphicsDevice.DrawIndexedPrimitives or GraphicsDevice.DrawUserIndexedPrimitives Methods.
Textures
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.
You can apply textures by using the Texture property of the BasicEffect class, or choose to write your own effect methods to apply textures.