-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
284 lines (210 loc) · 8.07 KB
/
main.cpp
File metadata and controls
284 lines (210 loc) · 8.07 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
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <chrono>
#include <thread>
#include "include/GLFW/glfw3.h"
#include "include/image.h"
#include "include/vec2.h"
#include "include/vec3.h"
#include "include/color.h"
#include "include/renderer.h"
#include "include/model.h"
#include "include/camera.h"
using namespace std;
const Color RED = Color(1, 0, 0);
const Color GREEN = Color(0, 1, 0);
const Color BLUE = Color(0, 0, 1);
const Color WHITE = Color(1, 1, 1);
const Color BLACK = Color(0, 0, 0);
struct GameState {
Camera camera = Camera();
};
GameState game_state;
void error_callback(int error, const char* description) {
cerr << "Error: " << description << endl;
}
float get_delta_time() {
static double last_time = glfwGetTime();
double current_time = glfwGetTime();
float delta_time = float(current_time - last_time);
last_time = current_time;
return delta_time;
}
bool keys[1024];
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key >= 0 && key < 1024) {
if (action == GLFW_PRESS) keys[key] = true;
else if (action == GLFW_RELEASE) keys[key] = false;
}
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
void process_movement() {
static float velocity = 0.0f;
float max_speed = 5.0f;
float accel = 10.0f;
float decel = 10.0f;
float delta_time = get_delta_time();
if (keys[GLFW_KEY_W] || keys[GLFW_KEY_A] || keys[GLFW_KEY_S] || keys[GLFW_KEY_D]) {
velocity += accel * delta_time;
if (velocity > max_speed) velocity = max_speed;
} else {
velocity -= decel * delta_time;
if (velocity < 0) velocity = 0;
}
float radians = to_radians(game_state.camera.rotation.x);
if (keys[GLFW_KEY_D]) {
float delta_z = cos(radians);
float delta_y = -sin(radians);
game_state.camera.pos.z += delta_y * velocity * delta_time;
game_state.camera.pos.y += delta_z * velocity * delta_time;
}
if (keys[GLFW_KEY_A]) {
float delta_z = -cos(radians);
float delta_y = sin(radians);
game_state.camera.pos.z += delta_y * velocity * delta_time;
game_state.camera.pos.y += delta_z * velocity * delta_time;
}
if (keys[GLFW_KEY_W]) {
float delta_z = cos(radians);
float delta_y = sin(radians);
game_state.camera.pos.z += delta_z * velocity * delta_time;
game_state.camera.pos.y += delta_y * velocity * delta_time;
}
if (keys[GLFW_KEY_S]) {
float delta_z = cos(radians);
float delta_y = sin(radians);
game_state.camera.pos.z -= delta_z * velocity * delta_time;
game_state.camera.pos.y -= delta_y * velocity * delta_time;
}
if (keys[GLFW_KEY_Q]) {
game_state.camera.pos.x -= 1.0f * delta_time;
}
if (keys[GLFW_KEY_E]) {
game_state.camera.pos.x += 1.0f * delta_time;
}
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
static const double sensitivity = 0.1;
static const double smoothing = 0.85;
static const double dead_zone = 0.05;
int window_width, window_height;
glfwGetWindowSize(window, &window_width, &window_height);
double center_x = window_width / 2.0;
double center_y = window_height / 2.0;
double xoffset = xpos - center_x;
double yoffset = ypos - center_y;
glfwSetCursorPos(window, center_x, center_y);
xoffset *= sensitivity;
yoffset *= sensitivity;
if (fabs(xoffset) < dead_zone) xoffset = 0.0;
if (fabs(yoffset) < dead_zone) yoffset = 0.0;
static double smoothed_x = 0.0;
static double smoothed_y = 0.0;
smoothed_x = smoothed_x * smoothing + xoffset * (1.0 - smoothing);
smoothed_y = smoothed_y * smoothing + yoffset * (1.0 - smoothing);
game_state.camera.rotation.x += smoothed_x;
game_state.camera.rotation.y += smoothed_y;
game_state.camera.rotation.z = 0;
}
void initialize_depth_buffer(int image_width, int image_height, std::vector<std::vector<double>>& depth_buffer) {
// Resize the depth buffer to match the image dimensions
depth_buffer.resize(image_width, std::vector<double>(image_height));
// Initialize all values to infinity
for (int i = 0; i < image_width; i++) {
for (int j = 0; j < image_height; j++) {
depth_buffer[i][j] = std::numeric_limits<double>::infinity();
//depth_buffer[i][j] = 0;
}
}
}
int main() {
if (!glfwInit()) {
cerr << "GLFW failed to initialize.\n";
return -1;
}
int image_width = 1000;
int image_height = 1000;
double viewport_width = 2;
double viewport_height = viewport_width * image_height / image_width;
double viewport_info[4] = { double(image_width), double(image_height), viewport_width, viewport_height };
double distance_to_projection_plane = 1;
game_state.camera = Camera(Vec3(0, 0, 0), Vec3(0, 0, 0), viewport_width, viewport_height);
auto renderer = Renderer(game_state.camera);
Scene scene;
vector<Vec3> vertices;
vertices.push_back(Vec3(1, 1, 1));
vertices.push_back(Vec3(-1, 1, 1));
vertices.push_back(Vec3(-1, -1, 1));
vertices.push_back(Vec3(1, -1, 1));
vertices.push_back(Vec3(1, 1, -1));
vertices.push_back(Vec3(-1, 1, -1));
vertices.push_back(Vec3(-1, -1, -1));
vertices.push_back(Vec3(1, -1, -1));
/*
vector<Vec3> vertices;
vertices.push_back(Vec3(-1, 1, 0));
vertices.push_back(Vec3(1, 1, 0));
vertices.push_back(Vec3(-1, -1, 0));
vertices.push_back(Vec3(1, -1, 0));
vertices.push_back(Vec3(0, 0, 2));
*/
Vec3 light_location = {3.0, 0.0, 7};
Model cube = Model(vertices, Vec3(-1.5, 3, 7), Vec3(0, 0, 0), 1.0, RED);
Model cube2 = Model(vertices, Vec3(0.0, 0.0, 7), RED);
Model cube_l = Model(vertices, light_location, Vec3(0, 0, 0), 0.1, RED);
scene.add_model(cube);
scene.add_model(cube2);
scene.add_model(cube_l);
Light light = {light_location, 1.0, Color(1.0, 1.0, 1.0)}; // White light at (0, 0, 10)
Material material = {
Color(0.1, 0.1, 0.1), // ambient
Color(0.7, 0.7, 0.7), // diffuse
Color(0.1, 0.1, 0.1), // specular
Color(1.0, 1.0, 1.0), // color
32.0 // shininess
};
const int frame_rate = 99999;
const chrono::milliseconds target_frame_duration(1000 / frame_rate);
GLFWwindow* window = glfwCreateWindow(image_width, image_height, "OpenGL Window", NULL, NULL);
if (!window) {
cerr << "Failed to create GLFW window.\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1); //Enable VSync
glfwSetWindowUserPointer(window, &game_state);
glfwSetKeyCallback(window, key_callback);
if (glfwRawMouseMotionSupported()) {
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
}
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
std::vector<std::vector<double>> depth_buffer;
while (!glfwWindowShouldClose(window)) {
auto frame_start_time = chrono::high_resolution_clock::now();
Image image = Image(image_width, image_height);
initialize_depth_buffer(image_width, image_height, depth_buffer);
renderer.set_camera(game_state.camera);
scene.render(renderer, material, light, image, viewport_info, depth_buffer);
image.output_image();
//clog << game_state.camera.rotation.x << " " << game_state.camera.rotation.y << " " << game_state.camera.rotation.z << endl;
glfwPollEvents();
process_movement();
auto frame_end_time = chrono::high_resolution_clock::now();
auto frame_duration = duration_cast<chrono::milliseconds>(frame_end_time - frame_start_time);
if (frame_duration < target_frame_duration) {
this_thread::sleep_for(target_frame_duration - frame_duration);
}
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}