Rise
The Vieneo Province
Loading...
Searching...
No Matches
RtsTerrainSurface.h
Go to the documentation of this file.
1#pragma once
2
3// RTS rows run south to north; terrain rows run north to south. Each plot
4// contains 26 RTS tiles and four terrain quads, split NE to SW by grid.cpp.
5namespace RtsTerrain
6{
7constexpr int TileCount = 26;
8constexpr int HalfTileCount = TileCount / 2;
9
11{
12 int east, north;
13};
14
15// Match the terrain diagonal and winding. A tile never crosses a terrain
16// triangle because the RTS grid has an even number of tiles on each axis.
17constexpr TileCorner TileCorners[6] = {
18 {0, 1}, {0, 0}, {1, 1}, {1, 1}, {0, 0}, {1, 0}
19};
20
22{
23 float u, v;
24};
25
26inline TexCoord TextureCoordinate(TileCorner corner, unsigned char rotation)
27{
28 const float u = static_cast<float>(1 - corner.east);
29 const float v = static_cast<float>(corner.north);
30 TexCoord result;
31 switch (rotation % 4)
32 {
33 case 0: result = {u, v}; break;
34 case 1: result = {v, 1.0f - u}; break;
35 case 2: result = {1.0f - u, 1.0f - v}; break;
36 default: result = {1.0f - v, u}; break;
37 }
38 if (rotation > 3)
39 result.u = 1.0f - result.u;
40 return result;
41}
42
43template <typename Vector>
44struct Surface
45{
46 // The exact terrain vertices, relative to the build zone's origin.
47 // Subtract the origin BEFORE interpolation to preserve sub-meter detail.
48 Vector points[3][3]; // [north-to-south row][west-to-east column]
49
50 Vector Sample(int east, int north) const
51 {
52 const int south = TileCount - north;
53 const int column = east < HalfTileCount ? 0 : 1;
54 const int row = south < HalfTileCount ? 0 : 1;
55 const int x = east - column * HalfTileCount;
56 const int y = south - row * HalfTileCount;
57 const float u = static_cast<float>(x) / HalfTileCount;
58 const float v = static_cast<float>(y) / HalfTileCount;
59 const Vector& ne = points[row][column + 1];
60 const Vector& sw = points[row + 1][column];
61 if (x + y <= HalfTileCount)
62 {
63 const Vector& nw = points[row][column];
64 return nw + (ne - nw) * u + (sw - nw) * v;
65 }
66 const Vector& se = points[row + 1][column + 1];
67 return se + (sw - se) * (1.0f - u) + (ne - se) * (1.0f - v);
68 }
69};
70}
constexpr TileCorner TileCorners[6]
TexCoord TextureCoordinate(TileCorner corner, unsigned char rotation)
constexpr int HalfTileCount
constexpr int TileCount
Vector Sample(int east, int north) const