-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanet.h
231 lines (178 loc) · 6.75 KB
/
Planet.h
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
#pragma once
#include <vector>
#include <vlCore/Vector3.hpp>
#include <set>
#include "util/fmath.h"
#include "util/Profiler.h"
#include "util/tree.h"
#include "util/parameter.h"
#include "util/PerlinNoise.h"
class Planet {
public:
Planet();
void generate();
struct edge {
std::size_t a;
std::size_t b;
std::size_t neighbor;
float distance;
float length;
float bearing;
bool plateBorder;
float edgeFactor;
float lengthFactor;
float neighborDirFactor;
float force;
bool convergent;
};
struct cell {
std::size_t point;
std::vector<edge> edges;
std::size_t plate;
vl::fvec3 color;
vl::fvec3 dir;
vl::fvec3 wind;
vl::fvec3 incomingWind;
float elevation;
float moisture;
float compression;
float dMnt;
float dCst;
float dOcn;
float illumination;
float atmosphere;
float annualIllumination;
float temperature;
std::size_t r;
bool plateBorder;
float bearing;
float divergentForce;
float convergentForce;
};
struct plate {
vl::vec3 origin;
std::size_t cell;
vl::fvec3 axisOfRotation;
bool oceanic;
std::size_t cellCount;
};
std::vector<vl::fvec3> points;
std::vector<vl::dvec2> coords;
std::vector<vl::fvec3> centers;
std::vector<std::size_t> triangles;
std::vector<std::size_t> halfedges;
std::vector<cell> cells;
std::vector<plate> plates;
numericParameter<int> phase = {1, "phase" };
numericParameter<int> pointCount = { 65536, "points" };
numericParameter<float> jitter = {13.9f, "jitter" };
numericParameter<int> plateCount = { 114, "plates" };
numericParameter<int> moisture = {0, "moisture" };
numericParameter<int> ocean = { 70, "ocean" };
boolParameter useCentroids = {true, "centroids" };
boolParameter normalizeCentroids = { true, "normalize" };
numericParameter<float> collisionThreshold = {1.70f, "collision threshold" };
numericParameter<float> axialTilt = { 23.5f, "axial tilt" };
numericParameter<float> noiseIntensity = { 0.1f, "noise intensity" };
numericParameter<int> noiseOctaves = { 4, "noise octaves" };
numericParameter<float> noiseScale = { 2.0f, "noise scale" };
numericParameter<float> atmosphere = {0.1f, "thickness of atmosphere" };
double nScale;
tree kdTree;
bool pointsDirty = false;
bool trianglesDirty = false;
bool cellsDirty = false;
bool platesDirty = false;
bool heightMapDirty = false;
bool cellColorsDirty = false;
bool regenerateTriangles = true;
bool regenerateCells = true;
bool regeneratePlates = true;
bool regenerateHeightMap = true;
bool regenerateColors = true;
void clearDirtyFlags() {
pointsDirty = false;
trianglesDirty = false;
cellsDirty = false;
platesDirty = false;
heightMapDirty = false;
cellColorsDirty = false;
}
static vl::dvec2 toStereo( const vl::fvec3& v );
vl::fvec3 getColor( float elevation, float moisture );
Profiler::resultset lastResults;
void calcLight( const vl::fvec3& ray );
void updateTemperature( float delta );
void calcAnnualIllumination( std::size_t yearSamples, std::size_t daySamples );
std::vector<vl::fvec2> getDailyIllumination(
std::size_t cell,
float timeOfYear,
std::size_t samples );
std::vector<vl::fvec2> getAnnualIllumination(
std::size_t cell,
std::size_t samples );
private:
void generatePoints( std::vector<vl::fvec3>& pts, float jitter );
std::vector<double> getStereoPoints();
void fillSouthPole();
static int nextSide( int side );
static int prevSide( int side );
static vl::fvec3 latLonToVec( double lat, double lon ) {
return vl::vec3( cos( lon ) * sin( lat ), cos( lat ), sin( lon ) * sin( lat ) );
}
static vl::dvec2 vecToLatLon( const vl::fvec3& v ) {
return vl::dvec2( vecToLat( v ), vecToLon( v ) );
}
static double vecToLat( const vl::fvec3& v ) {
return acos( v.y() );
}
static double vecToLon( const vl::fvec3& v ) {
return atan2( v.z(), v.x() );
}
void generateCenters( bool centroid, bool normalize );
void generateCells( float moisture );
void generatePlates( int plateCount, float ocean );
void applyPlateMotion( float collisionThreshold );
void updateCellColors();
void updateWind();
math::rng rnd;
static vl::fvec3 getDir( const vl::fvec3& v, float degrees );
static vl::fvec3 getDir( const vl::fvec3& v, const vl::fvec3& axis, float degrees );
std::vector<float> assignDistanceField( const std::set<size_t>& seeds, const std::set<size_t>& stops );
std::vector<vl::fvec3> colormap;
std::vector<double> annualIllumination;
void generateColorMap();
void calculateCoords();
/**
* Returns the angle between two vectors in radians
*
* Quickly calculates the angle between two given vectors
* Will take possible rounding errors into account without causing NaN errors
*
* @param v1 vector 1
* @param v2 vector 2
* @return angle between v1 and v2 in radians
*/
static double safeGetAngle( const vl::fvec3& v1, const vl::fvec3& v2 );
static double getGreatCircleDistance( const vl::fvec3& v1, const vl::fvec3& v2);
static double getBearing( const vl::fvec3& v1, const vl::fvec3& v2);
static double getBearing2( const vl::fvec3& v1, const vl::fvec3& v2 );
static double angleTo( const vl::fvec3& v1, const vl::fvec3& v2, const vl::fvec3& n );
static double sinAcos( double d );
static vl::fvec3 destinationPoint( const vl::fvec3& v, double distance, double bearing );
double getIllumination( const vl::fvec3& sun, const vl::fvec3& p );
/**
* Calculates the distance sunlight has to travel through the atmosphere before reaching a given point on the planet
*
* @param sun unity vector towards sun from center of planet
* @param p point on planet
* @return distance between point of planet and edge of atmosphere
*/
double getAtmosphericDistance( const vl::fvec3& sun, const vl::fvec3& p );
static double angleTo2( const vl::fvec3& v1, const vl::fvec3& v2, const vl::fvec3& n );
static double getBearing3( const vl::fvec3& v1, const vl::fvec3& v2 );
static double distanceToLine( const vl::fvec3& l, const vl::fvec3& p );
void applyPlateMotion2( float threshold );
std::vector<float> assignDistanceField2( const std::set<size_t>& seeds, const std::set<size_t>& stops, float factor );
float createNoise( const vl::fvec3& v, PerlinNoise<float>& noise );
};