#include <d3d9.h> // D3D标准头文件 #include <D3dx9math.h> // D3D数学库头文件 #include <stdio.h> // 这个不用我说了吧? #pragma comment( lib, "d3d9" ) // D3D的静态库 #pragma comment( lib, "d3dx9" ) // D3D数学库的静态库 |
#include "stdafx.h" #define MAX_LOADSTRING 100 HINSTANCE g_hInst; HWND g_hWnd; IDirect3D9 *g_pD3D; IDirect3DDevice9 *g_pd3dDevice; IDirect3DVertexBuffer9 *g_pVB; TCHAR szTitle[MAX_LOADSTRING]; TCHAR szWindowClass[MAX_LOADSTRING]; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); void OnIdle( void ); void OnCreate( HWND hWnd ); HRESULT InitD3D( void ); HRESULT CreateObject( void ); void ReleaseD3D( void ); HRESULT SetModalMatrix( void ); HRESULT SetProjMatrix( WORD wWidth, WORD wHeight ); void BeforePaint( void ); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; HACCEL hAclearcase/" target="_blank" >ccelTable; LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_D3DTEST, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_D3DTEST); while ( true ) { if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } continue; } if ( WM_QUIT == msg.message ) { break; } OnIdle(); } UnregisterClass( szWindowClass, g_hInst ); return (int)msg.wParam; } ATOM MyRegisterClass( HINSTANCE hInstance ) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_D3DTEST); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_D3DTEST; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } BOOL InitInstance( HINSTANCE hInstance, int nCmdShow ) { g_hInst = hInstance; CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ); if ( !g_hWnd ) { return FALSE; } ShowWindow( g_hWnd, nCmdShow ); UpdateWindow( g_hWnd ); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; switch (message) { case WM_CREATE: OnCreate( hWnd ); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_SIZE: SetProjMatrix( LOWORD( lParam ), HIWORD( lParam ) ); break; case WM_DESTROY: ReleaseD3D(); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } void OnCreate( HWND hWnd ) { g_hWnd = hWnd; InitD3D(); CreateObject(); } void ReleaseD3D( void ) { } HRESULT InitD3D( void ) { return S_OK; } void BeforePaint( void ) { } HRESULT CreateObject( void ) { return S_OK; } void OnIdle( void ) { } HRESULT SetModalMatrix( void ) { return S_OK; } HRESULT SetProjMatrix( WORD wWidth, WORD wHeight ) { return S_OK; } |
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ); if( NULL == g_pD3D ) { return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &g_pd3dDevice ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.3f, 0.3f, 0.3f, 1.0 ) ); g_pd3dDevice->LightEnable( 0, TRUE); D3DMATERIAL9 mtrl; ZeroMemory( &mtrl, sizeof(mtrl) ); mtrl.Diffuse.r = mtrl.Ambient.r = 140.0f / 255.0f; mtrl.Diffuse.g = mtrl.Ambient.g = 200.0f / 255.0f; mtrl.Diffuse.b = mtrl.Ambient.b = 255.0f / 255.0f; mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; g_pd3dDevice->SetMaterial( &mtrl ); return S_OK; |
d3dpp.Windowed = TRUE; // 设备是窗口设备而不是全屏 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // 翻转缓冲区时不改动后台缓冲 d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // ARGB颜色模式 |
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.6f, 0.6f, 0.6f, 1.0 ) ); |
mtrl.Diffuse.r = mtrl.Ambient.r = 140.0f / 255.0f; mtrl.Diffuse.g = mtrl.Ambient.g = 200.0f / 255.0f; mtrl.Diffuse.b = mtrl.Ambient.b = 255.0f / 255.0f; mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; |
D3DVECTOR SrcBox[] = { { 5.0f, 5.0f, 0.0f }, { 5.0f, 5.0f, 10.0f }, { 5.0f, -5.0f, 0.0f }, { 5.0f, -5.0f, 10.0f }, {-5.0f, -5.0f, 0.0f }, {-5.0f, -5.0f, 10.0f }, {-5.0f, 5.0f, 0.0f }, {-5.0f, 5.0f, 10.0f }, }; WORD wIndex[] ={ 0, 4, 6, 0, 2, 4, 0, 6, 7, 0, 7, 1, 0, 3, 2, 0, 1, 3, 5, 2, 3, 5, 4, 2, 5, 6, 4, 5, 7, 6, 5, 1, 7, 5, 3, 1, }; |
struct CUSTOMVERTEX { D3DVECTOR pos; D3DVECTOR normal; }; |
CUSTOMVERTEX ExpandBox[sizeof(wIndex) / sizeof(WORD)]; for ( int i = 0; i < 36; i++ ) { ExpandBox[i].pos = SrcBox[ wIndex[i] ]; } |
for ( i = 0; i < 12; i++ ) { D3DVECTOR Tri[3]; Tri[0] = ExpandBox[ i * 3 + 0 ].pos; Tri[1] = ExpandBox[ i * 3 + 1 ].pos; Tri[2] = ExpandBox[ i * 3 + 2 ].pos; ExpandBox[ i * 3 + 0 ].normal.x = 0.0f; ExpandBox[ i * 3 + 0 ].normal.y = 0.0f; ExpandBox[ i * 3 + 0 ].normal.z = 1.0f; CalcNormal( Tri, &(ExpandBox[ i * 3 + 0 ].normal) ); ExpandBox[ i * 3 + 1 ].normal = ExpandBox[ i * 3 + 0 ].normal; ExpandBox[ i * 3 + 2 ].normal = ExpandBox[ i * 3 + 0 ].normal; } |
#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL ) |
if( FAILED( g_pd3dDevice->CreateVertexBuffer( sizeof(ExpandBox), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) { return E_FAIL; } |
VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(ExpandBox), (void**)&pVertices, 0 ) ) ) return E_FAIL; MoveMemory( pVertices, ExpandBox, sizeof(ExpandBox) ); g_pVB->Unlock(); |
D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, (float)wWidth / (float)wHeight, 1.0f, 100.0f ); return g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); |
static float fRadius = 0.5f; fRadius -= 0.003f; if ( fRadius < 0) { fRadius = D3DX_PI * 2 ; } D3DXMATRIX matWorld; D3DXMatrixRotationZ( &matWorld, 0.0f ); if ( FAILED( g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ) ) ) { return E_FAIL; } D3DXMATRIX matView; D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( cosf( fRadius ) * 40.0f, sinf( fRadius ) * 40.0f, 30.0 ), &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ), &D3DXVECTOR3( 0.0f, 0.0f, 1.0f ) ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); return S_OK; |
if ( g_pd3dDevice != NULL ) { g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,200), 1.0f, 0 ); if ( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { BeforePaint(); if ( FAILED( SetModalMatrix() ) ) { return; } g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 ); g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } } |
D3DLIGHT9 light; ZeroMemory( &light, sizeof(light) ); light.Position = D3DXVECTOR3( 30.0f, 30.0f, 30.0f ); light.Attenuation1 = 0.05f; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; light.Range = 1000.0f; light.Type = D3DLIGHT_POINT; g_pd3dDevice->SetLight( 0, &light ); g_pd3dDevice->LightEnable( 0, TRUE); |
#include "stdafx.h" #include "D3DTest.h" #define MAX_LOADSTRING 100 #define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL ) struct CUSTOMVERTEX { D3DVECTOR pos; D3DVECTOR normal; }; HINSTANCE g_hInst; HWND g_hWnd; IDirect3D9 *g_pD3D; IDirect3DDevice9 *g_pd3dDevice; IDirect3DVertexBuffer9 *g_pVB; TCHAR szTitle[MAX_LOADSTRING]; TCHAR szWindowClass[MAX_LOADSTRING]; ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); void OnIdle( void ); void OnCreate( HWND hWnd ); HRESULT InitD3D( void ); HRESULT CreateObject( void ); void ReleaseD3D( void ); HRESULT SetModalMatrix( void ); HRESULT SetProjMatrix( WORD wWidth, WORD wHeight ); void BeforePaint( void ); void CalcNormal( const D3DVECTOR *pVertices, D3DVECTOR *pNormal ) { D3DVECTOR v1, v2; v1.x = pVertices[0].x - pVertices[1].x; v1.y = pVertices[0].y - pVertices[1].y; v1.z = pVertices[0].z - pVertices[1].z; v2.x = pVertices[1].x - pVertices[2].x; v2.y = pVertices[1].y - pVertices[2].y; v2.z = pVertices[1].z - pVertices[2].z; D3DXVECTOR3 Temp( v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z,v1.x * v2.y - v1.y * v2.x ); D3DXVec3Normalize( (D3DXVECTOR3*)pNormal, &Temp ); } class CTimer { public: CTimer() {QueryPerformanceFrequency(&m_Frequency); Start();} void Start() {QueryPerformanceCounter(&m_StartCount);} double End() {LARGE_INTEGER CurrentCount;QueryPerformanceCounter(&CurrentCount);return double(CurrentCount.LowPart - m_StartCount.LowPart) / (double)m_Frequency.LowPart;} private: LARGE_INTEGER m_Frequency; LARGE_INTEGER m_StartCount; }; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; HACCEL hAccelTable; LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_D3DTEST, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); if (!InitInstance (hInstance, nCmdShow)) return FALSE; hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_D3DTEST); while ( true ) { if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } continue; } if ( WM_QUIT == msg.message ) break; OnIdle(); } UnregisterClass( szWindowClass, g_hInst ); return (int)msg.wParam; } ATOM MyRegisterClass( HINSTANCE hInstance ) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_D3DTEST); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_D3DTEST; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex); } BOOL InitInstance( HINSTANCE hInstance, int nCmdShow ) { g_hInst = hInstance; CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL ); if ( !g_hWnd ) { return FALSE; } ShowWindow( g_hWnd, nCmdShow ); UpdateWindow( g_hWnd ); return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; switch (message) { case WM_CREATE: OnCreate( hWnd ); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_SIZE: SetProjMatrix( LOWORD( lParam ), HIWORD( lParam ) ); break; case WM_DESTROY: ReleaseD3D(); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } void OnCreate( HWND hWnd ) { g_hWnd = hWnd; InitD3D(); CreateObject(); } void ReleaseD3D( void ) { if( g_pVB != NULL ) { g_pVB->Release(); } if( g_pd3dDevice != NULL ) { g_pd3dDevice->Release(); } if( g_pD3D != NULL ) { g_pD3D->Release(); } } HRESULT InitD3D( void ) { g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &g_pd3dDevice ); g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.6f, 0.6f, 0.6f, 1.0 ) ); g_pd3dDevice->LightEnable( 0, TRUE); D3DMATERIAL9 mtrl; ZeroMemory( &mtrl, sizeof(mtrl) ); mtrl.Diffuse.r = mtrl.Ambient.r = 140.0f / 255.0f; mtrl.Diffuse.g = mtrl.Ambient.g = 200.0f / 255.0f; mtrl.Diffuse.b = mtrl.Ambient.b = 255.0f / 255.0f; mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f; g_pd3dDevice->SetMaterial( &mtrl ); return S_OK; } void BeforePaint( void ) { D3DLIGHT9 light; ZeroMemory( &light, sizeof(light) ); light.Position = D3DXVECTOR3( 30.0f, 30.0f, 30.0f ); light.Attenuation1 = 0.05f; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; light.Range = 1000.0f; light.Type = D3DLIGHT_POINT; g_pd3dDevice->SetLight( 0, &light ); g_pd3dDevice->LightEnable( 0, TRUE); } HRESULT CreateObject( void ) { D3DVECTOR SrcBox[] = { { 5.0f, 5.0f, 0.0f }, { 5.0f, 5.0f, 10.0f }, { 5.0f, -5.0f, 0.0f }, { 5.0f, -5.0f, 10.0f }, {-5.0f, -5.0f, 0.0f }, {-5.0f, -5.0f, 10.0f }, {-5.0f, 5.0f, 0.0f }, {-5.0f, 5.0f, 10.0f }, }; WORD wIndex[] ={ 0, 4, 6, 0, 2, 4, 0, 6, 7, 0, 7, 1, 0, 3, 2, 0, 1, 3, 5, 2, 3, 5, 4, 2, 5, 6, 4, 5, 7, 6, 5, 1, 7, 5, 3, 1, }; CUSTOMVERTEX ExpandBox[sizeof(wIndex) / sizeof(WORD)]; for ( int i = 0; i < 36; i++ ) ExpandBox[i].pos = SrcBox[ wIndex[i] ]; for ( i = 0; i < 12; i++ ) { D3DVECTOR Tri[3]; Tri[0] = ExpandBox[ i * 3 + 0 ].pos; Tri[1] = ExpandBox[ i * 3 + 1 ].pos; Tri[2] = ExpandBox[ i * 3 + 2 ].pos; ExpandBox[ i * 3 + 0 ].normal.x = 0.0f; ExpandBox[ i * 3 + 0 ].normal.y = 0.0f; ExpandBox[ i * 3 + 0 ].normal.z = 1.0f; CalcNormal( Tri, &(ExpandBox[ i * 3 + 0 ].normal) ); ExpandBox[ i * 3 + 1 ].normal = ExpandBox[ i * 3 + 0 ].normal; ExpandBox[ i * 3 + 2 ].normal = ExpandBox[ i * 3 + 0 ].normal; } g_pd3dDevice->CreateVertexBuffer( sizeof(ExpandBox), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ); VOID* pVertices; g_pVB->Lock( 0, sizeof(ExpandBox), (void**)&pVertices, 0 ); MoveMemory( pVertices, ExpandBox, sizeof(ExpandBox) ); g_pVB->Unlock(); return S_OK; } void OnIdle( void ) { static CTimer t; static double dt = t.End(); double temp = t.End(); char szValue[256]; sprintf( szValue, "当前帧率:%f", 1 / ( temp - dt ) ); SetWindowText( g_hWnd, szValue ); dt = temp; if ( g_pd3dDevice != NULL ) { g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,200), 1.0f, 0 ); if ( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { BeforePaint(); if ( FAILED( SetModalMatrix() ) ) { return; } g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 ); g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } } } HRESULT SetModalMatrix( void ) { static float fRadius = 0.5f; fRadius -= 0.003f; if ( fRadius < 0) { fRadius = D3DX_PI * 2 ; } D3DXMATRIX matWorld; D3DXMatrixRotationZ( &matWorld, 0.0f ); if ( FAILED( g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ) ) ) { return E_FAIL; } D3DXMATRIX matView; D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( cosf( fRadius ) * 40.0f, sinf( fRadius ) * 40.0f, 30.0 ), &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ), &D3DXVECTOR3( 0.0f, 0.0f, 1.0f ) ); g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView ); return S_OK; } HRESULT SetProjMatrix( WORD wWidth, WORD wHeight ) { D3DXMATRIX matProj; D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, (float)wWidth / (float)wHeight, 1.0f, 100.0f ); return g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); } |