Direct3D Learning Notes (4) Making a real 3D space

xiaoxiao2021-03-06  18

Making Our triangle three Dimensional looks 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 = system.drawing.color.purple.toargb (); and want to change your vertex format attribute 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. We have changed the data format, but why do our programs still have no Show? 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. Well, here we are mapping matrix function prototype public static Microsoft.DirectX.Matrix PerspectiveFovLH (System.Single fieldOfViewY, System.Single aspectRatio, System.Single znearPlane, System.Single zfarPlane) the mapping conversion is described a scene visual range Inside the flat section, you can imagine it into a pyramid that cut the head, which is a four-priced platform, which is the area you can see in this prize, which is displayed in the screen, in the function The latter two parameters, near-section and metaplans, respectively, described the front and rear boundaries of the prism, and the second parameter describes the positive cutting values ​​of the angle of this pyramid, which is the long-width ratio of the screen we often say. 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 process of setting the camera itself property called view conversion (do not know the translation right view transform), view conversion function prototype is as follows public static Microsoft.DirectX.Matrix LookAtLH (Microsoft.DirectX.Vector3 cameraPosition, Microsoft.DirectX.Vector3 cameraTarget, Microsoft.directX.Vector3 Cameraupvector This function is easier to understand, his three parameters are used to describe the properties of the camera, the first representation of the coordinate position indicating the camera, the second means that we want the camera to see, last One should be a positive direction. (The Last Argument "UP.") By mapping conversion and view conversion, Direct3D now has enough information to depict the new triangle we need, we now modify the previous code to display the correct display of the graphics Out, first, we join a new function of "setupcamera", the function is the following 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. 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 DEVICE.TRANSFORM.WORLD = Matrix.Rotationz ((float) math.pi / 6.0f); this code is telling Direct3D after this, each object will change until a new The world transformation, here, we created a transform of the Z-axis rotation, in this function, all variables must be curvature instead of angle, ok, now run our program, and front Comparison, we will find that the object is rotated by 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 program, it will find that the object has begun to slow down the Z axis and not The stop is rotated, but there is a problem, we see the object rotation seems to have a little jumping, that is because the default steps of system.environment.tickcount are 15 milliseconds, so it will be a bit jumping, don't worry We can use your own variable to change this situation.

转载请注明原文地址:https://www.9cbs.com/read-50594.html

New Post(0)