-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cpp
149 lines (128 loc) · 3.73 KB
/
Application.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
//
// Created by adamyuan on 4/17/18.
//
#include <GL/gl3w.h>
#include "Application.hpp"
#include "Resources.hpp"
#include "Config.hpp"
void Application::init_window()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
window_ = glfwCreateWindow(kWidth, kHeight, "", nullptr, nullptr);
glfwMakeContextCurrent(window_);
glfwSetWindowUserPointer(window_, (void*)this);
glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetKeyCallback(window_, key_callback);
glfwSetWindowFocusCallback(window_, focus_callback);
gl3wInit();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
}
Application::Application()
{
init_window();
debug_voxel_ = show_edge_ = false;
control_ = indirect_trace_ = show_albedo_ = true;
camera_.Position() = kInitialPos;
res::Initialize();
sun_pos_ = glm::vec3(-24.6f, 50.0f, -12.0f);
res::UpdateLight(sun_pos_);
renderer_.Initialize();
}
void Application::Run()
{
char title[512];
while(!glfwWindowShouldClose(window_))
{
framerate_.Update();
if(control_) cam_control();
renderer_.Render(debug_voxel_, indirect_trace_, show_albedo_, show_edge_);
glfwSwapBuffers(window_);
glfwPollEvents();
sprintf(title, "%f\n", framerate_.GetFps());
glfwSetWindowTitle(window_, title);
}
}
void Application::cam_control()
{
float speed = framerate_.GetDelta() * kSpeed;
if(glfwGetKey(window_, GLFW_KEY_W) == GLFW_PRESS)
camera_.MoveForward(speed, 0.0f);
if(glfwGetKey(window_, GLFW_KEY_A) == GLFW_PRESS)
camera_.MoveForward(speed, 90.0f);
if(glfwGetKey(window_, GLFW_KEY_D) == GLFW_PRESS)
camera_.MoveForward(speed, -90.0f);
if(glfwGetKey(window_, GLFW_KEY_S) == GLFW_PRESS)
camera_.MoveForward(speed, 180.0f);
if(glfwGetKey(window_, GLFW_KEY_SPACE) == GLFW_PRESS)
camera_.MoveUp(speed);
if(glfwGetKey(window_, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera_.MoveUp(-speed);
bool light_changed = false;
if(glfwGetKey(window_, GLFW_KEY_LEFT) == GLFW_PRESS)
{
sun_pos_.x -= speed;
light_changed = true;
}
if(glfwGetKey(window_, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
sun_pos_.x += speed;
light_changed = true;
}
if(glfwGetKey(window_, GLFW_KEY_UP) == GLFW_PRESS)
{
sun_pos_.z -= speed;
light_changed = true;
}
if(glfwGetKey(window_, GLFW_KEY_DOWN) == GLFW_PRESS)
{
sun_pos_.z += speed;
light_changed = true;
}
if(light_changed)
{
res::UpdateLight(sun_pos_);
renderer_.UpdateLight();
}
double mouse_x, mouse_y;
glfwGetCursorPos(window_, &mouse_x, &mouse_y);
camera_.MouseControl((float)(mouse_x - kMouseX), (float)(mouse_y - kMouseY), .3f);
glfwSetCursorPos(window_, kMouseX, kMouseY);
res::UpdateCam(camera_);
res::cam_view = camera_.GetMatrix();
res::cam_pos = camera_.GetPosition();
}
void Application::key_callback(GLFWwindow *window, int key, int, int action, int)
{
auto *app = (Application *)glfwGetWindowUserPointer(window);
if(action == GLFW_PRESS)
{
if(key == GLFW_KEY_X)
app->debug_voxel_ = !app->debug_voxel_;
else if(key == GLFW_KEY_V)
app->indirect_trace_ = !app->indirect_trace_;
else if(key == GLFW_KEY_C)
app->show_albedo_ = !app->show_albedo_;
else if(key == GLFW_KEY_E)
app->show_edge_ = !app->show_edge_;
else if(key == GLFW_KEY_ESCAPE)
{
app->control_ = !app->control_;
glfwSetCursorPos(window, kMouseX, kMouseY);
glfwSetInputMode(window, GLFW_CURSOR, app->control_ ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
}
}
}
void Application::focus_callback(GLFWwindow *window, int focused)
{
auto *app = (Application *)glfwGetWindowUserPointer(window);
if(focused == GLFW_FALSE)
{
app->control_ = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}