天天看点

在Mobile上使用Direct3D

好久以前写的,想做个比较漂亮的主界面,但后来因为用D3D太卡,所以只学了一点,后来就没再学

就是显示一个立方体,按方向键可以旋转

main.bmp就是那个唯一的资源(拿SDK里面的D3D的例子改的)

直接贴代码了:

  1. #pragma comment(linker, "/nodefaultlib:oldnames.lib")
  2. #include <windows.h>
  3. #include <d3dm.h>
  4. #include <d3dmx.h>
  5. #include <aygshell.h>
  6. #include <d3dmtypes.h>
  7. #define IDB_BANANA                     101
  8. //-----------------------------------------------------------------------------
  9. // Global variables
  10. //-----------------------------------------------------------------------------
  11. LPDIRECT3DMOBILE        g_pD3DM       = NULL;  // Used to create the D3DMDevice
  12. LPDIRECT3DMOBILEDEVICE  g_pd3dmDevice = NULL;  // Our rendering device
  13. HMODULE                 g_hRefDLL     = NULL;  // DLL handle for d3dmref.dll
  14. bool                    g_bUseRef     = false; // Flag denoting use of d3dmref
  15. LPDIRECT3DMOBILEVERTEXBUFFER g_pVB    = NULL; // Buffer to hold vertices
  16. LPDIRECT3DMOBILETEXTURE     g_pTexture   = NULL;
  17. // A structure for our custom vertex type
  18. struct CUSTOMVERTEX
  19. {
  20.     FLOAT x, y, z; // The transformed position for the vertex
  21.     DWORD color;        // The vertex color
  22.     FLOAT tu,tv;        //texture point
  23. };
  24. // Our custom FVF, which describes our custom vertex structure
  25. #define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZ_FLOAT | D3DMFVF_DIFFUSE | D3DMFVF_TEX1 | D3DMFVF_TEXCOORDSIZE2(0) | D3DMFVF_TEXCOORDFLOAT(0))
  26. //-----------------------------------------------------------------------------
  27. // Name: IsScreenRotated()
  28. // Desc: Currently the D3DM runtime does not support a rotated screen. If the 
  29. //       screen is rotated, we should inform the user this is not supported.
  30. //       If this routing returns TRUE the caller should cleanup and exit the application
  31. //-----------------------------------------------------------------------------
  32. BOOL IsScreenRotated()
  33. {
  34.     DEVMODE devMode  = {0};
  35.     devMode.dmSize   = sizeof(DEVMODE);
  36.     devMode.dmFields = DM_DISPLAYORIENTATION;
  37.     ChangeDisplaySettingsEx(NULL, &devMode, NULL, CDS_TEST, NULL);
  38.     if (devMode.dmDisplayOrientation != DMDO_0)
  39.     {
  40.         MessageBox(
  41.             NULL, 
  42.             TEXT("This D3DM sample will not work on a rotated screen./nThe application will now exit."),
  43.             TEXT("Notice"),
  44.             MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND
  45.             );
  46.         return TRUE;
  47.     }
  48.     return FALSE;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Name: InitD3DM()
  52. // Desc: Initializes Direct3D Mobile
  53. //-----------------------------------------------------------------------------
  54. HRESULT InitD3DM( HWND hWnd )
  55. {
  56.     // Create the D3DM object, which is needed to create the D3DMDevice.
  57.     if( NULL == ( g_pD3DM = Direct3DMobileCreate( D3DM_SDK_VERSION ) ) )
  58.         return E_FAIL;
  59.     // Set up the structure used to create the D3DMDevice. Most parameters are
  60.     // zeroed out. We set Windowed to TRUE, since we want to do D3DM in a
  61.     // window, and then set the SwapEffect to "discard", which is the most
  62.     // efficient method of presenting the back buffer to the display.  And 
  63.     // we request a back buffer format that matches the current desktop display 
  64.     // format.
  65.     D3DMPRESENT_PARAMETERS d3dmpp; 
  66.     memset( &d3dmpp, 0, sizeof(d3dmpp) );
  67.     d3dmpp.Windowed = TRUE;
  68.     d3dmpp.SwapEffect = D3DMSWAPEFFECT_DISCARD;
  69.     d3dmpp.BackBufferFormat = D3DMFMT_UNKNOWN;
  70.     d3dmpp.EnableAutoDepthStencil = TRUE;
  71.     d3dmpp.AutoDepthStencilFormat = D3DMFMT_D16;
  72.     // Create the Direct3D Mobile device.
  73.     UINT uAdapter;
  74.     if (g_bUseRef)
  75.     {
  76.         // Load the D3DM reference driver DLL
  77.         g_hRefDLL = (HMODULE)LoadLibrary(TEXT("d3dmref.dll"));
  78.         if (NULL == g_hRefDLL)
  79.         {
  80.             OutputDebugString(TEXT("Unable to load D3DM reference driver DLL./n"));
  81.             return E_FAIL;
  82.         }
  83.         // Get the reference driver's entry point
  84.         void* pfnD3DMInit = GetProcAddress(g_hRefDLL, TEXT("D3DM_Initialize"));   
  85.         if( NULL == pfnD3DMInit )
  86.         {
  87.             OutputDebugString(TEXT("Unable to retrieve D3DM reference driver entry point./n"));
  88.             return E_FAIL;
  89.         }
  90.         // Register the software device
  91.         if( FAILED( g_pD3DM->RegisterSoftwareDevice(pfnD3DMInit) ) )
  92.         {
  93.             OutputDebugString(TEXT("Unable to register D3DM reference driver./n"));
  94.             return E_FAIL;
  95.         }
  96.         uAdapter = D3DMADAPTER_REGISTERED_DEVICE;
  97.     }
  98.     else
  99.     {
  100.         // Use the default system D3DM driver    
  101.         uAdapter = D3DMADAPTER_DEFAULT;
  102.     }
  103.     if( FAILED( g_pD3DM->CreateDevice( uAdapter, 
  104.                                     D3DMDEVTYPE_DEFAULT,
  105.                                     hWnd, 0,
  106.                                     &d3dmpp, &g_pd3dmDevice ) ) )
  107.     {
  108.         OutputDebugString(TEXT("Unable to create a D3DM device./n"));
  109.         return E_FAIL;
  110.     }
  111.     // Device state would normally be set here
  112.     // Turn off culling
  113.     g_pd3dmDevice->SetRenderState( D3DMRS_CULLMODE, D3DMCULL_NONE );
  114.     // Turn off D3D lighting
  115.     g_pd3dmDevice->SetRenderState( D3DMRS_LIGHTING, FALSE );
  116.     // Turn on the zbuffer
  117.     g_pd3dmDevice->SetRenderState( D3DMRS_ZENABLE, TRUE );
  118.     return S_OK;
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Name: InitVB()
  122. // Desc: Creates a vertex buffer and fills it with our vertices. The vertex
  123. //       buffer is basically just a chuck of memory that holds vertices. After
  124. //       creating it, we must Lock()/Unlock() it to fill it. For indices, D3DM
  125. //       also uses index buffers. 
  126. //-----------------------------------------------------------------------------
  127. HRESULT InitVB()
  128. {
  129.     // Initialize three vertices for rendering a triangle
  130.     CUSTOMVERTEX vertices[] =
  131.     {
  132.         {-1.0f, -1.0f, -1.0f, 0xff000000, 0.0f, 0.5f},      //0
  133.         {1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3,0.5f},      //1
  134.         {-1.0f, 1.0f, -1.0f, 0xff000000, 0.0f, 0.0f},       //3
  135.         {1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3,0.5f},      //1
  136.         {-1.0f, 1.0f, -1.0f, 0xff000000, 0.0f, 0.0f},       //3
  137.         {1.0f, 1.0f, -1.0f, 0xff000000, 1.0f/3, 0.0f},      //2
  138.         {-1.0f, 1.0f, -1.0f, 0xff000000, 0.0f, 1.0f},       //3
  139.         {1.0f, 1.0f, -1.0f, 0xff000000, 1.0f/3, 1.0f},      //2
  140.         {-1.0f, 1.0f, 1.0f, 0xff000000, 0.0f, 0.5f},        //4
  141.         {1.0f, 1.0f, -1.0f, 0xff000000, 1.0f/3, 1.0f},      //2
  142.         {-1.0f, 1.0f, 1.0f, 0xff000000, 0.0f, 0.5f},        //4
  143.         {1.0f, 1.0f, 1.0f, 0xff000000, 1.0f/3, 0.5f},       //5
  144.         {-1.0f, 1.0f, 1.0f, 0xff000000, 1.0f/3, 0.5f},      //4
  145.         {1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},       //5
  146.         {-1.0f, -1.0f, 1.0f, 0xff000000, 1.0f/3, 0.0f},     //7
  147.         {1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},       //5
  148.         {-1.0f, -1.0f, 1.0f, 0xff000000, 1.0f/3, 0.0f},     //7
  149.         {1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 0.0f},      //6
  150.         {-1.0f, -1.0f, 1.0f, 0xff000000, 1.0f/3, 0.5f},     //7
  151.         {1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 1.0f},      //6
  152.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3, 0.5f},    //0
  153.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3, 0.5f},    //0
  154.         {1.0f, -1.0f, -1.0f, 0xff000000, 2.0f/3,0.5f},      //1
  155.         {1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 1.0f},      //6
  156.         {1.0f, -1.0f, -1.0f, 0xff000000, 2.0f/3,0.5f},      //1
  157.         {1.0f, 1.0f, -1.0f, 0xff000000, 2.0f/3, 0.0f},      //2
  158.         {1.0f, -1.0f, 1.0f, 0xff000000, 1.0f, 0.5f},        //6
  159.         {1.0f, 1.0f, -1.0f, 0xff000000, 2.0f/3, 0.0f},      //2
  160.         {1.0f, -1.0f, 1.0f, 0xff000000, 1.0f, 0.5f},        //6
  161.         {1.0f, 1.0f, 1.0f, 0xff000000, 1.0f, 0.0f},         //5
  162.         {-1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 1.0f},     //7
  163.         {-1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},      //4
  164.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f, 1.0f},      //0
  165.         {-1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},      //4
  166.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f, 1.0f},      //0
  167.         {-1.0f, 1.0f, -1.0f, 0xff000000, 1.0f, 0.5f},       //3
  168.     };
  169.     // Determine if the device can create vertex buffers in video memory
  170.     // by testing the device caps bits.
  171.     D3DMCAPS caps;
  172.     if( FAILED( g_pd3dmDevice->GetDeviceCaps(&caps) ) )
  173.     {
  174.         return E_FAIL;
  175.     }
  176.     D3DMPOOL texpool;
  177.     if (caps.SurfaceCaps & D3DMSURFCAPS_VIDTEXTURE)
  178.     {
  179.         texpool = D3DMPOOL_VIDEOMEM;
  180.     }
  181.     else
  182.     {
  183.         texpool = D3DMPOOL_SYSTEMMEM;
  184.     }
  185.     // Load the texture map resource (banana.bmp)
  186.      if( FAILED( D3DMXCreateTextureFromResourceEx( g_pd3dmDevice, GetModuleHandle(NULL), 
  187.         MAKEINTRESOURCE(IDB_BANANA), D3DMX_DEFAULT, D3DMX_DEFAULT, 1, 0, 
  188.         D3DMFMT_UNKNOWN, texpool, D3DMX_FILTER_POINT, D3DMX_FILTER_POINT, 
  189.         0, NULL, NULL, &g_pTexture ) ) )
  190.     {
  191.         return E_FAIL;
  192.     }      
  193.     D3DMPOOL pool;
  194.     if (caps.SurfaceCaps & D3DMSURFCAPS_VIDVERTEXBUFFER)
  195.     {
  196.         pool = D3DMPOOL_VIDEOMEM;
  197.     }
  198.     else
  199.     {
  200.         pool = D3DMPOOL_SYSTEMMEM;
  201.     }        
  202.     // Create the vertex buffer. Here we are allocating enough memory
  203.     // (from the default pool) to hold all our 3 custom vertices. We also
  204.     // specify the FVF, so the vertex buffer knows what data it contains.
  205.     if( FAILED( g_pd3dmDevice->CreateVertexBuffer( 36*sizeof(CUSTOMVERTEX),
  206.                                                   0, D3DMFVF_CUSTOMVERTEX,
  207.                                                   pool, &g_pVB ) ) )
  208.     {
  209.         return E_FAIL;
  210.     }
  211.     // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
  212.     // gain access to the vertices. This mechanism is required becuase vertex
  213.     // buffers may be in device memory.
  214.     void* pVertices;
  215.     if( FAILED( g_pVB->Lock( 0, sizeof(vertices), &pVertices, 0 ) ) )
  216.         return E_FAIL;
  217.     memcpy( pVertices, vertices, sizeof(vertices) );
  218.     g_pVB->Unlock();
  219.     return S_OK;
  220. }
  221. //-----------------------------------------------------------------------------
  222. // Name: Cleanup()
  223. // Desc: Releases all previously initialized objects
  224. //-----------------------------------------------------------------------------
  225. VOID Cleanup()
  226. {
  227.     if( g_pVB != NULL )        
  228.         g_pVB->Release();
  229.     if( g_pd3dmDevice != NULL) 
  230.         g_pd3dmDevice->Release();
  231.     if( g_pD3DM != NULL)
  232.     {
  233.         if (g_hRefDLL)
  234.         {
  235.             g_pD3DM->RegisterSoftwareDevice(NULL);
  236.             FreeLibrary(g_hRefDLL);
  237.         }
  238.         g_pD3DM->Release();
  239.     }
  240.     if (g_pTexture != NULL)
  241.         g_pTexture->Release();
  242. }
  243. VOID SetupMatrices()
  244. {
  245.     // For our world matrix, we will just leave it as the identity
  246.     D3DMXMATRIX matWorld;
  247.     D3DMXMatrixIdentity( &matWorld );
  248.     D3DMXMatrixRotationY( &matWorld, GetTickCount()/5000.0f );
  249.     g_pd3dmDevice->SetTransform( D3DMTS_WORLD, (D3DMMATRIX*)&matWorld, D3DMFMT_D3DMVALUE_FLOAT );
  250.     // Set up our view matrix. A view matrix can be defined given an eye point,
  251.     // a point to lookat, and a direction for which way is up. Here, we set the
  252.     // eye five units back along the z-axis and up three units, look at the
  253.     // origin, and define "up" to be in the y-direction.
  254.     D3DMXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
  255.     D3DMXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
  256.     D3DMXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
  257.     D3DMXMATRIX matView;
  258.     D3DMXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  259.     g_pd3dmDevice->SetTransform( D3DMTS_VIEW, (D3DMMATRIX*)&matView, D3DMFMT_D3DMVALUE_FLOAT );
  260.     // For the projection matrix, we set up a perspective transform (which
  261.     // transforms geometry from 3D view space to 2D viewport space, with
  262.     // a perspective divide making objects smaller in the distance). To build
  263.     // a perpsective transform, we need the field of view (1/4 pi is common),
  264.     // the aspect ratio, and the near and far clipping planes (which define at
  265.     // what distances geometry should be no longer be rendered).
  266.     D3DMXMATRIX matProj;
  267.     D3DMXMatrixPerspectiveFovLH( &matProj, D3DMX_PI/4.0f, 1.0f, 1.0f, 100.0f );
  268.     g_pd3dmDevice->SetTransform( D3DMTS_PROJECTION, (D3DMMATRIX*)&matProj, D3DMFMT_D3DMVALUE_FLOAT );
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Name: Render()
  272. // Desc: Draws the scene
  273. //-----------------------------------------------------------------------------
  274. VOID Render()
  275. {
  276.     if( NULL == g_pd3dmDevice )
  277.         return;
  278.     // Clear the backbuffer to a blue color
  279.     g_pd3dmDevice->Clear( 0, NULL, D3DMCLEAR_TARGET | D3DMCLEAR_ZBUFFER, D3DMCOLOR_XRGB(0,0,255), 1.0f, 0 );
  280.     // Begin the scene
  281.     if( SUCCEEDED( g_pd3dmDevice->BeginScene() ) )
  282.     {
  283.         SetupMatrices();
  284.         // Draw the triangles in the vertex buffer. This is broken into a few
  285.         // steps. We are passing the vertices down a "stream", so first we need
  286.         // to specify the source of that stream, which is our vertex buffer. Then,
  287.         // we call DrawPrimitive() which does the actual rendering of our geometry
  288.         // (in this case, just one triangle).
  289.         g_pd3dmDevice->SetTexture( 0, g_pTexture );
  290.         g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLOROP,   D3DMTOP_SELECTARG1);//D3DMTOP_MODULATE );
  291.         g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
  292.         g_pd3dmDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
  293.         //g_pd3dmDevice->SetFVF( D3DMFVF_CUSTOMVERTEX );
  294.         g_pd3dmDevice->DrawPrimitive( D3DMPT_TRIANGLELIST, 0, 12 );
  295.         // End the scene
  296.         g_pd3dmDevice->EndScene();
  297.     }
  298.     // Present the backbuffer contents to the display
  299.     g_pd3dmDevice->Present( NULL, NULL, NULL, NULL );
  300. }
  301. //-----------------------------------------------------------------------------
  302. // Name: MsgProc()
  303. // Desc: The window's message handler
  304. //-----------------------------------------------------------------------------
  305. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  306. {
  307.     switch( msg )
  308.     {
  309.     case WM_LBUTTONUP: 
  310.         PostMessage(hWnd, WM_CLOSE, 0, 0);
  311.         break;
  312.     case WM_KEYDOWN:
  313.         if (VK_ESCAPE == wParam)
  314.         {
  315.             PostMessage(hWnd, WM_CLOSE, 0, 0);
  316.         }
  317.         break;
  318.     case WM_CLOSE:
  319.         Cleanup();
  320.         break;
  321.     case WM_DESTROY:
  322.         PostQuitMessage( 0 );
  323.         return 0;
  324.     case WM_SETTINGCHANGE:
  325.         //we don't support screen rotation
  326.         if (IsScreenRotated())
  327.         {
  328.             PostMessage(hWnd, WM_CLOSE, 0, 0);
  329.         }
  330.         break;
  331.     }
  332.     return DefWindowProc( hWnd, msg, wParam, lParam );
  333. }
  334. //-----------------------------------------------------------------------------
  335. // Name: WinMain()
  336. // Desc: The application's entry point
  337. //-----------------------------------------------------------------------------
  338. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPTSTR szCmd, INT )
  339. {
  340.     // Parse command line to determine if user wants to use
  341.     // the D3DM reference driver instead of the default system driver
  342.     if (0 == lstrcmp(szCmd, TEXT("-ref")))
  343.         g_bUseRef = true;
  344.     //we don't support a rotated screen
  345.     if (IsScreenRotated())
  346.     {
  347.         return 0;
  348.     }
  349.     // Register the window class
  350.     WNDCLASS wc = { 0L, MsgProc, 0L, 0L, 
  351.                       hInst, NULL, NULL, NULL, NULL,
  352.                       TEXT("D3DM Tutorial") };
  353.     RegisterClass( &wc );
  354.     int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  355.     int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  356.     // Create the application's window
  357.     HWND hWnd = CreateWindow( TEXT("D3DM Tutorial"), 
  358.                               TEXT("D3DM Tutorial 02: Vertices"), 
  359.                               WS_VISIBLE, 
  360.                               0, 0, iScreenWidth, iScreenHeight,
  361.                               NULL, NULL, wc.hInstance, NULL );
  362.     //SHFullScreen(hWnd, SHFS_HIDESIPBUTTON | SHFS_HIDETASKBAR);
  363.     // Initialize Direct3D Mobile
  364.     if( SUCCEEDED( InitD3DM( hWnd ) ) )
  365.     { 
  366.         // Create the vertex buffer
  367.         if( SUCCEEDED( InitVB() ) )
  368.         {
  369.             // Show the window
  370.             ShowWindow( hWnd, SW_SHOWNORMAL );
  371.             UpdateWindow( hWnd );
  372.             // Enter the message loop
  373.             MSG msg;
  374.             memset( &msg, 0, sizeof(msg) );
  375.             while( msg.message!=WM_QUIT )
  376.             {
  377.                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  378.                 {
  379.                     TranslateMessage( &msg );
  380.                     DispatchMessage( &msg );
  381.                 }
  382.                 else
  383.                     Render();
  384.             }
  385.         }
  386.     }
  387.     return 0;
  388. }

继续阅读