Introduction to 3D game programming (10) d3d program debug skills and d3d Basics

Source: Internet
Author: User
Introduction to 3D game programming (10) d3d program debug skills and d3d Basics

(21:52:19)

ReprintedBytes
   
Call: now we have started to introduce d3d related things. I have been reviewing things for a whole day. I need to add some emotional things. Well, I don't want to spend too much time. It's really annoying. I'll think about something to do later.
 
    D3d program Bug testing skills
 
    This should have been summarized at the end of the program, but I thought it would be better for a friend who involved d3d programming for the first time, which is of great benefit to self-study.
    New programmers will understand that the biggest headache for a project development is a series of errors. Even after repeated debugging, there may be manyBugs are still hard to find. The following are some summarized experiences that will help you in future tests.
    1: at the beginning of the CPP file, if we define the (define) Strict symbol before include <window. h>, it will lead to a more rigorous type check to help us locate errors..
    2: In dxsdk, The dxdiag utility can report errors, but this is not worth recommending to new users. When using it, we must be clear about what is on our system.
    3: You can set the debugoutput level in the DX control panel. We recommend that you use level 4 and enable the four options maximumvalidation and enable in debugging.Shader debugging, break on memory leaks, and break on d3derror. Remember to set it to use debugversion when debugging the d3d program, remember to adjust it back, otherwise it will not work properly-(it seems that I made this mistake multiple times at the beginning)
    4: The d3dx library is a static library. To facilitate debugging, we can import a dynamic library, d3dx9d. Lib, which is a library specially used for debugging.
    5: downloading a memory manager from the Internet can reduce memory allocation and release issues. Of course, VC ++ provides a memory manager called CRT. According to my hobbies
    6. Download the VC ++. Net plug-in to debug vertex shader and pixel shader.
    7: Use outputdebugstring (); debugbreak ();
    8: try to avoid using full screen mode during debugging. When creating a window, you should pass the iswindowed parameter to control whether the full screen is enabled or not.
    9. During release, saferealse and safefree are often used to replace realse and free. In fact, you only need to add a definition that is null.
    10: Generally, the DX method returns an hresult. We can use the succeeded () and failed () functions to determine the return value to prevent errors.
    11: there are also tips for setting breakpoints and monitoring in the VC environment. I believe you are familiar with them.      D3d Basics      It is also the basis of Windows programming, vector matrix, dx, and d3d. Will it be depressing? Why haven't you started coding?Learning is accumulated slowly. Programming is like writing an article. This knowledge is thought, and Code such as function calling is text. For the articles we want to write, there is no way to break down without thinking, and the elevation of height is more of a grasp of the overall process framework, we are a low-level programmer who is as busy as a typist. I will not talk about it much. Now I will introduce some terms in d3d.
    1: pageflipping (page flip ). We need to understand that the images displayed on the screen are not directly drawn on the screen, but are first drawn on an invisible page.This page is called "backend buffer". After the page is drawn, the system quickly places it on the visible "front-end buffer" on the screen, in addition, place the page we just saw in the "backend buffer" and draw it again. Therefore, our screen display is divided into the frequency of 60 Gb/s, 75 Gb/s, and 85 Gb/s, that is, the frequency of background buffering placed on the front-end buffering page per second. When this frequency is too low and our eyes can be captured, we will find the screen flip. Think about whether the computer screen recorded on TV is flashing, this is because the recording frequency of the camera is faster than that of the computer screen.
  2: pixel and resolution pixels and resolutions. The image displayed on the screen is composed of small dots in the blocks. This smallest unit is called pixel, which is also in the color buffer.The smallest graphic unit. Resolution is the maximum number of pixels that our display device can display. For example, most of our computer screens now support 1024x768 pixels, which means there are more than 0.78 million x = pixels. That is to say, the screen at this time is composed of more than 0.78 million small points.
    3: Color buffer framebuffer. This is a piece of memory in the system memory or memory. It stores a two-dimensional display area that we need to display.
    4: device. It's our video card.
    5: local space. It refers to the coordinate system we use when defining a list of triangles that make up an object. When creating an object model, we do not need to care about the object location and other world spaces.The relationship between objects can greatly simplify the processing of our models.
    6: World Space. Each model is created in its own local space. After it is created, we need to combine these model objects to form an entire scenario,At this time, we need a unified world coordinate system. In this case, we began to consider the positional relationship with other objects. Who would block them?
    7: view space. We need to set up a virtual camera in the d3d scenario to confirm what we can see. The space determined by the virtual camera is the view space.Here, we need to consider what we can see and what we can't see?
    8: View. This is easy to understand. When our game is running in full screen mode, our viewport is the full screen. When we set it to window mode, the rectangular area of the game displayed in the window is the view.Port.
    9: vertices. It is a vertex of a model object. When we look at all the model objects as a triangle, the three vertices of these triangles are described here..
    10: T & L assembly line. I used to introduce OpenGL. When rendering a graph, we can usually divide it into two major steps. The first part is switching and lighting (T & L) at this stage weConvert the vertex matrix, and then calculate the illumination effect of each vertex Based on the lighting material. The second part is raster processing. Here we start to control the texture and depth buffering to determine which user-visible vertices and display the rendering results on the screen.
 
      In this pipeline, we send unprocessed vertices from one end. After processing, the required 3D images will be generated from the other end. Here is a detailed introduction.Right.
    1: first, convert the vertex coordinates. We can use the settransfrom method to implement the corresponding transformation. First, we need to create a pointer for the d3d device.
    You can write the following code:
    Idirect3ddeviceg_pd3ddevice = NULL; /* Create a d3d device pointer */
    G_pd3ddevice-> settransform (d3dts_world, & matworld ); /* D3dts_world indicates the type of matrix conversion. matworld is a transformation moment.Array */
    /* In the above Code, we implemented the function of placing an independent object in the world coordinate system. In the world transformation, we mainly completed the rotation, scaling, and translation of the model */
   
    After converting an object to a world space, we need to determine the view space. We need to define some attributes of the camera, including the camera location and orientation,We use vectors to represent the square direction of the camera.
      D3dxmatrix * d3dxmatrixlookatlh
    (
                  D3dxmatrix * pout,   // Output the matrix used for view Transformation
                  Const d3dxvector3 * peye, // Camera position
                  Const d3dxvector3 * Pat, // Camera orientation
                  Const d3dxvector3 * pup // The square direction of the camera
    );
    Here we need to pay attention to two points: 1. The function here starts with d3dx, which indicates that this is a relatively high-level interface, it is a method bound with a certain level of interface,To a certain extent, it facilitates our design, but it also makes it hard to bind some settings, which has great disadvantages for our adjustment during programming, we try to use functions starting with d3d as much as possible. 2: As you can see, the first parameter of this function is the output matrix. The matrix we will use next is the matrix, here we can declare an empty matrix d3dxmatrix * pout = NULL. After the function is run, we monitor it and find that its value has changed, we can use this transformed structure variable for the following processing. This is a common design pattern in dx.
    Then let's set the view transformation.
    G_pd3ddevice-> settransfrom (d3dts_view, & matview ); // The d3dts_view is still the conversion type, and the matview is a view transformation matrix.    Then we extract the vertices after the view transformation, calculate the brightness of each vertex based on its position, normal position, color, and material, and save its color information.Store in vertex information.
    After illumination calculation, we will perform projection transformation, which will help us to generate a projection image. In this figure, we will narrow down the imaging of objects far away from the camera, so that the image has a vertical depth., Still g_pd3ddevice-> settransfrom (d3dts_projection, & matproj ); // D3dts_projection conversion type. matproj is a projection transformation moment.Array
    Then we cut some invisible points. When we set the vertex of an object's triangle row, we need to pay attention to this point. By default, d3d will sort the vertex clockwise.The triangle type of a column is used as the forward polygon, and the backward polygon is used as the clockwise polygon. We can follow to confirm whether the element is visible.
    The code is g_pd3ddevice-> setrenderstate (d3drs_cullmode, value );   // The parameter is the cropping method.
    At this point, our general process is almost the same. Finally, we define the size of the screen display area (the viewport) and use it to convert the vertex from the projection coordinate system to the final screen coordinate.
    In d3d, we define the window view structure
    Typedef struct_d3dviewport9
  {
                  Dwordx;   // X coordinate in the upper left corner of the view area
                  Dwordy;   // Y coordinate in the upper left corner of the view area
                  Dwordwidth;   // Specifies the width of the area in the view.
                  Dwordheight;   // The height of the view area
                  Floatminz;   // The minimum depth of the scene in the video, usually between 0 and.
                  Floatmaxz;   // The maximum depth of the scene in the video, usually 1 between 0 and
              } D3dviewport9;
  We usually use the setviewport () function to set the d3d view.
  Hresult setviewport
  (                
  Const d3dviewport9 * pviewport   // View
  );Call, let's talk about other things tomorrow. If you're tired, >_< writing these things is quite troublesome .. There are still two days to continue working. 00

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.