Deep Dive: direct3d Cai junsheng All Rights Reserved

Source: Internet
Author: User

/***********************************
* Author: Cai junsheng
* Source: http://blog.csdn.net/caimouse/
************************************/
 
Deep Dive into direct3d
Deep Dive: direct3d Cai junsheng All Rights Reserved
· Chapter 3 basic 3D knowledge
· 1. 1. What is 3D?
· 1. 2 3D related concepts
· 1. 3 DirectX Introduction
· 1. 4 3D elements in direct3d
· 1. 5 direct3d surface and vertices Normal Vector
· Chapter 2 direct3d System Architecture
· 2. 1 position of direct3d IN THE SYSTEM
· 2.2.direct3d component locations
· 2. 3 Rendering
· Chapter 2 direct3d Program
 
· Appendix A 1. direct3d object
· Appendix A 2. direct3d Devices
· Appendix A 3. direct3d Resources
· Appendix A 4. direct3d coloring
· Appendix A 5. direct3d display environment
· Appendix A 6. direct3d primitive Rendering
· Appendix A 7. direct3d surface buffer flip
· Appendix A 8. 3D Coordinate Transformation in direct3d
· Appendix A 9. Matrix

What is 3D?
3D is the abbreviation of Three-dimensional, which is a three-dimensional image. 3D images are displayed in the computer, that is, three-dimensional images are displayed in the plane. Unlike the real world, the real 3D space has a real distance space. The computer just looks like the real world, so the 3D images displayed on the computer are just as real as they are. One feature of human eyes is that they are near, far, and small, which forms a stereoscopic effect.
The computer screen is a two-dimensional plane. The reason why we can enjoy a real 3D image is that the visual illusion is generated by the human eyes due to the different gray colors displayed on the computer screen, the two-dimensional computer screen is perceived as a three-dimensional image. Based on the Color learning knowledge, the protruding part of the three-dimensional object edge is generally high brightness, while the concave part is dark due to the occlusion of the light. This recognition is widely used in drawing buttons and 3D lines on webpages or other applications. For example, to draw a 3D text, that is, to display a high-brightness color in the original position, and to outline its contour with a low-brightness color in the lower left or upper right corner, in this way, 3D text is visually displayed. In specific implementation, two 2D texts of different colors can be drawn in different positions using the same font, as long as the coordinates of the two texts are appropriate, in this way, you can visually produce 3D texts with different effects.

Related Concepts
When learning ry, you must use the coordinate system to locate and calculate the ry. To display 3D images in a computer, that is, to display the ry, that is, to convert digital coordinates into geometric images. Therefore, the coordinate system must be learned. Otherwise, there will be no way to display the large amount of coordinate data, or it will be messy.
The coordinate system is also used in direct3d. Its coordinate system is the left-hand coordinate system. Currently, there are two common coordinate systems, as shown in:
The source image is lost.
 
Figure 1.1: Left-hand coordinate system and right-hand Coordinate System

