|
- #include "DXUT.h"
- #include "resource.h"
- LPDIRECT3DVERTEXBUFFER9 g_pVB=NULL;
- struct CUSTUMVERTEX
- {
- float x,y,z;
- DWORD color;
- };
- const DWORD CUSTUMVERTEXFVF = D3DFVF_XYZ|D3DFVF_DIFFUSE;
- HRESULT InitVB()
- {
- IDirect3DDevice9* pD3Device=DXUTGetD3D9Device();
- CUSTUMVERTEX vertices[]=
- {
- {-0.5f,-0.5f,0.0f,0xff00ffff},
- {0.0f,0.5f,0.0f,0xffff0000},
- {0.5f,-0.5f,0.0f,0xff00ff00}
- //{150.0f,50.0f,0.0f,0xffff0000},//x,y,z,rhw
- //{250.0f,150.0f,0.0f,0xff00ffff},
- //{50.0f,150.0f,0.0f,0xff00ff00}
- };
-
- if(FAILED(pD3Device->CreateVertexBuffer(3*sizeof(CUSTUMVERTEX),0,CUSTUMVERTEXFVF,D3DPOOL_DEFAULT, &g_pVB, NULL)))
- {
- return E_FAIL;
- }
- VOID* pVertices;
- if( FAILED( g_pVB->Lock( 0, sizeof( vertices ), ( void** )&pVertices, 0 ) ) )
- return E_FAIL;
- memcpy( pVertices, vertices, sizeof( vertices ) );
- g_pVB->Unlock();
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Rejects any D3D9 devices that aren't acceptable to the app by returning false
- //--------------------------------------------------------------------------------------
- bool CALLBACK IsD3D9DeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat,
- bool bWindowed, void* pUserContext )
- {
- // Typically want to skip back buffer formats that don't support alpha blending
- IDirect3D9* pD3D = DXUTGetD3D9Object();
- if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
- AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
- D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
- return false;
- return true;
- }
- //--------------------------------------------------------------------------------------
- // Before a device is created, modify the device settings as needed
- //--------------------------------------------------------------------------------------
- bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
- {
- return true;
- }
- //--------------------------------------------------------------------------------------
- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED)
- // and aren't tied to the back buffer size
- //--------------------------------------------------------------------------------------
- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
- void* pUserContext )
- {
- // Set up the structure used to create the D3DDevice
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof(d3dpp) );
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
- // Device state would normally be set here
- pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);//关闭关照
- pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT)
- // or that are tied to the back buffer size
- //--------------------------------------------------------------------------------------
- HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
- void* pUserContext )
- {
- return S_OK;
- }
- //--------------------------------------------------------------------------------------
- // Handle updates to the scene. This is called regardless of which D3D API is used
- //--------------------------------------------------------------------------------------
- void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
- {
-
- IDirect3DDevice9* pD3Device=DXUTGetD3D9Device();
- //World matrix
- D3DXMATRIXA16 matWorld;
- UINT iTime = timeGetTime() % 1000;
- FLOAT fAngle = iTime * (1.0f * D3DX_PI) / 1000.0f;
- D3DXMatrixRotationY( &matWorld, fAngle );
- pD3Device->SetTransform( D3DTS_WORLD, &matWorld );
- //view matrix
- D3DXVECTOR3 vEyePt(0.0f,0.5f,-0.5f);
- D3DXVECTOR3 vLookAtPt(0.0f,0.0f,0.0f);
- D3DXVECTOR3 vUpVec(0.0f,1.0f,0.0f);
- D3DXMATRIXA16 matView;
- D3DXMatrixLookAtLH(&matView,&vEyePt,&vLookAtPt,&vUpVec);
- pD3Device->SetTransform(D3DTS_VIEW,&matView);
- //projection matrix
-
- D3DXMATRIXA16 matProj;
- D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI/4,1.0f,1.0f,100.0f);
- pD3Device->SetTransform(D3DTS_PROJECTION,&matProj);
-
- }
- //--------------------------------------------------------------------------------------
- // Render the scene using the D3D9 device
- //--------------------------------------------------------------------------------------
- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
- {
- HRESULT hr;
- InitVB();
- // Clear the render target and the zbuffer
- V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 ) );
- // Render the scene
- if( SUCCEEDED( pd3dDevice->BeginScene() ) )
- {
- pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof( CUSTUMVERTEX ));
- pd3dDevice->SetFVF(CUSTUMVERTEXFVF);
- pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
- V( pd3dDevice->EndScene() );
- }
- }
- //--------------------------------------------------------------------------------------
- // Handle messages to the application
- //--------------------------------------------------------------------------------------
- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
- bool* pbNoFurtherProcessing, void* pUserContext )
- {
- return 0;
- }
- //--------------------------------------------------------------------------------------
- // Release D3D9 resources created in the OnD3D9ResetDevice callback
- //--------------------------------------------------------------------------------------
- void CALLBACK OnD3D9LostDevice( void* pUserContext )
- {
- if( g_pVB != NULL )
- g_pVB->Release();
- }
- //--------------------------------------------------------------------------------------
- // Release D3D9 resources created in the OnD3D9CreateDevice callback
- //--------------------------------------------------------------------------------------
- void CALLBACK OnD3D9DestroyDevice( void* pUserContext )
- {
- if( g_pVB != NULL )
- g_pVB->Release();
- }
- //--------------------------------------------------------------------------------------
- // Initialize everything and go into a render loop
- //--------------------------------------------------------------------------------------
- INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
- {
- // Enable run-time memory check for debug builds.
- #if defined(DEBUG) | defined(_DEBUG)
- _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
- #endif
- // Set the callback functions
- DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );
- DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );
- DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );
- DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );
- DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );
- DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );
- DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
- DXUTSetCallbackMsgProc( MsgProc );
- DXUTSetCallbackFrameMove( OnFrameMove );
- // TODO: Perform any application-level initialization here
- // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
- DXUTInit( true, true ); // Parse the command line and show msgboxes
- DXUTSetHotkeyHandling( true, true, true ); // handle the default hotkeys
- DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
- DXUTCreateWindow( L"EmptyProject" );
- DXUTCreateDevice( true, 640, 480 );
- // Start the render loop
- DXUTMainLoop();
- // TODO: Perform any application-level cleanup here
- return DXUTGetExitCode();
- }
复制代码
程序没有做矩阵变换时候,能够正常显示三角形,可是一变换,就不显示了。。。。想不明白。是按正常的参数来设置的。麻烦大家看看。 |
|