-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hpp
229 lines (188 loc) · 6.79 KB
/
main.hpp
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
#pragma once
#include "defines.hpp"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdint>
#include <exception>
#include <string>
#include <sstream>
#include <cuda_runtime.h>
#if USE_D3D11_RENDERER
#include <windows.h>
#include <windowsx.h>
#include <d3d11_1.h>
#include <cuda_d3d11_interop.h>
#else
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cuda_gl_interop.h>
#endif
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "terrain/terrain.hpp"
#include "player/player.hpp"
#include "rendering/optixRenderer.hpp"
#include "rendering/renderer.hpp"
#include "rendering/d3d11Renderer.h"
//
// Adapted from StepTimer.h by Chuck Walbourn
//
class StepTimer
{
public:
StepTimer() noexcept(false) :
m_elapsedTicks(0),
m_totalTicks(0),
m_leftOverTicks(0),
m_frameCount(0),
m_framesPerSecond(0),
m_framesThisSecond(0),
m_qpcSecondCounter(0)
{
if (!QueryPerformanceFrequency(&m_qpcFrequency))
{
throw std::exception();
}
if (!QueryPerformanceCounter(&m_qpcLastTime))
{
throw std::exception();
}
// Initialize max delta to 1/10 of a second.
m_qpcMaxDelta = static_cast<uint64_t>(m_qpcFrequency.QuadPart / 10);
}
// Get elapsed time since the previous Update call.
uint64_t GetElapsedTicks() const noexcept { return m_elapsedTicks; }
double GetElapsedSeconds() const noexcept { return TicksToSeconds(m_elapsedTicks); }
// Get total time since the start of the program.
uint64_t GetTotalTicks() const noexcept { return m_totalTicks; }
double GetTotalSeconds() const noexcept { return TicksToSeconds(m_totalTicks); }
// Get total number of updates since start of the program.
uint32_t GetFrameCount() const noexcept { return m_frameCount; }
// Get the current framerate.
uint32_t GetFramesPerSecond(bool* hasUpdate) noexcept { *hasUpdate = m_hasFPSUpdate; m_hasFPSUpdate = false; return m_framesPerSecond; }
// Integer format represents time using 10,000,000 ticks per second.
static constexpr uint64_t TicksPerSecond = 10000000;
static constexpr double TicksToSeconds(uint64_t ticks) noexcept { return static_cast<double>(ticks) / TicksPerSecond; }
static constexpr uint64_t SecondsToTicks(double seconds) noexcept { return static_cast<uint64_t>(seconds * TicksPerSecond); }
// After an intentional timing discontinuity (for instance a blocking IO operation)
// call this to avoid having the fixed timestep logic attempt a set of catch-up
// Update calls.
void ResetElapsedTime()
{
if (!QueryPerformanceCounter(&m_qpcLastTime))
{
throw std::exception();
}
m_leftOverTicks = 0;
m_framesPerSecond = 0;
m_framesThisSecond = 0;
m_qpcSecondCounter = 0;
}
// Update timer state
void Tick()
{
// Query the current time.
LARGE_INTEGER currentTime;
if (!QueryPerformanceCounter(¤tTime))
{
throw std::exception();
}
uint64_t timeDelta = static_cast<uint64_t>(currentTime.QuadPart - m_qpcLastTime.QuadPart);
m_qpcLastTime = currentTime;
m_qpcSecondCounter += timeDelta;
// Clamp excessively large time deltas (e.g. after paused in the debugger).
if (timeDelta > m_qpcMaxDelta)
{
timeDelta = m_qpcMaxDelta;
}
// Convert QPC units into a canonical tick format. This cannot overflow due to the previous clamp.
timeDelta *= TicksPerSecond;
timeDelta /= static_cast<uint64_t>(m_qpcFrequency.QuadPart);
const uint32_t lastFrameCount = m_frameCount;
// Variable timestep update logic.
m_elapsedTicks = timeDelta;
m_totalTicks += timeDelta;
m_leftOverTicks = 0;
m_frameCount++;
// Track the current framerate.
if (m_frameCount != lastFrameCount)
{
m_framesThisSecond++;
}
if (m_qpcSecondCounter >= static_cast<uint64_t>(m_qpcFrequency.QuadPart))
{
m_hasFPSUpdate = true;
m_framesPerSecond = m_framesThisSecond;
m_framesThisSecond = 0;
m_qpcSecondCounter %= static_cast<uint64_t>(m_qpcFrequency.QuadPart);
}
}
private:
// Source timing data uses QPC units.
LARGE_INTEGER m_qpcFrequency;
LARGE_INTEGER m_qpcLastTime;
uint64_t m_qpcMaxDelta;
// Derived timing data uses a canonical tick format.
uint64_t m_elapsedTicks;
uint64_t m_totalTicks;
uint64_t m_leftOverTicks;
// Members for tracking the framerate.
uint32_t m_frameCount;
uint32_t m_framesPerSecond;
uint32_t m_framesThisSecond;
uint64_t m_qpcSecondCounter;
bool m_hasFPSUpdate;
};
//====================================
// D3D11 Stuff
//====================================
#if USE_D3D11_RENDERER
HWND g_hWnd;
std::unique_ptr<D3D11Renderer> d3dRenderer;
#endif
//====================================
// Render Stuff
//====================================
// Pick your Ray Tracing Resolution -> set USE_UPSCALE = 1 to RayTrace at half resolution then 2x upscale
//glm::uvec2 renderSize = uvec2(3840, 2160);
//glm::uvec2 renderSize = uvec2(2560, 1440);
glm::uvec2 renderSize = uvec2(1920, 1080);
glm::uvec2 windowSize = uvec2(1920, 1080);
//glm::ivec2 windowSize = ivec2(1920 / 2, 1080 / 2);
bool windowSizeChanged;
std::unique_ptr<OptixRenderer> optixRenderer;
std::unique_ptr<Renderer> renderer;
std::unique_ptr<StepTimer> timer;
//====================================
// Game things
//====================================
std::unique_ptr<Terrain> terrain;
std::unique_ptr<Player> player;
//====================================
// Setup/init Stuff
//====================================
bool init(int argc, char** argv);
void constructTerrainAndPlayer();
//====================================
// Main
//====================================
int main(int argc, char* argv[]);
//====================================
// Main loop
//====================================
void mainLoop();
void tick(float deltaTime);
void errorCallback(int error, const char* description);
#if USE_D3D11_RENDERER
void initD3DWindow();
LRESULT CALLBACK MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void keyCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// void mouseButtonCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void mousePositionCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
#else
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void mousePositionCallback(GLFWwindow* window, double xpos, double ypos);
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
#endif