|
|
我把代码贴上来,诸位帮忙运行一下看看是什么原因.
-
- #include <Windows.h>
- #include <mmsystem.h>
- #include <d3dx9.h>
- #pragma warning( disable : 4996 ) // disable deprecated warning
- #include <strsafe.h>
- #pragma warning( default : 4996 )
- #include "Camera8.h"
- #include "Terrain8.h"
- //-----------------------------------------------------------------------------
- // Global variables
- //-----------------------------------------------------------------------------
- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
- LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
- ID3DXMesh* mesh = 0;
- const int Width = 800;
- const int Height = 600;
-
- POINT m_ptLastMouse; // position of last mouse point
- float Z=0.0f;
- D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f);//( .5f, .55f, -.2f );//+Z
- float X=0.0f;
- D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );//( .5f+X, .125f, .5f+Z );
- D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
- D3DXMATRIXA16 g_matWorld;
- Terrain* g_pTerrain;
- Camera* theCamera = new Camera(vEyePt,vLookatPt,vUpVec);
-
- //-----------------------------------------------------------------------------
- // Name: InitD3D()
- // Desc: Initializes Direct3D
- //-----------------------------------------------------------------------------
- HRESULT InitD3D( HWND hWnd )
- {
- // Create the D3D object.
- if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
- return E_FAIL;
- // Set up the structure used to create the D3DDevice. Since we are now
- // using more complex geometry, we will create a device with a zbuffer.
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof(d3dpp) );
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
- d3dpp.EnableAutoDepthStencil = TRUE;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- // Create the D3DDevice
- if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- &d3dpp, &g_pd3dDevice ) ) )
- {
- return E_FAIL;
- }
- // Turn on the zbuffer
- g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- // Turn on ambient lighting
- g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
- g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
- // m_pCamera1 = new Camera();
- g_pTerrain = new Terrain(g_pd3dDevice,21, 21, 10);//, 15.0
- // m_pTerrain1->SetTexture("Grass.bmp");
- D3DXCreateTeapot(g_pd3dDevice,&mesh,0);
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: Cleanup()
- // Desc: Releases all previously initialized objects
- //-----------------------------------------------------------------------------
- VOID Cleanup()
- {
- delete(theCamera);
- mesh->Release();
- mesh = 0;
- delete(g_pTerrain);
- if( g_pd3dDevice != NULL )
- g_pd3dDevice->Release();
- if( g_pD3D != NULL )
- g_pD3D->Release();
- //SafeDelete(g_pTerrain);
- //SafeDelete(m_pCamera1);
- }
- void OnBegin( int nX, int nY)
- {
- // m_bDrag = true;
- // m_qDown = m_qNow;
- //m_vDownPt = ScreenToVector( (float)nX, (float)nY );
- m_ptLastMouse.x =nX;
- m_ptLastMouse.y =nY;
- }
- //-----------------------------------------------------------------------------
- // Name: SetupMatrices()
- // Desc: Sets up the world, view, and projection transform matrices.
- //-----------------------------------------------------------------------------
- VOID SetupMatrices()
- {
- //D3DXMatrixTranslation( &g_matWorld, -2.56f,0.0f,0.0f);
- D3DXMatrixIdentity(&g_matWorld);
- g_pd3dDevice->SetTransform( D3DTS_WORLD,&g_matWorld );
- D3DXMATRIXA16 matView;
- D3DXMatrixLookAtLH( &matView,&vEyePt, &vLookatPt , &vUpVec );
- g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
- D3DXMATRIXA16 matProj;
- D3DXMatrixPerspectiveFovLH( &matProj,D3DX_PI/4, 1.250f, 1.0f, 1000.0f);
- g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
- }
- //-----------------------------------------------------------------------------
- // Name: Render()
- // Desc: Draws the scene
- //-----------------------------------------------------------------------------
- VOID Render()
- {
- // Clear the backbuffer and the zbuffer
- g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
- D3DCOLOR_XRGB(250,250,250), 1.0f, 0 );
-
- // Begin the scene
- if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
- {
- // Setup the world, view, and projection matrices
- SetupMatrices();
- // Meshes are divided into subsets, one for each material. Render them in
- mesh->DrawSubset(0);
- // {
- g_pTerrain->Render();
- // }
- // End the scene
- g_pd3dDevice->EndScene();
- }
- // Present the backbuffer contents to the display
- g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
- }
- //-----------------------------------------------------------------------------
- // Name: MsgProc()
- // Desc: The window's message handler
- //-----------------------------------------------------------------------------
- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- // Current mouse position
- int iMouseX = (short)LOWORD(lParam);
- int iMouseY = (short)HIWORD(lParam);
- switch( msg )
- {
- case WM_DESTROY:
- Cleanup();
- PostQuitMessage( 0 );
- return 0;
-
- case WM_RBUTTONDOWN:
- case WM_LBUTTONDOWN:
- SetCapture( hWnd );
- OnBegin( iMouseX, iMouseY);//
- return TRUE;
- /* case WM_LBUTTONUP:
- ReleaseCapture();
- return TRUE;
- case WM_CAPTURECHANGED:
- if( (HWND)lParam != hWnd )
- {
- ReleaseCapture();
- }
- return TRUE;
- */
- case WM_MOUSEMOVE:
- /* if( MK_LBUTTON&wParam )
- {
- //D3DXMatrixRotationY( &g_matWorld, timeGetTime()/1000.0f );
- OnMove( iMouseX, iMouseY);
- // D3DXMatrixRotationZ( &g_matWorld, Angle);
- m_ptLastMouse.x = iMouseX;
- m_ptLastMouse.y = iMouseY;
-
- }
- */
- //ADD 摄象机的代码
- if( MK_RBUTTON&wParam )//case WM_RBUTTONDBLCLK:
- {
- // Compute the drag rectangle in screen coord.
- POINT ptCursor = { (short)LOWORD(lParam), (short)HIWORD(lParam) };
- // Update member var state
- if( ( msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK ))// && PtInRect( &g_Camera.m_rcDrag, ptCursor ) )
- // { g_Camera.m_bMouseRButtonDown = true; g_Camera.m_nCurrentButtonMask |= MOUSE_RIGHT_BUTTON; }
-
- // Capture the mouse, so if the mouse button is
- // released outside the window, we'll get the WM_LBUTTONUP message
- SetCapture(hWnd);
- // GetCursorPos( &g_Camera.m_ptLastMousePosition );
- /* g_Camera.GetInput(iMouseX, iMouseY );
- vLookatPt = g_Camera.m_vLookAt;
- m_ptLastMouse.x = iMouseX;
- m_ptLastMouse.y = iMouseY;
- */
- Z = (iMouseY-m_ptLastMouse.x)/10.0f;//
- X = (iMouseX-m_ptLastMouse.x)/10.0f;//
- //vEyePt = D3DXVECTOR3(0.0f, 3.0f,-5.0f+Z);
- //vLookatPt = D3DXVECTOR3( 0.0f+X, 0.0f, 0.0f+Z );//
- theCamera->yaw(Z) ;//
- // TheCamera.walk(X) ;
- D3DXMATRIX V;
- theCamera->getViewMatrix(&V);
- g_pd3dDevice->SetTransform(D3DTS_VIEW,&V);
-
- }
- return TRUE;
- case WM_RBUTTONUP:
- {
- ReleaseCapture();
- break;
- }
-
- }
- return DefWindowProc( hWnd, msg, wParam, lParam );
- }
- //-----------------------------------------------------------------------------
- // Name: WinMain()
- // Desc: The application's entry point
- //-----------------------------------------------------------------------------
- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
- {
- // Register the window class
- WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
- GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
- "Terrain8", NULL };
- RegisterClassEx( &wc );
- // Create the application's window
- HWND hWnd = CreateWindow( "Terrain8", "Terrain8",
- WS_OVERLAPPEDWINDOW, 0, 0, 800, 800,
- NULL, NULL, wc.hInstance, NULL );
- // Initialize Direct3D
- if( SUCCEEDED( InitD3D( hWnd ) ) )
- {
- // Show the window
- ShowWindow( hWnd, SW_SHOWDEFAULT );
- UpdateWindow( hWnd );
- // Enter the message loop
- MSG msg;
- ZeroMemory( &msg, sizeof(msg) );
- while( msg.message!=WM_QUIT )
- {
- if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- else
- Render();
- }
-
- }
- UnregisterClass( "Terrain8", wc.hInstance );
- return 0;
- }
复制代码 |
|