Rise
The Vieneo Province
Toggle main menu visibility
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.
5
namespace
RtsTerrain
6
{
7
constexpr
int
TileCount
= 26;
8
constexpr
int
HalfTileCount
=
TileCount
/ 2;
9
10
struct
TileCorner
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.
17
constexpr
TileCorner
TileCorners
[6] = {
18
{0, 1}, {0, 0}, {1, 1}, {1, 1}, {0, 0}, {1, 0}
19
};
20
21
struct
TexCoord
22
{
23
float
u
,
v
;
24
};
25
26
inline
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
43
template
<
typename
Vector>
44
struct
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
}
RtsTerrain
Definition
RtsTerrainSurface.h:6
RtsTerrain::TileCorners
constexpr TileCorner TileCorners[6]
Definition
RtsTerrainSurface.h:17
RtsTerrain::TextureCoordinate
TexCoord TextureCoordinate(TileCorner corner, unsigned char rotation)
Definition
RtsTerrainSurface.h:26
RtsTerrain::HalfTileCount
constexpr int HalfTileCount
Definition
RtsTerrainSurface.h:8
RtsTerrain::TileCount
constexpr int TileCount
Definition
RtsTerrainSurface.h:7
RtsTerrain::Surface
Definition
RtsTerrainSurface.h:45
RtsTerrain::Surface::points
Vector points[3][3]
Definition
RtsTerrainSurface.h:48
RtsTerrain::Surface::Sample
Vector Sample(int east, int north) const
Definition
RtsTerrainSurface.h:50
RtsTerrain::TexCoord
Definition
RtsTerrainSurface.h:22
RtsTerrain::TexCoord::u
float u
Definition
RtsTerrainSurface.h:23
RtsTerrain::TexCoord::v
float v
Definition
RtsTerrainSurface.h:23
RtsTerrain::TileCorner
Definition
RtsTerrainSurface.h:11
RtsTerrain::TileCorner::east
int east
Definition
RtsTerrainSurface.h:12
RtsTerrain::TileCorner::north
int north
Definition
RtsTerrainSurface.h:12
Viewscreen
RtsTerrainSurface.h
Generated on
for Rise by
1.18.0