-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.cpp
572 lines (468 loc) · 13.5 KB
/
face.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "face.h"
Face::Face()
{
hasHires = false;
created = false;
creating = false;
m_parent = 0;
}
Face::~Face()
{
}
bool Face::Initialize(Transform transform, int recursion, int faceDir, float distance,
ID3D11Device* device, ID3D11DeviceContext* context, XMFLOAT3 offset, float size,
int divide, Mapping* map, Mapping* hires, void* planet)
{
creating = true;
hasHires = false;
m_planet = planet;
m_transform = transform;
m_faceDir = faceDir;
m_distance = distance;
m_offset = offset;
m_size = size;
m_recursion = recursion;
m_divide = divide;
//m_temperature = temperature;
//m_temperature = 400;
//m_map = h_map;
//m_res = h_res;
float temperature = 400;
m_map = map;
lowres_map = map;
hires_map = hires;
if (hires_map->IsBuilt() && !hires_map->Cancelled() && hires_map->CurrentPlanet() == m_planet)
{
m_map = hires_map;
hasHires = true;
}
viewDist = distance;
//std::thread new_thread(&Face::MakeFace, this, device, context);
//new_thread.detach();
MakeFace(device, context);
return true;
}
void Face::MakeFace(ID3D11Device* device, ID3D11DeviceContext* context)
{
int divide = m_divide;
int recursion = m_recursion;
float size = m_size;
XMFLOAT3 offset = m_offset;
float distance = m_distance;
int faceDir = m_faceDir;
Transform transform = m_transform;
float temperature = 400;
bool smooth = true;
//create the face buffers
std::vector<ModelClass::VertexType> vertexList;
std::vector<unsigned long> indexList;
std::vector<int> modifyList;
std::vector<XMFLOAT3> posList;
std::list<Triangle> triList;
//create the initial face, front facing.
//number of squares per face- will end up being doubled
//int divide = 10;
//tesselate
float tes = 1.0f / ((float)divide / 2.0f);
tes *= size;
int pushed = 0;
XMFLOAT3 origin = offset;
//find the center
XMFLOAT3 center = origin;
center.x += size;
center.y += size;
center.z = -1.0f;
int extend = 2;
if (!smooth)
{
extend = 1;
}
int s = divide + (extend * 2);
//this just makes the face
for (int x = -extend; x < divide + extend; x++)
{
for (int y = -extend; y < divide + extend; y++)
{
ModelClass::VertexType temp;
//start at the 'origin'
temp.position = XMFLOAT3(x * tes, y * tes, -1.0f);
temp.position = AddFloat3(temp.position, origin);
//vertexList.push_back(temp);
//indexList.push_back(pushed);
//pushed++;
if ((pushed + 1) % (s) != 0 && floor((pushed + 1) / s) != s - 1 && smooth)
{
if (temp.position.x < 1.0f || temp.position.y < 1.0f)
{
triList.push_back(Triangle(pushed, pushed + 1, pushed + s));
triList.push_back(Triangle(pushed + 1, pushed + s + 1, pushed + s));
}
}
if (smooth)
{
//dealing with 'cracks'
if (x < 0 || y < 0 || x > divide || y > divide)
{
modifyList.push_back(pushed);
if (temp.position.x < -1.0f && temp.position.y < -1.0f
|| temp.position.x > 1.0f && temp.position.y < -1.0f
|| temp.position.x < -1.0f && temp.position.y > 1.0f
|| temp.position.x > 1.0f && temp.position.y > 1.0f)
{
temp.position.z += tes * 2;
temp.color = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f);
}
else
{
bool doThing = false;
float add = 0.0f;
if (temp.position.x < -1.0f)
{
add += abs(-1.0f - temp.position.x);
temp.position.x = -1.0f;
doThing = true;
}
if (temp.position.y < -1.0f)
{
add += abs(-1.0f - temp.position.y);
temp.position.y = -1.0f;
doThing = true;
}
if (temp.position.x > 1.0f)
{
add += abs(temp.position.x - 1.0f);
temp.position.x = 1.0f;
doThing = true;
}
if (temp.position.y > 1.0f)
{
add += abs(temp.position.y - 1.0f);
temp.position.y = 1.0f;
doThing = true;
}
if (doThing)
{
//add /= tes;
temp.position.z += add;
//temp.color = XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f);
}
}
}
vertexList.push_back(temp);
if (x >= -1 && y >= -1 && x < divide + 1 && y < divide + 1)
{
if ((pushed + 1) % (s) != 0 && floor((pushed + 1) / s) != s - 1)
{
indexList.push_back(pushed);
indexList.push_back(pushed + 1);
indexList.push_back(pushed + s);
indexList.push_back(pushed + 1);
indexList.push_back(pushed + s + 1);
indexList.push_back(pushed + s);
//triList.push_back(Triangle(pushed, pushed + 1, pushed + s));
//triList.push_back(Triangle(pushed + 1, pushed + s + 1, pushed + s));
}
}
pushed++;
}
else
{
vertexList.push_back(temp);
indexList.push_back(pushed);
pushed++;
//now go up
temp.position = XMFLOAT3(x * tes, y * tes + tes, -1.0f);
temp.position = AddFloat3(temp.position, origin);
vertexList.push_back(temp);
indexList.push_back(pushed);
pushed++;
//and bottom right
temp.position = XMFLOAT3(x * tes + tes, y * tes, -1.0f);
temp.position = AddFloat3(temp.position, origin);
vertexList.push_back(temp);
indexList.push_back(pushed);
pushed++;
//second triangle, start up
temp.position = XMFLOAT3(x * tes, y * tes + tes, -1.0f);
temp.position = AddFloat3(temp.position, origin);
vertexList.push_back(temp);
indexList.push_back(pushed);
pushed++;
//up right
temp.position = XMFLOAT3(x * tes + tes, y * tes + tes, -1.0f);
temp.position = AddFloat3(temp.position, origin);
vertexList.push_back(temp);
indexList.push_back(pushed);
pushed++;
//and bottom right
temp.position = XMFLOAT3(x * tes + tes, y * tes, -1.0f);
temp.position = AddFloat3(temp.position, origin);
vertexList.push_back(temp);
indexList.push_back(pushed);
pushed++;
}
}
}
//now rotate the entire face
XMMATRIX cubeRot = XMMatrixRotationY(((90.0f * (float)faceDir)) * 0.0174532925f);
if (faceDir > 3)
{
cubeRot = XMMatrixRotationX((90.0f + (180.0f * (float)(faceDir - 4))) * 0.0174532925f);
}
if (faceDir >= 0)
{
int h_res = m_map->GetHeightMapRes();
// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
float pitch = transform.rot.x * 0.0174532925f;
float yaw = transform.rot.y * 0.0174532925f;
float roll = transform.rot.z * 0.0174532925f;
// Create the rotation matrix from the yaw, pitch, and roll values.
XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
for (auto it = vertexList.begin(); it != vertexList.end(); it++)
{
XMFLOAT3 tempCoord = it->position;
//tempCoord = TakeFloat3(tempCoord, origin);
//reading from heightmap
//first, convert coordinates to the heightmap coordinates
//int h_res = m_map->GetHeightMapRes();
//int n_res = m_map->GetNormalMapRes();
float xCoordFloat = ((min(max(tempCoord.x, -1.0f), 1.0f) + 1.0f) / 2.0f) * (float)(h_res - 1);
float yCoordFloat = ((min(max(tempCoord.y, -1.0f), 1.0f) + 1.0f) / 2.0f) * (float)(h_res - 1);
float value = m_map->GetHeightMapValueFloat(faceDir, xCoordFloat, yCoordFloat);
//xCoordFloat = ((min(max(tempCoord.x, -1.0f), 1.0f) + 1.0f) / 2.0f) * (float)(n_res - 1);
//yCoordFloat = ((min(max(tempCoord.y, -1.0f), 1.0f) + 1.0f) / 2.0f) * (float)(n_res - 1);
//it->normal = m_map->GetNormalMapValue(faceDir, (int)xCoordFloat, (int)yCoordFloat);
XMFLOAT3 col = m_map->GetColorMapValue(faceDir, (int)xCoordFloat, (int)yCoordFloat);
it->color = XMFLOAT4(col.x, col.y, col.z, 0);
XMVECTOR tempPos = XMLoadFloat3(&tempCoord);
tempPos = XMVector3TransformCoord(tempPos, cubeRot);
XMStoreFloat3(&tempCoord, tempPos);
it->position = tempCoord;
tempCoord = origin;
origin = XMFLOAT3(-1.0f, -1.0f, 0.0f);
tempPos = XMLoadFloat3(&it->position);
tempPos = XMVector3TransformCoord(tempPos, rotationMatrix);
//normalize
tempPos = XMVector3Normalize(tempPos);
XMStoreFloat3(&it->position, tempPos);
it->position = MultFloat3(it->position, XMFLOAT3((float)value, (float)value, (float)value));
it->position = MultFloat3(it->position, transform.scale);
it->position = AddFloat3(it->position, transform.pos);
it->position = TakeFloat3(it->position, origin);
origin = tempCoord;
}
ModelClass::VertexType temp;
temp.position = center;
float xCoordFloat = ((min(max(center.x, -1.0f), 1.0f) + 1.0f) / 2.0f) * (float)(h_res - 1);
float yCoordFloat = ((min(max(center.y, -1.0f), 1.0f) + 1.0f) / 2.0f) * (float)(h_res - 1);
float value = m_map->GetHeightMapValueFloat(faceDir, xCoordFloat, yCoordFloat);
XMVECTOR tempPos = XMLoadFloat3(&temp.position);
tempPos = XMVector3TransformCoord(tempPos, cubeRot);
XMStoreFloat3(&temp.position, tempPos);
center = temp.position;
tempPos = XMLoadFloat3(¢er);
tempPos = XMVector3TransformCoord(tempPos, rotationMatrix);
//normalize
tempPos = XMVector3Normalize(tempPos);
XMStoreFloat3(¢er, tempPos);
center = MultFloat3(center, XMFLOAT3((float)value, (float)value, (float)value));
center = MultFloat3(center, transform.scale);
center = AddFloat3(center, transform.pos);
center = TakeFloat3(center, XMFLOAT3(-1.0f, -1.0f, 0.0f));
}
if (!smooth)
{
for (int i = 0; i < indexList.size(); i += 3)
{
XMFLOAT3 normal = ComputeNormal(vertexList[indexList[i]].position,
vertexList[indexList[i + 1]].position, vertexList[indexList[i + 2]].position);
vertexList[indexList[i]].normal = normal;
vertexList[indexList[i + 1]].normal = normal;
vertexList[indexList[i + 2]].normal = normal;
}
}
else
{
for each(Triangle tri in triList)
{
XMFLOAT3 normal = ComputeNormal(vertexList[tri.v1].position,
vertexList[tri.v2].position, vertexList[tri.v3].position);
vertexList[tri.v1].normal = normal;
vertexList[tri.v2].normal = normal;
vertexList[tri.v3].normal = normal;
}
}
//now the normals are calculated, the extended faces can be pushed under
for each(int vertex in modifyList)
{
vertexList[vertex].position = AddFloat3(vertexList[vertex].position, origin);
vertexList[vertex].position = TakeFloat3(vertexList[vertex].position, transform.pos);
vertexList[vertex].position = MultFloat3(vertexList[vertex].position, XMFLOAT3(0.9f, 0.9f, 0.9f));
vertexList[vertex].position = AddFloat3(vertexList[vertex].position, transform.pos);
vertexList[vertex].position = TakeFloat3(vertexList[vertex].position, origin);
//vertexList[vertex].color = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f);
}
TexParam temp;
temp.type = GenType::NONE;
m_model = new ModelClass();
m_model->Initialize(device, context, vertexList, indexList, "", temp);
m_pos = center;
origin = offset;
if (recursion)
{
for (int i = 0; i < 4; i++)
{
m_children[i] = new Face();
m_children[i]->SetParent(this);
}
}
m_map = lowres_map;
created = true;
}
void Face::CreateChildren(ID3D11Device* device, ID3D11DeviceContext* context)
{
if (!created) return;
XMFLOAT3 origin = m_offset;
if (m_recursion)
{
for (int i = 0; i < 4; i++)
{
if (!m_children[i]->Creating())
{
m_offset = origin;
m_offset.x += (i % 2) * m_size;
m_offset.y += (i > 1) * m_size;
m_children[i]->Initialize(m_transform, m_recursion - 1, m_faceDir, m_distance * 0.5f, device, context, m_offset, m_size * 0.5f, m_divide + 1,
m_map, hires_map, m_planet);
}
}
}
m_offset = origin;
}
std::list<ModelClass*> Face::GetModels(XMFLOAT3 camPos, ID3D11Device* device, ID3D11DeviceContext* context)
{
XMVECTOR mPos = XMLoadFloat3(&m_pos);
XMVECTOR cPos = XMLoadFloat3(&camPos);
float distance;
mPos -= cPos;
mPos = XMVector3Length(mPos);
XMStoreFloat(&distance, mPos);
std::list<ModelClass*> result;
if (!created) return result;
if (distance > viewDist || !m_recursion)
{
if(created)
{
result.push_back(m_model);
for (int i = 0; i < 4; i++)
{
if (m_children[i] != NULL)
{
m_children[i]->Shutdown();
//delete m_children[i];
}
}
}
}
else
{
CreateChildren(device, context);
bool returnSelf = false;
for (int i = 0; i < 4; i++)
{
if (m_children[i]->Created())
{
if (distance > m_children[i]->viewDist || !m_children[i]->m_recursion)
{
if (!m_children[i]->hasHires && hires_map->IsBuilt() && !hires_map->Cancelled() && hires_map->CurrentPlanet() == m_planet)
{
m_children[i]->Rebuild(device, context);
returnSelf = true;
}
}
}
else if (!m_children[i]->Created())
{
returnSelf = true;
}
}
if (returnSelf)
{
result.push_back(m_model);
}
else
{
for (int i = 0; i < 4; i++)
{
std::list<ModelClass*> tempList = m_children[i]->GetModels(camPos, device, context);
for each(ModelClass* model in tempList)
{
result.push_back(model);
}
}
}
}
return result;
}
int Face::GetIndexCount(float distance)
{
int result = 0;
if (distance > viewDist || m_children[0] == NULL)
{
result += m_model->GetIndexCount();
}
else
{
for (int i = 0; i < 4; i++)
{
result += m_children[i]->GetIndexCount(distance);
}
}
return result;
}
XMFLOAT3 Face::GetPos()
{
return m_pos;
}
bool Face::Shutdown()
{
for (int i = 0; i < 4; i++)
{
if (m_children[i])
{
if(!m_children[i]->Shutdown()) return false;
delete m_children[i];
m_children[i] = 0;
}
}
if (m_model)
{
m_model->Shutdown();
delete m_model;
m_model = 0;
}
created = false;
creating = false;
return true;
}
bool Face::Created()
{
return created;
}
bool Face::Creating()
{
return creating;
}
void Face::Rebuild(ID3D11Device* device, ID3D11DeviceContext* context)
{
Shutdown();
Initialize(m_transform, m_recursion, m_faceDir, m_distance, device, context, m_offset, m_size, m_divide, m_map, hires_map, m_planet);
}
bool Face::IsHires()
{
return hasHires;
}
void Face::SetParent(Face* set)
{
m_parent = set;
}