Directx9.0 tutorial 1-first program

Source: Internet
Author: User
Tags win32 window

After selecting n books in the library, I still feel that the book that directly talks about code is more suitable for me. I add some comments to a complete column, it is much easier to understand than those tutorials that are described in a large text section.

Therefore, my notes are recorded in this way.

Written at the beginning: dx (I am short for DirectX) dx versions mainly include 9 and 11 series. The differences between APIs and functions are still significant. However, DX9 has complete book notes. You can start watching DX9. In addition, learning has always been to learn his thoughts. Although it may be necessary to change the configuration environment or the API, the idea remains unchanged. It is always good to learn.

The following is the first column to learn this book.

To put it bluntly, it is a small framework of VC ++. Then there is a bit of DX Code. However, this small framework can be further enriched by itself. The Win32 API in the shoes that may conflict with MFC is obscure, but there is no need to pay attention to any details. This small framework is just to display a window to display Dx and message processing. So it will be used.

From http://blog.csdn.net/cq361106306/article/details/39549091

First, we will introduce the main working process of DX under Win32:


1. winmain Function

This is the entry function of the entire project. The Code also starts from here.

// First define the window class wndclassex WC = {...} // you can't remember the details. // register the window class registerclassex (& WC); // then create the window class hwnd = createwindow (....) // The parameters are mainly window size and title, you can modify it by yourself. // --------- the above is the Win32 window. // --------- The following is the initialization of d3d. // The Source Code contains many. If you only check the process, I will omit it. // first pass the window to d3d. Initd3d (hwnd); // The content in this function is described later. Remember the general process first. // then load the model initgeometry (); // This is also described later. // Initialization is complete. Of course, showwindow (hwnd,...); updatewindow (hwnd); // message loop-this is required by VC ++. It is a loop that can change the window and variables while (msg. messge! = Wm_quit) {// execute the system message .. // execute d3d rendering render (); // you only need to add something in the render for the columns in the future. So other functions are part of the framework. .}


The above is the overall process. Is it quite simple to find out. It is a bunch of initialization, and then the loop update starts.

The following describes the initialization functions.

/// Functions // function: initd3d () // function: Initialize d3d and 3D devices // parameter: two global variables g_pd3d g_pd3ddevice are initialized // inithresult initd3d (hwnd) {// create a 3D object. if (null = (g_pd3d = direct3dcreate9 (d3d_sdk_version) return e_fail; // tell the system to set the parameter to be created, that is, fill in the d3dpresent_parameters parameter D3 Dpresent_parameters d3dpp; zeromemory (& d3dpp, sizeof (d3dpp); d3dpp. required wed = true; d3dpp. swapeffect = d3dswapeffect_discard; d3dpp. backbufferformat = d3dfmt_unknown; d3dpp. enableautodepthstencel = true; d3dpp. autodepthstencilformat = d3dfmt_d16; d3dpp. presentationinterval = d3dpresent_interval_immediate; // create a device if (failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd, d3dcr Eate_software_vertexprocessing, & d3dpp, & g_pd3ddevice) return e_fail; return s_ OK;} // whether the above Code looks so cool, right? It's a series of parameters. Put it first.

The initgeometry function is mainly used to set the suffix of the effect file. FX.

// Outputs // function: initgeometry () // function: sets the directory, loads the model, texture, effect file, and transmits the effect parameters to HLSL // parameter: initialize the other three global variables g_pmesh g_peffect g_pmeshtextures // specify hresult initgeometry () {hresult hr; // set the directory tchar szapppath [max_path]; tchar szrcpath [max_path]; getcurrentdirectory (max_path, szapppath); // lstrcpy (szrcpath, szapppath); lstrcat (szrcpath, l "\ resfile_1_model"); setcurrentdirectory (szrcpath ); // mount the effect file lpd3dxbuffer pcode = NULL; If (failed (hR = d3dxcreateeffectfromfile (g_pd3ddevice, l "filefx. FX ", null, null, 0, null, & g_peffect, & pcode) {lpvoid pbuferr = pcode-> getbufferpointer (); char * errinfo = (char *) pbuferr; pcode-> release (); Return hr;} If (failed (hR = g_peffect-> settechnique ("defaulttech") return hr; // After the above effect file is successfully loaded, we need to set the model of the vertex structure to initvb (); If (failed (hR = initvb () return hr; // return s_ OK correctly ;}

