-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathground_plane.cpp
More file actions
509 lines (425 loc) · 18 KB
/
ground_plane.cpp
File metadata and controls
509 lines (425 loc) · 18 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/render/ground_plane.h"
#include "polyscope/polyscope.h"
#include "polyscope/render/engine.h"
#include "polyscope/render/material_defs.h"
#include "imgui.h"
#include "stb_image.h"
namespace polyscope {
namespace render {
// quick helper function
namespace {
std::tuple<int, float> getGroundPlaneAxisAndSign() {
int iP = 0;
switch (view::upDir) {
case view::UpDir::NegXUp:
case view::UpDir::XUp:
iP = 0;
break;
case view::UpDir::NegYUp:
case view::UpDir::YUp:
iP = 1;
break;
case view::UpDir::NegZUp:
case view::UpDir::ZUp:
iP = 2;
break;
}
float sign = 1.0;
if (view::upDir == view::UpDir::NegXUp || view::upDir == view::UpDir::NegYUp || view::upDir == view::UpDir::NegZUp) {
sign = -1.;
}
return std::tuple<int, float>{iP, sign};
}
}; // namespace
void GroundPlane::populateGroundPlaneGeometry() {
int iP;
float sign;
std::tie(iP, sign) = getGroundPlaneAxisAndSign();
// Geometry of the ground plane, using triangles with vertices at infinity
// clang-format off
glm::vec4 cVert{0., 0., 0., 1.};
glm::vec4 v1{0., 0., 0., 0.}; v1[(iP+2)%3] = sign * 1.;
glm::vec4 v2{0., 0., 0., 0.}; v2[(iP+1)%3] = sign * 1.;
glm::vec4 v3{0., 0., 0., 0.}; v3[(iP+2)%3] = sign *-1.;
glm::vec4 v4{0., 0., 0., 0.}; v4[(iP+1)%3] = sign *-1.;
std::vector<glm::vec4> positions = {
cVert, v2, v1,
cVert, v3, v2,
cVert, v4, v3,
cVert, v1, v4
};
// clang-format on
groundPlaneProgram->setAttribute("a_position", positions);
groundPlaneViewCached = view::upDir;
}
void GroundPlane::prepare() {
if (options::groundPlaneMode == GroundPlaneMode::None) {
// quick-out for the none case
groundPlanePrepared = true;
groundPlanePreparedMode = options::groundPlaneMode;
return;
}
// The program that draws the ground plane
std::vector<std::string> rules;
if (options::transparencyMode == TransparencyMode::Pretty) rules.push_back("TRANSPARENCY_PEEL_GROUND");
switch (options::groundPlaneMode) {
case GroundPlaneMode::None:
break;
case GroundPlaneMode::Tile:
groundPlaneProgram =
render::engine->requestShader("GROUND_PLANE_TILE", rules, render::ShaderReplacementDefaults::Process);
break;
case GroundPlaneMode::TileReflection:
groundPlaneProgram =
render::engine->requestShader("GROUND_PLANE_TILE_REFLECT", rules, render::ShaderReplacementDefaults::Process);
break;
case GroundPlaneMode::ShadowOnly:
groundPlaneProgram =
render::engine->requestShader("GROUND_PLANE_SHADOW", rules, render::ShaderReplacementDefaults::Process);
break;
}
populateGroundPlaneGeometry();
if (options::groundPlaneMode == GroundPlaneMode::Tile ||
options::groundPlaneMode == GroundPlaneMode::TileReflection) { // Load the ground texture
int w, h, comp;
unsigned char* image = nullptr;
image = stbi_load_from_memory(reinterpret_cast<const unsigned char*>(&render::bindata_concrete[0]),
render::bindata_concrete.size(), &w, &h, &comp, STBI_rgb);
if (image == nullptr) exception("Failed to load material image");
groundPlaneProgram->setTexture2D("t_ground", image, w, h, false, false, true);
stbi_image_free(image);
}
// For all effects which will use the alternate scene buffers, prepare them
if (options::groundPlaneMode == GroundPlaneMode::TileReflection ||
options::groundPlaneMode == GroundPlaneMode::ShadowOnly) {
if (options::groundPlaneMode == GroundPlaneMode::TileReflection) {
// only use color buffer for reflection
sceneAltColorTexture =
render::engine->generateTextureBuffer(TextureFormat::RGBA16F, view::bufferWidth, view::bufferHeight);
sceneAltColorTexture->setFilterMode(FilterMode::Linear);
}
sceneAltDepthTexture =
render::engine->generateTextureBuffer(TextureFormat::DEPTH24, view::bufferWidth, view::bufferHeight);
sceneAltFrameBuffer = render::engine->generateFrameBuffer(view::bufferWidth, view::bufferHeight);
if (options::groundPlaneMode == GroundPlaneMode::TileReflection) {
sceneAltFrameBuffer->addColorBuffer(sceneAltColorTexture);
}
sceneAltFrameBuffer->addDepthBuffer(sceneAltDepthTexture);
sceneAltFrameBuffer->setDrawBuffers();
sceneAltFrameBuffer->clearColor = glm::vec3{1., 1., 1.};
sceneAltFrameBuffer->clearAlpha = 0.0;
}
if (options::groundPlaneMode == GroundPlaneMode::TileReflection) { // Mirrored scene buffer
groundPlaneProgram->setTextureFromBuffer("t_mirrorImage", sceneAltColorTexture.get());
}
if (options::groundPlaneMode == GroundPlaneMode::ShadowOnly) {
// Blur buffers and program
for (int i = 0; i < 2; i++) {
blurColorTextures[i] =
render::engine->generateTextureBuffer(TextureFormat::RGBA16F, view::bufferWidth, view::bufferHeight);
blurColorTextures[i]->setFilterMode(FilterMode::Linear);
blurFrameBuffers[i] = render::engine->generateFrameBuffer(view::bufferWidth, view::bufferHeight);
blurFrameBuffers[i]->addColorBuffer(blurColorTextures[i]);
blurFrameBuffers[i]->setDrawBuffers();
blurFrameBuffers[i]->clearColor = glm::vec3{1., 1., 1.};
blurFrameBuffers[i]->clearAlpha = 0.0;
}
blurProgram = render::engine->requestShader("BLUR_RGB", {}, render::ShaderReplacementDefaults::Process);
blurProgram->setAttribute("a_position", render::engine->screenTrianglesCoords());
copyTexProgram = render::engine->requestShader("DEPTH_TO_MASK", {}, render::ShaderReplacementDefaults::Process);
copyTexProgram->setAttribute("a_position", render::engine->screenTrianglesCoords());
copyTexProgram->setTextureFromBuffer("t_depth", sceneAltDepthTexture.get());
groundPlaneProgram->setTextureFromBuffer("t_shadow", blurColorTextures[0].get());
}
// Respect global effects
if (options::transparencyMode == TransparencyMode::Pretty) {
groundPlaneProgram->setTextureFromBuffer("t_minDepth", render::engine->sceneDepthMin.get());
}
groundPlanePrepared = true;
groundPlanePreparedMode = options::groundPlaneMode;
}
void GroundPlane::freeAllOwnedResources() {
groundPlaneProgram.reset();
sceneAltColorTexture.reset();
sceneAltDepthTexture.reset();
sceneAltFrameBuffer.reset();
blurProgram.reset();
copyTexProgram.reset();
for (int i = 0; i < 2; i++) {
blurColorTextures[i].reset();
blurFrameBuffers[i].reset();
}
}
void GroundPlane::draw(bool isRedraw) {
if (options::groundPlaneMode == GroundPlaneMode::None) {
return;
}
// don't draw ground in planar mode
if (view::style == view::NavigateStyle::Planar) return;
// don't draw the ground in Simple transparency mode
// (there's not really any way to do so that doesn't look weird at the horizon boundary)
if (render::engine->getTransparencyMode() == TransparencyMode::Simple) {
return;
}
if (!groundPlanePrepared || groundPlanePreparedMode != options::groundPlaneMode) {
prepare();
}
if (view::upDir != groundPlaneViewCached) {
populateGroundPlaneGeometry();
}
// Get logical "up" direction to which the ground plane is oriented
int iP;
float sign;
std::tie(iP, sign) = getGroundPlaneAxisAndSign();
glm::vec3 baseUp{0., 0., 0.};
glm::vec3 baseForward{0., 0., 0.};
glm::vec3 baseRight{0., 0., 0.};
baseUp[iP] = 1.;
baseForward[(iP + 1) % 3] = sign;
baseRight[(iP + 2) % 3] = sign;
// Location for ground plane
double bboxBottom = sign == 1.0 ? std::get<0>(state::boundingBox)[iP] : std::get<1>(state::boundingBox)[iP];
double bboxHeight = std::get<1>(state::boundingBox)[iP] - std::get<0>(state::boundingBox)[iP];
double heightEPS = state::lengthScale * 1e-4;
double groundHeight = -777;
switch (options::groundPlaneHeightMode) {
case GroundPlaneHeightMode::Automatic: {
groundHeight = bboxBottom - sign * (options::groundPlaneHeightFactor.asAbsolute() + heightEPS);
break;
}
case GroundPlaneHeightMode::Manual: {
groundHeight = options::groundPlaneHeight;
break;
}
}
// Viewport
glm::vec4 viewport = render::engine->getCurrentViewport();
glm::vec2 viewportDim{viewport[2], viewport[3]};
int factor = render::engine->getSSAAFactor();
auto setUniforms = [&]() {
glm::mat4 viewMat = view::getCameraViewMatrix();
groundPlaneProgram->setUniform("u_viewMatrix", glm::value_ptr(viewMat));
glm::mat4 projMat = view::getCameraPerspectiveMatrix();
groundPlaneProgram->setUniform("u_projMatrix", glm::value_ptr(projMat));
groundPlaneProgram->setUniform("u_viewportDim", viewportDim);
if (options::groundPlaneMode == GroundPlaneMode::Tile ||
options::groundPlaneMode == GroundPlaneMode::TileReflection) {
groundPlaneProgram->setUniform("u_center", state::center());
groundPlaneProgram->setUniform("u_basisX", baseForward);
groundPlaneProgram->setUniform("u_basisY", baseRight);
}
if (options::groundPlaneMode == GroundPlaneMode::ShadowOnly) {
groundPlaneProgram->setUniform("u_shadowDarkness", options::shadowDarkness);
}
switch (view::projectionMode) {
case ProjectionMode::Perspective: {
float camHeight = view::getCameraWorldPosition()[iP];
groundPlaneProgram->setUniform("u_cameraHeight", camHeight);
break;
}
case ProjectionMode::Orthographic: {
glm::vec4 lookDir = glm::vec4(0, 0, 1, 0) * viewMat;
groundPlaneProgram->setUniform("u_cameraHeight", (lookDir[iP] * state::lengthScale) + groundHeight);
break;
}
}
groundPlaneProgram->setUniform("u_upSign", sign);
groundPlaneProgram->setUniform("u_basisZ", baseUp);
groundPlaneProgram->setUniform("u_groundHeight", groundHeight);
groundPlaneProgram->setUniform("u_lengthScale", state::lengthScale);
};
/*
// For all effects which will use the alternate scene buffers, prepare them
if (options::groundPlaneMode == GroundPlaneMode::TileReflection ||
options::groundPlaneMode == GroundPlaneMode::ShadowOnly) {
sceneAltFrameBuffer->resize(factor * view::bufferWidth / 2, factor * view::bufferHeight / 2);
sceneAltFrameBuffer->setViewport(0, 0, factor * view::bufferWidth / 2, factor * view::bufferHeight / 2);
render::engine->setCurrentPixelScaling(factor / 2.);
sceneAltFrameBuffer->bindForRendering();
sceneAltFrameBuffer->clearColor = {view::bgColor[0], view::bgColor[1], view::bgColor[2]};
sceneAltFrameBuffer->clear();
}
*/
// Render the scene to implement the mirror effect
if (!isRedraw && options::groundPlaneMode == GroundPlaneMode::TileReflection) {
// Prepare the alternate scene buffers
// (use a texture 1/4 the area of the view buffer, it's supposed to be blurry anyway and this saves perf)
render::engine->setBlendMode(BlendMode::AlphaOver);
render::engine->setDepthMode(DepthMode::Less);
sceneAltFrameBuffer->resize(factor * view::bufferWidth / 2, factor * view::bufferHeight / 2);
sceneAltFrameBuffer->setViewport(0, 0, factor * view::bufferWidth / 2, factor * view::bufferHeight / 2);
render::engine->setCurrentPixelScaling(factor / 2. * options::uiScale);
sceneAltFrameBuffer->bindForRendering();
sceneAltFrameBuffer->clearColor = {view::bgColor[0], view::bgColor[1], view::bgColor[2]};
sceneAltFrameBuffer->clear();
// Render to a texture so we can sample from it on the ground
sceneAltFrameBuffer->bindForRendering();
// Push a reflected view matrix
glm::mat4 origViewMat = view::viewMat;
glm::vec3 mirrorN = baseUp * sign;
glm::mat3 mirrorMat3 = glm::mat3(1.0) - 2.0f * glm::outerProduct(mirrorN, mirrorN);
glm::vec3 tVec{0., 0., 0.};
tVec[iP] = -groundHeight;
glm::mat4 mirrorMat =
glm::translate(glm::mat4(1.0), -tVec) * glm::mat4(mirrorMat3) * glm::translate(glm::mat4(1.0), tVec);
view::viewMat = view::viewMat * mirrorMat;
// Flip the orientation
render::engine->setFrontFaceCCW(!render::engine->getFrontFaceCCW());
// Draw everything
if (!render::engine->transparencyEnabled()) { // skip when transparency is turned on
drawStructures();
}
// Restore original values
render::engine->setFrontFaceCCW(!render::engine->getFrontFaceCCW());
view::viewMat = origViewMat;
}
// Render the scene to implement the shadow effect
if (!isRedraw && options::groundPlaneMode == GroundPlaneMode::ShadowOnly) {
// Prepare the alternate scene buffers
render::engine->setBlendMode(BlendMode::AlphaOver);
render::engine->setDepthMode(DepthMode::Less);
sceneAltFrameBuffer->resize(factor * view::bufferWidth, factor * view::bufferHeight);
sceneAltFrameBuffer->setViewport(0, 0, factor * view::bufferWidth, factor * view::bufferHeight);
sceneAltFrameBuffer->bindForRendering();
sceneAltFrameBuffer->clearColor = {view::bgColor[0], view::bgColor[1], view::bgColor[2]};
sceneAltFrameBuffer->clear();
// Make sure all framebuffers are the right shape
for (int i = 0; i < 2; i++) {
blurFrameBuffers[i]->resize(factor * view::bufferWidth / 2, factor * view::bufferHeight / 2);
blurFrameBuffers[i]->setViewport(0, 0, factor * view::bufferWidth / 2, factor * view::bufferHeight / 2);
blurFrameBuffers[i]->clear();
}
// Render to a texture so we can sample from it on the ground
sceneAltFrameBuffer->bindForRendering();
// Push a view matrix which projects on to the ground plane
glm::mat4 origViewMat = view::viewMat;
glm::mat4 projMat = glm::mat4(1.0);
projMat[iP][iP] = 0.;
projMat[3][iP] = groundHeight;
view::viewMat = view::viewMat * projMat;
view::overrideClipPlanes = true; // set the clip planes manually
view::overrideNearClipRelative = view::defaultNearClipRatio;
view::overrideFarClipRelative = view::defaultFarClipRatio;
// Draw everything
render::engine->setDepthMode(DepthMode::Less);
render::engine->setBlendMode(BlendMode::Disable);
drawStructures();
// Copy the depth buffer to a texture (while upsampling)
render::engine->setBlendMode(BlendMode::Disable);
blurFrameBuffers[0]->bindForRendering();
copyTexProgram->draw();
// == Blur
// Do some blur iterations (ends in same buffer it started in)
int nBlur = options::shadowBlurIters * render::engine->getSSAAFactor();
// int nBlur = 0;
for (int i = 0; i < nBlur; i++) {
// horizontal blur
blurFrameBuffers[1]->bindForRendering();
blurProgram->setTextureFromBuffer("t_image", blurColorTextures[0].get());
blurProgram->setUniform("u_horizontal", 1);
blurProgram->draw();
// vertical blur
blurFrameBuffers[0]->bindForRendering();
blurProgram->setTextureFromBuffer("t_image", blurColorTextures[1].get());
blurProgram->setUniform("u_horizontal", 0);
blurProgram->draw();
}
// Restore original view matrix
view::viewMat = origViewMat;
view::overrideClipPlanes = false;
}
render::engine->bindSceneBuffer();
// Render the ground plane
render::engine->applyTransparencySettings();
setUniforms();
groundPlaneProgram->draw();
}
void GroundPlane::buildGui() {
auto modeName = [](const GroundPlaneMode& m) -> std::string {
switch (m) {
case GroundPlaneMode::None:
return "None";
case GroundPlaneMode::Tile:
return "Tile";
case GroundPlaneMode::TileReflection:
return "Tile Reflection";
case GroundPlaneMode::ShadowOnly:
return "Shadow Only";
}
return "";
};
auto heightModeName = [](const GroundPlaneHeightMode& m) -> std::string {
switch (m) {
case GroundPlaneHeightMode::Automatic:
return "Automatic";
case GroundPlaneHeightMode::Manual:
return "Manual";
}
return "";
};
ImGui::SetNextItemOpen(false, ImGuiCond_FirstUseEver);
if (ImGui::TreeNode("Ground Plane")) {
ImGui::PushItemWidth(160 * options::uiScale);
if (ImGui::BeginCombo("Mode", modeName(options::groundPlaneMode).c_str())) {
for (GroundPlaneMode m : {GroundPlaneMode::None, GroundPlaneMode::Tile, GroundPlaneMode::TileReflection,
GroundPlaneMode::ShadowOnly}) {
std::string mName = modeName(m);
if (ImGui::Selectable(mName.c_str(), options::groundPlaneMode == m)) {
options::groundPlaneMode = m;
requestRedraw();
}
}
ImGui::EndCombo();
}
ImGui::PopItemWidth();
// Height
ImGui::PushItemWidth(80 * options::uiScale);
switch (options::groundPlaneHeightMode) {
case GroundPlaneHeightMode::Automatic:
if (ImGui::SliderFloat("##HeightValue", options::groundPlaneHeightFactor.getValuePtr(), -1.0, 1.0))
requestRedraw();
break;
case GroundPlaneHeightMode::Manual:
int iP;
float sign;
std::tie(iP, sign) = getGroundPlaneAxisAndSign();
double bboxBottom = sign == 1.0 ? std::get<0>(state::boundingBox)[iP] : std::get<1>(state::boundingBox)[iP];
double bboxHeight = std::get<1>(state::boundingBox)[iP] - std::get<0>(state::boundingBox)[iP];
if (ImGui::SliderFloat("##HeightValue", &options::groundPlaneHeight, bboxBottom - 0.5 * bboxHeight,
bboxBottom + bboxHeight)) {
requestRedraw();
}
break;
}
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::PushItemWidth(100 * options::uiScale);
if (ImGui::BeginCombo("Height##Mode", heightModeName(options::groundPlaneHeightMode).c_str())) {
for (GroundPlaneHeightMode m : {GroundPlaneHeightMode::Automatic, GroundPlaneHeightMode::Manual}) {
std::string mName = heightModeName(m);
if (ImGui::Selectable(mName.c_str(), options::groundPlaneHeightMode == m)) {
options::groundPlaneHeightMode = m;
requestRedraw();
}
}
ImGui::EndCombo();
}
ImGui::PopItemWidth();
switch (options::groundPlaneMode) {
case GroundPlaneMode::None:
break;
case GroundPlaneMode::Tile:
break;
case GroundPlaneMode::TileReflection:
break;
case GroundPlaneMode::ShadowOnly:
if (ImGui::SliderFloat("Shadow Darkness", &options::shadowDarkness, .0, 1.0)) requestRedraw();
if (ImGui::InputInt("Blur Iterations", &options::shadowBlurIters, 1)) requestRedraw();
break;
}
ImGui::TreePop();
}
}
} // namespace render
} // namespace polyscope