RPG handwriting (3) 2D drawing using d3d

Source: Internet
Author: User

In the second article of this series, we explained how to initialize the d3d rendering device !!! I admit that I am an irresponsible blogger, and I have not written anything in detail at all, hum !! (Proud ?) However, I think it is better to look at the d3d SDK documentation than reading the author's articles. That is the best teaching material.

Okay, in this third article, the blogger is going to talk about how to use d3d for 2D plotting. As we all know, d3d has no longer existed since it entered d3d 8, even if it still exists, we should not continue to use DirectDraw because of its limited functions. Think about the long code that initialized DirectDraw in the past, and compare the lines of code that initialized d3d in the previous article, DirectDraw or something, although it is very easy to initialize d3d, it does not mean that it is very easy to use d3d, because the DirectDraw d3d is abandoned, in terms of operation, it is completely closer to 3D. That is to say, we can no longer directly set the target coordinates of the surface, nor directly set the key colors. Alpha mixing is much more complicated, the new 3D operating mechanism has shut down many comrades with poor mathematical foundations. At least, the bloggers have been blocked from matrix transformation (laughs ).

Now, go to the body. Both DirectDraw and GDI are drawn based on the form coordinate system. The upper left corner of the form customer area is [0, 0]. the bottom right corner of the customer area is [width-1, height-1]. This coordinate system is called the device coordinate system. Yes, you have not heard the error. It is the device coordinate system !! For Windows GDI, every HDC has its own coordinate system. Surprisingly, we can even perform coordinate transformation on the GDI object !! Believe it? If you don't believe it, open msdn immediately and check the setworldtransform function. How can this problem be solved.

Well, in the DirectDraw era, we can directly call the BLT method to specify the location to be drawn to the customer zone and the size to be drawn. In the d3d era, the blogger hopes to find the BLT function, Nima... no more... at that time, the blogger called it messy... later I realized that there was a Coordinate Transformation in d3d... in d3d, how can we easily specify the image coordinates for plotting like GDI? Let the bloggers uncover the secrets for you!

If you use d3d for 2D, the XY plane in the 3D space is usually projected onto the window's passenger area. Of course, for special effects, there are also projection from different angles to the screen, if we follow the habit of GDI coordinates, let the upper left corner of the image is (0, 0), let the lower right corner of the image is (width, height) to set the coordinates of the d3d vertex, the image will be like the following in the 3D space:

Therefore, we need to make some effort to make some changes to the image so that it can be normally displayed in the form client area. First, we flip the image up and down. After turning it over, images in 3D space are like this:

Then, let's move the image to the height of a customer zone. After moving the image, it looks like the following in a 3D space:

,

In the last step, we use orthogonal projection to project the (0, 0)-(client width, client height) area in the XY plane of the 3D space to the form client area, the complete d3d code looks like this:

 1     D3DXMATRIX matProj, matT, matS; 2  3     D3DXMatrixScaling( & matS, 1.0f, -1.0f, 1.0f ); 4     D3DXMatrixTranslation( & matT, 0.0f, ( float ) client_height, 0.0f ); 6     device->SetTransform( D3DTS_WORLD, & ( matS * matT ) ); 7  8     D3DXMatrixOrthoOffCenterLH( & matProj,  9         0.0f, ( float ) client_width,10         0.0f, ( float ) client_height, 11         0.0f, 1.0f );12     device->SetTransform( D3DTS_PROJECTION, & matProj );

Matproj is an orthogonal projection matrix, mats is the zoom matrix for up and down flip, and Matt is the translation matrix for moving up a customer's height.
In this way, we can ignore all kinds of 3D transformations and directly set coordinates for images using the habit of using the GDI coordinate system ~~

By the way, the famous 2D game engine HGE also uses this method, but its scaling and moving are combined into the projection matrix, the blogger applies the zoom translation to the world transformation, and then separately applies the orthogonal projection. The following code is from HGE:

1 void HGE_Impl::_SetProjectionMatrix(int width, int height)2 {3     D3DXMATRIX tmp;4     D3DXMatrixScaling(&matProj, 1.0f, -1.0f, 1.0f);5     D3DXMatrixTranslation(&tmp, -0.5f, height+0.5f, 0.0f);6     D3DXMatrixMultiply(&matProj, &matProj, &tmp);7     D3DXMatrixOrthoOffCenterLH(&tmp, 0, (float)width, 0, (float)height, 0.0f, 1.0f);8     D3DXMatrixMultiply(&matProj, &matProj, &tmp);9 }

 

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.