C # Implementing 3D rendering using the Device class in DirectX
1. Provide a brief introduction to the Device class
The Device class abstracts real graphics cards from specific hardware and defines a set of universal functions in the class that directly operate the graphics card hardware.
Function prototype:
(1) Public Device (IntPtr unmanaged Object)
(2) Public Device (int adapter, DeviceType deviceType, Control renderWindow, Create Flags behaviorFlags, params PresentParameters [] presentationParameters)
(3) Public Device (int adapter, DeviceType deviceType, IntPtr renderWindow Handle, Create Flags behaviorFlags, params PresentParameters [] presentationParameters);
2. Provide parameter explanations for the second constructor.
Public Device (int adapter, DeviceType deviceType, Control renderWindow, CreatFlags behaviorFlags, params PresentParameters [] presentationParameters) .
Parameter Analysis:
Adapter: Indicates which graphics card to use, with a default graphics card of 0
DeviceType: represents the type of device to be created.
- DeviceType Hardware, create a device that directly controls the hardware of the graphics card (requires the graphics card to support DirectX)
- DeviceType Reference, device type is a reference grating device, software simulation, slow running speed
- DeviceType Software, custom software rasterizer
RenderWindow: represents the window displayed in the graphics
BehaviorFlags.
- Create Flags SoftwareVertexProcessing, software processing for calculating all vertices in 3D graphics
- Create Flags HardwareVertexProcessing, all vertex calculations are processed using GPU
- Create Flags MixedVertexProcessing, selecting based on the principle of best results
The above are mutually exclusive, only one can be selected, and can be combined with the following parameters- FPU_ Preserve, using double precision floating-point calculations to slow down speed
- Multithreaded, using multiple threads
- PureDevice requires all calculations to use GPU as much as possible, and can only be used with CreatFlags HardwareVertexProcessing
Commonly used properties of the PresentParameters class:
- Window, false is full screen mode, true is window mode, Direct 3D programs created using the Form class can only work in window mode;
- SwapEffect, used to control the way the backup cache area and screen display area are exchanged;
- SwapEffect Flip: Create multiple backup cache areas in the graphics card's graphics memory that are the same size as the screen display area. After modifying the data in the backup cache area, specify it as the screen display area and display it in full screen mode only;
- SwapEffect Copy: There is only one backup cache area. In order to display it, it is necessary to copy the data from the backup cache area to the screen display area;
- SwapEffect Discard: and SwapEffect Copy is basically the same, but when a new frame arrives, any unfinished data in the backup buffer will be discarded;
- BackBufferCount, the number of backup caches (1 to 3);
- BackBufferFormat, the format for recording colors;
- BackBufferWidth, number of units per row BackBufferHeight, total number of rows;
- AutoDepthStencilFormat, specifies the number of bits and format of the depth cache unit, such as DepthFormat D16;
- Enable AutoDepthStencil, set whether to allow automatic use of deep testing;
- DeviceWindowHandle, the window handle used, with a value of null indicating the use of the current active window.
Example program for 3D rendering using DirectX in C # form program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Demo_3D
{
public partial class MainFrm : Form
{
Device device = null;//define drawing equipment
public MainFrm()
{
InitializeComponent();
this.ClientSize = new Size(300, 300);//specify form size
this.Text = "DirectX sample program"; //specify form title
}
public bool InitializeDirect3D()
{
try
{
PresentParameters presentParams = new PresentParameters();//defining objects
presentParams.Windowed = true; //specify to Windows display in form of a form, using Form class created Direct 3D program can only work in window mode
presentParams.SwapEffect = SwapEffect.Discard; //after the current screen is drawn, it will be automatically deleted from memory
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams); //instantiation device object
return true;
}
catch (DirectXException e)
{
MessageBox.Show(e.ToString(), "Error"); //handling exceptions
return false;
}
}
public void Render()
{
if (device == null) //if device if empty, do not render
{
return;
}
device.Clear(ClearFlags.Target, Color.DarkGreen, 1.0f, 0); //clean up windows the interface is dark green
device.BeginScene();
//add rendering graphic code here
CustomVertex.TransformedColored[] vertices = new CustomVertex.TransformedColored[3]; //define vertices
vertices[0].Position = new Vector4(10f, 300f, 1f, 1f);
vertices[0].Color = Color.Red.ToArgb();
vertices[1].Position = new Vector4(this.Width / 2, 50f, 0f, 1f);
vertices[1].Color = Color.Green.ToArgb();
vertices[2].Position = new Vector4(this.Width - 150f, 100f, 10f, 1f);
vertices[2].Color = Color.Blue.ToArgb();
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
device.EndScene();
device.Present();
}
static void Main()
{
MainFrm basicForm = new MainFrm(); //create form object
if (basicForm.InitializeDirect3D() == false) //inspect Direct3D start or not
{
MessageBox.Show("unable to start Direct3D!", "Error");
return;
}
basicForm.Show(); //if everything is initialized successfully, display the form
while (basicForm.Created) //set up a loop to update rendering status in real-time
{
basicForm.Render(); //keep device rendering until the program ends
Application.DoEvents(); //handle input events such as keyboard and mouse
}
}
}
}
Reference Demo Link https://download.csdn.net/download/CXYLVCHF/14022457.