-
Notifications
You must be signed in to change notification settings - Fork 1
/
landscape.cpp
119 lines (96 loc) · 3.06 KB
/
landscape.cpp
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
112
113
114
115
116
117
118
119
/*
* LoD terrain generation and displaying
* Svoboda Petr, Vojvoda Jakub
*
* 2014
*
*/
#include "pgr.h"
#include "patch.h"
#include "landscape.h"
int Landscape::m_NextTriNode;
TriTreeNode Landscape::m_TriPool[POOL_SIZE];
TriTreeNode *Landscape::AllocateTri()
{
TriTreeNode *pTri;
if ( m_NextTriNode >= POOL_SIZE )
return NULL;
pTri = &(m_TriPool[m_NextTriNode++]);
pTri->LeftChild = pTri->RightChild = NULL;
return pTri;
}
void Landscape::Init(float **hMap)
{
if ( hMap == NULL ) {
std::cerr<<"Landscape Init NULL pointer"<<std::endl;
}
Patch *patch;
heightMap = new float*[MAP_SIZE+1];
for ( int i=0;i < MAP_SIZE+1;i++) {
heightMap[i] = new float[MAP_SIZE+1];
}
for ( int i=0; i < MAP_SIZE ; i++) {
for ( int j = 0;j < MAP_SIZE ;j++) {
heightMap[i][j] = hMap[i][j];
}
heightMap[i][MAP_SIZE] = hMap[i][MAP_SIZE -1 ];
heightMap[MAP_SIZE][i] = hMap[MAP_SIZE-1][i];
}
for ( int Y=0; Y < NUM_PATCHES_PER_SIDE; Y++) {
for ( int X=0; X < NUM_PATCHES_PER_SIDE; X++ ) {
patch = &(m_Patches[Y][X]);
patch->Init(X*(PATCH_SIZE), Y*(PATCH_SIZE), heightMap );
sorted_distance.push_back(patch);
}
}
}
void Landscape::Reset()
{
Patch *patch;
SetNextTriNode(0);
for ( int Y=0; Y < NUM_PATCHES_PER_SIDE; Y++ ) {
for ( int X=0; X < NUM_PATCHES_PER_SIDE; X++) {
patch = &(m_Patches[Y][X]);
patch->Reset();
if ( X > 0 )
patch->GetBaseLeft()->LeftNeighbor = m_Patches[Y][X-1].GetBaseRight();
else
patch->GetBaseLeft()->LeftNeighbor = NULL;
if ( X < (NUM_PATCHES_PER_SIDE-1) )
patch->GetBaseRight()->LeftNeighbor = m_Patches[Y][X+1].GetBaseLeft();
else
patch->GetBaseRight()->LeftNeighbor = NULL;
if ( Y > 0 )
patch->GetBaseLeft()->RightNeighbor = m_Patches[Y-1][X].GetBaseRight();
else
patch->GetBaseLeft()->RightNeighbor = NULL;
if ( Y < (NUM_PATCHES_PER_SIDE-1) )
patch->GetBaseRight()->RightNeighbor = m_Patches[Y+1][X].GetBaseLeft();
else
patch->GetBaseRight()->RightNeighbor = NULL;
}
}
}
void Landscape::updateDistance()
{
for (std::vector<Patch*>::iterator it=sorted_distance.begin(); it!=sorted_distance.end(); ++it) {
(*it)->distance = glm::distance(glm::vec2(camera.z,camera.x),(*it)->getPosition());
}
}
void Landscape::splitBasedOnDistance()
{
int i = 0;
int deep = 12;
std::sort( sorted_distance.begin(),sorted_distance.end(), Patch::porovnani );
for (std::vector<Patch*>::iterator it=sorted_distance.begin(); it!=sorted_distance.end(); ++it) {
i++;
if ( i == 5) { deep--; i=0; }
(*it)->SplitPath(deep);
}
}
void Landscape::renderBasedOnDistance()
{
for (std::vector<Patch*>::iterator it=sorted_distance.begin(); it!=sorted_distance.end(); ++it) {
(*it)->Render();
}
}