-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.cpp
309 lines (292 loc) · 8.88 KB
/
space.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// space.cpp : Devon McKee
#define _USE_MATH_DEFINES
#include <glad.h>
#define GLFW_INCLUDE_NONE
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#include <GLFW/glfw3.h>
#pragma clang diagnostic pop
#include <chrono>
#include <vector>
#include <string>
#include <stdexcept>
#include "VecMat.h"
#include "GeomUtils.h"
#include "dRenderPass.h"
#include "dMesh.h"
#include "dMisc.h"
#include "dSkybox.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using std::vector;
using std::string;
using std::runtime_error;
using time_p = std::chrono::system_clock::time_point;
using sys_clock = std::chrono::system_clock;
using float_ms = std::chrono::duration<float, std::milli>;
GLFWwindow* window;
int windowed_width = 1000, windowed_height = 800;
int win_width = windowed_width, win_height = windowed_height;
GLFWmonitor* monitor;
const GLFWvidmode* videoModes;
GLFWvidmode currentVidMode;
int videoModesCount;
bool fullscreen = false;
float dt;
RenderPass mainPass;
RenderPass mainPassInst;
const char* mainVert = R"(
#version 410 core
layout(location = 0) in vec3 point;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 normal;
out vec3 vPoint;
out vec2 vUv;
out vec3 vNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 persp;
void main() {
vPoint = (view * model * vec4(point, 1)).xyz;
vNormal = (view * model * vec4(normal, 0)).xyz;
vUv = uv;
gl_Position = persp * view * model * vec4(point, 1);
}
)";
const char* mainVertInstanced = R"(
#version 410 core
layout (location = 0) in vec3 point;
layout (location = 1) in vec2 uv;
layout(location = 2) in vec3 normal;
layout (location = 3) in mat4 transform;
out vec3 vPoint;
out vec2 vUv;
out vec3 vNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 persp;
void main() {
vPoint = (view * transform * model * vec4(point, 1)).xyz;
vNormal = (view * transform * model * vec4(normal, 0)).xyz;
vUv = uv;
gl_Position = persp * view * transform * model * vec4(point, 1);
}
)";
const char* mainFrag = R"(
#version 410 core
in vec3 vPoint;
in vec2 vUv;
in vec3 vNormal;
out vec4 pColor;
uniform vec3 light = vec3(-.2, .1, -3);
uniform sampler2D txtr;
void main() {
vec3 n = normalize(vNormal);
vec3 l = normalize(light - vPoint);
vec3 e = normalize(vPoint);
vec3 r = reflect(l, n);
float d = abs(dot(n, l));
float s = abs(dot(r, e));
float intensity = clamp(d+pow(s, 50), 0, 1);
pColor = vec4(intensity * texture(txtr, vUv).rgb, 1);
}
)";
vector<Skybox> skyboxes;
vector<string> skyboxPaths{
"textures/skybox/empty-space/",
"textures/skybox/sunshine/",
"textures/skybox/space/"
};
vector<string> skyboxNames{
"Empty Space",
"Sunshine",
"Space"
};
int cur_skybox = 0;
Camera camera(win_width, win_height, 60, 0.5, 1200.0f);
Mesh rock_mesh;
vector<mat4> rock_transforms;
struct Ship {
Mesh mesh;
vec3 pos = vec3(0, 0, 0);
vec3 dir = vec3(1, 0, 0);
vec3 up = vec3(0, 1, 0);
vec3 vel = vec3(0, 0, 0);
float mass = 500.0f;
float engine = 12.0f;
float drag = 30.0f;
mat4 transform() {
return Translate(pos) * Orientation(dir, up) * mesh.model;
}
void update(float dt) {
bool shift = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
// Adjust roll
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
up = vec3(RotateAxis(dir, dt * 1.25f) * vec4(up, 1));
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
up = vec3(RotateAxis(dir, dt * -1.25f) * vec4(up, 1));
// Adjust yaw
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
dir = vec3(RotateAxis(up, dt * 0.5f) * vec4(dir, 1));
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
dir = vec3(RotateAxis(up, dt * -0.5f) * vec4(dir, 1));
// Adjust pitch
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
mat4 r = RotateAxis(cross(dir, up), dt * 0.75f);
dir = vec3(r * vec4(dir, 1));
up = vec3(r * vec4(up, 1));
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
mat4 r = RotateAxis(cross(dir, up), dt * -0.75f);
dir = vec3(r * vec4(dir, 1));
up = vec3(r * vec4(up, 1));
}
vec3 force = vec3(0.0);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
force += dir * engine * (shift ? 1.5 : 1.0);
force -= drag * vel * length(vel);
vec3 acc = force / mass;
vel += dt * acc;
pos += dt * vel;
}
} ship;
void WindowResized(GLFWwindow* window, int _width, int _height) {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
camera.width = win_width = width;
camera.height = win_height = height;
glViewport(0, 0, win_width, win_height);
}
void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_Q && glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_BACKSLASH && action == GLFW_PRESS) {
cur_skybox++;
if (cur_skybox >= skyboxes.size()) cur_skybox = 0;
}
if (key == GLFW_KEY_F && action == GLFW_PRESS) {
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) != GLFW_PRESS) {
if (fullscreen) {
win_width = windowed_width;
win_height = windowed_height;
glfwSetWindowMonitor(window, NULL, 100, 100, win_width, win_height, 0);
fullscreen = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
} else {
glfwSetWindowMonitor(window, monitor, 0, 0, currentVidMode.width, currentVidMode.height, currentVidMode.refreshRate);
fullscreen = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
}
}
}
void setup() {
// Initialize GLFW callbacks
glfwSetKeyCallback(window, Keyboard);
glfwSetWindowSizeCallback(window, WindowResized);
// Load shaders into render passes
mainPass.loadShaders(&mainVert, &mainFrag);
mainPassInst.loadShaders(&mainVertInstanced, &mainFrag);
// Setup meshes
rock_mesh = Mesh("objects/rock/rock1.obj", "textures/rock/rock1.png");
ship.mesh = Mesh("objects/sparrow/StarSparrow04.obj", "textures/starsparrow.png");
ship.pos = vec3(2, 0, 0);
// Setup instances
for (int i = 0; i < 10000; i++) {
mat4 m;
m = RotateX(rand_float(-180, 180)) * m;
m = RotateY(rand_float(-180, 180)) * m;
m = RotateZ(rand_float(-180, 180)) * m;
m = Scale(rand_float(0.5, 8.0)) * m;
m = Translate(rand_float(-600, 600), rand_float(-600, 600), rand_float(-600, 600)) * m;
rock_transforms.push_back(m);
}
rock_mesh.setupInstanceBuffer((GLsizei)rock_transforms.size());
rock_mesh.loadInstances(rock_transforms);
// Setup skyboxes
for (string path : skyboxPaths) {
Skybox skybox;
skybox.setup();
skybox.loadCubemap(path);
skyboxes.push_back(skybox);
}
// Set some GL drawing context settings
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
}
void cleanup() {
rock_mesh.cleanup();
ship.mesh.cleanup();
for (Skybox skybox : skyboxes)
skybox.cleanup();
mainPass.cleanup();
mainPassInst.cleanup();
}
void draw() {
camera.loc = ship.pos - (ship.dir * 3.0) + (ship.up * 1.0);
camera.look = ship.pos + (ship.dir * 4.0);
camera.up = ship.up;
camera.fov = 60 + (length(ship.vel) * 45);
camera.update();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glCullFace(GL_BACK);
mainPass.use();
mainPass.set("txtr", 0);
mainPass.set("persp", camera.persp);
mainPass.set("view", camera.view);
mainPass.set("model", ship.transform());
ship.mesh.render();
mainPass.set("model", rock_mesh.model);
mainPassInst.use();
mainPassInst.set("txtr", 0);
mainPassInst.set("persp", camera.persp);
mainPassInst.set("view", camera.view);
mainPassInst.set("model", rock_mesh.model);
rock_mesh.renderInstanced();
skyboxes[cur_skybox].draw(camera.look - camera.loc, camera.up, camera.persp);
glFlush();
}
int main() {
srand((int)time(NULL));
// Initializing GLFW and creating window
if (!glfwInit())
return 1;
glfwWindowHint(GLFW_SAMPLES, 4);
#ifdef __APPLE__
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
window = glfwCreateWindow(win_width, win_height, "Space", NULL, NULL);
if (!window)
throw runtime_error("Failed to create GLFW window!");
// Setting up GLFW/OpenGL context
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glfwSetWindowPos(window, 100, 100);
glfwGetFramebufferSize(window, &win_width, &win_height);
glfwSwapInterval(1);
if (!(monitor = glfwGetPrimaryMonitor()))
throw runtime_error("Failed to get GLFW primary monitor!");
videoModes = glfwGetVideoModes(monitor, &videoModesCount);
currentVidMode = videoModes[videoModesCount - 1];
setup();
time_p lastSim = sys_clock::now();
while (!glfwWindowShouldClose(window)) {
time_p cur = sys_clock::now();
float_ms since = cur - lastSim;
dt = 1 / (1000.0f / since.count() / 60.0f);
lastSim = cur;
ship.update(dt);
draw();
glfwPollEvents();
glfwSwapBuffers(window);
}
cleanup();
glfwDestroyWindow(window);
glfwTerminate();
}