Making Our Triangle Three Dimensional
Looking back to see our previous procedure, it doesn't seem to three dimensions. We just draw a triangle in the Windows window, and this is very easy to do with GDI, now I have to do it, how to draw an object to let him look three-dimensionalization, and these, very It is easy to improve in our existing procedures.
If you still remember, when we created the first triangle earlier, we used something called transformed coordinates, which can be identified for the screen, and we can easily define it. These conversions are very common applications in many modern 3D games.
If we define these matches, we must define each vertex in the world coordinates, not just in the screen area as it is as simple as it is, you can imagine the world coordinates into an infinite Cartesian three-dimensional space, you can Anything you want to place anything else, ok, now let us modify the program to complete a three-dimensional triangle that is converted.
First, we must change the format of the triangular vertex data, we use CustomvertEx.PositionColored to change the format of the triangular data.
CustomvertEX.PositionColored [] VERTS = New CustomvertEX.PositionColored [3]; VERTS [0] .SetPosition (New Vector3 (0.0F, 1.0F, 1.0F)); VERTS [0] .color = system.drawing.color.aqua .Toargb (); VERTS [1] .SETPSITION (New Vector3 (-1.0F, -1.0F, 1.0F)); VERTS [1] .Color = system.drawing.color.black.toargb (); VERTS [2 ] .SetPosition (New Vector3 (1.0F, -1.0F, 1.0F)); VERTS [2] .color = system.drawing.color.purple.toargb ();
And to change the properties of your vertex format together
Device.VertexFormat = CustomvertEx.PositionColored.Format;
What did you do? If you run the program now, you will find what changes have occurred. Before we understand, let's explain the above change, you will find that we will use the positionColored structure instead of the TransforMedColored structure, replacing the product4 this class, these new structures save the 3D spatial coordinates of the graphical vertex And the colors of the vertex, and we must let Direct3D we have to change the graphic after the data type, so we must also add the last sentence to let Direct3D know the format
Since we have changed the data format, why is our programs still have any display? The reason is that although we told Direct3D we have to draw three-dimensional and color information, but did not tell him how to display this information, that is, did not tell him which position to observe the graphics we draw, so we must add one The camera allows Direct3D know which place to display graphics on the screen.
The camera must be configured in the device through two transformations, each transformation being defined as a matrix of 4 * 4 to pass to Direct3D, which is typically defined to make the real scene change to the correct display of the screen. One of the easiest ways is to complete this conversion relationship using the PerspectiveFovlh function in the Matrix class, which will create a three-dimensional projection in the visual area, in which the content in this projection will be displayed. As for what is the left hand coordinate system, you can check the information yourself. . Ha ha. The left hand coordinate system is used in Direct3D. Ok, the following is the prototype of our mapping matrix function. Public Static Microsoft.directX.matrix PerspectiveFovlh (System.single FieldofViewy, System.Single Znearplane, System.single Zfarplane)
This image conversion describes a flat section within a scene, you can imagine it into a pyramid that cut the head, that is, a four-priced platform, in this prize is the area you can see, That is, the latter two parameters, proximal sections and metades in the function are displayed in the screen, which describes the front and rear boundaries of the prism, and the second parameter describes the pyramid of the pyramid, which is to Always say the length of the screen. When we didn't set our mapping conversion, our visible area couldn't see anything, but even if we set a map conversion, and did not set some properties of the camera itself, we can't see something, we put This setup camera itself property is called view conversion (do not know that translation is not for View Transform), and the function prototype of view conversion is as follows.
Public Static Microsoft.directx.matrix Lookatlh (Microsoft.directx.Vector3 CameraPosition, Microsoft.directX.Vector3 Cameratarget, Microsoft.directX.Vector3 CameraupVector)
This function is more likely to understand, his three parameters are used to describe the properties of the camera, the first representation of the coordinate position of the camera, the second means that the object we want the camera to see, the last one should be a positive direction. (The Last Argument is The Direction That Will Be Considered "UP.")
By mapping conversion and view conversion, Direct3D now has enough information to depict the new triangle we need, we now change the previous code to make the graphic correctly, first, we join a new function, function of "setupcamera" as follows
Private void setupcamera () {device.Transform.Projection = Matrix.PerspectiveFovLH (FLOAT) Math.pi / 4, this.width / this.height, 1.0F, 100.0F); device.transform.view = matrix.lookatlh New Vector3 (0, 5.0F), New Vector3 (), New Vector3 (0, 1, 0));
Now you can see, we created a mapping transform in the device and told Direct3D our visual area, then we created a visual conversion in the device, let Direct3D know some of the basic properties of the camera. Then in the onPaint function, the display is cleared to call it to initialize the camera. protected override void OnPaint (System.Windows.Forms.PaintEventArgs e) {device.Clear (ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0); SetupCamera (); CustomVertex.PositionColored [] verts = new CustomVertex .PositionColored [3]; . . . }
Now we can run our program again.
But it seems that it is not good. Although we have already drawn a triangle, it is all black, even if we specify his color, he is also black. Problem between Pretransformed Triangle and Nontransformed Triangle, we have done the graphic of the converted system, and now we are unconverted graphics, in the unconverted system, Direct3D will use our defined lights under the default conditions. Bright objects to determine the pixel color of the final vertices of the graph, but we didn't determine the color of the outside light, that is, there is no light illuminated our object, naturally he didn't have a color display but black. We will tell you how to define the color color of the outside world, but in the eyes, we have a simple way to solve this problem, just don't light, all colors will display the colors we define instead of the color calculated by light, we are in Setupcamera's last Add a line
Device.renderstate.lighting = false;
Now run our program, we found that the things running and the same is exactly the same, it seems that we have done so much work, and finally seems to return to the same place, but in fact, we have established a complete 3D world. The triangle in the middle, but we want users to look like this is a three-dimensional world, then we have to do something.
How to make the triangle of us look more than three-dimensional? The easiest way is to let this triangle rotate some angles, and we can simply call some methods to implement this angle rotation.
The world transformation in the device is a transformation that transforms our real-world models to look at the screen looks close to the natural world we have seen (leaning, this sentence is too long, it is probably like this .. I I have dizzy), and this transformation has many kinds, including angle rotation, proportional dimensions, etc., we can change one angle, there are many functions in Matrix objects to complete this work, we are in setupcamera functions Add the following code to add it.
Device.Transform.World = Matrix.Rotationz ((float) math.pi / 6.0f);
This code is telling Direct3D after this, each object will change until a new world transformation is specified, here, we have created a transformation of the Z-axis rotation, in this function, all variables Must be an arc rather than an angle, ok, now run our program, then compare with the previous, we will find that the object is rotated about a angle about the Z-axis. Just this, it seems to be too simple, we can modify the above function to make the object constantly rotate about the Z axis, so it should be more interesting.
Device.Transform.World = Matrix.Rotationz ((System.Environment.TickCount / 450.0F) / (FLOAT) Math.pi);
Ok, now run our procedure, it will find that the object has begun to rotate the Z axis and the rotation is not stopped, but there is a problem, we see the object rotation seems to have a little jumping, that is because SYSTEM.ENVIRONMENT .Tickcount This variable default step is 15 milliseconds, so it seems a bit jumping, don't worry, we can use your own variable to change this situation. Modify the function.
Device.Transform.World = Matrix.Rotationz (Angle / (FLOAT) Math.pi; Angle = 0.1F;
Note, don't forget to define Angle variables in front
Ok, now run again, we will find that the rotation of the object is very smooth, basically reached our needs, and we sometimes hope that the object is only rotated about a shaft, but Direct3D has thought a lot for us. , Haha, we can rewrite this function
Device.Transform.World = Matrix.RotationAxis (New Vector3 ((Float) Math.pi * 2.0F), Angle / (Float) Math.pi * 4.0F), Angle / ((Float) Math.pi * 6.0f)), angle / (float) math.pi;
Obviously, this is to tell Direct3D to rotate around the three coordinate axes, run, huh, is it started to rotate, it seems that it has been more than three-dimensionalized, no longer the plane in front. Good look. .
In the end, we sometimes can't see it when we rotate in triangles, then we can add the following code to the top of the setupcamera function.
Device.renderstate.cullmode = Cull.none;
Ok, our first three-dimensional triangle has been completed. In this way, it is basically a real 3D program, don't worry, there will be more things in the future. Interested friends can wait patiently, I will stick to it, but now the graduation design has begun, but also translated articles, I am going to English every day, my head is big. . . Oh, oh, my graduation design is "Digital Watermarking Technology Based on Feature Space". If any friend has this information, please tell me. . Haha, thank you. . . My email is wyh267@163.com QQ: 128102 haha, thank you, I don't want the source, as long as some information and related documents can, thank you. . .