-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandelbulbViewer.cpp
211 lines (161 loc) · 6.46 KB
/
MandelbulbViewer.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
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
#include "MandelbulbViewer.h"
#include "Constants.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
MandelbulbViewer::MandelbulbViewer(const int &windowWidth, const int &windowHeight, const int& max_fps)
: engine(nullptr)
, viewer(new MandelbulbViewer::ViewerInputListener())
, cam(new CameraController((float)windowWidth, (float)windowHeight))
, shader(new sf::Shader())
, quad(nullptr)
, infoFont()
, info()
, infoBg()
{
this->engine = new Engine("Mandelbulb Viewer", windowWidth, windowHeight, max_fps);
}
MandelbulbViewer::~MandelbulbViewer()
{
if(engine != nullptr) delete engine;
if(viewer != nullptr) delete viewer;
if(cam != nullptr) delete cam;
if(shader != nullptr) delete shader;
if(quad != nullptr) delete quad;
}
int MandelbulbViewer::run()
{
engine->setInitFunc(std::bind(&MandelbulbViewer::init, this));
engine->setPreupdateFunc(std::bind(&MandelbulbViewer::preupdate, this));
engine->setUpdateFunc(std::bind(&MandelbulbViewer::update, this, std::placeholders::_1));
engine->setDrawFunc(std::bind(&MandelbulbViewer::draw, this));
return engine->run();
}
void MandelbulbViewer::init()
{
if (!sf::Shader::isAvailable())
{
std::cout << "Shading not supported!" << std::endl;
system("PAUSE");
exit(EXIT_FAILURE);
}
if (!shader->loadFromFile("mandelbulb.vert", "mandelbulb.frag"))
{
// some error occurred
std::cout << "Unable to load shaders." << std::endl;
system("PAUSE");
exit(EXIT_FAILURE);
}
float screenWidth = (float)engine->getWindow()->getSize().x;
float screenHeight = (float)engine->getWindow()->getSize().y;
quad = new sf::RectangleShape(sf::Vector2f(engine->getWindow()->getSize()));
infoBg.setFillColor(sf::Color(0, 0, 0, 150));
if (!infoFont.loadFromFile("arial.ttf")) {
cout << "Unable to find font arial.tff" << endl;
system("PAUSE");
exit(EXIT_FAILURE);
}
info.setString("hello");
info.setFont(infoFont);
info.setCharacterSize(INFO_FONT_SIZE_PX);
info.setFillColor(sf::Color::White);
sf::Shader::bind(shader);
sf::Transform viewMatrix = engine->getWindow()->getView().getTransform();
shader->setUniform("projViewMatrix", (sf::Glsl::Mat4)viewMatrix.getMatrix());
shader->setUniform("screen_width", screenWidth);
shader->setUniform("screen_height", screenHeight);
sf::Shader::bind(NULL);
engine->registerInputListener(viewer);
engine->registerInputListener(cam);
}
void MandelbulbViewer::preupdate()
{
}
void MandelbulbViewer::update(const float dt)
{
// this is when the camera needs to be passed to the shader
sf::Shader::bind(shader);
sf::Vector3f camera_position = cam->camera->position.asSFML();
shader->setUniform("camera_position", (sf::Glsl::Vec3)camera_position);
sf::Vector3f camera_direction = cam->camera->direction.asSFML();
shader->setUniform("camera_direction", (sf::Glsl::Vec3)camera_direction);
sf::Vector3f camera_up = cam->camera->up.asSFML();
shader->setUniform("camera_up", (sf::Glsl::Vec3)camera_up);
float scale = cam->camera->scale();
shader->setUniform("scale", scale);
float screenWidth = (float)engine->getWindow()->getSize().x;
float screenHeight = (float)engine->getWindow()->getSize().y;
shader->setUniform("aspect", screenWidth/screenHeight);
shader->setUniform("fov", FOV);
shader->setUniform("screen_width", screenWidth);
shader->setUniform("screen_height", screenHeight);
shader->setUniform("epsilon_factor", EPSILON_FACTOR);
shader->setUniform("epsilon_limit", EPSILON_LIMIT);
shader->setUniform("max_dist", MAX_DIST);
shader->setUniform("max_bailout", MAX_BAILOUT);
shader->setUniform("max_iter", MAX_ITER);
shader->setUniform("min_iter", MIN_ITER);
shader->setUniform("max_steps", MAX_STEPS);
shader->setUniform("power", POWER);
shader->setUniform("fog_enabled", viewer->fogToggle);
shader->setUniform("fog_max_dist", FOG_MAX_DIST);
shader->setUniform("glow_dist", GLOW_DIST);
shader->setUniform("glow_enabled", viewer->glowToggle);
shader->setUniform("heat_enabled", viewer->heatToggle);
sf::Shader::bind(NULL);
if (viewer->infoToggle) updateInfo();
}
void MandelbulbViewer::draw()
{
engine->getWindow()->draw(*quad, shader);
if (viewer->infoToggle) {
engine->getWindow()->draw(infoBg);
engine->getWindow()->draw(info);
}
}
float lerp(float a, float b, float f) { return a + f * (b - a); }
void MandelbulbViewer::updateInfo()
{
stringstream ss;
float fps = engine->getFPS();
ss << "fps: " << fps << endl;
float speed = cam->getSpeed();
ss << "speed: " << speed << endl;
float mindist = cam->camera->estimateMandelbulbDistance();
ss << "mindist: " << mindist << endl;
float scale = cam->camera->scale();
ss << "scale: " << scale << endl;
ss << "1/scale: " << 1.0f/scale << endl;
ss << "pow(scale, 2): " << pow(scale, 2) << endl;
float epsilon = EPSILON_FACTOR * scale;
ss << "epsilon: " << epsilon << endl;
float viewlimit = MAX_DIST * scale;
ss << "viewlimit: " << viewlimit << endl;
//float curr_max_iter = lerp(MIN_ITER, MAX_ITER, 1.0 - scale);
//ss << "curr_max_iter: " << curr_max_iter << endl;
//float max_bailout = MAX_BAILOUT * scale;
//ss << "max_bailout: " << max_bailout << endl;
info.setString(ss.str());
infoBg.setSize(sf::Vector2f(info.getGlobalBounds().width, info.getGlobalBounds().height));
}
MandelbulbViewer::ViewerInputListener::ViewerInputListener()
: infoToggle(false)
, fogToggle(FOG_ENABLED)
, glowToggle(GLOW_ENABLED)
, heatToggle(HEAT_ENABLED)
{}
void MandelbulbViewer::ViewerInputListener::update(const float dt) {}
void MandelbulbViewer::ViewerInputListener::keyPressed(const sf::Keyboard::Key &key, const float dt)
{
if (key == sf::Keyboard::Tab) infoToggle = !infoToggle;
if (key == sf::Keyboard::Num1) fogToggle = !fogToggle;
if (key == sf::Keyboard::Num2) glowToggle = !glowToggle;
if (key == sf::Keyboard::Num3) heatToggle = !heatToggle;
}
void MandelbulbViewer::ViewerInputListener::keyHeld(const sf::Keyboard::Key &key, const float dt) {}
void MandelbulbViewer::ViewerInputListener::keyReleased(const sf::Keyboard::Key &key, const float dt) {}
void MandelbulbViewer::ViewerInputListener::mouseMoved(const int mouseX, const int mouseY, const int dx, const int dy, const float dt) {}
void MandelbulbViewer::ViewerInputListener::mousePressed(const sf::Mouse::Button &btn, const int mouseX, const int mouseY, const float dt) {}
void MandelbulbViewer::ViewerInputListener::mouseReleased(const sf::Mouse::Button &btn, const int mouseX, const int mouseY, const float dt) {}
void MandelbulbViewer::ViewerInputListener::mouseScrolled(const float delta, const int mouseX, const int mouseY, const float dt) {}