Rise
The Vieneo Province
Loading...
Searching...
No Matches
grid.cpp
Go to the documentation of this file.
1#include "grid.h"
2#include "Viewscreen.h"
3#include "CloudBaseGeometry.h"
4
5grid::grid(Viewscreen* prmViewscreen)
6{
7 viewscreen = prmViewscreen;
8
9 g_bAboveCloudsOld = false; // if we change this to TRUE then we won't have to do the client init one?
11}
12
13void grid::RemoveBuildZone(const short ordinal) const
14{
15 // call to release these in Buildings
17
18 //char msg[99];
19 //sprintf_s(msg, sizeof msg, "Adjusting remaining ordinals (%i)", ordinal);
20 //Trace(msg);
21
22 // adjust remaining ordinals
23 for (int ll = 0; ll < 64; ll++)
24 {
25 for (int mm = 0; mm < 64; mm++)
26 {
27 if (gridarray[ll][mm].buildzoneOrdinal > ordinal)
28 {
29 //sprintf_s(msg, sizeof msg, " gridarray %i,%i down from %i", ll, mm, gridarray[ll][mm].buildzoneOrdinal);
30 //Trace(msg);
32 }
33 }
34 }
35}
36
37void grid::Trace(const char* msg, Logger::Level severity) const
38{
39 char newmsg[999];
40 sprintf_s(newmsg, sizeof newmsg, "GRID: %s", msg);
41 viewscreen->logger->Log(newmsg, severity);
42}
43
44void grid::ClearGrid(short l, short m) const
45{
46 gridarray[l][m].position = centerC; // 4600
47 gridarray[l][m].cloudpos = centerC; // 4603
48 gridarray[l][m].emissive = 0UL; // 4542
49 gridarray[l][m].clouddif = 0UL; // 4587
50 gridarray[l][m].pri_landform = 0; // 4553
51 gridarray[l][m].pri_transition = 0; // 4559
52 gridarray[l][m].sec_landform = 0; // 4564
53 gridarray[l][m].sec_transition = 0; // 4569
54 gridarray[l][m].ter_landform = 0; // 4574
55 gridarray[l][m].raster = 0UL; // 4545
56 gridarray[l][m].elevation = 0; // 4581
57 gridarray[l][m].elevationAvailable = false;
59
60 if (gridarray[l][m].buildzoneOrdinal != -1)
61 {
62 char msg[99];
63 sprintf_s(msg, sizeof(msg), "HG2 released VB slot %i!", gridarray[l][m].buildzoneOrdinal);
64 Trace(msg);
65
66 RemoveBuildZone(gridarray[l][m].buildzoneOrdinal);
67
68 // unassign
69 gridarray[l][m].buildzoneOrdinal = -1;
70 }
71
72 gridarray[l][m].type = PlotType::NotZoned; // 4541
73 gridarray[l][m].zone = 0; // 4535
74 gridarray[l][m].layout1 = gridarray[l][m].layout2 = gridarray[l][m].layout3 = gridarray[l][m].layout4 = 0L; // 4536-4539
75 gridarray[l][m].powered = false; // 4540
76}
77
79{
80 Trace("ClearGrid");
81 viewscreen->logger->AddToCallStack("grid::ClearGrid");
82
83 geometryReady = false; // VB not set
84 dataReady = false; // Wait on stuff until main grid is received, reset if above clouds
85 patchGeometry = false;
86 dataPending = 0;
87 wholeGridPending = false;
88 wholeGridReceived = false;
89
91
92 for (short t = 0; t < 64; t++)
93 {
94 for (short s = 0; s < 64; s++)
95 {
96 ClearGrid(t, s);
97 }
98 }
99}
100
102{
104 if (cube == -1)
105 {
106 char msg[99];
107 sprintf_s(msg, sizeof(msg), "HG3 trapped a violation cube -1 for position x:%f y:%f z:%f", playerships[0].position.x, playerships[0].position.y, playerships[0].position.z);
108 Trace(msg, Logger::Fatal);
109 }
110
111 tcp = viewscreen->CalculateCGC(playerships[0].position, cube);
112 if (tcp.u == 30000)
113 {
114 char msg[99];
115 sprintf_s(msg, sizeof(msg), "HG3 trapped a violation tcp for position x:%f y:%f z:%f cube: %i", playerships[0].position.x, playerships[0].position.y, playerships[0].position.z, cube);
116 Trace(msg, Logger::Fatal);
117 }
118
119 char rotate;
120 tcp = viewscreen->CalculateWrap(tcp.u, tcp.v, cube, &rotate); // this grep found when he was out and about... left of NP goes to top of WP plate
121 if (tcp.u == 30000)
122 {
123 char msg[99];
124 sprintf_s(msg, sizeof(msg), "HG4 trapped a violation tcp for position x:%f y:%f z:%f cube: %i", playerships[0].position.x, playerships[0].position.y, playerships[0].position.z, cube);
125 Trace(msg, Logger::Fatal);
126 }
127
128 viewscreen->gameclass->bus->DebugApn.u = tcp.u + 3038;
129 viewscreen->gameclass->bus->DebugApn.v = tcp.v + 2038;
130}
131
132bool grid::TryElevation(char face, short u, short v, short& meters) const
133{
134 bool water;
135 return TryRadarSurface(face, u, v, meters, water);
136}
137
138bool grid::TryRadarSurface(char face, short u, short v, short& meters, bool& water) const
139{
140 if (!dataReady || face < 0 || face > 5) return false;
141 char rotation;
142 const auto at = viewscreen->CalculateWrap(u, v, face, &rotation);
143 if (at.u == 30000 || at.v == 30000) return false;
144 const auto key = [](VECTOR2SHORT p) { return uint32_t(uint16_t(p.u)) << 16 | uint16_t(p.v); };
146 {
147 elevationByPlot.clear();
148 for (const auto& column : gridarray) for (const auto& cell : column)
149 if (cell.elevationAvailable)
150 {
151 // Use supplied landforms, never MSL: Vieneo's seas have different
152 // surface elevations. Mixed coastal vertices are a water proxy.
153 const bool hasWater = cell.pri_landform == 1 ||
154 (cell.pri_transition != 0 && cell.pri_transition != 255 && cell.sec_landform == 1) ||
155 (cell.sec_transition != 0 && cell.sec_transition != 255 && cell.ter_landform == 1);
156 elevationByPlot[key(cell.tcp)] = {cell.elevation, hasWater};
157 }
159 }
160 const auto found = elevationByPlot.find(key(at));
161 if (found == elevationByPlot.end()) return false;
162 meters = found->second.meters;
163 water = found->second.water;
164 return true;
165}
166
167bool grid::InterpolateElevation(char face, double u, double v, float& meters) const
168{
169 float waterFraction;
170 return InterpolateRadarSurface(face, u, v, meters, waterFraction);
171}
172
173bool grid::InterpolateRadarSurface(char face, double u, double v, float& meters, float& waterFraction) const
174{
175 if (!std::isfinite(u) || !std::isfinite(v) || u < -32768 || u >= 32767 || v < -32768 || v >= 32767)
176 return false;
177 const int u0 = static_cast<int>(std::floor(u)), v0 = static_cast<int>(std::floor(v));
178 const double fu = u - u0, fv = v - v0;
179 double surface = 0, waterCoverage = 0;
180 for (int du = 0; du <= 1; ++du) for (int dv = 0; dv <= 1; ++dv)
181 {
182 const double weight = (du ? fu : 1 - fu) * (dv ? fv : 1 - fv);
183 if (weight == 0) continue;
184 short elevation;
185 bool water;
186 if (!TryRadarSurface(face, static_cast<short>(u0 + du), static_cast<short>(v0 + dv), elevation, water))
187 return false; // do not partially publish or blend missing terrain with sea level
188 surface += weight * elevation;
189 waterCoverage += weight * (water ? 1 : 0);
190 }
191 meters = static_cast<float>(surface);
192 waterFraction = static_cast<float>(waterCoverage);
193 return true;
194}
195
197{
198 const float peak = 9.0f;
199 const float sigma = 2.0f;
200 float delta = playerships[0].mslKm - peak;
201 return expf(-(delta * delta) / (2.0f * sigma * sigma));
202}
203
204#ifdef DumpGridToWld
205int grid::FindOrAddVertex(std::vector<struct RiseMath::Vector3>* unique_vertices, RiseMath::Vector3 position)
206{
207 for (UINT i = 0; i < unique_vertices->size(); i++)
208 {
209 if (fabsf(unique_vertices->at(i).x - position.x) <= FLT_EPSILON &&
210 fabsf(unique_vertices->at(i).y - position.y) <= FLT_EPSILON &&
211 fabsf(unique_vertices->at(i).z - position.z) <= FLT_EPSILON)
212 return i;
213 }
214 unique_vertices->emplace_back(position);
215 return static_cast<int>(unique_vertices->size() - 1);
216}
217#endif
218
219void grid::ReceiveWholeGrid(const SRenderGrid& datapacket)
220{
221 // Only one whole-grid request is in flight. Its face is not carried in the
222 // response, so keep the requested coordinates separate from the visible grid.
223 if (!wholeGridPending || datapacket.tcp.u != requestedTcp.u || datapacket.tcp.v != requestedTcp.v)
224 {
225 Trace("Ignoring whole grid that does not match the pending request");
226 return;
227 }
228
229 // Retire the old terrain/buildings only when the replacement has arrived.
230 // On initial load, retain any building layouts received before the terrain.
231 if (dataReady)
232 ClearGrid();
234 tcp = datapacket.tcp;
235 wholeGridPending = false;
237
238 char msg[99];
239 sprintf_s(msg, sizeof(msg), "Received SRenderGrid center at u%i v%i (%i)", datapacket.tcp.u, datapacket.tcp.v, cube);
240 Trace(msg);
241
242 for (short t = 0; t < 64; t++)
243 {
244 for (short s = 0; s < 64; s++)
245 {
246 gridarray[t][s] = viewscreen->CalculateSRV(t, s, datapacket.tcp, cube, datapacket.gridarray[t][s]);
247 }
248 }
249
250 // Need to calculate previous first since we use surrounding vertices
251 for (short t = 16; t < 48; t++) // can't do outside edges
252 {
253 for (short s = 16; s < 48; s++)
254 {
256 }
257 }
258
259 geometryReady = false; // need to create new VB
260 dataReady = true; // data is ready though
262 dataPending = 0;
263 wholeGridReceived = true;
264
265 oldtcp = datapacket.tcp;
266 oldcube = cube;
267
268 Trace(" grid::ReceiveWholeGrid DONE");
269}
270
272{
273 // A teleport invalidates ground/collision data even within the same cell.
274 // Keep this one-shot flag through clearing and any wait above the clouds.
275 ClearGrid();
277}
278
280{
281 Trace("RequestWholeGrid");
282 viewscreen->logger->AddToCallStack("grid::RequestWholeGrid");
283
285 return;
286
289 if (dataReady)
290 {
291 // Keep the CPU grid, vertex buffer, shader basis, cloud offsets and
292 // buildings describing the same terrain throughout the network wait.
293 cube = oldcube;
294 tcp = oldtcp;
295 }
296 else
297 ClearGrid();
298 wholeGridPending = true;
299 dataPending = 64;
300
301 SClientPacket outpacket; // NOLINT
302 outpacket.type = 16; // Terrain request
303 outpacket.f_x = requestedTcp.u;
304 outpacket.f_y = requestedTcp.v;
305 outpacket.f_z = requestedCube;
306 outpacket.f_w = 0.0f; // Whole grid
308}
309
311{
312 viewscreen->logger->AddToCallStack("grid::Process");
313
314 if (g_bAboveClouds)
315 {
317 {
318 g_bAboveCloudsOld = true;
319 ClearGrid();
320 }
321 return;
322 }
323
324 char msg[199];
325 HRESULT hr;
326
327 // Just came below cloud level ACL/BCL line
329 {
330 g_bAboveCloudsOld = false;
331
332 Trace("Process calling RWG coming out of the clouds...");
333
335
337 }
338
340 {
343 }
344
345 // Render the last complete grid without shifting or rebuilding it using
346 // coordinates from the next face while its response is still pending.
348 return;
349
350 short t, s;
351
352 bool keepgoing = true;
353 // Upload a newly received whole grid before following any further movement.
355 {
357
358 if (cube != oldcube) // cube face changed
359 {
360 sprintf_s(msg, sizeof(msg), "Process calling RWG changing gnomonic cube from %i to %i", oldcube, cube);
361 Trace(msg);
362
364
365 keepgoing = false;
366 }
367
368 // by data 32x32 is worth getting a whole new grid... but time and overhead-wise it is 8
369 if (keepgoing && (abs(tcp.u - oldtcp.u) >= 8 || abs(tcp.v - oldtcp.v) >= 8))
370 {
371 sprintf_s(msg, sizeof(msg), "Process calling RWG moving u%i and v%i", abs(tcp.u - oldtcp.u), abs(tcp.v - oldtcp.v));
372 Trace(msg);
373
375
376 keepgoing = false;
377 }
378
379 if (tcp.u != oldtcp.u && keepgoing)
380 {
381 SClientPacket outpacket = SClientPacket();
382
383 if (tcp.u > oldtcp.u)
384 {
385 // Eastbound movement, shift map and red out flatten clear release beyond data
386 sprintf_s(msg, sizeof(msg), "Shifted base on movement EAST,%i,%i (%i,%i)", tcp.u, tcp.v, oldtcp.u, oldtcp.v);
387 Trace(msg);
388
390 if (viewscreen->BCLoffsetu < 0)
391 viewscreen->BCLoffsetu += static_cast<short>(csC);
392
393 for (t = 0; t < 63; t++) // shift buildzones to the left
394 {
395 memcpy(gridarray[t], gridarray[t + 1], sizeof(LOCALGRID2) * 64); // Shift left
396 if (t > 15 && t < 48) // don't want to do t==47
397 memcpy(insidearray[t], insidearray[t + 1], 36 * 63);// 3*RiseMath::Vector3
398 }
399
400 for (s = 0; s < 64; s++) // blank out new column
401 {
402 ClearGrid(63, s);
403 }
404 for (s = 16; s < 48; s++) // east edge, s runs north to south
405 {
407 }
408
409 oldtcp.u++;
410 sprintf_s(msg, sizeof(msg), "Now 2,%i,%i", oldtcp.u, oldtcp.v);
411 Trace(msg);
412
413 outpacket.type = 16; // Terrain request
414 outpacket.f_x = oldtcp.u;
415 outpacket.f_y = oldtcp.v;
416 outpacket.f_z = 0.0f; // NULL
417 outpacket.f_w = 3.0f; // East
419
420 dataPending++;
421 }
422 else if (tcp.u < oldtcp.u)
423 {
424 // Westbound movement, shift map and red out flatten beyond data
425 sprintf_s(msg, sizeof(msg), "Shifted base on movement WEST %i,%i (%i,%i)", tcp.u, tcp.v, oldtcp.u, oldtcp.v);
426 Trace(msg);
427
429 if (viewscreen->BCLoffsetu >= (short)csC)
430 viewscreen->BCLoffsetu -= (short)csC;
431
432 for (t = 63; t > 0; t--) // was >-1
433 {
434 memcpy(gridarray[t], gridarray[t - 1], sizeof(LOCALGRID2) * 64); // Shift right
435 if (t > 16 && t < 48) // don't want to do t==16
436 memcpy(insidearray[t], insidearray[t - 1], 36 * 63);
437 }
438
439 for (s = 0; s < 64; s++)
440 {
441 ClearGrid(0, s);
442 }
443 for (s = 16; s < 48; s++) // west edge, s runs north to south
444 {
446 }
447
448 oldtcp.u--;
449 sprintf_s(msg, sizeof(msg), "Now 3,%i,%i", oldtcp.u, oldtcp.v);
450 Trace(msg);
451
452 outpacket.type = 16; // Terrain request
453 outpacket.f_x = oldtcp.u;
454 outpacket.f_y = oldtcp.v;
455 outpacket.f_z = 0.0f; // NULL
456 outpacket.f_w = 4.0f; // West
458
459 dataPending++;
460 }
461 else
462 {
463 sprintf_s(msg, sizeof(msg), "Unhandled case for tcp.u %i oldtcp.u %i", tcp.u, oldtcp.u);
464 Trace(msg);
465 }
466 keepgoing = false;
467 }
468
469 if (tcp.v != oldtcp.v && keepgoing)
470 {
471 SClientPacket outpacket = SClientPacket();
472
473 if (tcp.v < oldtcp.v)
474 {
475 sprintf_s(msg, sizeof(msg), "Shifted base on movement NORTH %i,%i (%i,%i)", tcp.u, tcp.v, oldtcp.u, oldtcp.v);
476 Trace(msg);
477
479 if (viewscreen->BCLoffsetv >= (short)csC)
480 viewscreen->BCLoffsetv -= (short)csC;
481
482 // Northbound movement
483 for (t = 0; t < 64; t++)
484 {
485 for (s = 63; s > 0; s--)
486 {
487 gridarray[t][s] = gridarray[t][s - 1]; // Shift down
488 if (s > 16 && s < 48 && t > 15 && t < 48) // don't do s==16
489 memcpy(insidearray[t][s], insidearray[t][s - 1], 36);
490 }
491 }
492 for (t = 0; t < 64; t++)
493 {
494 ClearGrid(t, 0);
495 }
496 for (t = 16; t < 48; t++) // north edge, t runs west to east
497 {
499 }
500
501 oldtcp.v--;
502 sprintf_s(msg, sizeof(msg), "Now 0,%i,%i", oldtcp.u, oldtcp.v);
503 Trace(msg);
504
505 outpacket.type = 16; // Terrain request
506 outpacket.f_x = oldtcp.u;
507 outpacket.f_y = oldtcp.v;
508 outpacket.f_z = 0.0f; // NULL
509 outpacket.f_w = 1.0f; // North
511
512 dataPending++;
513 }
514 else if (tcp.v > oldtcp.v)
515 {
516 sprintf_s(msg, sizeof(msg), "Shifted base on movement SOUTH,%i,%i (%i,%i)", tcp.u, tcp.v, oldtcp.u, oldtcp.v);
517 Trace(msg);
518
520 if (viewscreen->BCLoffsetv < 0)
521 viewscreen->BCLoffsetv += (short)csC;
522
523 // Southbound movement
524 for (t = 0; t < 64; t++)
525 {
526 for (s = 0; s < 63; s++)
527 {
528 gridarray[t][s] = gridarray[t][s + 1]; // Shift up
529 if (s > 15 && s < 47 && t > 15 && t < 48) // don't do s==47
530 memcpy(insidearray[t][s], insidearray[t][s + 1], 36);
531 }
532 }
533 for (t = 0; t < 64; t++)
534 {
535 ClearGrid(t, 63);
536 }
537 for (t = 16; t < 48; t++) // south edge, t runs west to east
538 {
540 }
541
542 oldtcp.v++;
543 sprintf_s(msg, sizeof(msg), "Now 1,%i,%i", oldtcp.u, oldtcp.v);
544 Trace(msg);
545
546 outpacket.type = 16; // Terrain request
547 outpacket.f_x = oldtcp.u;
548 outpacket.f_y = oldtcp.v;
549 outpacket.f_z = 0.0f; // NULL
550 outpacket.f_w = 2.0f; // South
552
553 dataPending++;
554 }
555 else
556 {
557 sprintf_s(msg, sizeof(msg), "Unhandled case for tcp.v %i oldtcp.v %i", tcp.v, oldtcp.v);
559 }
560 keepgoing = false;
561 }
562 }
563
564
565 // make sure it is still initialized and that the grid is now ready
566 if (dataReady && !geometryReady && keepgoing)
567 {
568 Trace("gridproc.cpp (NAF) rebuilding terrain matrix...");
569
571 V(viewscreen->m_avLocalGrid->Lock(0, 0, reinterpret_cast<void**>(&vtxg), 0));
572
574 // Vertices below subtract the rounded terpos. Drawing and ground-contact
575 // queries must restore that exact origin, not the unrounded player
576 // position: their difference can open centimeter-wide city/road seams.
577 viewscreen->terprecisionx = static_cast<double>(viewscreen->terpos.x);
578 viewscreen->terprecisiony = static_cast<double>(viewscreen->terpos.y);
579 viewscreen->terprecisionz = static_cast<double>(viewscreen->terpos.z);
580
581 int n = 0;
582
583#ifdef DumpGridToWld
584 // create wld file for visualization
585 std::vector<RiseMath::Vector3> uniqueVertices;
586 struct Spoly
587 {
588 int a, b, c, d;
589 DWORD color;
590 };
591
592 std::vector<Spoly> polygons;
593#endif
594
595 for (int r = 0; r < drawlistC; r++)
596 {
597 int t = drawlist[r].u;
598 int s = drawlist[r].v;
599
600 if (t < 0 || t >= 64 || s < 0 || s >= 64)
601 {
602 viewscreen->logger->Log("t and/or s in drawlist[r].u/v out-of-bounds", Logger::Level::Fatal);
603 }
604
605 // why are we storing ones we will never render? this gets regenerated each time we move
606 if (!visiblegrid[t][s])
607 {
608 drawlist[r].pri_landform = 255;
609 continue;
610 }
611
614 if (gridarray[t][s].UsesRts())
615 drawlist[r].sec_landform = 255; // flag for RTS
616 else
620
621 if (gridarray[t][s].type == PlotType::UserAirfield)
622 {
623 drawlist[r].runways = buildings::CheckLayout(98, gridarray[t][s]) ? 1 : 0;
625 }
626 else
628
629 drawlist[r].roads = gridarray[t][s].roads;
630
631 if (t + 1 >= 64 || s + 1 >= 64)
632 {
633 viewscreen->logger->Log("t+1 and/or s+1 in grid processing >= 64", Logger::Level::Fatal);
634 }
635
636 if (gridarray[t + 1][s].type == PlotType::DoNotRender ||
637 gridarray[t + 1][s + 1].type == PlotType::DoNotRender ||
638 gridarray[t][s + 1].type == PlotType::DoNotRender)
639 drawlist[r].pri_landform = 255; // don't draw if attached to someone that is invalid
640
642
643 // EVERYONE STARTS WITH TOP LEFT
644 vtxg->tu = 0; vtxg->tv = 0;
646#ifdef DumpGridToWld
647 if (drawlist[r].pri_landform != 255)
648 {
649 Spoly poly1;
650 poly1.a = FindOrAddVertex(&uniqueVertices, gridarray[t][s].position);
651 poly1.b = FindOrAddVertex(&uniqueVertices, gridarray[t + 1][s].position);
652 poly1.c = FindOrAddVertex(&uniqueVertices, gridarray[t + 1][s + 1].position);
653 poly1.d = FindOrAddVertex(&uniqueVertices, gridarray[t][s + 1].position);
654 if (t == 31 && s == 31)
655 poly1.color = Rendering::ColorARGB(0, t * 4, s * 4, 255);
656 else
657 poly1.color = Rendering::ColorARGB(0, t * 4, s * 4, 0);
658
659 polygons.emplace_back(poly1);
660 }
661#endif
662 vtxg->raster = gridarray[t][s].raster;
663 vtxg->emissive = gridarray[t][s].emissive;
665 vtxg++; n++;
666
667
668 RiseMath::Color tempcolor;
669 // 0-15 (16) ... 16-46 (31) ... 47-63 (17)
670 // For patch data (embedded in buffer now for optimizations
671 if (t > 15 && t < 47 && s > 15 && s < 47)
672 {
673 // Only the outer edge meets coarse triangles. Interior edges must use
674 // the same cached midpoint as the neighboring smooth quad.
675 const bool meld = t == 16 || t == 46 || s == 16 || s == 46;
676
677 // MID UPPER
678 vtxg->tu = 0.5f; vtxg->tv = 0;
679 if (s == 16)
680 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s].position * 0.5f - viewscreen->terpos;
681 else
682 vtxg->position = insidearray[t][s][0] - viewscreen->terpos;
683 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s].raster, 0.5f);
684 vtxg->raster = tempcolor;
685 vtxg->emissive = gridarray[t][s].emissive;
687 vtxg++; n++;
688
689 // LEFT MID
690 vtxg->tu = 0; vtxg->tv = 0.5f;
691 if (t == 16)
692 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t][s + 1].position * 0.5f - viewscreen->terpos;
693 else
694 vtxg->position = insidearray[t][s][1] - viewscreen->terpos;
695 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t][s + 1].raster, 0.5f);
696 vtxg->raster = tempcolor;
697 vtxg->emissive = gridarray[t][s].emissive;
699 vtxg++; n++;
700
701
702 // MID UPPER
703 vtxg->tu = 0.5f; vtxg->tv = 0;
704 if (s == 16)
705 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s].position * 0.5f - viewscreen->terpos;
706 else
707 vtxg->position = insidearray[t][s][0] - viewscreen->terpos;
708 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s].raster, 0.5f);
709 vtxg->raster = tempcolor;
710 vtxg->emissive = gridarray[t][s].emissive;
712 vtxg++; n++;
713
714 // CENTER
715 vtxg->tu = 0.5f; vtxg->tv = 0.5f;
716 if (meld)
717 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
718 else
719 vtxg->position = insidearray[t][s][2] - viewscreen->terpos;
720 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
721 vtxg->raster = tempcolor;
722 tempcolor.r = gridarray[t][s].emissive.r * 2.0f;
723 tempcolor.g = gridarray[t][s].emissive.g * 2.0f;
724 tempcolor.b = gridarray[t][s].emissive.b * 2.0f;
725 vtxg->emissive = tempcolor;
727 vtxg++; n++;
728
729 // LEFT MID
730 vtxg->tu = 0; vtxg->tv = 0.5f;
731 if (t == 16)
732 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t][s + 1].position * 0.5f - viewscreen->terpos;
733 else
734 vtxg->position = insidearray[t][s][1] - viewscreen->terpos;
735 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t][s + 1].raster, 0.5f);
736 vtxg->raster = tempcolor;
737 vtxg->emissive = gridarray[t][s].emissive;
739 vtxg++; n++;
740
741
742 // MID UPPER
743 vtxg->tu = 0.5f; vtxg->tv = 0;
744 if (s == 16)
745 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s].position * 0.5f - viewscreen->terpos;
746 else
747 vtxg->position = insidearray[t][s][0] - viewscreen->terpos;
748 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s].raster, 0.5f);
749 vtxg->raster = tempcolor;
750 vtxg->emissive = gridarray[t][s].emissive;
752 vtxg++; n++;
753
754 // UPPER RIGHT
755 vtxg->tu = 1; vtxg->tv = 0;
756 vtxg->position = gridarray[t + 1][s].position - viewscreen->terpos;
757 vtxg->raster = gridarray[t + 1][s].raster;
758 vtxg->emissive = gridarray[t + 1][s].emissive;
760 vtxg++; n++;
761
762 // CENTER
763 vtxg->tu = 0.5f; vtxg->tv = 0.5f;
764 if (meld)
765 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
766 else
767 vtxg->position = insidearray[t][s][2] - viewscreen->terpos;
768 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
769 vtxg->raster = tempcolor;
770 tempcolor.r = gridarray[t][s].emissive.r * 2.0f;
771 tempcolor.g = gridarray[t][s].emissive.g * 2.0f;
772 tempcolor.b = gridarray[t][s].emissive.b * 2.0f;
773 vtxg->emissive = tempcolor;
775 vtxg++; n++;
776
777 // UPPER RIGHT
778 vtxg->tu = 1; vtxg->tv = 0;
779 vtxg->position = gridarray[t + 1][s].position - viewscreen->terpos;
780 vtxg->raster = gridarray[t + 1][s].raster;
781 vtxg->emissive = gridarray[t + 1][s].emissive;
783 vtxg++; n++;
784
785 // RIGHT MID
786 vtxg->tu = 1; vtxg->tv = 0.5f;
787 if (t == 46)
788 vtxg->position = gridarray[t + 1][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
789 else
790 vtxg->position = insidearray[t + 1][s][1] - viewscreen->terpos;
791 RiseMath::ColorLerp(&tempcolor, &gridarray[t + 1][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
792 vtxg->raster = tempcolor;
793 vtxg->emissive = gridarray[t + 1][s].emissive;
795 vtxg++; n++;
796
797 // CENTER
798 vtxg->tu = 0.5f; vtxg->tv = 0.5f;
799 if (meld)
800 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
801 else
802 vtxg->position = insidearray[t][s][2] - viewscreen->terpos;
803 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
804 vtxg->raster = tempcolor;
805 tempcolor.r = gridarray[t][s].emissive.r * 2.0f;
806 tempcolor.g = gridarray[t][s].emissive.g * 2.0f;
807 tempcolor.b = gridarray[t][s].emissive.b * 2.0f;
808 vtxg->emissive = tempcolor;
810 vtxg++; n++;
811
812
813 // LEFT MID
814 vtxg->tu = 0; vtxg->tv = 0.5f;
815 if (t == 16)
816 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t][s + 1].position * 0.5f - viewscreen->terpos;
817 else
818 vtxg->position = insidearray[t][s][1] - viewscreen->terpos;
819 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t][s + 1].raster, 0.5f);
820 vtxg->raster = tempcolor;
821 vtxg->emissive = gridarray[t][s].emissive;
823 vtxg++; n++;
824
825 // CENTER
826 vtxg->tu = 0.5f; vtxg->tv = 0.5f;
827 if (meld)
828 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
829 else
830 vtxg->position = insidearray[t][s][2] - viewscreen->terpos;
831 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
832 vtxg->raster = tempcolor;
833 tempcolor.r = gridarray[t][s].emissive.r * 2.0f;
834 tempcolor.g = gridarray[t][s].emissive.g * 2.0f;
835 tempcolor.b = gridarray[t][s].emissive.b * 2.0f;
836 vtxg->emissive = tempcolor;
838 vtxg++; n++;
839
840 // LOWER LEFT
841 vtxg->tu = 0; vtxg->tv = 1;
842 vtxg->position = gridarray[t][s + 1].position - viewscreen->terpos;
843 vtxg->raster = gridarray[t][s + 1].raster;
844 vtxg->emissive = gridarray[t][s + 1].emissive;
846 vtxg++; n++;
847
848
849 // CENTER
850 vtxg->tu = 0.5f; vtxg->tv = 0.5f;
851 if (meld)
852 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
853 else
854 vtxg->position = insidearray[t][s][2] - viewscreen->terpos;
855 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
856 vtxg->raster = tempcolor;
857 tempcolor.r = gridarray[t][s].emissive.r * 2.0f;
858 tempcolor.g = gridarray[t][s].emissive.g * 2.0f;
859 tempcolor.b = gridarray[t][s].emissive.b * 2.0f;
860 vtxg->emissive = tempcolor;
862 vtxg++; n++;
863
864 // BOTTOM MID
865 vtxg->tu = 0.5f; vtxg->tv = 1;
866 if (s == 46)
867 vtxg->position = gridarray[t][s + 1].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
868 else
869 vtxg->position = insidearray[t][s + 1][0] - viewscreen->terpos;
870 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s + 1].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
871 vtxg->raster = tempcolor;
872 vtxg->emissive = gridarray[t][s + 1].emissive;
874 vtxg++; n++;
875
876 // LOWER LEFT
877 vtxg->tu = 0; vtxg->tv = 1;
878 vtxg->position = gridarray[t][s + 1].position - viewscreen->terpos;
879 vtxg->raster = gridarray[t][s + 1].raster;
880 vtxg->emissive = gridarray[t][s + 1].emissive;
882 vtxg++; n++;
883
884
885 // CENTER
886 vtxg->tu = 0.5f; vtxg->tv = 0.5f;
887 if (meld)
888 vtxg->position = gridarray[t][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
889 else
890 vtxg->position = insidearray[t][s][2] - viewscreen->terpos;
891 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
892 vtxg->raster = tempcolor;
893 tempcolor.r = gridarray[t][s].emissive.r * 2.0f;
894 tempcolor.g = gridarray[t][s].emissive.g * 2.0f;
895 tempcolor.b = gridarray[t][s].emissive.b * 2.0f;
896 vtxg->emissive = tempcolor;
898 vtxg++; n++;
899
900 // RIGHT MID
901 vtxg->tu = 1; vtxg->tv = 0.5f;
902 if (t == 46)
903 vtxg->position = gridarray[t + 1][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
904 else
905 vtxg->position = insidearray[t + 1][s][1] - viewscreen->terpos;
906 RiseMath::ColorLerp(&tempcolor, &gridarray[t + 1][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
907 vtxg->raster = tempcolor;
908 vtxg->emissive = gridarray[t + 1][s].emissive;
910 vtxg++; n++;
911
912 // BOTTOM MID
913 vtxg->tu = 0.5f; vtxg->tv = 1;
914 if (s == 46)
915 vtxg->position = gridarray[t][s + 1].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
916 else
917 vtxg->position = insidearray[t][s + 1][0] - viewscreen->terpos;
918 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s + 1].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
919 vtxg->raster = tempcolor;
920 vtxg->emissive = gridarray[t][s + 1].emissive;
922 vtxg++; n++;
923
924
925 // RIGHT MID
926 vtxg->tu = 1; vtxg->tv = 0.5f;
927 if (t == 46)
928 vtxg->position = gridarray[t + 1][s].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
929 else
930 vtxg->position = insidearray[t + 1][s][1] - viewscreen->terpos;
931 RiseMath::ColorLerp(&tempcolor, &gridarray[t + 1][s].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
932 vtxg->raster = tempcolor;
933 vtxg->emissive = gridarray[t + 1][s].emissive;
935 vtxg++; n++;
936
937 // LOWER RIGHT
938 vtxg->tu = 1; vtxg->tv = 1;
939 vtxg->position = gridarray[t + 1][s + 1].position - viewscreen->terpos;
940 vtxg->raster = gridarray[t + 1][s + 1].raster;
941 vtxg->emissive = gridarray[t + 1][s + 1].emissive;
943 vtxg++; n++;
944
945 // BOTTOM MID
946 vtxg->tu = 0.5f; vtxg->tv = 1;
947 if (s == 46)
948 vtxg->position = gridarray[t][s + 1].position * 0.5f + gridarray[t + 1][s + 1].position * 0.5f - viewscreen->terpos;
949 else
950 vtxg->position = insidearray[t][s + 1][0] - viewscreen->terpos;
951 RiseMath::ColorLerp(&tempcolor, &gridarray[t][s + 1].raster, &gridarray[t + 1][s + 1].raster, 0.5f);
952 vtxg->raster = tempcolor;
953 vtxg->emissive = gridarray[t][s + 1].emissive;
955 vtxg++; n++;
956 }
957 else // not in patch data
958 {
959 if (gridarray[t][s].buildzoneOrdinal != -1)
960 {
961 char msg[99];
962 sprintf_s(msg, sizeof(msg), "NiPD releasing building slot %i!", gridarray[t][s].buildzoneOrdinal);
963 Trace(msg);
964
965 RemoveBuildZone(gridarray[t][s].buildzoneOrdinal);
966
967 // unassign
968 gridarray[t][s].buildzoneOrdinal = -1;
969 }
970
971 // UPPER RIGHT
972 vtxg->tu = 1; vtxg->tv = 0;
973 vtxg->position = gridarray[t + 1][s].position - viewscreen->terpos;
974 vtxg->raster = gridarray[t + 1][s].raster;
975 vtxg->emissive = gridarray[t + 1][s].emissive;
977 vtxg++; n++;
978
979 // LOWER LEFT
980 vtxg->tu = 0; vtxg->tv = 1;
981 vtxg->position = gridarray[t][s + 1].position - viewscreen->terpos;
982 vtxg->raster = gridarray[t][s + 1].raster;
983 vtxg->emissive = gridarray[t][s + 1].emissive;
985 vtxg++; n++;
986
987
988 // UPPER RIGHT
989 vtxg->tu = 1; vtxg->tv = 0;
990 vtxg->position = gridarray[t + 1][s].position - viewscreen->terpos;
991 vtxg->raster = gridarray[t + 1][s].raster;
992 vtxg->emissive = gridarray[t + 1][s].emissive;
994 vtxg++; n++;
995
996 // LOWER RIGHT
997 vtxg->tu = 1; vtxg->tv = 1;
998 vtxg->position = gridarray[t + 1][s + 1].position - viewscreen->terpos;
999 vtxg->raster = gridarray[t + 1][s + 1].raster;
1000 vtxg->emissive = gridarray[t + 1][s + 1].emissive;
1001 drawlist[r].bounds.Include(vtxg->position);
1002 vtxg++; n++;
1003
1004 // LOWER LEFT
1005 vtxg->tu = 0; vtxg->tv = 1;
1006 vtxg->position = gridarray[t][s + 1].position - viewscreen->terpos;
1007 vtxg->raster = gridarray[t][s + 1].raster;
1008 vtxg->emissive = gridarray[t][s + 1].emissive;
1009 drawlist[r].bounds.Include(vtxg->position);
1010 vtxg++; n++;
1011 }
1012 }
1013
1014 sprintf_s(msg, sizeof(msg), "Total vertices in terrain vertex buffer: %i/%i", n, visiblevertC);
1015 Trace(msg);
1016 assert(n <= visiblevertC);
1017
1018#ifdef DumpGridToWld
1019
1020 // dump to wld
1021 sprintf_s(msg, sizeof(msg), "%04i%04i.wld", tcp.u + 3038, tcp.v + 2038);
1022 int wldfile = -1;
1023 _sopen_s(&wldfile, msg, _O_WRONLY | _O_CREAT | _O_TRUNC | O_TEXT, _SH_DENYNO, _S_IWRITE);
1024 sprintf_s(msg, sizeof(msg), " 2.23\n %i ,\n", uniqueVertices.size());
1025 _write(wldfile, msg, strlen(msg));
1026 float largestX = -FLT_MAX, largestY = -FLT_MAX, largestZ = -FLT_MAX;
1027 float smallestX = FLT_MAX, smallestY = FLT_MAX, smallestZ = FLT_MAX;
1028 for (int i = 0; i < uniqueVertices.size(); i++)
1029 {
1030 if (uniqueVertices.at(i).x > largestX)
1031 largestX = uniqueVertices.at(i).x;
1032 if (uniqueVertices.at(i).y > largestY)
1033 largestY = uniqueVertices.at(i).y;
1034 if (uniqueVertices.at(i).z > largestZ)
1035 largestZ = uniqueVertices.at(i).z;
1036 if (uniqueVertices.at(i).x < smallestX)
1037 smallestX = uniqueVertices.at(i).x;
1038 if (uniqueVertices.at(i).y < smallestY)
1039 smallestY = uniqueVertices.at(i).y;
1040 if (uniqueVertices.at(i).z < smallestZ)
1041 smallestZ = uniqueVertices.at(i).z;
1042 }
1043 float avgX = (largestX - smallestX) * 0.5f + smallestX;
1044 float avgY = (largestY - smallestY) * 0.5f + smallestY;
1045 float avgZ = (largestZ - smallestZ) * 0.5f + smallestZ;
1046 for (int i = 0; i < uniqueVertices.size(); i++)
1047 {
1048 sprintf_s(msg, sizeof(msg), "%+f ,%+f ,%+f ,\n", uniqueVertices.at(i).x - avgX, uniqueVertices.at(i).y - avgY, uniqueVertices.at(i).z - avgZ);
1049 _write(wldfile, msg, strlen(msg));
1050 } sprintf_s(msg, sizeof(msg), " %i ,\n 0.125000 ,0.500000 ,0 ,0 ,1 ,\n", polygons.size());
1051 _write(wldfile, msg, strlen(msg));
1052 for (int i = 0; i < polygons.size(); i++)
1053 {
1054 sprintf_s(msg, sizeof(msg), " 4 , %i , %i , %i , %i , %x ,0.347995 ,0.248828 ,0.027999 ,0.248828 ,0.027999 ,0.246328 ,0.347995 ,0.246328 ,-1.000000 , 0 ,\n",
1055 polygons.at(i).a, polygons.at(i).b, polygons.at(i).c, polygons.at(i).d, polygons.at(i).color);
1056 _write(wldfile, msg, strlen(msg));
1057 }
1058 sprintf_s(msg, sizeof(msg), "unused\nunused\nunused\n");
1059 _write(wldfile, msg, strlen(msg));
1060 _close(wldfile);
1061
1062#endif
1063
1064
1066
1067 makeBuildings = true;
1068 makeClouds = true;
1069 keepgoing = false;
1070
1071
1072 // calculate view vector over grid for optimization
1073 // commented out 6/17/2018 for https://jira.risetvp.com/view.php?id=918 because we need north vector above clouds too!
1074 //RiseMath::Vector3 leftside = gridarray[31][32].position - gridarray[31][30].position;
1075 //RiseMath::Vec3Normalize(&leftside, &leftside);
1076 //RiseMath::Vector3 rightside = gridarray[32][32].position - gridarray[32][30].position;
1077 //RiseMath::Vec3Normalize(&rightside, &rightside);
1078 //RiseMath::Vec3Add(&viewnorth, &leftside, &rightside);
1079 //RiseMath::Vec3Scale(&viewnorth, &viewnorth, -0.5f);
1080 //RiseMath::Vec3Normalize(&viewnorth, &viewnorth);
1081
1082
1083 Trace("gridproc.cpp (NAF) finished!");
1084
1085
1086 geometryReady = true;
1087 wholeGridReceived = false;
1088 patchGeometry = true;
1089 }
1090
1091
1092 if (dataReady && makeClouds && keepgoing)
1093 {
1094 sprintf_s(msg, sizeof(msg), "gridproc.cpp (NAC) started... BCLoffsetu: %i BCLoffsetv: %i", viewscreen->BCLoffsetu, viewscreen->BCLoffsetv);
1095 Trace(msg);
1096
1101
1102
1104 CloudVolume::GroundLighting cloudLighting;
1105 cloudLighting.cube=cube; cloudLighting.u=tcp.u; cloudLighting.v=tcp.v;
1106 RiseLocalgridVertex* vtxh;
1107
1108 V(viewscreen->m_avCloudGrid->Lock(0, 0, reinterpret_cast<void**>(&vtxh), 0));
1109 // Cycle through all the upper-left vertices (grid squares)
1110 for (t = 0; t < 64; t++)
1111 {
1112 for (s = 0; s < 64; s++)
1113 {
1114 vtxh->tu = static_cast<float>(t - 31 - viewscreen->BCLoffsetu) / csC;
1115 vtxh->tv = static_cast<float>(s - 31 - viewscreen->BCLoffsetv) / csC;
1117 cube, tcp.u + t - 31, tcp.v + s - 31, radiusC) - viewscreen->cloudpos;
1118
1119
1120 //sprintf_s(msg, sizeof(msg), "\tcloudpos t%i s%i tu%.1f tv%.1f x%.3f y%.3f z%.3f", t, s, vtxh->tu, vtxh->tv , vtxh->position.x, vtxh->position.y, vtxh->position.z);
1121 //Trace(msg);
1122
1123
1124 RiseMath::Color tempcolor;
1125 if (t > 0 && t < 63 && s > 0 && s < 63)
1126 {
1127 // i think this is the light from the ground that is reflecting on the cloud base
1128 tempcolor.r = gridarray[t][s].emissive.r + gridarray[t - 1][s - 1].emissive.r + gridarray[t][s - 1].emissive.r + gridarray[t + 1][s - 1].emissive.r + gridarray[t - 1][s].emissive.r + gridarray[t + 1][s].emissive.r + gridarray[t - 1][s + 1].emissive.r + gridarray[t][s + 1].emissive.r + gridarray[t + 1][s + 1].emissive.r;
1129 tempcolor.g = gridarray[t][s].emissive.g + gridarray[t - 1][s - 1].emissive.g + gridarray[t][s - 1].emissive.g + gridarray[t + 1][s - 1].emissive.g + gridarray[t - 1][s].emissive.g + gridarray[t + 1][s].emissive.g + gridarray[t - 1][s + 1].emissive.g + gridarray[t][s + 1].emissive.g + gridarray[t + 1][s + 1].emissive.g;
1130 tempcolor.b = gridarray[t][s].emissive.b + gridarray[t - 1][s - 1].emissive.b + gridarray[t][s - 1].emissive.b + gridarray[t + 1][s - 1].emissive.b + gridarray[t - 1][s].emissive.b + gridarray[t + 1][s].emissive.b + gridarray[t - 1][s + 1].emissive.b + gridarray[t][s + 1].emissive.b + gridarray[t + 1][s + 1].emissive.b;
1131 tempcolor.r /= 9.0f; tempcolor.g /= 9.0f; tempcolor.b /= 9.0f;
1132 tempcolor.a = 1.0f;
1133 }
1134 else
1135 {
1136 tempcolor = gridarray[t][s].emissive;
1137 }
1138
1139 // Publish city light separately from airglow for the volume renderer.
1140 cloudLighting.pixels[s*64+t]={tempcolor.r,tempcolor.g,tempcolor.b};
1141 // Keep source height aligned with the light's 3x3 smoothing.
1142 float lightWeight=0,weightedAltitude=0;
1143 const int blurRadius=(t>0 && t<63 && s>0 && s<63) ? 1 : 0;
1144 for(int dy=-blurRadius;dy<=blurRadius;++dy)for(int dx=-blurRadius;dx<=blurRadius;++dx)
1145 {
1146 const auto& source=gridarray[t+dx][s+dy];
1147 const float weight=source.emissive.r+source.emissive.g+source.emissive.b;
1148 lightWeight+=weight;weightedAltitude+=weight*float(source.elevation)*.001f;
1149 }
1150 cloudLighting.sourceAltitudeKm[s*64+t]=lightWeight>0
1151 ? weightedAltitude/lightWeight : float(gridarray[t][s].elevation)*.001f;
1152 // airglow
1153 const RiseMath::Color airglowBase = RiseMath::Color(0.25f, 0.75f, 0.5f, 1.0f) * 0.5f; // Blue-green chemiluminescence
1154 float intensity = AirglowGaussian();
1155 RiseMath::Color airglow = airglowBase * intensity;
1156 tempcolor.r = RiseMath::Max(tempcolor.r, airglow.r);
1157 tempcolor.g = RiseMath::Max(tempcolor.g, airglow.g);
1158 tempcolor.b = RiseMath::Max(tempcolor.b, airglow.b);
1159
1160 vtxh->emissive = tempcolor;
1161 vtxh->diffuse = gridarray[t][s].clouddif;
1162 vtxh++;
1163 }
1164 }
1167
1168 makeClouds = false;
1169 keepgoing = false;
1170
1171 Trace("gridproc.cpp (NAC) finished!");
1172 }
1173
1174
1175 if (dataReady && makeBuildings && keepgoing)
1176 {
1177 Trace("gridproc.cpp (NAB) started...!");
1178
1179 bool addedBuilding = false;
1180 for (t = 16; t < 47; t++) // not 48 because zone 2 and 3 look to right
1181 {
1182 for (s = 16; s < 47; s++)
1183 {
1184 // if apn has construction whose VB hasn't been created
1185 if (gridarray[t][s].type > PlotType::NotZoned &&
1186 gridarray[t][s].type < PlotType::PlotTypeEnum &&
1187 gridarray[t][s].buildzoneOrdinal == -1)
1188 {
1189 HRESULT hr;
1190 if (gridarray[t][s].IsUserColony())
1191 {
1192 // we may not have yet gotten the SRenderBuilding data so hold off until we do!
1193 if (gridarray[t][s].layout1 != 0L || gridarray[t][s].layout2 != 0L || gridarray[t][s].layout3 != 0L || gridarray[t][s].layout4 != 0L)
1194 {
1195 // if we got here the grid wasn't ready when we got case 16
1197 addedBuilding = true;
1198 }
1199 else
1200 {
1201 sprintf_s(msg, sizeof msg, "We have a building on the grid with a type %i (u%i v%i) but no zone or layout information yet ... waiting!", gridarray[t][s].type, t + tcp.u, s + tcp.v);
1202 Trace(msg, Logger::Warn);
1203 }
1204 }
1205 else
1206 {
1207 // government
1209 addedBuilding = true;
1210 }
1211 // lets offset the clouds by 1km and 1km so we can set the vertex
1212 // over the cities to be the specular highlight
1213 }
1214 if (addedBuilding)
1215 t = s = 48;
1216 }
1217 }
1218 if (addedBuilding)
1219 Trace("Added a building, leaving checkconstruct true!");
1220 else
1221 {
1222 makeBuildings = false;
1223 makeClouds = true;
1224 }
1225
1226 Trace("gridproc.cpp (NAB) finished!");
1227 }
1228}
1229
1230void grid::CalculateInsideArray(short t, short s) const
1231{
1232 if (gridarray[t][s].pri_landform == 1 || gridarray[t][s].sec_landform == 1 || gridarray[t][s].ter_landform == 1 || // water
1233 gridarray[t - 1][s].pri_landform == 1 || gridarray[t - 1][s].sec_landform == 1 || gridarray[t - 1][s].ter_landform == 1 || // water
1234 gridarray[t][s - 1].pri_landform == 1 || gridarray[t][s - 1].sec_landform == 1 || gridarray[t][s - 1].ter_landform == 1 || // water
1235
1236 gridarray[t - 1][s].pri_landform == 255 || gridarray[t][s - 1].pri_landform == 255 ||
1237 gridarray[t - 1][s - 1].pri_landform == 255 || // diagonal Qerp support can be in a cube-net void
1238
1239 // Deois is flat
1240 gridarray[t - 1][s].IsDeois() || gridarray[t][s - 1].IsDeois() || gridarray[t][s].IsDeois() ||
1241
1242 // User Airfields are flat
1243 gridarray[t][s].type == PlotType::UserAirfield || gridarray[t - 1][s].type == PlotType::UserAirfield || gridarray[t][s - 1].type == PlotType::UserAirfield ||
1244
1245 // Arcridge is flat
1246 gridarray[t][s].type == PlotType::Arcridge || gridarray[t - 1][s].type == PlotType::Arcridge || gridarray[t][s - 1].type == PlotType::Arcridge)
1247 {
1248 insidearray[t][s][0] = gridarray[t][s].position * 0.5f + gridarray[t + 1][s].position * 0.5f;
1249 insidearray[t][s][1] = gridarray[t][s].position * 0.5f + gridarray[t][s + 1].position * 0.5f;
1250 insidearray[t][s][2] = gridarray[t][s].position * 0.25f + gridarray[t + 1][s + 1].position * 0.25f +
1251 gridarray[t + 1][s].position * 0.25f + gridarray[t][s + 1].position * 0.25f;
1252 }
1253 else
1254 {
1256 // Upper left 1
1257 insidearray[t][s][0] = viewscreen->QuadraticVectorInterpolation(&gridarray[t - 1][s].position, &gridarray[t][s].position, &gridarray[t + 1][s].position, 1.5f);
1258 // Lower left 2
1259 insidearray[t][s][1] = viewscreen->QuadraticVectorInterpolation(&gridarray[t][s - 1].position, &gridarray[t][s].position, &gridarray[t][s + 1].position, 1.5f);
1260 // Lower right 3
1261 insidearray[t][s][2] = viewscreen->QuadraticVectorInterpolation(&gridarray[t - 1][s - 1].position, &gridarray[t][s].position, &gridarray[t + 1][s + 1].position, 1.5f);
1262 }
1263
1264 // A physical cube edge occurs twice around a cut in the flat grid. Its two
1265 // views have different Qerp support vertices, so use the shared endpoints.
1266 const auto onCubeEdge = [](const VECTOR2SHORT& tcp)
1267 {
1268 return tcp.u % 2038 == 0 || tcp.v % 2038 == 0;
1269 };
1270 if (onCubeEdge(gridarray[t][s].tcp))
1271 {
1272 if (onCubeEdge(gridarray[t + 1][s].tcp))
1273 insidearray[t][s][0] = gridarray[t][s].position * 0.5f + gridarray[t + 1][s].position * 0.5f;
1274 if (onCubeEdge(gridarray[t][s + 1].tcp))
1275 insidearray[t][s][1] = gridarray[t][s].position * 0.5f + gridarray[t][s + 1].position * 0.5f;
1276 }
1277}
#define V(expression)
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
@ ORDERING_TERRAIN
Definition ServerTypes.h:14
@ PlotTypeEnum
@ Arcridge
@ UserAirfield
@ DoNotRender
@ NotZoned
const RiseMath::Vector3 centerC
VECTOR2SHORT DebugApn
Definition Bus.h:550
void SetGroundLighting(const CloudVolume::GroundLighting &value)
std::unique_ptr< Bus > bus
Definition GameClass.h:108
std::unique_ptr< Networking > networking
Definition GameClass.h:103
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
void AddToCallStack(const char *msg)
Definition Logger.cpp:355
Level
Definition Logger.h:23
@ Warn
Definition Logger.h:26
@ Fatal
Definition Logger.h:29
void SendToServer(void *pData, DWORD dwSize, bool bGuaranteed, PacketOrdering order=ORDERING_NONE) const
HRESULT Lock(UINT offset, UINT bytes, void **output, DWORD flags)
double cloudprecisiony
Definition Viewscreen.h:371
CloudVolumeRenderer cloudVolume
Definition Viewscreen.h:396
Rendering::RenderBuffer * m_avLocalGrid
Definition Viewscreen.h:222
RiseMath::Vector3 QuadraticVectorInterpolation(RiseMath::Vector3 *vel0, RiseMath::Vector3 *vel1, RiseMath::Vector3 *vel2, float t) const
GameClass * gameclass
Definition Viewscreen.h:439
double cloudprecisionx
Definition Viewscreen.h:371
double terprecisionz
Definition Viewscreen.h:370
VECTOR2SHORT CalculateCGC(RiseMath::Vector3 position, char cube) const
short BCLoffsetv
Definition Viewscreen.h:419
VECTOR2SHORT CalculateWrap(short u, short v, char cube, char *rotate) const
short BCLoffsetu
Definition Viewscreen.h:419
std::unique_ptr< buildings > ptrBuildings
Definition Viewscreen.h:428
double cloudprecisionz
Definition Viewscreen.h:371
RiseMath::Vector3 cloudpos
Definition Viewscreen.h:392
LOCALGRID2 CalculateSRV(short t, short s, VECTOR2SHORT tcp, char cube, SRenderVertex sourcedata)
double terprecisionx
Definition Viewscreen.h:370
Rendering::RenderBuffer * m_avCloudGrid
Definition Viewscreen.h:220
RiseMath::Vector3 terpos
Definition Viewscreen.h:368
char CalculateCube(RiseMath::Vector3 position) const
double terprecisiony
Definition Viewscreen.h:370
Logger * logger
Definition Viewscreen.h:440
static bool CheckLayout(unsigned i, LOCALGRID2 localgrid2)
void RemoveBuildZone(size_t ordinal)
HRESULT Building(short t, short s)
bool geometryReady
Definition grid.h:37
void ClearGrid()
Definition grid.cpp:78
char requestedCube
Definition grid.h:20
VECTOR2SHORT requestedTcp
Definition grid.h:21
bool InterpolateElevation(char face, double u, double v, float &meters) const
Definition grid.cpp:167
grid(Viewscreen *ptrGame)
Definition grid.cpp:5
std::unordered_map< uint32_t, SurfacePoint > elevationByPlot
Definition grid.h:26
void ElevationChanged() const
Definition grid.h:55
bool g_bAboveCloudsOld
Definition grid.h:19
bool wholeGridPending
Definition grid.h:40
void ReceiveWholeGrid(const SRenderGrid &datapacket)
Definition grid.cpp:219
char cube
Definition grid.h:45
bool dataReady
Definition grid.h:36
void CalculateInsideArray(short t, short s) const
Definition grid.cpp:1230
float AirglowGaussian()
Definition grid.cpp:196
void CalculateCubeAndTcp()
Definition grid.cpp:101
void Process()
Definition grid.cpp:310
uint64_t elevationRevision
Definition grid.h:24
void Trace(const char *msg, Logger::Level severity=Logger::Level::Info) const
Definition grid.cpp:37
bool TryElevation(char face, short u, short v, short &meters) const
Definition grid.cpp:132
bool TryRadarSurface(char face, short u, short v, short &meters, bool &water) const
Definition grid.cpp:138
void RequestWholeGrid()
Definition grid.cpp:279
void RemoveBuildZone(short ordinal) const
Definition grid.cpp:13
char oldcube
Definition grid.h:17
void PrepareForReposition()
Definition grid.cpp:271
bool wholeGridReceived
Definition grid.h:22
bool makeClouds
Definition grid.h:43
VECTOR2SHORT oldtcp
Definition grid.h:18
bool patchGeometry
Definition grid.h:38
bool makeBuildings
Definition grid.h:42
Viewscreen * viewscreen
Definition grid.h:15
VECTOR2SHORT tcp
Definition grid.h:44
bool resetWeatherOnNextGrid
Definition grid.h:41
bool InterpolateRadarSurface(char face, double u, double v, float &meters, float &waterFraction) const
Definition grid.cpp:173
char dataPending
Definition grid.h:39
uint64_t elevationIndexRevision
Definition grid.h:24
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
bool visiblegrid[63][63]
Definition globals.cpp:101
RiseMath::Vector3 insidearray[63][63][3]
Definition globals.cpp:102
BATCHINDEX2 drawlist[drawlistC]
Definition globals.cpp:104
bool g_bAboveClouds
Definition globals.cpp:16
LOCALGRID2 gridarray[64][64]
Definition globals.cpp:100
#define drawlistC
Definition globals.h:65
#define radiusC
Definition globals.h:87
#define visiblevertC
Definition globals.h:66
#define csC
Definition globals.h:26
Vector Position(int cube, int u, int v, float radius)
constexpr PackedColor ColorARGB(unsigned a, unsigned r, unsigned g, unsigned b)
Color * ColorLerp(Color *out, const Color *a, const Color *b, float t)
Definition ClientMath.h:173
constexpr auto Max(A first, B second)
Definition ClientMath.h:16
unsigned char u
Definition globals.h:330
unsigned char pri_landform
Definition globals.h:334
unsigned char v
Definition globals.h:330
unsigned char sec_transition
Definition globals.h:334
unsigned char pri_transition
Definition globals.h:334
unsigned char roads
Definition globals.h:331
GeometryBounds bounds
Definition globals.h:328
unsigned char sec_landform
Definition globals.h:334
unsigned char runways
Definition globals.h:331
unsigned char runwayLights
Definition globals.h:332
unsigned char ter_landform
Definition globals.h:334
std::array< Point, 64 *64 > pixels
std::array< float, 64 *64 > sourceAltitudeKm
void Include(const Vector &p)
PlotType type
Definition globals.h:276
unsigned char sec_landform
Definition globals.h:280
RiseMath::Color raster
Definition globals.h:283
long layout3
Definition globals.h:287
RiseMath::Color clouddif
Definition globals.h:275
RiseMath::Vector3 cloudpos
Definition globals.h:273
unsigned char pri_landform
Definition globals.h:278
long layout4
Definition globals.h:287
long layout2
Definition globals.h:287
unsigned char zone
Definition globals.h:285
unsigned char sec_transition
Definition globals.h:281
short buildzoneOrdinal
Definition globals.h:286
long layout1
Definition globals.h:287
bool powered
Definition globals.h:292
RiseMath::Vector3 position
Definition globals.h:273
short elevation
Definition globals.h:288
unsigned char pri_transition
Definition globals.h:279
unsigned char ter_landform
Definition globals.h:282
RiseMath::Color emissive
Definition globals.h:274
bool elevationAvailable
Definition globals.h:291
unsigned char roads
Definition globals.h:285
RiseMath::Vector3 position
Definition globals.h:263
RiseMath::Vector3 position
Definition globals.h:256
unsigned char type
VECTOR2SHORT tcp
SRenderVertex gridarray[64][64]
RiseMath::Vector3 position
Definition globals.h:511
double precisionz
Definition globals.h:512
double precisiony
Definition globals.h:512
double precisionx
Definition globals.h:512