Although the previous section is a complete Direct 3D program, you don't look at the code, you can't see what the difference between him and a default C # project, so we must establish something in our scene to explain that we have written Is a complete Direct3D program
In the Windows Forms class, there is an internal method to redraw a window at an appropriate time. What we have to do is to rewrite this method, and whenever the window changes, it will load this.
Method, so this method is a good place to write code. It is not necessary to pay the user to change the window. Every judgment is automatically finished by Windows. Haha function is like protected override void onpaint (system.windows.Forms.Painteventargs e) {device.clear (clearflags.target, system.drawing.color.cornflowerblue, 1.0f, 0); device.present ();
Here tells us to draw a blue window. The CLEAR method is responsible for clearing the window display and draws the background color. Now you compile this program, it will find that the window has become blue, and no matter what you enlarge, narrow, maximize, minimize, blue is always full of full Window, here, you may think that I am still in the rhombus, and directly in the Form's property settings can achieve this effect, so, next, we have to come a bit slightly different.
Oh, in the 3D graphics system, the most basic object is a triangle. With a sufficient triangle, we can create anything, including smooth surfaces, so in our procedure, it is natural to create a triangle. Here, the coordinate system we use is a screen coordinate system instead of the world coordinates, (World coordinate system and coordinate conversion We will discuss in detail) in our creation of triangles must do two things: First, we need some data information To construct our triangle; second, we have to tell us that the equipment just built is in this triangle.
Very happy things, the Managed Direct3D run library already has a ready-made constructor to build our triangle, this is the CustomvertEX class below Direct3D namespace, which includes some basic formats to create basic graphics. His constructor can create some Direct3D to understand and we can also use the data, we will discuss this class in detail, but now we will use the TransforMedColored constructor to construct our triangle, use this function to tell Direct3D we build The triangle does not need to change, that is, do not need to rotate or move, and will also specify a color for each vertex, well, we continue to add the following statement in the onpaint method. CustomvertEx.TransformedColored [] VERTS = New CustomvertEx.transformedColored [3]; VERTS [0] .SetPosition (New Vector4 (this.width / 2.0F, 50.0F, 0.5F, 1.0F)); VERTS [0] .color = System.drawing.color.aqua.toargb (); VERTS [1] .SetPosition (new vector4 (this.width - (this.width / 5.0F), this.Height - (this.Height / 5.0F), 0.5F , 1.0F)); VERTS [1] .Color = system.drawing.color.black.toargb (); VERTS [2] .SetPosition (new vector4 (this.width / 5.0f, this.height) / 5.0F), 0.5F, 1.0F); VERTS [2] .Color = system.drawing.color.purple.toargb (); My ManageD DirectX is a 1.900 version, so there is a setPosition method, some people may 1.2039 or other version, it may not be able to compile, then change setPosition to position =. Some places may also be modified, so I suggest that you have three vertices in a total of the full management DirectX, and each vertex stores the information we need, the front is X coordinates and Y coordinates. The third is Z coordinate, the fourth is the two behind W (three-dimensional coordinates) we can ignore, then set the color, the ToARGB () method is to convert the set color to Direct3D 32-bit colors that can be identified. Here, we used a vector to represent four parameters.
Ok, we already have data, the next job is drawing. Device.beginscene (); device.vertexFormat = CustomvertEx.TransformedColored.Format; Device.DrawuserPrimities (PrimitiveType.triangLIST, 1, VERTS); device.endscene ();
Device.beginscene () method is to tell Direct3D We have to start drawing. The same EndScene () is telling it that we have ended. The VertexFormat property tells it what we will draw, that is, tell it that we will draw in what format, note that the data we apply in front is this format, so this format drawing here.
DrawUserPrimitives function is telling Direct3D what we will paint, how much, the first primitiType.trianglelist is telling it that we have to draw triangles, the second 1 is quantity, this numerical our previously defined array divided by 3, finally One is our data.
Ok, on the last Plus code: this.invalidate (); if you compile the program now, you will find but move or reset the window size, does not update the display. The reason is that when we need to redraw the entire window, Windows does not calculate the shrinkage of the window every time. Therefore, you just remove the displayed data, when there is no deletion already displayed. Fortunately, there is a simple way to solve this problem, we can tell the Windows window to always need to be redirected throughout.
Oh, now I can only show a blank, and our triangle looks still whimming, especially when you adjust the window size. It turns out that Windows always tries to draw the current window (ie blank this window) in the invalidate () method. There are other drawings in our onpaint way! It is easier to solve it by changing the "style" attribute of the window. Add the following code in the constructor
THIS.SETSTYLE (ControlStyles.allpaintingInwmpaint | constolstyles.opaque, true);
Ok, now start running the program, I will find a triangle in the screen, here, we have completed the Direct3D drawing function, the following chapter, we have to start drawing the real 3D graphics.
Next chapter I am still waiting for one or two, everyone can go see a lot of www.gameres.com, there are some people who have translated a lot, but he is selective translation, I will here. One chapter translation, only to see that there is that ability and physical strength, I have a big four, and the English level 4 is just knowing, so huh, the level is limited, the translation is not good, I hope everyone should point out, thank you. . . .