1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
void C_MYGE_Terrain::init(LPDIRECT3DDEVICE9 lpDevice, const char *lpHeightMap)
{
HRESULT hr;
int i, j, k;
this->m_lpD3DDevice = lpDevice;
LPDIRECT3DSURFACE9 lpSurface;
D3DXIMAGE_INFO ImgInfo;
D3DXGetImageInfoFromFile(lpHeightMap, &ImgInfo);
m_Width = ImgInfo.Width;
m_Height = ImgInfo.Height;
if(FAILED(hr = this->m_lpD3DDevice->CreateOffscreenPlainSurface(m_Width,
m_Height,
D3DFMT_L8,
D3DPOOL_SCRATCH,
&lpSurface, 0)))
{
this->writeDirectXError("m_lpD3DDevice->CreateOffscreenPlainSurface", hr);
MessageBox(NULL, "Fehler beim Anlegen der Oberfläche", "Fehler", MB_ICONERROR);
exit(0);
}
D3DXLoadSurfaceFromFile(lpSurface, 0, 0, lpHeightMap, 0, D3DX_DEFAULT, 0, 0);
int SizeVertices = m_Width * m_Height;
int SizeIndices = (m_Width-1)*(m_Height-1)*2*3;
if(FAILED(hr = this->m_lpD3DDevice->CreateVertexBuffer(SizeVertices*sizeof(TERRAIN_VERTEX), 0, D3DFVF_TERRAINVERTEX, D3DPOOL_DEFAULT, &m_lpVB, 0)))
{
this->writeDirectXError("m_lpD3DDevice->CreateVertexBuffer", hr);
MessageBox(NULL, "Fehler beim Anlegen des Vertexbuffers", "Fehler", MB_ICONERROR);
exit(0);
}
if(FAILED(hr = this->m_lpD3DDevice->CreateIndexBuffer(SizeIndices * 2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_lpIB, 0)))
{
this->writeDirectXError("m_lpD3DDevice->CreateIndexBuffer", hr);
MessageBox(NULL, "Fehler beim Anlegen des Indexbuffers", "Fehler", MB_ICONERROR);
exit(0);
}
D3DLOCKED_RECT LockedRect;
if(FAILED(hr = lpSurface->LockRect(&LockedRect, 0, 0)))
{
this->writeDirectXError("lpSurface->LockRect", hr);
MessageBox(NULL, "Fehler beim sperren der Oberfläche", "Fehler", MB_ICONERROR);
exit(0);
}
BYTE* lpHeights = static_cast<BYTE*>(LockedRect.pBits);
int nPitch = LockedRect.Pitch;
TERRAIN_VERTEX* vertices = new TERRAIN_VERTEX[SizeVertices];
for(i = 0; i < m_Height; i++)
{
for(j = 0; j < m_Width; j++)
{
vertices[i*m_Width+j].x = j;
vertices[i*m_Width+j].y = lpHeights[i*nPitch+j];
vertices[i*m_Width+j].z = (m_Height - i);
vertices[i*m_Width+j].Color = D3DCOLOR_XRGB(lpHeights[i*m_Width+j],
lpHeights[i*m_Width+j],
lpHeights[i*m_Width+j]);
}
short* Indices = new short[SizeIndices];
k = 0;
for(i = 0; i < m_Height-1; i++)
{
for(j=0; j < m_Width-1; j++)
{
Indices[k++] = m_Width * i + 1 + j;
Indices[k++] = m_Width * i +j;
Indices[k++] = m_Width * i + 1 + j + 1;
Indices[k++] = m_Width * i + j;
Indices[k++] = m_Width * i + j + 1;
Indices[k++] = m_Width * i + 1 + j + 1;
}
}
void* pVertices;
m_lpVB->Lock(0, SizeVertices*sizeof(TERRAIN_VERTEX), (void**)& pVertices, 0);
memcpy(pVertices, vertices, SizeVertices*sizeof(TERRAIN_VERTEX));
m_lpVB->Unlock();
void* pIndices;
m_lpIB->Lock(0, SizeIndices * sizeof(int), reinterpret_cast<void**>(&pIndices), 0);
memcpy(pIndices, Indices, SizeIndices*sizeof(short));
m_lpIB->Unlock();
delete[] vertices;
delete[] Indices;
lpSurface->Release();
}
}
|