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
|
void Terrain::RenderPatch(GraphicsDevice* device, Camera *camera,
float offsetHeightX, float offsetHeightY, float scaleHeight,
float offsetPositionX, float offsetPositionY, float scalePosition,
int level)
{
float centerX = offsetPositionX + _patchSize * scalePosition * 0.5f;
float centerZ = offsetPositionY - _patchSize * scalePosition * 0.5f;
XMFLOAT3 patchCenter(centerX, 0.0f, centerZ);
float triangleSize = scalePosition;
float distance;
XMStoreFloat(&distance, XMVector3Length(XMLoadFloat3(&patchCenter) - XMLoadFloat3(&camera->GetPosition())));
float distanceToSizeQuotient = distance / triangleSize;
if (distanceToSizeQuotient > _lodParameter || level < 1)
{
_terrainEffect->SetTerrainPatchData(XMFLOAT2(offsetHeightX, offsetHeightY), scaleHeight, XMFLOAT2(offsetPositionX, offsetPositionY), scalePosition);
_terrainEffect->Apply(device->GetImmediateContext());
device->DrawIndexed(_indexCount);
}
else
{
float halfHeightMapSize = scaleHeight * 0.5f;
float halfScalePosition = scalePosition * 0.5f;
RenderPatch(device, camera, offsetHeightX, offsetHeightY, halfHeightMapSize,
offsetPositionX, offsetPositionY, halfScalePosition, level-1);
RenderPatch(device, camera, offsetHeightX+halfHeightMapSize, offsetHeightY, halfHeightMapSize,
centerX, offsetPositionY, halfScalePosition, level-1);
RenderPatch(device, camera, offsetHeightX, offsetHeightY+halfHeightMapSize, halfHeightMapSize,
offsetPositionX, centerZ, halfScalePosition, level-1);
RenderPatch(device, camera, offsetHeightX+halfHeightMapSize, offsetHeightY+halfHeightMapSize, halfHeightMapSize,
centerX, centerZ, halfScalePosition, level-1);
}
}
|