游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1746|回复: 1

初学DXUT 简单程序出错啦。。。实在不知道哪里错。

[复制链接]

2

主题

5

帖子

5

积分

新手上路

Rank: 1

积分
5
发表于 2010-3-22 11:39:00 | 显示全部楼层 |阅读模式


  1. #include "DXUT.h"
  2. #include "resource.h"

  3. LPDIRECT3DVERTEXBUFFER9     g_pVB=NULL;


  4. struct CUSTUMVERTEX
  5. {
  6.                 float x,y,z;
  7.                 DWORD color;
  8. };

  9. const DWORD CUSTUMVERTEXFVF = D3DFVF_XYZ|D3DFVF_DIFFUSE;

  10. HRESULT InitVB()
  11. {
  12.         IDirect3DDevice9* pD3Device=DXUTGetD3D9Device();

  13.                         CUSTUMVERTEX vertices[]=
  14.                 {
  15.                         {-0.5f,-0.5f,0.0f,0xff00ffff},
  16.                         {0.0f,0.5f,0.0f,0xffff0000},
  17.                         {0.5f,-0.5f,0.0f,0xff00ff00}
  18.                 //{150.0f,50.0f,0.0f,0xffff0000},//x,y,z,rhw
  19.                 //{250.0f,150.0f,0.0f,0xff00ffff},
  20.                 //{50.0f,150.0f,0.0f,0xff00ff00}
  21.                 };
  22.                
  23.                 if(FAILED(pD3Device->CreateVertexBuffer(3*sizeof(CUSTUMVERTEX),0,CUSTUMVERTEXFVF,D3DPOOL_DEFAULT, &g_pVB, NULL)))
  24.                 {
  25.                                 return E_FAIL;
  26.                 }

  27.                 VOID* pVertices;
  28.     if( FAILED( g_pVB->Lock( 0, sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
  29.         return E_FAIL;
  30.     memcpy( pVertices, vertices, sizeof( vertices ) );
  31.     g_pVB->Unlock();

  32.     return S_OK;
  33. }
  34. //--------------------------------------------------------------------------------------
  35. // Rejects any D3D9 devices that aren't acceptable to the app by returning false
  36. //--------------------------------------------------------------------------------------
  37. bool CALLBACK IsD3D9DeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat,
  38.                                       bool bWindowed, void* pUserContext )
  39. {
  40.     // Typically want to skip back buffer formats that don't support alpha blending
  41.     IDirect3D9* pD3D = DXUTGetD3D9Object();
  42.     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
  43.                                          AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
  44.                                          D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
  45.         return false;

  46.     return true;
  47. }


  48. //--------------------------------------------------------------------------------------
  49. // Before a device is created, modify the device settings as needed
  50. //--------------------------------------------------------------------------------------
  51. bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
  52. {
  53.     return true;
  54. }


  55. //--------------------------------------------------------------------------------------
  56. // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED)
  57. // and aren't tied to the back buffer size
  58. //--------------------------------------------------------------------------------------
  59. HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
  60.                                      void* pUserContext )
  61. {

  62.     // Set up the structure used to create the D3DDevice
  63.     D3DPRESENT_PARAMETERS d3dpp;
  64.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  65.     d3dpp.Windowed = TRUE;
  66.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  67.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

  68.     // Device state would normally be set here
  69.                 pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);//关闭关照
  70.                 pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);

  71.     return S_OK;
  72. }


  73. //--------------------------------------------------------------------------------------
  74. // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT)
  75. // or that are tied to the back buffer size
  76. //--------------------------------------------------------------------------------------
  77. HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
  78.                                     void* pUserContext )
  79. {
  80.     return S_OK;
  81. }


  82. //--------------------------------------------------------------------------------------
  83. // Handle updates to the scene.  This is called regardless of which D3D API is used
  84. //--------------------------------------------------------------------------------------
  85. void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
  86. {
  87.                
  88.                 IDirect3DDevice9* pD3Device=DXUTGetD3D9Device();

  89.         //World matrix
  90.                 D3DXMATRIXA16 matWorld;
  91.                 UINT  iTime  = timeGetTime() % 1000;
  92.     FLOAT fAngle = iTime * (1.0f * D3DX_PI) / 1000.0f;
  93.     D3DXMatrixRotationY( &matWorld, fAngle );
  94.                 pD3Device->SetTransform( D3DTS_WORLD, &matWorld );

  95.                 //view matrix
  96.                 D3DXVECTOR3 vEyePt(0.0f,0.5f,-0.5f);
  97.                 D3DXVECTOR3 vLookAtPt(0.0f,0.0f,0.0f);
  98.                 D3DXVECTOR3 vUpVec(0.0f,1.0f,0.0f);
  99.                 D3DXMATRIXA16 matView;
  100.                 D3DXMatrixLookAtLH(&matView,&vEyePt,&vLookAtPt,&vUpVec);
  101.                 pD3Device->SetTransform(D3DTS_VIEW,&matView);

  102.                 //projection matrix
  103.        
  104.                 D3DXMATRIXA16 matProj;
  105.                 D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,100.0f);
  106.                 pD3Device->SetTransform(D3DTS_PROJECTION,&matProj);
  107.                
  108. }


  109. //--------------------------------------------------------------------------------------
  110. // Render the scene using the D3D9 device
  111. //--------------------------------------------------------------------------------------
  112. void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
  113. {
  114.     HRESULT hr;

  115.                 InitVB();

  116.     // Clear the render target and the zbuffer
  117.     V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 ) );

  118.     // Render the scene
  119.     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
  120.     {
  121.                                 pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof( CUSTUMVERTEX ));
  122.                                 pd3dDevice->SetFVF(CUSTUMVERTEXFVF);
  123.                                 pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
  124.         V( pd3dDevice->EndScene() );
  125.     }
  126. }


  127. //--------------------------------------------------------------------------------------
  128. // Handle messages to the application
  129. //--------------------------------------------------------------------------------------
  130. LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
  131.                           bool* pbNoFurtherProcessing, void* pUserContext )
  132. {
  133.     return 0;
  134. }


  135. //--------------------------------------------------------------------------------------
  136. // Release D3D9 resources created in the OnD3D9ResetDevice callback
  137. //--------------------------------------------------------------------------------------
  138. void CALLBACK OnD3D9LostDevice( void* pUserContext )
  139. {
  140.                 if( g_pVB != NULL )
  141.         g_pVB->Release();
  142. }


  143. //--------------------------------------------------------------------------------------
  144. // Release D3D9 resources created in the OnD3D9CreateDevice callback
  145. //--------------------------------------------------------------------------------------
  146. void CALLBACK OnD3D9DestroyDevice( void* pUserContext )
  147. {
  148.         if( g_pVB != NULL )
  149.         g_pVB->Release();
  150. }


  151. //--------------------------------------------------------------------------------------
  152. // Initialize everything and go into a render loop
  153. //--------------------------------------------------------------------------------------
  154. INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
  155. {
  156.     // Enable run-time memory check for debug builds.
  157. #if defined(DEBUG) | defined(_DEBUG)
  158.     _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  159. #endif

  160.     // Set the callback functions
  161.     DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );
  162.     DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );
  163.     DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );
  164.     DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );
  165.     DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );
  166.     DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );
  167.     DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
  168.     DXUTSetCallbackMsgProc( MsgProc );
  169.     DXUTSetCallbackFrameMove( OnFrameMove );

  170.     // TODO: Perform any application-level initialization here

  171.     // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
  172.     DXUTInit( true, true ); // Parse the command line and show msgboxes
  173.     DXUTSetHotkeyHandling( true, true, true );  // handle the default hotkeys
  174.     DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
  175.     DXUTCreateWindow( L"EmptyProject" );
  176.     DXUTCreateDevice( true, 640, 480 );

  177.     // Start the render loop
  178.     DXUTMainLoop();

  179.     // TODO: Perform any application-level cleanup here

  180.     return DXUTGetExitCode();
  181. }

复制代码



程序没有做矩阵变换时候,能够正常显示三角形,可是一变换,就不显示了。。。。想不明白。是按正常的参数来设置的。麻烦大家看看。

4

主题

137

帖子

311

积分

中级会员

Rank: 3Rank: 3

积分
311
发表于 2010-3-22 17:27:00 | 显示全部楼层

Re:初学DXUT 简单程序出错啦。。。实在不知道哪里错。

matWorld 初始化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-12 17:45

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表