The left-hand coordinate system points the positive direction of the X axis to the right, the positive direction of the Y axis to the batch up, and the positive direction of the Z axis to the inside. When the left hand is stretched out, let the four fingers point to the positive direction of the X axis, the points to the positive direction of the Z axis, and the points to the positive direction of the Y axis. The coordinate system that complies with these rules is called the left-hand coordinate system.
The right-hand coordinate system points the positive direction of the X axis to the right, the positive direction of the Y axis to the batch up, and the positive direction of the Z axis to the outside. When the right hand is reached, let the four fingers point to the positive direction of the X axis, the points to the positive direction of the Z axis, and the points to the positive direction of the Y axis. The coordinate system that complies with these rules is called the right-hand coordinate system.
Of course there are many other coordinate systems, so we will not discuss them one by one.
If you want to read from a file a geometric model that is not in the left-hand coordinate system, you need to convert it accordingly. For example, switching from the right-hand coordinate system to the left-hand coordinate system means changing and turning the sequence of triangle vertices.
3D plotting is to create a 3D illusion on a two-dimensional computer screen. To achieve this, mathematical transformations are used to model and process images. To familiarize yourself with 3D algorithms and concepts, you can use direct3d to create and process 3D worlds and objects. Understanding how 3D algorithms are implemented will help you understand how to use direct3d.
3D drawing is much more complex than 2D drawing, because the program needs to understand the principle of 3D graphics in two-dimensional plane display, much more than display on the screen. Creating 3D images usually uses commercial 3D modeling software, such as 3DS or 3D Max.
You can use 3D graphics algorithms to process various models like this in real time, making your models in the world more realistic. This realistic model consists of objects, cameras, and light sources.
3D objects in the 3D world have various attributes that can be changed by processing these attributes.
The shape of an object is defined by the surface, and the surface can be defined as a plane or quadratic surface. If you have read some contour models, you will know the basic surface used for an object. Each object has a texture, which defines the surface roughness and smoothness. Each object has a diffuse reflection and a mirror reflection. The reflection attribute determines the degree of light reflection on the object. Diffuse Reflection indicates the brightness of the object. A new clean ball has a higher diffuse reflection than an old dirty ball. Mirror reflection represents the brightness of the object, and the crystal bottle has a higher mirror reflection than the wood block. The position of an object, which is defined by three coordinate axes in 3D space: X, Y, and zcoordinate. The orientation of an object, that is, a certain direction of three-dimensional space. The rotation of the orientation is to rotate the object.
This processing refers to a combination of small or most basic objects. The combination simplifies the movement of complex large objects. For example, a bicycle consists of two rotating wheels. When the wheel rotates, the whole car moves forward. A relatively static Coordinate System for wheel rotation and movement. The combination allows the system to move the coordinates forward while keeping the wheels on top; no need to move the wheels forward explicitly. Combination is very useful for complex mobile objects, such as cars, airplanes, and animals.
To see objects, a video camera is defined to represent a user's association with the 3D world. The camera has a position and a direction that defines where the observer views and sees the object in the 3D world. In the 3D world, move the camera and change its direction to see different parts of the object.
Although you have an object and a camera, you still can't see anything unless you hit some light on the object surface. You can create different lighting styles and change the appearance of an object in a scene by changing the degree of illumination of the light. Different Lighting types and effects occupy different CPU resources and other resources.
Ambient Light provides fixed lighting for all objects in the scenario, and the intensity and direction of all other lights are different.
A light source emits light from a point. The angle of reflection depends on the angle between the point of light and the position of the object. Reflection will occur on the surface of the object.
Parallel Light is similar to a point light source, but it is a light emitted from a plane. After reflection, the incident angle is the same size as the anti-reflection angle. This is the reason for the low amount of parallel light computing, But it loses some sense of realism.
The concentrating source is like its name. As it approaches an object, the light intensity changes.
Direction light is most often used to simulate the sun and other infinite light sources. This type of light source is considered to be from infinity, and its intensity does not change as it approaches an object. In the only direction, the brightness is fixed.
The brightness or intensity can be determined to influence the degree to which the object is illuminated. The concentrating source and other light sources have a direction to determine where the light goes from.
The intensity of some light sources is affected by the distance. But this is not the case for ambient light. It has no location, not a direction. Usually there is an infinite distance from the scene.
You can create different colored lights. This is much more complex than the white light, because it changes the color of each object.
3D plotting has two important cropping concepts: View volumes and visible surface ). The view area is used to reduce the number of rendered objects. The closer the camera is, the larger the object is and the more details it has. When you stay away from the camera, the object will become smaller, with fewer details. When an object is very far away, it can disappear from the field of view without losing the perceived sense of reality. In fact, it is easier to simulate objects at an appropriate distance without being noticed.
Objects behind the camera can be well cropped; in 2D applications, objects larger than four sides of the screen are cropped. Two projection types are used in cropping. One is parallel (orthogonal) projection, and the other is perspective projection. No shadow points are used for perspective projection. As its name suggests, objects far away from the observer become smaller.
Visual surface detection is a method to accelerate rendering algorithms by avoiding computing and rendering invisible objects. It needs to calculate which object's surface is not cropped in the view area and which faces are far from the camera.
Rendering 3D scenes in real time requires processing a large amount of computing. Therefore, we need to find some good methods to reduce the amount of computing and optimize program performance. However, with the development of technology, we do not pay more and more attention to optimization, and the display environment is very authentic.
With the overview of 3D graphics programming, you can consider using direct3d to start making applications. Direct3d allows you to access the 3D hardware acceleration functions of many graphics cards without Directly Writing drivers, allowing many 3D algorithms to run directly in the graphics card, greatly accelerating the performance of the 3D program.

DirectX Introduction
About DirectX

