DIRECX generally use VC + + development environment, and C # related to less information, a recent project to use a 3D model, and the development environment is Visual C #, there is no way to collect information around, look for relevant information, and then found a few related blog, But these posts are not very careful, some of the content has lost some key code, this time we need to collect data, we learn to complete these key code.
Talk less, let's talk about the steps directly.
First, we have to prepare a 3D model, Now the 3D models are all. Max or. obj or other format, but because I am using the Directx9.0 development environment, so load the 3D model when the need to convert the file to the. x format, it does not matter, we can download a 3dsMax software, and then install the Microsoft Panda Plug-in, the software can be. Max The format 3d file is converted to. x format output. The main purpose of this step is to get a 3D model of the. x format.
two . The next step is to download the DIRECTX9.0SDK and install the development tool after the download is complete.
three . Then enter the project, create a new Visual C # WinForm form program, and click OK, and our programming work begins.
First, the project needs to use the DirectX development package, so we need to add a reference to the project, right-click the project, select Add Reference, and then select Browse in the referenced dialog box, where the reference is: C:\Windows\Microsoft.NET\DirectX for Managed code\1.0.2902.0, note that the final application of the folder is 1.0.2902.0, we quote this inside the three files: DirectX.dll, DirectX3D.dll And DirectX3DX.dll, select these three items and click OK, then add the reference, and then we need to add the following reference to the form code:
Using Microsoft.directx;
Then our citation work is done.
Because I'm going to display the 3D model on a control, I've chosen a panel control to be placed on the main window, and our model will be displayed on the panel later. First, set the following global variables in the form:
#region
//save 3D Files
private mesh mesh = null;
Device
private Device Device = null;
Material
private material[] meshmaterials;
Private texture[] meshtextures;
Gets the current program's debug path
string path = System.Windows.Forms.Application.StartupPath;
Angle
private float angle = 0.0f;
#endregion
Then write the function of our initialization graphics device:
<span style= "FONT-SIZE:14PX;" > //Initialize graphics device public
void Initializegraphics ()
{
//Set Variable
presentparameters presentparams = new PresentParameters ();
Set to run
presentparams.windowed = true in windowed mode;
Set the exchange effect to discard
presentparams.swapeffect = Swapeffect.discard;
Presentparams.autodepthstencilformat = Depthformat.d16;
Presentparams.enableautodepthstencil = true;
Create device
//Because I am displayed in Panel1, so the third variable in device is the name of the panel
device = new device (0, Devicetype.hardware, Panel1,
Createflags.softwarevertexprocessing, presentparams);
My 3D file is in the debug model file, so temp gets the address
string temp = path for the 3D model;
Temp = temp + "\\Model\\Model.X";
This function is used to load the 3D model and save it in mesh
loadmesh (temp);
} </span>
Then the Loadmesh (string file) function that loads the 3D model
private void Loadmesh (string file) {extendedmaterial[] Mtrl = null;
Load try {mesh = mesh.fromfile (file, meshflags.managed, device, out Mtrl); If material is available, load the IF (Mtrl!= null) && (Mtrl. Length > 0) {//These are the global variables defined previously, preserving textures and textures meshmaterials = new Mat Erial[mtrl.
Length]; Meshtextures = new Texture[mtrl.
Length]; for (int i = 0; i < Mtrl. Length;
++i) {/* The current temp is the model file in debug, *model files have files for storing textures and materials
* * Temp = path + "\\Model\\"; Meshmaterials[i] = Mtrl[i].
Material3d; if ((mtrl[i). Texturefilename!= null) && Mtrl[i]. Texturefilename!= String.
Empty) {Meshtextures[i] = Textureloader.fromfile (device, temp + mtrl[i).
Texturefilename);
catch (Direct3dxexception ex) { MessageBox.Show (ex.
ToString ());
Return }
}
After loading the model, you need to draw the model, so you need a function to draw the model:
Draw mesh material and texture
private void DrawMesh (float yaw, float pitch, float roll, float x, float y, float z)
{
angle = 0.01f;
Device. Transform.world = Matrix.rotationyawpitchroll (yaw, pitch, Roll) * Matrix.translation (x, y, z);
for (int i = 0; i < meshmaterials.length ++i)
{
//Set material
device. Material = Meshmaterials[i];
Sets the texture
device. SetTexture (0, meshtextures[i]);
Draw
Mesh. Drawsubset (i);
}
}
Finally, a photographic light is set up, the angle used for lighting:
Set the camera
private void Setupcamera ()
{
//(float) MATH.PI/12 Set object size
device. Transform.projection = MATRIX.PERSPECTIVEFOVLH (float) MATH.PI/12, this. Width/this. Height, 0.80f, 10000.0f);
Device. Transform.view = MATRIX.LOOKATLH (New Vector3 ( -400, -100, 600.0f), New Vector3 (), new Vector3 (0, 1, 0));
Device. Renderstate.ambient = Color.Black;
Device. Lights[0]. Type = lighttype.directional;
Device. Lights[0]. diffuse = Color.antiquewhite;
Device. Lights[0]. Direction = new Vector3 (0, 1, 0);
Device. Lights[0]. Update ();
Device. Lights[0]. Enabled = true;
}
So far, basically done, and finally the gradual call to these functions, and then show that I use the panel when painting the control of these functions, as follows:
Panelpaint is my own name, the corresponding event is paint
private void Panelpaint (object sender, PaintEventArgs e)
{
Initializegraphics ();
Device. Clear (Clearflags.target | Clearflags.zbuffer, Color.skyblue, 1.0f, 1);
Setupcamera ();
Device. Present ();
Device. BeginScene ();
Draw our Mesh
drawmesh (angle/(float) Math.PI, angle/(float) Math.PI * 2.0f, Angle/(float) math.pi/4.0f, 0.0f, 0.0f, 0.0f);
Device. EndScene ();
Render ();
Device. Present ();
This. Invalidate ();
}
Finally put out an effect chart:
So far, basically accomplished. But here are a few more questions to note:
1. A problem may be encountered during the process of compiling a program:
Mixed-mode assemblies are generated for the runtime of the "v1.1.4322" version, and cannot be loaded in the 4.0 runtime without additional information being configured.
The solution to this problem is to open the app.config file in the file, comment out the original content, and change it to the following:
<span style= "FONT-SIZE:14PX;" ><span style= "font-family: ' Microsoft Yahei ';" ><?xml version= "1.0"?>
<configuration>
<startup uselegacyv2runtimeactivationpolicy= " True ">
<supportedruntime version=" v4.0 "/>
</startup>
</configuration></span ></span>
The program should be no more problematic.
2. If you are importing your own 3D model, When converting a model file to an. x file using the imported panda control in 3dsmax, there is an option that the Xfilesetting setting must be set to binary, and if it is set to text, DirectX will not recognize it when it is read into the file. Screenshot below:
The problem that can encounter basically is this, here enclose source code link:
http://download.csdn.net/detail/t46414704152abc/8709251
Well, that's basically it. Hope to help everyone.