【编者按】:网学网ASP.net为您提供VC.NET的Direct3D极速入门宝典参考,解决您在VC.NET的Direct3D极速入门宝典学习中工作中的难题,参考学习。
听说DirectX9.0发布了吗?如果没有,你现在也应该听我说过了,那就去http://download.microsoft.com/download/b/6/a/b6ab32f3-39e8-4096-9445-d38e6675de85/dx90bsdk.exe下载一个最新的DirectX9.0的SDK,因为我将使用D3D9来进行全文的讲解。其实DirectX9.0里有非常详细的教程和参考,大多数人只需要看看这些帮助就可以自己学习D3D了,我的这篇文章适合那些很懒但想快速入门、不懂英文或编程知识很欠缺的人看。装好DirectX9.0后,打开VC.net,新建一个Win32工程,在StdAfx.h里添加下面的语句:#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 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 ) { } 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; } |