-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleWindErosion.cpp
234 lines (177 loc) · 6.91 KB
/
SimpleWindErosion.cpp
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
229
230
231
232
233
234
#include <TinyEngine/TinyEngine>
#include <TinyEngine/camera>
#include <TinyEngine/image>
#include "source/vertexpool.hpp"
#include "source/world.hpp"
#include "source/model.hpp"
#include <random>
mappool::pool<quad::cell> cellpool;
Vertexpool<Vertex> vertexpool;
int main( int argc, char* args[] ) {
assert(TINYENGINE_VERSION == "1.7");
Tiny::view.vsync = false;
Tiny::view.blend = false;
Tiny::window("Simple Wind Erosion", WIDTH, HEIGHT);
glDisable(GL_CULL_FACE);
//Initialize the World
World world;
if(argc >= 2){
World::SEED = std::stoi(args[1]);
srand(std::stoi(args[1]));
}
else {
World::SEED = time(NULL);
srand(World::SEED);
}
cellpool.reserve(quad::area);
vertexpool.reserve(quad::tilearea, quad::maparea);
World::map.init(vertexpool, cellpool, World::SEED);
for(auto& node: world.map.nodes){
updatenode(vertexpool, node);
}
// Initialize the Visualization
// Camera
cam::near = -800.0f;
cam::far = 800.0f;
cam::moverate = 10.0f;
cam::look = glm::vec3(quad::size/2, quad::mapscale/2, quad::size/2);
cam::roty = 60.0f;
cam::rot = 180.0f;
cam::init(3, cam::ORTHO);
cam::update();
//Setup Shaders
Shader defaultshader({"source/shader/default.vs", "source/shader/default.fs"}, {"in_Position", "in_Normal", "in_Tangent", "in_Bitangent"});
Shader defaultdepth({"source/shader/depth.vs", "source/shader/depth.fs"}, {"in_Position"});
Shader ssaoshader({"source/shader/ssao.vs", "source/shader/ssao.fs"}, {"in_Quad", "in_Tex"});
Shader imageshader({"source/shader/image.vs", "source/shader/image.fs"}, {"in_Quad", "in_Tex"});
Shader mapshader({"source/shader/map.vs", "source/shader/map.fs"}, {"in_Quad", "in_Tex"});
// Rendering Targets
Billboard image(WIDTH, HEIGHT); //1200x800, color and depth
Texture shadowmap(8000, 8000, {GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT});
Target shadow(8000, 8000);
shadow.bind(shadowmap, GL_DEPTH_ATTACHMENT);
Square2D flat;
// SSAO
Texture gPosition(WIDTH, HEIGHT, {GL_RGBA16F, GL_RGBA, GL_FLOAT});
Texture gNormal(WIDTH, HEIGHT, {GL_RGBA16F, GL_RGBA, GL_FLOAT});
Texture gColor(WIDTH, HEIGHT, {GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE});
Texture gDepth(WIDTH, HEIGHT, {GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE});
Target gBuffer(WIDTH, HEIGHT);
gBuffer.bind(gPosition, GL_COLOR_ATTACHMENT0);
gBuffer.bind(gNormal, GL_COLOR_ATTACHMENT1);
gBuffer.bind(gColor, GL_COLOR_ATTACHMENT2);
gBuffer.bind(gDepth, GL_DEPTH_ATTACHMENT);
Texture ssaotex(WIDTH, HEIGHT, {GL_RED, GL_RED, GL_FLOAT});
Target ssaofbo(WIDTH, HEIGHT);
ssaofbo.bind(ssaotex, GL_COLOR_ATTACHMENT0);
// generate sample kernel
// ----------------------
std::uniform_real_distribution<GLfloat> randomFloats(0.0, 1.0); // generates random floats between 0.0 and 1.0
std::default_random_engine generator;
std::vector<glm::vec3> ssaoKernel;
for (unsigned int i = 0; i < 64; ++i){
glm::vec3 sample(randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, randomFloats(generator));
sample = glm::normalize(sample);
sample *= randomFloats(generator);
float scale = float(i) / 64.0f;
scale = 0.1f + scale*scale*(1.0f-0.1f);
sample *= scale;
ssaoKernel.push_back(sample);
}
// generate noise texture
// ----------------------
std::vector<glm::vec3> ssaoNoise;
for (unsigned int i = 0; i < 16; i++) {
glm::vec3 noise(randomFloats(generator) * 2.0 - 1.0, randomFloats(generator) * 2.0 - 1.0, 0.0f); // rotate around z-axis (in tangent space)
ssaoNoise.push_back(noise);
}
Texture noisetex(4, 4, {GL_RGBA32F, GL_RGB, GL_FLOAT}, &ssaoNoise[0]);
glBindTexture(GL_TEXTURE_2D, noisetex.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Map
Texture windmap(image::make([&](const ivec2 p){
return vec4(0,0,0,0);
}, quad::res));
glm::mat4 mapmodel = glm::mat4(1.0f);
mapmodel = glm::scale(mapmodel, glm::vec3(1,1,1)*glm::vec3((float)HEIGHT/(float)WIDTH, 1.0f, 1.0f));
// Event Handler
Tiny::event.handler = [&](){
cam::handler();
if(!Tiny::event.press.empty() && Tiny::event.press.back() == SDLK_p)
paused = !paused;
if(!Tiny::event.press.empty() && Tiny::event.press.back() == SDLK_m)
viewmap = !viewmap;
};
Tiny::view.interface = [](){};
Tiny::view.pipeline = [&](){
// Full GBuffer
gBuffer.target(vec3(0));
defaultshader.use();
defaultshader.uniform("proj", cam::proj);
defaultshader.uniform("view", cam::view);
defaultshader.uniform("flatColor", sedimentColor);
defaultshader.uniform("steepColor", steepColor);
vertexpool.render(GL_TRIANGLES);
// SSAO Texture
ssaofbo.target(vec3(0));
ssaoshader.use();
for (unsigned int i = 0; i < 64; ++i)
ssaoshader.uniform("samples[" + std::to_string(i) + "]", ssaoKernel[i]);
ssaoshader.uniform("projection", cam::proj);
ssaoshader.texture("gPosition", gPosition);
ssaoshader.texture("gNormal", gNormal);
ssaoshader.texture("texNoise", noisetex);
ssaoshader.uniform("radius", ssaoradius);
flat.render();
//Render Shadowmap
shadow.target(); //Prepare Target
defaultdepth.use(); //Prepare Shader
defaultdepth.uniform("dvp", dvp);
vertexpool.render(GL_TRIANGLES); //Render Surface Model
//Render Scene to Screen
Tiny::view.target(skyCol); //Prepare Target
imageshader.use();
imageshader.texture("gPosition", gPosition);
imageshader.texture("gNormal", gNormal);
imageshader.texture("gColor", gColor);
imageshader.texture("gDepth", gDepth);
imageshader.texture("ssaoTex", ssaotex);
imageshader.texture("shadowMap", shadowmap);
imageshader.uniform("view", cam::view);
imageshader.uniform("dbvp", dbvp);
imageshader.uniform("lightCol", lightCol);
imageshader.uniform("skyCol", skyCol);
imageshader.uniform("lightPos", lightPos);
imageshader.uniform("lookDir", cam::pos);
imageshader.uniform("lightStrength", lightStrength);
flat.render();
//Render Map to Screen
if(viewmap){
mapshader.use();
mapshader.texture("windmap", windmap);
mapshader.uniform("model", mapmodel);
flat.render();
}
};
int n = 0;
Tiny::loop([&](){
if(paused)
return;
world.erode(8*quad::tilesize);
for(auto& node: world.map.nodes){
updatenode(vertexpool, node);
}
cout<<n++<<endl;
windmap.raw(image::make([&](const ivec2 p){
auto node = world.map.get(p);
auto cell = node->get(p);
float mx = cell->momentumx;
float my = cell->momentumy;
float mz = cell->momentumz;
return glm::vec4(0.5f*(1.0f+erf(2*mx)), 0.5f*(1.0f+erf(2*my)), 0.5f*(1.0f+erf(2*mz)), 1.0); }, quad::res));
});
return 0;
}