This repository was archived by the owner on May 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
227 lines (190 loc) · 8.01 KB
/
main.cpp
File metadata and controls
227 lines (190 loc) · 8.01 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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <shader.h>
#include <camera.h>
#include <scene.h>
#include <postprocess.h>
#include <bloom.h>
#include <bluePlane.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow *window, Scene& scene, PostProcessor& postProcessor, Bloom& bloom, bool& beeCamera, bool& isMouseCaptured);
Camera camera;
bool firstMouse = true;
bool isMouseCaptured = false;
float lastX = 400.0f, lastY = 300.0f;
float deltaTime = 0.0f, lastFrame = 0.0f;
int screenWidth = 1920;
int screenHeight = 1080;
bool windowResized = false;
GLFWwindow* initWindow() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "LearnOpenGL", NULL, NULL);
if (!window) { glfwTerminate(); return nullptr; }
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) return nullptr;
return window;
}
int main() {
GLFWwindow* window = initWindow();
if (!window) return -1;
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, screenWidth, screenHeight);
// inladen van shaders
Shader lightingShader("shaders/lighting.vert", "shaders/lighting.frag");
Shader lampShader("shaders/light.vert", "shaders/light.frag");
Scene scene;
// inladen postprocessor:
PostProcessor* postProcessor = new PostProcessor(screenWidth, screenHeight);
Bloom* bloom = new Bloom(screenWidth, screenHeight);
// inladen chroma key
BluePlane BluePlane("models/Overlay/test1.jpg", glm::vec3(-4.0f, 0.0f, -0.5f), glm::vec2(4.0f, 2.0f));
bool beeCamera = false;
while (!glfwWindowShouldClose(window)) {
// --- 1. Tijd en Frame updates ---
float currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// FPS teller
static int frameCount = 0;
static float fpsTimer = 0.0f;
frameCount++;
fpsTimer += deltaTime;
if (fpsTimer >= 1.0f) {
std::cout << "FPS: " << frameCount << std::endl;
frameCount = 0;
fpsTimer = 0.0f;
}
// updates wanneer resized
if (windowResized && screenWidth > 0 && screenHeight > 0) {
bloom->Delete();
postProcessor->Delete();
delete bloom;
delete postProcessor;
bloom = new Bloom(screenWidth, screenHeight);
postProcessor = new PostProcessor(screenWidth, screenHeight);
windowResized = false;
}
// --- 2. Input verwerken ---
if (isMouseCaptured) {
camera.ProcessKeyboard(window, deltaTime);
}
processInput(window, scene, *postProcessor, *bloom, beeCamera, isMouseCaptured);
// --- 3. Scherm schoonmaken ---
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// --- 4. Matrixen berekenen ---
glm::mat4 view;
if (beeCamera) {
glm::vec3 beePos = scene.getBeePosition();
glm::vec3 beeDir = scene.getBeeDirection();
view = glm::lookAt(beePos, beePos + beeDir, glm::vec3(0.0f, 1.0f, 0.0f));
} else {
view = camera.GetViewMatrix();
}
glm::mat4 projection = glm::perspective(glm::radians(camera.Fov), (float)screenWidth / (float)screenHeight, 0.1f, 300.0f);
// Muisklik check
static bool leftMousePressed = false;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
if (!leftMousePressed) {
leftMousePressed = true;
if (!isMouseCaptured) {
isMouseCaptured = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
firstMouse = true;
} else {
scene.checkMouseClick(view, projection, screenWidth, screenHeight);
}
}
} else {
leftMousePressed = false;
}
// --- 5. Renderen (Tekenen) ---
bloom->bindScene();
scene.Draw(lightingShader, lampShader, view, projection, camera.Position);
BluePlane.DrawPlane(view, projection);
// Overlay togglen (V)
static bool vWasPressed = false;
bool vPressed = glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS;
if (vPressed && !vWasPressed) BluePlane.showOverlay = !BluePlane.showOverlay;
vWasPressed = vPressed;
bloom->process();
bloom->render();
postProcessor->DrawFromTexture(bloom->getResultTexture());
// --- 6. Buffers wisselen en events pollen ---
glfwSwapBuffers(window);
glfwPollEvents();
}
// cleanup en afsluiten van scene
scene.Delete();
postProcessor->Delete();
bloom->Delete();
BluePlane.Delete();
delete postProcessor;
delete bloom;
glfwTerminate();
return 0;
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
if (!isMouseCaptured) return;
if (firstMouse) { lastX = xpos; lastY = ypos; firstMouse = false; }
float xoffset = xpos - lastX, yoffset = lastY - ypos;
lastX = xpos; lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
if (!isMouseCaptured) return;
camera.ProcessMouseScroll(yoffset);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
screenWidth = width;
screenHeight = height;
windowResized = true;
}
void processInput(GLFWwindow *window, Scene& scene, PostProcessor& postProcessor, Bloom& bloom, bool& beeCamera, bool& isMouseCaptured) {
// afsluiten (Q)
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// muis loslaten (ESC)
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
isMouseCaptured = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
// lampen toggelen (R)
static bool rWasPressed = false;
bool rPressed = glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS;
if (rPressed && !rWasPressed) scene.redstoneLampsOn = !scene.redstoneLampsOn;
rWasPressed = rPressed;
// Bee camera togglen (C)
static bool cWasPressed = false;
bool cPressed = glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS;
if (cPressed && !cWasPressed) {
beeCamera = !beeCamera;
scene.showBee = !beeCamera;
}
cWasPressed = cPressed;
// Bloom togglen (B)
static bool bWasPressed = false;
bool bPressed = glfwGetKey(window, GLFW_KEY_B) == GLFW_PRESS;
if (bPressed && !bWasPressed) bloom.enabled = !bloom.enabled;
bWasPressed = bPressed;
// Post-processing effecten
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) postProcessor.setEffect(PostEffect::NONE);
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) postProcessor.setEffect(PostEffect::GAUSSIAN_BLUR);
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) postProcessor.setEffect(PostEffect::EDGE_DETECT);
}