Implement the 3D image engine from scratch: (1) Environment configuration and project framework

Source: Internet
Author: User
Tags pixel coloring

0. to learn about 3D programming, you must be proficient in 3D-Related Linear Algebra, 3D ry, complex analysis, and other related knowledge. Because of this, I started writing this blog series and did not implement it myself, it is not your own things. From today on, we will record all the math knowledge learned, from mathematical derivation to code implementation. Finally, an independent 3D image engine is created, which completes the basic learning of 3D image knowledge.

 

1. Article layout. All articles except this article will be composed of three parts.

1) mathematical theory derivation.

2) If you do not refer to any examples, you can only implement the code based on mathematical principles.

3) download the project code.

 

2. Language and development environment

1) language: C/C ++.

To put it bluntly, 3D games have two words: speed. Therefore, object-oriented things can be completely ignored in the face of speed, and there is no excessive relationship between objects for the mathematics and graphics libraries, mostly the relationship between data structures and functions, therefore, I should not use many c ++ features, and sometimes use ASM, SIMD, and FPU for speed.

 

2) Development Tool: vs2010. It's powerful.

 

3) graphic interface: d3d. (DirectX SDK June 2010,: http://www.microsoft.com/downloads/en/details.aspx? Familyid = 3021d52b-514e-41d3-ad02-438a3ba730ba)

"Give me a video address and I will be able to create a game ". This is the goal of achieving the 3D image engine from scratch, and also the only way to learn 3D programming. Originally, I wanted to use DirectDraw instead of d3d, and even DirectDraw only used it to get the video address. No other APIs are required. However, because the DirectX SDK has no ddraw. H, it is difficult to use ddraw, so I will use d3d, but only use it to obtain the surface address. Therefore, I will completely discard hardware acceleration, But I can better understand what hardware acceleration is doing. PS: the initialization capability of d3d is more powerful. In ddraw, we also need to re-adjust the window size, and the pixel offset caused by the window border should be taken into account each time the pixels are written, d3d Initialization is complete.

 

3. Conventions and configurations

For this graphics library, we have the following conventions:

1) supports Unicode

2) window program for easy debugging

3) 32 BPP screen color

4) The loaded bitmap is a 24-Bit Bitmap.

5) x86-based systems

 

There are only two places to configure:

1) Add the DirectX SDK directory to include and Lib of VC ++ directories.

2) Add d3d9. lib and d3dx9. lib to linker input.

 

4. Project framework

This is the file we will use in the project:

3 dconsole: the console of our experiment

3 dlib: 3D functions, including d3d Surface Control

Math: math

Diagnosis: Diagnostic database used to generate some logs

Helper: auxiliary function library, such as reading and writing files

 

Math, diagnosis, and helper are useless today. They are empty.

 

3 dconsole, responsible for creating windows, system message loops, and other general Win32 programs, and insert game_init (), game_main (), game_shutdown () method call. As you can see from the Code, some timing work has been done here to lock the output to a certain number of frames, and the CPU will not always be fully occupied when idle. The timer code is as follows:

// Function Definition <br/> DWORD getclock () <br/> {<br/> return gettickcount (); <br/>}</P> <p> void startclock () <br/>{< br/> g_clock = getclock (); <br/>}</P> <p> void waitclock () <br/>{< br/> while (getclock ()-g_clock) <wait_time) <br/>{< br/> sleep (5); <br/>}< br/>}

 

3 dlib: This article implements d3d initialization and draws a pixel Code. The Code includes comments:

# Include "cppyin.3dlib. H "</P> <p> bool _ cppyin_3dlib: init3dlib (hinstance, hwnd, int width, int height) <br/>{< br/> idirect3d9 * d3d9 = direct3dcreate9 (d3d_sdk_version); </P> <p> d3dpresent_parameters d3dpp; <br/> d3dpp. backbufferwidth = width; <br/> d3dpp. backbufferheight = height; <br/> d3dpp. backbufferformat = d3dfmt_a8r8g8b8; <br/> d3dpp. backbuffercount = 1; <br/> d3dpp. multisampletype = d3dmultisample_none; <br/> d3dpp. multisamplequality = 0; <br/> d3dpp. swapeffect = d3dswapeffect_discard; <br/> d3dpp. hdevicewindow = hwnd; <br/> d3dpp. required wed = true; <br/> d3dpp. enableautodepthstencel = true; <br/> d3dpp. autodepthstencilformat = d3dfmt_d24s8; <br/> d3dpp. flags = 0; <br/> d3dpp. fullscreen_refreshrateinhz = d3dpresent_rate_default; <br/> d3dpp. presentationinterval = d3dpresent_interval_immediate; </P> <p> d3d9-> createdevice (d3dadapter_default, d3ddevtype_ref, hwnd, success, & d3dpp, & pdevice ); <br/> d3d9-> release (); </P> <p> pdevice-> createoffscreenplainsurface (width, height, d3dfmt_a8r8g8b8, d3dpool_default, & psurface, 0 ); </P> <p> return true; <br/>}</P> <p> int _ cppyin_3dlib: drawpixel (int x, int y, DWORD color) <br/>{< br/> // create and initialize the locked area <br/> d3dlocked_rect LR; <br/> memset (& LR, 0, sizeof (LR )); </P> <p> // lock <br/> psurface-> lockrect (& LR, null, d3dlock_discard ); </P> <p> // pixel coloring <br/> DWORD * pbits = (DWORD *) LR. pbits; <br/> pbits [x + y * (LR. pitch> 2)] = color; </P> <p> // unlock <br/> psurface-> unlockrect (); </P> <p> return 1; <br/>}</P> <p> void _ cppyin_3dlib: flipsurface () <br/> {<br/> // obtain the background cache <br/> idirect3dsurface9 * backbuffer = 0; <br/> pdevice-> getbackbuffer (0, 0, d3dbackbuffer_type_mono, & backbuffer); </P> <p> // fill the background cache with a custom surface <br/> pdevice-> stretchrect (psurface, 0, backbuffer, 0, d3dtexf_none ); </P> <p> // The cache obtained by getbackbuffer needs to be released; otherwise, memory leakage <br/> backbuffer-> release (); </P> <p> // display the background cache in the switch chain <br/> pdevice-> present (0, 0, 0, 0 ); <br/>}</P> <p> void _ cppyin_3dlib: release3dlib () <br/>{< br/> psurface-> release (); <br/> pdevice-> release (); <br/>}

 

Finally, download the framework project source code:> click to enter the download page <

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.