Direct3D极速入门宝典(三)

类别:VC语言 点击:0 评论:0 推荐:

 

现在你的代码应该是这样样子:

 

#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();

       }

}

 

 

本文地址:http://com.8s8s.com/it/it919.htm