-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cpp
287 lines (227 loc) · 11.9 KB
/
App.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
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
/** \file App.cpp */
#include "App.h"
#include "RayTracer.h"
// Tells C++ to invoke command-line main() function even on OS X and Win32.
G3D_START_AT_MAIN();
int main(int argc, const char* argv[]) {
initGLG3D(G3DSpecification());
GApp::Settings settings(argc, argv);
// Change the window and other startup parameters by modifying the
// settings class. For example:
settings.window.caption = argv[0];
// Set enable to catch more OpenGL errors
// settings.window.debugContext = true;
// Some common resolutions:
// settings.window.width = 10; settings.window.height = 10;
//settings.window.width = 854; settings.window.height = 480;
// settings.window.width = 1024; settings.window.height = 768;
settings.window.width = 1280; settings.window.height = 720;
//settings.window.width = 1920; settings.window.height = 1080;
//settings.window.width = OSWindow::primaryDisplayWindowSize().x; settings.window.height = OSWindow::primaryDisplayWindowSize().y;
settings.window.fullScreen = false;
settings.window.resizable = ! settings.window.fullScreen;
settings.window.framed = ! settings.window.fullScreen;
settings.window.defaultIconFilename = "icon.png";
// Set to true for a significant performance boost if your app can't render at 60fps, or if
// you *want* to render faster than the display.
settings.window.asynchronous = false;
settings.hdrFramebuffer.depthGuardBandThickness = Vector2int16(64, 64);
settings.hdrFramebuffer.colorGuardBandThickness = Vector2int16(0, 0);
settings.dataDir = FileSystem::currentDirectory();
settings.screenCapture.outputDirectory = "../journal/";
settings.screenCapture.includeAppRevision = false;
settings.screenCapture.includeG3DRevision = false;
settings.screenCapture.filenamePrefix = "_";
settings.renderer.deferredShading = true;
settings.renderer.orderIndependentTransparency = true;
return App(settings).run();
}
App::App(const GApp::Settings& settings) : GApp(settings) {
}
// Called before the application loop begins. Load data here and
// not in the constructor so that common exceptions will be
// automatically caught.
void App::onInit() {
GApp::onInit();
setFrameDuration(1.0f / 60.0f);
// Call setScene(shared_ptr<Scene>()) or setScene(MyScene::create()) to replace
// the default scene here.
showRenderingStats = false;
makeGUI();
// For higher-quality screenshots:
// developerWindow->videoRecordDialog->setScreenShotFormat("PNG");
// developerWindow->videoRecordDialog->setCaptureGui(false);
loadScene(
# ifndef G3D_DEBUG
//"G3D Simple Cornell Box"
//"G3D Debug Triangle"
//"G3D Simple Cornell Box (Empty CO)"
//"G3D Simple Cornell Box (Area Light)"
//"G3D Simple Cornell Box"
//"G3D Simple Cornell Box (Mirror)"
//"G3D Sponza"
//"G3D Sibernik (Statue)"
"G3D Simple Cornell Box (Spheres)"
//"G3D Sponza (Area Light)"
//"G3D Debug Teapot"
# else
//"G3D Simple Cornell Box" // Load something simple
//"G3D Debug Triangle"
//"G3D Simple Cornell Box (Empty CO)"
//"G3D Simple Cornell Box (Area Light)"
//"G3D Simple Cornell Box (Mirror)"
//"G3D Simple Cornell Box"
//"G3D Sibernik (Statue)"
//"G3D Sponza"
"G3D Simple Cornell Box (Spheres)"
//"G3D Sponza (Area Light)"
//"G3D Debug Teapot"
# endif
//developerWindow->sceneEditorWindow->selectedSceneName() // Load the first scene encountered
);
}
void App::makeGUI() {
debugWindow->setVisible(true);
developerWindow->videoRecordDialog->setEnabled(true);
// More examples of debugging GUI controls:
// debugPane->addCheckBox("Use explicit checking", &explicitCheck);
//debugPane->addTextBox("Rays Per Pixel", &myName);
//debugPane->addNumberBox("Rays Per Pixel", &raysPerPixel, "rays", GuiTheme::NO_SLIDER, 1, 2048, 1);
debugPane->addButton("Render", [this](){ onRender(); });
debugWindow->pack();
debugWindow->setRect(Rect2D::xywh(0, 0, (float)window()->width(), debugWindow->rect().height()));
}
// This default implementation is a direct copy of GApp::onGraphics3D to make it easy
// for you to modify. If you aren't changing the hardware rendering strategy, you can
// delete this override entirely.
void App::onGraphics3D(RenderDevice* rd, Array<shared_ptr<Surface> >& allSurfaces) {
if (! scene()) {
if ((submitToDisplayMode() == SubmitToDisplayMode::MAXIMIZE_THROUGHPUT) && (!rd->swapBuffersAutomatically())) {
swapBuffers();
}
rd->clear();
rd->pushState(); {
rd->setProjectionAndCameraMatrix(activeCamera()->projection(), activeCamera()->frame());
drawDebugShapes();
} rd->popState();
return;
}
if (m_result) {
rd->push2D(); {
Draw::rect2D(rd->viewport(), rd, Color3::white(), m_result);
} rd->pop2D();
}
GBuffer::Specification gbufferSpec = m_gbufferSpecification;
extendGBufferSpecification(gbufferSpec);
m_gbuffer->setSpecification(gbufferSpec);
m_gbuffer->resize(m_framebuffer->width(), m_framebuffer->height());
m_gbuffer->prepare(rd, activeCamera(), 0, -(float)previousSimTimeStep(), m_settings.hdrFramebuffer.depthGuardBandThickness, m_settings.hdrFramebuffer.colorGuardBandThickness);
m_renderer->render(rd, activeCamera(), m_framebuffer, scene()->lightingEnvironment().ambientOcclusionSettings.enabled ? m_depthPeelFramebuffer : shared_ptr<Framebuffer>(),
scene()->lightingEnvironment(), m_gbuffer, allSurfaces);
// Debugging visualizations and post-process effects
rd->pushState(m_framebuffer); {
rd->setProjectionAndCameraMatrix(activeCamera()->projection(), activeCamera()->frame());
// Call to make the App show the output of debugDraw(...)
drawDebugShapes();
const shared_ptr<Entity>& selectedEntity = (notNull(developerWindow) && notNull(developerWindow->sceneEditorWindow)) ? developerWindow->sceneEditorWindow->selectedEntity() : shared_ptr<Entity>();
scene()->visualize(rd, selectedEntity, allSurfaces, sceneVisualizationSettings(), activeCamera());
// Post-processed special effects
m_depthOfField->apply(rd, m_framebuffer->texture(0), m_framebuffer->texture(Framebuffer::DEPTH), activeCamera(), m_settings.hdrFramebuffer.depthGuardBandThickness - m_settings.hdrFramebuffer.colorGuardBandThickness);
m_motionBlur->apply(rd, m_framebuffer->texture(0), m_gbuffer->texture(GBuffer::Field::SS_POSITION_CHANGE),
m_framebuffer->texture(Framebuffer::DEPTH), activeCamera(),
m_settings.hdrFramebuffer.depthGuardBandThickness - m_settings.hdrFramebuffer.colorGuardBandThickness);
} rd->popState();
// We're about to render to the actual back buffer, so swap the buffers now.
// This call also allows the screenshot and video recording to capture the
// previous frame just before it is displayed.
if (submitToDisplayMode() == SubmitToDisplayMode::MAXIMIZE_THROUGHPUT) {
swapBuffers();
}
// Clear the entire screen (needed even though we'll render over it, since
// AFR uses clear() to detect that the buffer is not re-used.)
rd->clear();
// Perform gamma correction, bloom, and SSAA, and write to the native window frame buffer
m_film->exposeAndRender(rd, activeCamera()->filmSettings(), m_framebuffer->texture(0), settings().hdrFramebuffer.colorGuardBandThickness.x + settings().hdrFramebuffer.depthGuardBandThickness.x, settings().hdrFramebuffer.depthGuardBandThickness.x,
Texture::opaqueBlackIfNull(notNull(m_gbuffer) ? m_gbuffer->texture(GBuffer::Field::SS_POSITION_CHANGE) : nullptr),
activeCamera()->jitterMotion());
}
void App::onAI() {
GApp::onAI();
// Add non-simulation game logic and AI code here
}
void App::onNetwork() {
GApp::onNetwork();
// Poll net messages here
}
void App::onSimulation(RealTime rdt, SimTime sdt, SimTime idt) {
GApp::onSimulation(rdt, sdt, idt);
// Example GUI dynamic layout code. Resize the debugWindow to fill
// the screen horizontally.
debugWindow->setRect(Rect2D::xywh(0, 0, (float)window()->width(), debugWindow->rect().height()));
}
bool App::onEvent(const GEvent& event) {
// Handle super-class events
if (GApp::onEvent(event)) { return true; }
// If you need to track individual UI events, manage them here.
// Return true if you want to prevent other parts of the system
// from observing this specific event.
//
// For example,
// if ((event.type == GEventType::GUI_ACTION) && (event.gui.control == m_button)) { ... return true; }
// if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey::TAB)) { ... return true; }
// if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == 'p')) { ... return true; }
if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == 'p')) {
shared_ptr<DefaultRenderer> r = dynamic_pointer_cast<DefaultRenderer>(m_renderer);
r->setDeferredShading(! r->deferredShading());
return true;
}
return false;
}
void App::onUserInput(UserInput* ui) {
GApp::onUserInput(ui);
(void)ui;
// Add key handling here based on the keys currently held or
// ones that changed in the last frame.
}
void App::onPose(Array<shared_ptr<Surface> >& surface, Array<shared_ptr<Surface2D> >& surface2D) {
GApp::onPose(surface, surface2D);
// Append any models to the arrays that you want to later be rendered by onGraphics()
}
void App::onRender() {
const int width = int(window()->width());
const int height = int(window()->height());
m_currentImage = Image3::createEmpty(width, height);
RayTracer raytrace;
//setActiveCamera(debugCamera());
Stopwatch timer;
scene()->onPose(m_surfaceArray); //Extracts all surfaces from the scene into an array
scene()->getTypedEntityArray<Light>(m_LightArray);
raytrace.TraceImage(m_debugCamera, m_currentImage, m_surfaceArray, m_LightArray);
timer.printElapsedTime("Trace");
debugPrintf("%f s\n", timer.elapsedTime());
activeCamera()->filmSettings().setSensitivity(0.25f);
// Post-process
//Converting from image to texture
//Image is a CPU bound while texture is GPU bound
const shared_ptr<PixelTransferBuffer>& ptb = CPUPixelTransferBuffer::fromData(m_currentImage->width(), m_currentImage->height(), ImageFormat::RGB32F(), m_currentImage->getCArray(), 1, 1);
const shared_ptr<Texture>& src = Texture::fromPixelTransferBuffer("Source", ptb, ImageFormat::RGB32F(), Texture::DIM_2D, false);
if (m_result) {
m_result->resize(width, height);
}
//debugPrintf("%f", activeCamera()->filmSettings().sensitivity());
m_film->exposeAndRender(renderDevice, activeCamera()->filmSettings(), src, settings().hdrFramebuffer.colorGuardBandThickness.x, settings().hdrFramebuffer.depthGuardBandThickness.x, m_result);
show(m_result, "Raw Radiance");
}
void App::onGraphics2D(RenderDevice* rd, Array<shared_ptr<Surface2D> >& posed2D) {
// Render 2D objects like Widgets. These do not receive tone mapping or gamma correction.
Surface2D::sortAndRender(rd, posed2D);
}
void App::onAfterLoadScene(const Any & any, const String & sceneName) {
GApp::onAfterLoadScene(any, sceneName);
//scene()->onPose(m_surfaceArray); //Extracts all surfaces from the scene into an array
//scene()->getTypedEntityArray<Light>(m_LightArray);
}
void App::onCleanup() {
// Called after the application loop ends. Place a majority of cleanup code
// here instead of in the constructor so that exceptions can be caught.
}