Microsoft released DirectX 9.0 with "A number of exciting features! DirectX delivers better image display quality, making multiplayer games scalable and delivering better audio. DirectX 9.0 performs well in at least the following aspects:
1. Enhanced interfaces for DirectDraw and direct3d, simplified application expansion, and improved performance
2. Improved graphic creation tools to make it easier to make the best 3-D roles and environments
3. Point Light and pixel light make the image more realistic
4. Enhanced directsound and directmusic and simplified application extension
5. The dls2 audio synthesis function improves the realism of the instrument audio.
6. The device shooting function of directinput makes it easier to support devices;
7. directplay improves the performance and scalability of multiplayer games;
8. directplay provides IP voice communication;
9. DirectShow's application programming interface provides real-time audio/video synthesis and instant editing;
10. DirectShow supports reading and writing Windows Media Audio and Video (WMA and WMV;
11. Microsoft TV technology supports digital TV programs

DirectX is a graphical application interface (API), which is not hard to understand. Direct is the direct meaning, and X is a lot of things, together, it is a group of things with a bunch of hardware access. Microsoft defines it as "Hardware Device independence, improving very multimedia capabilities ". DirectX consists of the following parts:
1. DirectDraw (2D drawing)
2. direct3d (3D drawing) (Note: DirectDraw and direct3d have been merged)
3. directsound (sound-related)
4. directmusic (MIDI related)
5. directinput)
6. directplay (network related)
7. directsetup (setup related)
8. DirectShow (animation playback)
9. DirectX Media objects (media data stream object)
Microsoft launched wing and wavemix in 94 years. Wing has high-speed graphics processing capabilities, and wavemix can play eight-channel sound simultaneously. Later, wintoon was launched to speed up the image, but these products are not very practical. After Windows 95 was launched, Microsoft developed DirectX 1.0 and updated the DirectX standard jointly by hardware manufacturers and game manufacturers.
With the upgrade of the DirectX version, the more powerful the function is, the DirectX has become the industry standard of the game.

 
/***********************************
* Author: Cai junsheng
* Source: http://blog.csdn.net/caimouse/
************************************/
 
Appendix A 1. direct3d object
Appendix A 1. direct3d object (All rights reserved by Cai junsheng)
In the direct3d application, the first object to be created is the direct3d object, and the last object to be deleted is also the direct3d object. So what is direct3d object used? How was it created?
Because direct3d objects are implemented using COM, to use direct3d objects, you must first initialize the com library and then create the direct3d COM object. If you create a direct3d object in C ++ as follows:
Lpdirect3d9 g_pd3d = NULL;
If (null = (g_pd3d = direct3dcreate9 (d3d_sdk_version )))
Return e_fail;
The d3d_sdk_version parameter is used to determine which version of the SDK header file to compile. After creating a direct3d object, you can enumerate the devices and check the compatibility of the devices. You can create a display device object, and select a suitable display mode for a specific device, especially hardware acceleration programming.
 

Appendix A 2. direct3d Devices
· Appendix A 2. direct3d devices (All rights reserved by Cai junsheng)
In direct3d applications, direct3d devices are very important. Because direct3d devices provide matrix transformation, 3D image rendering, and 3D image raster display. These operations require the largest and largest operations, most of which are currently processed in the video card. The direct3d device is an abstraction of the hardware graphics card. Of course, it is higher than the hardware, because it can not only directly use hardware functions, but also use software to simulate functions that do not exist in the video card.
To use direct3d devices, you need to select appropriate devices based on different types. If a video card is not exclusive to an application, the device will be lost. Therefore, to handle this situation, the application tries to recover the device when it finds the device is lost. If the device cannot be recovered, it is treated as an error.
Before using the device, check whether the device supports the features required by the application. If not, exit the application. If you do not exit, the entire system may fail. It also checks what kind of vertex processing the hardware supports, whether it supports hardware, software, or software hardware mixing, or only one of them. The program is optimized based on different processes. We also need to transmit the vertex data to the appropriate device, otherwise it will not be able to process it.
There are two types of direct3d devices: Hal device and reference device ). Hal devices are mainly used in programs with high speed requirements and fast real-time processing because they are accelerated by hardware. The benchmark equipment is implemented by software, and all data is processed by CPU, which is mainly used for testing and research. correctness and accuracy are the main purpose, regardless of the speed. Therefore, the devices used in the game should be Hal devices.

Appendix A 3. direct3d Resources
· Appendix A 3. direct3d resources (All rights reserved by Cai junsheng)
There are many types of resources in direct3d, but there are mainly textures and buffers. Applications must create, load, copy, and use these resources. All resource interfaces inherit from the COM interface idirect3dresource9, and the texture and buffer day interfaces inherit from the above interfaces. Shows the interfaces for these resources:

There are many things to do for resources, such as understanding resource attributes, operating resources, synchronizing resource access, and using locks for synchronization. There are also resource relationships and how to manage and allocate resources for applications. It is very important to deal with the performance of programs that process the quality of resources, such as what kind of texture is put into the graphics card memory, and what kind of vertex data is put into the system memory.
 
