World matrix and network in direct

Source: Internet
Author: User

World matrix and network in direct

 /* <Br/> * demo name: shapes <br/> * Author: cl_gamer@qq.com <br/> */</P> <p> # include <windows. h> <br/> # include <assert. h> <br/> # include <d3d9. h> <br/> # include <d3dx9. h> </P> <p> # define win_class "tenderfoot" <br/> # define win_title "tenderfoot shapes" <br/> # define win_width 800 <br/> # define win_height 600 </P> <p> # pragma comment (Lib, "d3d9. lib ") <br/> # pragma comment (Lib," d3dx9. lib ") </P> <p> // Function <Br/> bool initd3d (hwnd, bool fullscreen); <br/> bool initobjects (); <br/> void renderscene (); <br/> void Shutdown (); </P> <p> // globals <br/> // d3d <br/> lpdirect3d9 g_d3d = NULL; <br/> lpdirect3ddevice9 g_d3dd = NULL; </P> <p> // matrices <br/> d3dxmatrix g_projection; <br/> d3dxmatrix g_viewmatrix; <br/> d3dxmatrix g_worldmatrix; </P> <p> // mesh objects <br/> lpd3dxmesh g_teapot = NULL; <br/> lpd3dxmesh g _ Cube = NULL; <br/> lpd3dxmesh g_sphere = NULL; <br/> lpd3dxmesh g_torus = NULL; </P> <p> lresult winapi msgproc (hwnd, uint MSG, wparam, lparam) <br/>{< br/> switch (MSG) <br/>{< br/> case wm_destroy: <br/> postquitmessage (0 ); <br/> break; </P> <p> case wm_keyup: <br/> If (vk_escape = wparam) <br/>{< br/> postquitmessage (0); <br/>}< br/> break; </P> <p> default: <br/> break; <br/>}</P> <p> return Defwindowproc (hwnd, MSG, wparam, lparam); <br/>}</P> <p> int winapi winmain (hinstance, hinstance hprevinstance, <br/> lpstr lpcmdline, int ncmdshow) <br/>{< br/> // desige window class <br/> wndclassex wcex; <br/> wcex. cbsize = sizeof (wcex); <br/> wcex. style = cs_classdc; <br/> wcex. lpfnwndproc = (wndproc) msgproc; <br/> wcex. cbclsextra = 0l; <br/> wcex. cbwndextra = 0l; <br/> wcex. hinstance = H Instance; <br/> wcex. hicon = NULL; <br/> wcex. hcursor = NULL; <br/> wcex. hbrbackground = (hbrush) (color_window + 1); <br/> wcex. lpszmenuname = NULL; <br/> wcex. lpszclassname = win_class; <br/> wcex. hiconsm = NULL; </P> <p> // register window class <br/> registerclassex (& wcex ); </P> <p> // create the application's window <br/> hwnd = create1_wex (0, win_class, win_title, ws_overlappedwindow, <br/> 0, 0, W In_width, win_height, (hwnd) null, (hmenu) null, hinstance, null); </P> <p> // show and uptata window <br/> showwindow (hwnd, sw_showdefault); <br/> updatewindow (hwnd); </P> <p> // message loop <br/> MSG; <br/> zeromemory (& MSG, sizeof (MSG); <br/> If (! Initd3d (hwnd, false) <br/>{< br/> return 0; <br/>}< br/> while (msg. message! = Wm_quit) <br/>{< br/> If (peekmessage (& MSG, null, 0u, 0u, pm_remove )) <br/>{< br/> translatemessage (& MSG); <br/> dispatchmessage (& MSG ); <br/>}< br/> else <br/>{< br/> renderscene (); <br/>}</P> <p> // release any and all resources <br/> Shutdown (); </P> <p> unregisterclass (win_class, wcex. hinstance); </P> <p> return 0; <br/>}</P> <p> bool initd3d (hwnd, bool fullscreen) <br/>{< br/> If (null = (G_d3d = direct3dcreate9 (d3d_sdk_version) <br/>{< br/> MessageBox (null, "fail to create d3d! "," Error ", mb_ OK); <br/> return false; <br/>}</P> <p> // get the desktop display mode <br/> d3ddisplaymode d3ddm; <br/> If (d3d_ OK! = G_d3d-> getadapterdisplaymode (d3dadapter_default, <br/> & d3ddm) <br/>{< br/> MessageBox (null, "fail to getadapterdisplaymode! "," Error ", mb_ OK); <br/> return false; <br/>}</P> <p> // set up the sturcte used to create the d3dd <br/> d3dpresent_parameters d3dpp; <br/> zeromemory (& d3dpp, sizeof (d3dpp); <br/> If (fullscreen) <br/>{< br/> d3dpp. backbufferwidth = win_width; <br/> d3dpp. backbufferheight = win_height; <br/>}</P> <p> d3dpp. export wed =! Fullscreen; <br/> d3dpp. swapeffect = d3dswapeffect_discard; <br/> d3dpp. backbufferformat = d3ddm. format; </P> <p> // create the d3ddevice <br/> If (failed (g_d3d-> createdevice (d3dadapter_default, d3ddevtype_hal, <br/> hwnd, d3dcreate_software_vertexprocessing, & d3dpp, & g_d3dd) <br/>{< br/> return false; <br/>}</P> <p> // set the projection matrix <br/> d3dxmatrixperspectivefovlh (& g_projection, 45.0f, <Br/> win_width/win_height, 0.1f, 1000.0f); <br/> g_d3dd-> settransform (d3dts_projection, & g_projection ); </P> <p> // initialize any objects we will be displaying <br/> If (initobjects () <br/>{< br/> return true; <br/>}</P> <p> return true; <br/>}</P> <p> bool initobjects () <br/>{< br/> // set default rendering States <br/> g_d3dd-> setrenderstate (d3drs_lighting, false ); </P> <p> // create the objects <br/> If (FAI LED (d3dxcreateteapot (g_d3dd, & g_teapot, null) <br/>{< br/> return 0; <br/>}< br/> If (failed (d3dxcreatebox (g_d3dd, 2, 2, 2, & g_cube, null ))) <br/>{< br/> return 0; <br/>}< br/> If (failed (d3dxcreatesphere (g_d3dd, 1.5, 25, 25, & g_sphere, null) <br/>{< br/> return 0; <br/>}< br/> If (failed (d3dxcreatetorus (g_d3dd, 0.5f, 1.2f, 25, 25, & g_torus, null) <br/>{< br/> return 0; <br/>}</P> <p> // define camera I Nformation. <br/> d3dxvector3 camerapos (0.0f, 0.0f,-8.0f); <br/> mongolookatpos (0.0f, 0.0f, 0.0f); <br/> d3dxvector3 updir (0.0f, 1.0f, 0.0f); </P> <p> // bulid view matrix <br/> d3dxmatrixlookatlh (& g_viewmatrix, & camerapos, & lookatpos, & updir ); </P> <p> return true; <br/>}</P> <p> void renderscene () <br/>{< br/> assert (g_d3dd ); </P> <p> // clear the backbuffer <br/> g_d3dd-> clear (0, null, d3dclear_targe T, <br/> d3dcolor_xrgb (0, 0, 0), 1.0f, 0); </P> <p> // begin the scene. start rendering <br/> g_d3dd-> beginscene (); </P> <p> // apply the view <br/> g_d3dd-> settransform (d3dts_view, & g_viewmatrix ); </P> <p> // draw teapot <br/> d3dxmatrixtranslation (& g_worldmatrix, 2.0f,-2.0, 0.0f); <br/> g_d3dd-> settransform (d3dts_world, & g_worldmatrix); <br/> g_teapot-> drawsubset (0); </P> <p> // draw cube <br/> d3dxmatrixtranslat Ion (& g_worldmatrix,-2.0f,-2.0f, 0.0f); <br/> g_d3dd-> settransform (d3dts_world, & g_worldmatrix ); <br/> g_cube-> drawsubset (0); </P> <p> // draw sphere <br/> d3dxmatrixtranslation (& g_worldmatrix, 2.0f, 2.0, 0.0f ); <br/> g_d3dd-> settransform (d3dts_world, & g_worldmatrix); <br/> g_sphere-> drawsubset (0 ); </P> <p> // draw torus <br/> d3dxmatrixtranslation (& g_worldmatrix,-2.0f, 2.0, 0.0f); <br/> g_d3dd-> settransfo Rm (d3dts_world, & g_worldmatrix); <br/> g_torus-> drawsubset (0); </P> <p> // end the scene. stop rendering <br/> g_d3dd-> endscene (); </P> <p> // display the scene <br/> g_d3dd-> present (null, null); <br/>}</P> <p> void Shutdown () <br/>{< br/> // release all resource. <br/> If (null! = G_d3dd) <br/>{< br/> g_d3dd-> release (); <br/>}< br/> If (null! = G_d3d) <br/>{< br/> g_d3d-> release (); <br/>}< br/> If (null! = G_teapot) <br/>{< br/> g_teapot-> release (); <br/>}< br/> If (null! = G_cube) <br/>{< br/> g_cube-> release (); <br/> g_teapot = NULL; <br/>}< br/> If (null! = G_sphere) <br/>{< br/> g_sphere-> release (); <br/> g_cube = NULL; <br/>}< br/> If (null! = G_torus) <br/>{< br/> g_torus-> release (); <br/> g_torus = NULL; <br/>}</P> <p>}

 

Related Article

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.