-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCanvas.cpp
More file actions
143 lines (114 loc) · 3.91 KB
/
Canvas.cpp
File metadata and controls
143 lines (114 loc) · 3.91 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
#include "Canvas.h"
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
constexpr int WINDOW_WIDTH = 800;
constexpr int WINDOW_HEIGHT = 800;
static void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
Canvas& Canvas::GetInstance() {
static Canvas canvas(WINDOW_WIDTH, WINDOW_HEIGHT);
return canvas;
}
Canvas::~Canvas() {
_renderer.reset();
_lightningTree.reset();
glfwDestroyWindow(_window);
glfwTerminate();
}
void Canvas::Run() {
while (!glfwWindowShouldClose(_window)) {
update();
draw();
glfwSwapBuffers(_window);
glfwPollEvents();
}
}
Canvas::Canvas(int width, int height) : _width(width), _height(height) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
_window = glfwCreateWindow(width, height, "Lightning Generator", nullptr, nullptr);
if (!_window) {
fprintf(stderr, "Failed to create window\n");
glfwTerminate();
return;
}
glfwMakeContextCurrent(_window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fprintf(stderr, "Failed to load glad!\n");
glfwTerminate();
return;
}
glfwSetFramebufferSizeCallback(_window, framebuffer_size_callback);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
init();
}
void Canvas::update() {
}
void Canvas::draw() {
auto projection = glm::ortho(0.f, static_cast<float>(WINDOW_WIDTH), 0.f, static_cast<float>(WINDOW_HEIGHT));
auto view = glm::identity<glm::mat4>();
auto projView = projection * view;
_renderer->SwitchFrameBuffer(0);
_renderer->ClearCurrentFrame();
_renderer->Begin(projView);
{
_renderer->DrawLightningToScreen(_fboTexture[1]);
}
_renderer->End();
}
void Canvas::init() {
_renderer = std::make_shared<Renderer>(_width, _height);
_fbo[0] = _renderer->CreateNewFrameBuffer(_width, _height, _fboTexture[0]);
_fbo[1] = _renderer->CreateNewFrameBuffer(_width, _height, _fboTexture[1]);
// A straight vertical path from top to bottom
LightningNode top = { glm::vec2(400, 800), 0, 10 };
LightningNode bot = { glm::vec2(400, 0), 0, 8 };
std::vector<LightningNode> keyNodes{ top, bot };
//for (int i = 0; i < 32; i++) {
// float x = i * 800.f / 32;
// float t = x / 800.f * 10;
// keyNodes.push_back({ glm::vec2(x, 400 + sin(t) * 200), 0, 2 });
// if (i != 0 && i != 31) {
// keyNodes.push_back({ glm::vec2(x, 400 + sin(t) * 200), 0, 2 });
// }
//}
_lightningTree = std::make_shared<LightningTree>(keyNodes, glm::vec2(1.14, 3.554), 0.5f, 0.3f, 0.6f);
for (int i = 0; i < 12; i++) {
_lightningTree->RunOneStep(_renderer);
}
auto projection = glm::ortho(0.f, static_cast<float>(WINDOW_WIDTH), 0.f, static_cast<float>(WINDOW_HEIGHT));
auto view = glm::identity<glm::mat4>();
auto projView = projection * view;
// Render to texture module
_renderer->SwitchFrameBuffer(_fbo[0]);
_renderer->ClearCurrentFrame();
_renderer->Begin(projView);
{
glEnable(GL_BLEND);
glBlendEquation(GL_MAX);
_lightningTree->Draw(_renderer);
glDisable(GL_BLEND);
}
_renderer->End();
//Render bloom effect
_renderer->SwitchFrameBuffer(_fbo[1]);
_renderer->ClearCurrentFrame();
_renderer->Begin(projView);
{
_renderer->DrawLightningGaussian(_fboTexture[0], true);
}
_renderer->End();
//_renderer->SwitchFrameBuffer(_fbo[0]);
//_renderer->ClearCurrentFrame();
//_renderer->Begin(projView);
//{
// _renderer->DrawLightningGaussian(_fboTexture[1], false);
//}
//_renderer->End();
}