The above code is basically fixed. Because each column must be used. What really needs to be changed at any time is the model and rendering logic.

Review what has been done above: Window definition, window display, d3d initialization,. FX effect file initialization, model initialization, and message loop.

The following is what we really want to do. Show a triangle

Create a vertex model

// Function: initvb // function: Initialize g_pvb // parameter: NONE // initialize hresult initvb () {hresult hr; customvertex vertices [] ={{ 5.0f, 0.0f, 0.0f, 1.0f, 0xffff0000}, // X, Y, Z, rhw, color red {0.0f, 5.0f, 0.0f, 1.0f, 0xff00ff00}, // green {-5.0f, 0.0f, 0.0f, 1.0f, 0xff0000ff}, // blue}; If (failed (hR = g_pd3ddevice-> createvertexbuffer (3 * sizeof (customvertex), 0, d3dfvf_customvertex, d3dpool_default, & g_pvb, null) return hr; void * pvertices; If (failed (hR = g_pvb-> lock (0, sizeof (vertices), (void **) & pvertices, 0) return hr; memcpy (pvertices, vertices, sizeof (vertices); g_pvb-> unlock (); Return s_ OK ;}

All preparations are complete. Then it's rendering.

Mainly the render () function

Void render () {hresult hr; // clear the depth buffer hR = g_pd3ddevice-> clear (0, null, d3dclear_target | d3dclear_zbuffer, d3dxcolor (0.0f, 0.25f, 0.25f, 0.55f), 1.0f, 0); HR = g_pd3ddevice-> setstreamsource (0, g_pvb, 0, sizeof (customvertex); HR = g_pd3ddevice-> setfvf (d3dfvf_customvertex ); if (succeeded (g_pd3ddevice-> beginscene () {// you can set the transformation to set the actual matrix to rotate setupmatrices () around the Y axis (); // rendering uint IPASS, cpasses; HR = g_peffect-> begin (& cpasses, 0); For (IPASS = 0; IPASS <cpasses; IPASS ++) {hR = g_peffect-> beginpass (IPASS); HR = g_pd3ddevice-> drawprimitive (d3dpt_trianglelist, 0, 1); HR = g_peffect-> endpass ();} hR = g_peffect-> end (); HR = g_pd3ddevice-> endscene ();} // switch the rendering result to the foreground buffer hR = g_pd3ddevice-> present (null, null, null, null);} // variable // function: setupmatrices () // function: Transfers four variables to the effect file, which is calculated by the effect file. // parameter: None, this module involves global variables: effect file interface // effecvoid setupmatrices () {hresult hr; d3dxmatrixa16 matworld; float ftime = timegettime ()/1000.0f; d3dxmatrixrotationy (& matworld, ftime ); // d3dxmatrixidentity (& matworld); HR = g_peffect-> setfloat ("g_ftime", ftime); // world matrix hR = g_peffect-> setmatrix ("g_matworld ", & matworld); // observation matrix d3dxvector3 veyept (15.0f, 3.0f, 0.0f); d3dxvector3 vlookatpt (0.0f, 3.0f, 0.0f); export vupvec (0.0f, 1.0f, 0.0f ); d3dxmatrixa16 matview; Evaluate (& matview, & veyept, & vlookatpt, & vupvec); HR = g_peffect-> setmatrix ("g_matview", & matview); // projection matrix d3dxmatrixa16 matproj; d3dxmatrixperspectivefovlh (& matproj, d3dx_pi/4, 64.0/41.0f, 1.0, 100366f); HR = g_peffect-> setmatrix ("g_matproject", & matproj );}

The effect is that a triangle keeps turning.



Source code link: http://pan.baidu.com/s/1sjI131j password: 8l7s

Directx9.0 tutorial 1-first program

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.