-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (110 loc) · 4.48 KB
/
Copy pathmain.cpp
File metadata and controls
137 lines (110 loc) · 4.48 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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <memory>
#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 <ChromaKey.h>
#include "InputManager.h"
// globale vars
Camera camera;
AppState appState;
std::unique_ptr<InputManager> inputManager;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// Callbacks naar InputManager
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
if (inputManager) inputManager->onMouseMove(xpos, ypos);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
if (inputManager) inputManager->onMouseScroll(yoffset);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
appState.screenWidth = width;
appState.screenHeight = height;
appState.windowResized = true;
}
GLFWwindow* initWindow(int width, int height) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(width, height, "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(appState.screenWidth, appState.screenHeight);
if (!window) return -1;
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0, 0, appState.screenWidth, appState.screenHeight);
// init managers en scene
inputManager = std::make_unique<InputManager>(window, camera, appState);
Shader lightingShader("shaders/lighting.vert", "shaders/lighting.frag");
Shader lampShader("shaders/light.vert", "shaders/light.frag");
Scene scene;
auto postProcessor = std::make_unique<PostProcessor>(appState.screenWidth, appState.screenHeight);
auto bloom = std::make_unique<Bloom>(appState.screenWidth, appState.screenHeight);
// inladen chroma key
ChromaKey chromaKey("textures/spongebob.jpg");
// --- RENDER LOOP ---
while (!glfwWindowShouldClose(window)) {
// 1. Tijd updates
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// Resizing afhandelen
if (appState.windowResized && appState.screenWidth > 0 && appState.screenHeight > 0) {
bloom = std::make_unique<Bloom>(appState.screenWidth, appState.screenHeight);
postProcessor = std::make_unique<PostProcessor>(appState.screenWidth, appState.screenHeight);
appState.windowResized = false;
}
// 2. Input verwerken
inputManager->update(deltaTime, scene, *postProcessor, *bloom, chromaKey);
// 3. Clear Buffers
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 4. Matrixen bereken
glm::mat4 view = appState.beeCamera ?
glm::lookAt(scene.getBeePosition(), scene.getBeePosition() + scene.getBeeDirection(), glm::vec3(0.0f, 1.0f, 0.0f)) :
camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(
glm::radians(camera.Fov),
(float)appState.screenWidth / (float)appState.screenHeight,
0.1f, 300.0f
);
// 4. Muisklik afhandelen
inputManager->handleMouseClick(scene, view, projection);
// 5. Render Pipeline
bloom->bindScene();
scene.Draw(lightingShader, lampShader, view, projection, camera.Position);
chromaKey.DrawPlane();
bloom->process();
bloom->render();
postProcessor->Draw(bloom->getResultTexture());
// 6. Swap Buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}