-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet.cpp
187 lines (142 loc) · 3.66 KB
/
planet.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "planet.h"
Planet::Planet()
{
h_built = false;
hires = false;
}
Planet::~Planet()
{
}
void Planet::Initialize(XMFLOAT3 position, float size, XMFLOAT3 perlin, XMFLOAT3 mapPerlin, XMFLOAT4 sky, ID3D11Device* device, ID3D11DeviceContext* context,
StarParam star, Mapping* hires_map, float waterHeight, float flatten, float temperature)
{
h_built = false;
m_map = new Mapping();
hires = false;
m_perlin = perlin;
m_mapPerlin = mapPerlin;
m_sky = sky;
m_size = size;
m_pos = position;
m_waterHeight = waterHeight;
m_flatten = flatten;
std::thread new_thread(&Planet::Setup, this, position, size, perlin, mapPerlin, sky, device, context, star, hires_map, waterHeight, flatten, temperature);
new_thread.detach();
//new_thread.join();
}
void Planet::Setup(XMFLOAT3 position, float size, XMFLOAT3 perlin, XMFLOAT3 mapPerlin, XMFLOAT4 sky, ID3D11Device* device, ID3D11DeviceContext* context,
StarParam star, Mapping* hires_map, float waterHeight, float flatten, float temperature)
{
//calculation for the average surface temperature
XMVECTOR _pos = XMLoadFloat3(&position);
XMVECTOR _spos = XMLoadFloat3(&star.pos);
float p_distance;
_pos -= _spos;
_pos = XMVector3Length(_pos);
XMStoreFloat(&p_distance, _pos);
p_distance *= 200000.0f;
float albedo = 0.0f;
m_temp = star.temperature * powf(1.0f - albedo, 0.25f) * powf(star.radius / (2.0f * p_distance), 0.5f);
if (temperature != -1) m_temp = temperature;
m_map->CreateMaps(256, m_temp, perlin, mapPerlin, waterHeight, flatten);
Face::Transform tempTransform;
tempTransform = Face::Transform(position, XMFLOAT3(size, size, size), XMFLOAT3(0.0f, 0.0f, 0.0f), perlin);
for (int i = 0; i < 6; i++)
{
m_faces[i] = new Face();
m_faces[i]->Initialize(tempTransform, 6, i, (size * 5.0f), device, context, XMFLOAT3(-1.0f, -1.0f, 0.0f), 1.0f, 8, m_map, hires_map, this);
}
h_built = true;
}
std::list<ModelClass*> Planet::GetModels(XMFLOAT3 camPos, ID3D11Device* device, ID3D11DeviceContext* context, Mapping* hires_map)
{
std::list<ModelClass*> result;
if (!h_built) return result;
for (int i = 0; i < 6; i++)
{
std::list<ModelClass*> tempList = m_faces[i]->GetModels(camPos, device, context);
for each(ModelClass* model in tempList)
{
result.push_back(model);
}
}
return result;
}
bool Planet::Shutdown()
{
//first, make sure the heightmaps aren't currently generating or anything goofy
if (!h_built) return false;
h_built = false;
for (int i = 0; i < 6; i++)
{
if(!m_faces[i]->Shutdown()) return false;
delete m_faces[i];
}
if(!m_map->Shutdown()) return false;
delete m_map;
return true;
}
bool Planet::DrawSky(XMFLOAT3 camPos)
{
if (!h_built) return false;
XMVECTOR mPos = XMLoadFloat3(&m_pos);
XMVECTOR cPos = XMLoadFloat3(&camPos);
float distance;
mPos -= cPos;
mPos = XMVector3Length(mPos);
XMStoreFloat(&distance, mPos);
tempDistance = distance;
if (distance < m_size * 2.0f)
{
return true;
}
return false;
}
XMFLOAT4 Planet::GetSky()
{
if (!h_built) return XMFLOAT4(0, 0, 0, 0);
XMFLOAT4 result = m_sky;
float scalar = (m_size * 1 - tempDistance) / (m_size * 1);
result.x *= scalar;
result.y *= scalar;
result.z *= scalar;
return result;
}
XMFLOAT4 Planet::GetUnscaledSky()
{
return m_sky;
}
XMFLOAT3 Planet::GetPosition()
{
if (!h_built) return XMFLOAT3(0, 0, 0);
return m_pos;
}
float Planet::GetSize()
{
if (!h_built) return 0.0f;
return m_size;
}
float Planet::GetTemperature()
{
return m_temp;
}
float Planet::GetWaterHeight()
{
return m_waterHeight;
}
XMFLOAT3 Planet::GetPerlin()
{
return m_perlin;
}
XMFLOAT3 Planet::GetMapPerlin()
{
return m_mapPerlin;
}
bool Planet::Built()
{
return h_built;
}
float Planet::GetFlat()
{
return m_flatten;
}