) Managed DirectX + C # Development (Getting Started) (8)
Source: Internet
Author: User
Chapter 7 vertex BufferingI. Index BufferingTriangles in 3D objects often share many vertices. If two triangles are combined into a rectangle and two vertices are used repeatedly, the number of repeated vertices increases when a more refined and complex model is to be presented. The cube has only eight vertices, but when it is represented by a triangle column, all vertices are reused. The index buffer is a buffer that stores the vertex data index. The index in the buffer is a 32-bit or 16-bit integer. For example, when an index 0, 1, and 6 is used to draw a triangle, the image is rendered by ing the index to the corresponding vertex. The following code creates an index Buffer: int numberverts = 8; short [] indices = {, 2, // front face, 2, // front face, 6, // back face 6, 5, 7, // back face, 4, // top face, 5, // top face, 7, // bottom face, 3, // bottom face, 1, // left face, 0, // left face, 7, // right face, 7 // right face }; mesh mesh = new mesh (numberverts * 3, numberverts, meshflags. managed, customvertex. positioncolor Ed. format, device); Using (vertexbuffer VB = mesh. vertexbuffer) {graphicsstream DATA = VB. lock (0, 0, lockflags. none); data. write (New customvertex. positioncolored (-1.0f, 1.0f, 1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (-1.0f,-1.0f, 1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (1.0f, 1.0f, 1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (1.0f,-1. 0f, 1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (-1.0f, 1.0f,-1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (1.0f, 1.0f,-1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (-1.0f,-1.0f,-1.0f, 0x00ff00ff); data. write (New customvertex. positioncolored (1.0f,-1.0f,-1.0f, 0x00ff00ff); Vb. unlock ();} using (indexbuffer IB = mesh. indexbuffer) {IB. set Data (indices, 0, lockflags. None);} the vertex is similar to the index cache. A vertex cache is a continuous memory that stores vertex data. Similarly, an index cache stores the index data continuously. Use the vertex and index cache to save data because they can be stored in the video memory. Rendering data in the video memory is much faster than rendering data in the system memory.
Ii. Index buffer instanceThe following is an example of using index buffer to display a rotating cube. The Code is as follows: first, define the object variable; private vertexbuffer VB = NULL; private indexbuffer IB = NULL; instantiate the object in the graphic initialization function: Public void initializegraphics (){...... VB = new vertexbuffer (typeof (customvertex. positioncolored), 8, device, usage. dynamic | usage. writeonly, customvertex. positioncolored. format, pool. default); Vb. created + = new eventhandler (this. onvertexbuffercreate); onvertexbuffercreate (VB, null); IB = new indexbuffer (typeof (short), indices. length, device, usage. writeonly, pool. default); IB. created + = new eventhandler (onindexbuffercreated); this. oninde Xbuffercreated (IB, null);} For the indexbuffer () constructor, the parameters are described as follows :?
Indices. Length-- Size of the bytes allocated to the cache. If you want to obtain a vertex cache that can store eight vertices, you must set this parameter to 8 * sizeof (vertex) in the vertex structure ).?
Usage specifies additional information about how to use the cache. The value can be 0, not marked, or a combination of the following tags:-- Dynamic: set this parameter to make the cache dynamic. Points: this parameter specifies the original cache point. Softwareprocessing: use software vertices to process writeonly: specifies that the application can only write to the cache. It allows the driver to allocate the most suitable memory address as the write cache.
Pool-- The cache in which the cache is stored is called static cache without using dynamic parameters. Static cache is usually placed in the video memory, and the data in it can be well processed. However, for static cache, reading and writing data from it is very slow, because access to the video memory is very slow. Therefore, static cache is generally used to store data that does not need to be changed frequently. For example, you can store terrain and buildings. Static cache should be filled during application initialization, rather than during runtime. The dynamic cache is usually stored in the AGP memory, and the data in the memory can be updated quickly. Processing data in the dynamic cache is not faster than processing data in the static cache, because the data must be transferred to the video memory before rendering. The advantage of the dynamic cache is that it can be updated more quickly. Therefore, if you need to update the data in the cache frequently, you should use the dynamic cache. For example, for particle systems, Dynamic Caching is generally applied. Let's take a look at the code for filling in the data: Private void onindexbuffercreated (Object sender, eventargs e) {indexbuffer buffer = (indexbuffer) sender; buffer. setdata (indices, 0, lockflags. none);} in the rendering function: protected override void onpaint (system. windows. forms. painteventargs e ){...... Device. setstreamsource (0, VB, 0); device. indices = Ib; angle + = 0.05f; device. transform. world = matrix. rotationyawpitchroll (angle/(float) math. pi, angle/(float) math. pI * 2.0f, angle/(float) math. PI/4.0f); device. drawindexedprimitives (primitivetype. trianglelist, 0, 0, 8, 0, indices. length/3 );.......} Note that the setstreamsource () method is used for vertex buffering, while the indices attribute is set for index buffering. Because only the same type of index buffer can be used at the same time. In addition, change the previous drawprimitives to drawindexedprimitives. Take a look at the parameters of this method as follows: public void primitive (primitivetype, int basevertex, int minvertexindex, int numvertices, int startindex, int primcount); primitivetype: indicates the type of the elements to draw. Basevertex: the offset from the index buffering start point to the first vertex index to be used. Minvertexindex: The smallest vertex index value among the vertices. Numvertices: Number of vertices to be used. Startindex: where the vertex is read from the array. Primcount: number of elements to be drawn. After the index buffer is created, you can use the indexbuffer. description attribute to obtain relevant information later;
Iii. Deep BufferingThe depth buffer is not used to store image data, but to record the depth information of pixels. To determine which pixel is finally drawn. In 3D scenarios, an object often hides a part of another object. In order for direct3d to determine the frontend and backend relationships of objects and draw them out correctly, we need to use depth buffering. It is also known as Z-buffer or W-buffer. It calculates the depth value for each pixel and performs a depth test. Through deep testing, we can determine which pixel is closer to the camera and draw it out. In this way, only the pixels closest to the camera can be drawn, and the hidden pixels will not be produced. The format of the depth buffer determines the accuracy of the Depth test. A 24-bit depth buffer is more precise than a 16-bit depth buffer. Normally, applications can work well under 24-bit depth buffering, but direct3d also supports 32-bit depth buffering.
Iv. Example of deep BufferOpen the cube drawn above, add two cubes in the scenario, and translate the added cubes for convenience. The Code is as follows: device. transform. world = matrix. rotationyawpitchroll (angle/(float) math. pi, angle/(float) math. pI * 2.0f, angle/(float) math. PI/4.0f); device. drawindexedprimitives (primitivetype. trianglelist, 0, 0, 8, 0, indices. length/3); device. transform. world = matrix. rotationyawpitchroll (angle/(float) math. pi, angle/(float) math. pI * 2.0f, angle/(float) mat H. PI/4.0f) * matrix. translation (0, 1, 5); device. drawindexedprimitives (primitivetype. trianglelist, 0, 0, 8, 0, indices. length/3); device. transform. world = matrix. rotationyawpitchroll (angle/(float) math. pi, angle/(float) math. pI * 2.0f, angle/(float) math. PI/4.0f) * matrix. translation (0,-1,-5); device. drawindexedprimitives (primitivetype. trianglelist, 0, 0, 8, 0, indices. length/3); execute the program, note that this is not added with in-depth buffering effect: Add the following depth buffer, which is very easy to add. You only need to change the parameter attributes when creating a device: presentparameters presentparams = new presentparameters (); presentparams. enableautodepthstencel = true; presentparams. autodepthstencilformat = depthformat. d16; presentparams. swapeffect = swapeffect. discard; format current = manager. adapters [0]. currentdisplaymode. format; enableautodepthstencel indicates whether to use the depth buffer. Autodepthstencilformat defines the precision. The common enumeration formats and meanings are as follows: d32 -- indicates 32-bit depth buffer d24s8 -- indicates 24-bit depth buffer and retains the 8-bit template buffer )? D24x8 -- 24-bit depth buffer? D24x4s4 -- indicates the 24-bit depth buffer and retains the 4-bit template buffer? D16 -- indicates that the 16-bit deep buffer cannot get the correct result if the program is executed. In the rendering function, set the code to device. clear (clearflags. target, color. black, 1.0f, 0); changed to: device. clear (clearflags. target | clearflags. zbuffer, color. black, 1.0f, 0); now, execute the program. The result is as follows: the filtered advertisement. Now, the three cubes have a clear sense of attention. From: dandancool http://blog.csdn.net/dandancool/archive/2007/06/26/1666576.aspx
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.