Rise
The Vieneo Province
Loading...
Searching...
No Matches
AssayMapProjection.h
Go to the documentation of this file.
1#pragma once
2
4
6{
7 inline int PacketFace(unsigned char tag)
8 {
9 return tag >= 0xA0 && tag < 0xA6 ? tag - 0xA0 : -1;
10 }
11
12 // The packet's cells use the same native-cell ownership as server TerrainQuad.
13 // Fold a whole cell onto its owning face before projecting its four corners.
14 // Never subtract TCPs from different cube faces or approximate plots as 2 km squares.
15 inline bool CellCorners(int u, int v, int face, RiseMath::Vector3 (&corners)[4])
16 {
17 if (face < 0 || face >= 6) return false;
18 static const RiseMath::Vector3 normal[] = {{0,-1,0},{1,0,0},{-1,0,0},{0,1,0},{0,0,-1},{0,0,1}};
19 static const RiseMath::Vector3 right[] = {{1,0,0},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{1,0,0}};
20 static const RiseMath::Vector3 down[] = {{0,0,1},{0,0,1},{0,0,1},{0,0,1},{0,-1,0},{0,1,0}};
21 static const int originU[] = {0,2038,-2038,4076,0,0};
22 static const int originV[] = {0,0,0,0,-2038,2038};
23 const int a = 2 * (u - originU[face]) + 1 - 2038;
24 const int b = 2 * (v - originV[face]) + 1 - 2038;
25 const bool crossU = std::abs(a) > 2038, crossV = std::abs(b) > 2038;
26 if ((crossU && crossV) || std::abs(a) >= 6114 || std::abs(b) >= 6114) return false;
27 const float sign = (crossU ? a : b) < 0 ? -1.f : 1.f;
28 const int du[] = {0,1,1,0}, dv[] = {0,0,1,1};
29 for (int i = 0; i < 4; ++i)
30 {
31 const float x = float(u + du[i] - originU[face] - 1019);
32 const float y = float(v + dv[i] - originV[face] - 1019);
33 auto p = crossU ? right[face] * (sign * 1019.f) + normal[face] * (2038.f - std::fabs(x)) + down[face] * y
34 : crossV ? down[face] * (sign * 1019.f) + normal[face] * (2038.f - std::fabs(y)) + right[face] * x
35 : normal[face] * 1019.f + right[face] * x + down[face] * y;
37 corners[i] = -p; // TerrainMapProjection accepts render coordinates.
38 }
39 return true;
40 }
41
42 inline bool CellTransform(int u, int v, int face, float radius,
43 const RiseMath::Matrix& mapView, float pixelsPerKmX, float pixelsPerKmY,
44 RiseMath::Vector2 center, RiseMath::Matrix& transform)
45 {
46 RiseMath::Vector3 surface[4];
47 if (!CellCorners(u, v, face, surface)) return false;
48 RiseMath::Vector2 projected[4];
49 for (int i = 0; i < 4; ++i)
50 projected[i] = TerrainMapProjection::Project(surface[i], radius, mapView, pixelsPerKmX, pixelsPerKmY, center);
51 return TerrainMapProjection::QuadTransform(projected, 32, 32, .1f, transform);
52 }
53}
bool CellCorners(int u, int v, int face, RiseMath::Vector3(&corners)[4])
int PacketFace(unsigned char tag)
bool CellTransform(int u, int v, int face, float radius, const RiseMath::Matrix &mapView, float pixelsPerKmX, float pixelsPerKmY, RiseMath::Vector2 center, RiseMath::Matrix &transform)
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
RiseMath::Vector2 Project(const RiseMath::Vector3 &terrainPosition, float radius, const RiseMath::Matrix &mapView, float pixelsPerKmX, float pixelsPerKmY, RiseMath::Vector2 center)
bool QuadTransform(const RiseMath::Vector2(&p)[4], float width, float height, float depth, RiseMath::Matrix &transform)