Appendix A 4. direct3d coloring
· Appendix A 4. direct3d coloring (All rights reserved by Cai junsheng)
Now we will introduce the 3D polygon coloring in direct3d. What kind of coloring mode is there in direct3d? How to set the coloring mode? There are two coloring modes in direct3d: plane coloring and Gouraud coloring. The difference between the two colors is the difference of each vertex coloring in a polygon. Plane coloring uses the color of the first vertex of a polygon as the color of each vertex in the polygon. The Gouraud coloring is based on the color of each vertex, the normal and light are used to calculate the color of each vertex in the polygon. This is used for interpolation and coloring. For example, if the red component of vertex 1 is 0.8 and the red component of vertex 2 is 0.4, the red component in the middle of vertex 1 and vertex 2 is 0.6 calculated using Gouraud coloring. The advantage of plane coloring is that the coloring speed is fast. The advantage of Gouraud coloring is that the plane is smooth and looks more realistic.
Compare two different colored teapot types:

There are many differences between the two coloring modes. The plane coloring mode makes the edges connected to the two planes more sharp, and the Gouraud coloring looks like the edges become smooth. But in the light coloring, the plane coloring is not good, because the whole plane has a uniform color and looks unreal, but the Gouraud coloring is just the opposite, it looks more realistic, it is closer to the light-shining objects in reality. However, Gouraud coloring will also discard some physical details due to interpolation. Like Gouraud coloring, the middle spotlight is lost.

In direct3d, the default coloring mode is Gouraud. You can call the idirect3ddevice9: setrenderstate function to set the parameter d3drs_shademode to select different coloring modes. The following code sets the coloring mode.
// Set it to the flat color mode.
// Pdev is the pointer to idirect3ddevice9.
HR = pdev-> setrenderstate (d3drs_shademode, d3dshade_flat );
If (failed (HR ))
{
// Handle the error process here.
}

// Set the Gouraud coloring mode. This mode is used by default.
HR = pdev-> setrenderstate (d3drs_shademode, d3dshade_gouraud );
If (failed (HR ))
{
// Handle the error process here.
}
 
Appendix A 5. direct3d display environment
· Appendix A 5. direct3d display environment (All rights reserved by Cai junsheng)
In direct3d, how does one display the environment? Or how to display a frame of video? With this problem, let's take a look at the direct3d display process. Any animation is based on the playing principle of a movie. Because the human eye has an image visual delay, when you see a picture, it cannot immediately disappear from the brain. If at least 24 screenshots are played within one second, the pictures you see are continuous animations. Therefore, in direct3d, you only need to have the ability to display a picture.
In direct3d, The idirect3ddevice9: Present function is used to display an animation. There is a lot of work to do to display this frame of animation. First, we need to calculate the image position, color, texture, background, and so on, and then put the data in the back buffer, then we use the above function to replace the backup buffer with the front buffer to display an animation. Each Display Device in direct3d has a swap chain to save the buffer. Of course, you can add many buffer zones to it, the key depends on the program needs and the memory size or system memory size. Direct3d allows the video card to be animated on the display by continuously obtaining the backup buffer from the backup buffer switching list and displaying it in the front buffer. Store the front buffer that has been displayed in the backup buffer switching list, so that the application can continuously copy the display data to the backup buffer in the backup buffer switching list.
 

Appendix A 8. 3D Coordinate Transformation in direct3d
· Appendix A 8. 3D Coordinate Transformation in direct3d (All rights reserved by Cai junsheng)
There are many objects in the game. Taking CS for example, we often see many wooden boxes in it. how are these wooden boxes placed? Because these wooden boxes have the same size and texture, but they are placed in different positions. to represent these different positions, 3D coordinate space is required, the coordinates are used to represent the relative positions of two wooden boxes.

Coordinate transformation is the most basic knowledge in 3D games. For example, from the coordinate space of a model to the world coordinate space, because the coordinate space when many models are created is relative to the coordinate space when they are created. For example, to create a cube, it is used to represent a wooden box. Its coordinate space is the left-hand coordinate, but its central vertex is the origin of the left-hand coordinate system. You need to place the wooden box on different positions, translation is required. To face the user in different directions, it is necessary to rotate. How are the translation and rotation calculated? Actually, I have learned about ry and use matrices for transformation. Matrix is the best tool for 3D transformation. The matrix can save the transformed values, and also obtain various transformations by multiplying the matrix. The following describes how to generate conversion matrices such as World, view, and projection.

Matrix
World transformation-from model space to World Space
View conversion-from World Space to view space
Projection Transformation-from view space to projection Space

 
· Appendix A 9. Matrix (All rights reserved by Cai junsheng)

Direct3d uses the matrix tool for 3D transformation. In fact, matrix is the most convenient tool for Linear Algebra calculation, because coordinate transformation is composed of linear equations. It is the best method to use the matrix tool for calculation. In 3D, the most commonly used matrix is four rows and four columns, because it can calculate the translation, rotation and scaling of three-dimensional coordinates more conveniently. If a three-row matrix is used, it is difficult to represent translation.
The following content will be discussed later:
3D Transformation
Translation and Scaling
Rotate
Matrix combination

 

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.