It has seen Managed DirectX for a few days. Today, I made the first program --- The rotating square is the experience of the procedure made today, deepening the understanding of the procedure (hope all the high-end finger points):
1. Make a D3D program, first to reference its dynamic joint library Microsoft.directX and Microsoft.Directx.direct3d2 in C #, the program first has to instigate the hardware device, here is implemented with the initialgraphics () method: establish a device (Device), To establish Device, PresentParameters, which is mainly equivalent to the role of setting device parameters. After Device is established, you must handle its DeviceReset event, I mainly modify the properties of Device.Renderstate in this:
Device.renderstate.cullmode = Cull.none; // Turn Off Culling, So We See The Front and Back of The TriangleDevice.Renderstate.lighting = FALSE
In the initialgraphics method, you must also call this DeviceRest event manually to first initialize.
3. Initialgraphics not only establishes and initializes the device, but also to establish a VertexBuffer task, I put this task in this method in this method, first create VertexBuffer: VertexBuffer = New VertexBuffer (Typeof), 18, Device, 0, CustomvertEX.PositionNormalColored.Format, pool.default); // The first parameter is the specified vertex type, the second parameter is the number of vertices. . . VertexBuffer.created = new eventhandler (VertexBuffer_created); // More Created events, this event is used to process, one but the VertexBuffer is established, what is needed next steps. Ha ha. . What do you need to do? Of course, it is an establishment of a top! ! VertexBuffer_created (VertexBuffer, Null); // Transport the current VertexBuffer to the CREATED event
4. In the VertexBuffer_created event processing method, create a vertex. I want to achieve a square, here I build the vertex is stupid, did not use the indexbuffer method (still not ...), so a total of 18 top points, 4, around 10, below 4. When establishing a vertex, pay attention to use the clockwise direction.
5. Set up Camera, here I named setcamera (). The main content is to set the Device.Transform.World (World Matrix), Device.Transform.View (observation matrix), Device.Transform.Projection (Chinese call?)
Int itime = environment.tickcount% 1000; // itime value from (0,999) float fangle = itime * ((float) math.pi * 2.0F) /1000.0F; // 2 pidevice.transform.World = Matrix .ROTATIONY (Fangle); // Winding Y rotation Device.Transform.View = Matrix.lookatlh (New Vector3 (0.0F, 10.0F, -10F), New Vector3 (0.0F, 0.0F, 0.0F), New Vector3 (0.0F, 1.0F, 0.0F)); // Where is the three parameters Look (at, to, Up), where, the third parameter is equivalent to being a positive direction, generally (0, 1, 0 Device.Transform.Projection = Matrix.PerspectiveFovLH (FLOAT) Math.pi / 4.0F, 1, 1.0F, 100.0F); // The first parameter is angle, proportion, can be seen, can see A far point 6. Finally, the render! First set up the client area background, then start the scene rendering: 1, call setcamera () method 2, specify VertexBuffer 3, VertexFormat 4, painting 5, fast speed scene if (device == null) // Look at the answer Return; if (pause) // read the answer to complement Return; device.clear (Clearflags.Target, System.drawing.color.black.toargb (), 1.0F, 0); device.beginscene ();
SetCamera (); // set the first observation matrix, etc. device.SetStreamSource (0, vertexBuffer, 0); // then the source device.VertexFormat = CustomVertex.PositionNormalColored.Format; // format device.DrawPrimitives (PrimitiveType.TriangleStrip, 0, 12); // Draw Device.EndScene (); device.present (); 7. Main method main. First instantiate Form, then adjust the initialgraphics () to initialize the device, display the Form, and call the render (), process the message queue when the Form is not locked. Using (MyGame FRM = new mygame ()) // USING statement Note {if (! frm.initializegraphics ()) {MessageBox.show ("Error Occurs"); return;} fm.show (); while (FRM. Created) {frm.render (); Application.doevents ();}} Note that the USING statement is that an instance is created in the USING statement to ensure that DISPOSE is called on the object when exiting the USING statement. When you reach the end of the USING statement, or if an exception is triggered before the end of the statement, you can exit the USING statement.
.
After writing, I have been in deepening the impression, you have seen it, I don't know how my process is not understood, or conceptual mistakes, please guidelines. Thank you!