|
|
#include<d3dx8.h>
#include<d3d8.h>
#include<vector>
#include<fstream>
#pragma comment(lib,"d3dx8.lib")
#pragma comment(lib,"d3d8.lib")
#pragma comment(lib,"Winmm.lib")
LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Buffer to hold vertices
LPDIRECT3DINDEXBUFFER8 _ib;
IDirect3DTexture8* _tex;
IDirect3DVertexBuffer8* _vb;
std::vector<int> _heightmap;
bool readRawFile();
bool InitIndexBuffer();
struct CUSTOMVERTEX
{
float x,y,z; //位置坐标
// float nx,ny,nz; //法线坐标
float diffuse; //颜色
float u,v; //纹理坐标
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
CUSTOMVERTEX m_Vertex[4225];
WORD m_Index[64*64*6];
int m_Length=320;
int m_Block=10;
HRESULT InitialiseD3D(HWND hWnd)
{
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
if(g_pD3D == NULL)
{
return E_FAIL;
}
//Get the current display mode
D3DDISPLAYMODE d3ddm;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
{
return E_FAIL;
}
//Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
d3dpp.BackBufferFormat = d3ddm.Format;
//Create a Direct3D device.
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp,
&g_pD3DDevice)))
{
return E_FAIL;
}
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
if(!readRawFile())
::MessageBox(NULL,NULL,NULL,NULL);
if(FAILED(D3DXCreateTextureFromFile(g_pD3DDevice,"desert.bmp",&_tex)))
MessageBox(NULL,"loadFilefailed","Information",MB_OK);
return S_OK;
}
//读高度图
bool readRawFile()
{
std::vector<BYTE> in(64*64);
std::ifstream inFile("coastMountain64.raw");
if(inFile == 0)
return false;
inFile.read((char*)&in[0],in.size());
inFile.close();
_heightmap.resize(64*64);
for(int i=0;i<in.size();i++)
_heightmap=in;
return true;
}
HRESULT InitialiseVertexBuffer()
{
if(FAILED(g_pD3DDevice->CreateVertexBuffer(2*sizeof(m_Vertex),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,&_vb)))
{
return E_FAIL;
}
_vb->Lock(0,sizeof(m_Vertex),(BYTE**)&m_Vertex,0);
for(int j=0;j<65;j++)
{
for(int i=0;i<65;i++)
{
m_Vertex[j*65+i].x =-m_Length*0.5f+m_Block*i;
m_Vertex[j*65+i].y = (float)_heightmap [j*65+i];
m_Vertex[j*65+i].z = m_Length*0.5f-m_Block*j;
m_Vertex[j*65+i].diffuse = 0xff00ffff;
m_Vertex[j*65+i].u = 0;
m_Vertex[j*65+i].v = 1;
}
}
_vb->Unlock();
return S_OK;
}
bool InitIndexBuffer()
{
HRESULT hr=0;
hr = g_pD3DDevice->CreateIndexBuffer(sizeof(m_Index),
0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&_ib);
if(FAILED(hr))
return false;
int _numVertsPerRow = 64;
WORD * indices = 0;
_ib->Lock(0,0,(BYTE**)&indices,0);
for(int i=0;i<64;i++)
{
for(int j=0;j<64;j++)
{
m_Index[j*64*6+i*6+0]=(WORD)(j*65+i);
m_Index[j*64*6+i*6+1]=(WORD)(j*65+i+65);
m_Index[j*64*6+i*6+2]=(WORD)(j*65+i+65+1);
m_Index[j*64*6+i*6+3]=(WORD)(j*65+i);
m_Index[j*64*6+i*6+4]=(WORD)(j*65+i+1);
m_Index[j*64*6+i*6+5]=(WORD)(j*65+i+65+1);
}
}
_ib->Unlock();
return true;
}
void SetupWorldCamera()
{
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
g_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
}
void SetupCamera()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 200.0f,-120.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}
void SetupPerspective()
{
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/3, 1.0f, 100.0f, 100000.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
g_pD3DDevice->BeginScene();
g_pD3DDevice->SetStreamSource(0, _vb,sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
g_pD3DDevice->SetIndices(_ib,0);
g_pD3DDevice->SetTexture(0,_tex);
g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,64*64,0,64*64*2);
g_pD3DDevice->EndScene();
g_pD3DDevice-> resent(NULL, NULL, NULL, NULL);
}
void CleanUp()
{
SafeRelease(g_pVertexBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}
void GameLoop()
{
MSG msg;
BOOL fMessage;
PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
if(fMessage)
{
//Process message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//No message to process, so render the current scene
Render();
}
}
}
//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
switch (wParam)
{
case VK_ESCAPE:
//User has pressed the escape key, so quit
DestroyWindow(hWnd);
return 0;
break;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
//Register the window class
WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"DX Project 3", NULL};
RegisterClassEx(&wc);
//Create the application's window
HWND hWnd = CreateWindow("DX Project 3", "www.andypike.com: Tutorial 3",
WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
GetDesktopWindow(), NULL, wc.hInstance, NULL);
//Initialize Direct3D
if(SUCCEEDED(InitialiseD3D(hWnd)))
{
//Show our window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
//Initialize Vertex Buffer
if(SUCCEEDED(InitialiseVertexBuffer()))
{
InitIndexBuffer();
SetupCamera();
SetupPerspective();
SetupWorldCamera();
//Start game running: Enter the game loop
GameLoop();
}
}
CleanUp();
UnregisterClass("DX Project 3", wc.hInstance);
return 0;
}
程序没错,但是看不到画面,不知道错那了,假如出现这样的问题如何去解决?
我刚学,希望高手指教 |
|