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
|
#include "../log.h"
#include "object.h"
#include "scene.h"
#include "pass.h"
using namespace Mauri;
auto Object::parse(Parser &pr, const json &root) -> Object *
{
Object *object = new Object();
pr.map_value<u32>(root, "id", &object->id, 0);
pr.map_value<std::string>(root, "name", &object->name, "");
if (root.contains("dependencies"))
{
for (const json &id : root["dependencies"])
{
object->deps.push_back(id);
}
}
// Filter deps that are either have the same id as this
// object or are repeated. This could be handled be sorting objects
// on the scene in order of deps but I'm not sure if that would
// break other things.
auto it = object->deps.begin();
while (it != object->deps.end())
{
bool remove = ((u32)(*it) == object->id);
for (auto rt = object->deps.begin(); rt != object->deps.end(); ++rt)
{
if (rt != it && *rt == *it)
{
remove = true;
break;
}
}
if (remove) object->deps.erase(it);
else it++;
}
pr.map_value<bool>(root, "visible", &object->visible, true);
pr.map_value<vec3>(root, "angles", &object->angles, vec3(0.f));
pr.map_value<vec2>(root, "size", &object->size, vec2(0.f));
pr.map_value<vec3>(root, "scale", &object->scale, vec3(1.f));
pr.map_value<vec3>(root, "origin", &object->origin, vec3(0.f));
if (object->origin[2] != 0.f)
{
warn("non 0 origin[2]");
object->origin[2] = 0.f;
}
pr.map_value<vec3>(root, "color", &object->color, vec3(1.f));
pr.map_value<f32>(root, "alpha", &object->alpha, 1.f);
pr.map_value<s32>(root, "colorBlendMode", &object->color_blend_mode, 0);
object->color4 = vec4{object->color[0], object->color[1], object->color[2], object->alpha};
if (root.contains("image"))
{
const json &image = root["image"];
if (image.is_string()) object->model = Model::parse(pr, image);
if (!object->model) parse_fail(object);
}
if (root.contains("config"))
{
pr.map_value<bool>(root["config"], "passthrough", &object->passthrough, false);
}
if (root.contains("effects"))
{
for (const json &effect : root["effects"])
{
Effect *eff = Effect::parse(pr, effect);
if (eff) object->effects.emplace_back(eff);
}
}
return object;
}
auto Object::load(Engine *engine) -> void
{
if (this->loaded_as_dep || this->loaded) return;
for (u32 dep : this->deps)
{
if (dep == this->id) continue;
Object *object = engine->scene->get_object_by_id(dep);
if (object)
{
object->load(engine);
object->loaded_as_dep = true;
}
}
if (this->model)
{
if (this->model->fullscreen) this->fullscreen = true;
if (this->model->passthrough) this->passthrough = true;
}
if (this->size[0] == 0.f) this->size[0] = this->model->width;
if (this->size[1] == 0.f) this->size[1] = this->model->height;
if (this->fullscreen)
{
this->size[0] = engine->scene->width;
this->size[1] = engine->scene->height;
}
else if (this->passthrough)
{
if (this->size[0] == 0.f) this->size[0] = engine->scene->width;
if (this->size[1] == 0.f) this->size[1] = engine->scene->height;
}
const std::string buffer_name = "_rt_imageLayerComposite_" + std::to_string(this->id) + "_";
this->buffer_a = buffer_name + "a";
this->buffer_b = buffer_name + "b";
engine->create_framebuffer(this->buffer_a, this->size[0], this->size[1], 1.f);
engine->create_framebuffer(this->buffer_b, this->size[0], this->size[1], 1.f);
if (this->effects.size() > 0)
{
for (auto it = this->effects.begin(); it != this->effects.end();)
{
if (!(*it)->visible) it = this->effects.erase(it);
else it++;
}
}
if (this->model) this->model->load(engine, this);
for (u32 i = 0; i < this->effects.size(); i++)
{
this->effects[i]->load(engine, this, i == (this->effects.size() - 1));
}
if (this->effects.size() != 0 && this->color_blend_mode != 0)
{
Pass blend_pass;
blend_pass.intermediate = false;
blend_pass.object = this;
blend_pass.effect = nullptr;
blend_pass.model = false;
blend_pass.combine = true;
blend_pass.target = "previous";
blend_pass.textures[0] = "previous";
blend_pass.textures[1] = COMBINE_BUFFER;
blend_pass.shader = "passthroughblend";
blend_pass.combos.push_back(Combo("BLENDMODE", this->color_blend_mode));
blend_pass.combos.push_back(Combo("TRANSFORM", 1));
this->effects.back()->passes.emplace_back(new RenderPass(engine, &blend_pass));
}
this->loaded = true;
}
auto Object::create_objects(Engine *engine, Texture *texture) -> void
{
this->model_object = new RenderObject(MODEL, this, texture);
this->combine_object = new RenderObject(COMBINE, this, texture);
this->combine_model = glm::ortho(0.f, (f32)engine->scene->width, (f32)engine->scene->height, 0.f, -3.f, 1.f);
this->combine_model = glm::translate(this->combine_model, this->origin);
if (texture && texture->is_gif())
{
// Something about positioning is wrong, affects most gifs.
}
if (this->passthrough && !this->fullscreen)
{
this->model_model = this->combine_model;
this->model_model = glm::rotate(this->model_model, this->angles[0], vec3{1.f, 0.f, 0.f});
this->model_model = glm::rotate(this->model_model, this->angles[1], vec3{0.f, 1.f, 0.f});
this->model_model = glm::rotate(this->model_model, this->angles[2], vec3{0.f, 0.f, 1.f});
this->model_model = glm::scale(this->model_model, this->scale);
this->model_model = glm::translate(this->model_model, vec3{this->size[0] / -2.f, this->size[1] / -2.f, 0.f});
}
else
{
this->model_model = glm::ortho(0.f, this->size[0], this->size[1], 0.f, -3.f, 1.f);
}
this->combine_model = glm::rotate(this->combine_model, this->angles[0], vec3{1.f, 0.f, 0.f});
this->combine_model = glm::rotate(this->combine_model, this->angles[1], vec3{0.f, 1.f, 0.f});
this->combine_model = glm::rotate(this->combine_model, this->angles[2], vec3{0.f, 0.f, 1.f});
this->combine_model = glm::scale(this->combine_model, this->scale);
}
auto Object::draw_internal(Engine *engine) -> void
{
#ifdef FRAME_STEP
al_printf("%s (model):\n", this->name.c_str());
#endif
this->buffer_switch = true;
for (RenderPass *pass : this->passes)
{
#ifdef FRAME_STEP
al_printf("\t%s\n", pass->shader->origin()->name.c_str());
#endif
pass->draw(engine);
}
for (Effect *effect : this->effects)
{
if (effect->visible) effect->draw(engine);
}
}
auto Object::draw(Engine *engine) -> void
{
for (s32 dep : this->deps)
{
Object *object = engine->scene->get_object_by_id(dep);
if (object) object->draw_internal(engine);
}
this->draw_internal(engine);
}
auto Object::get_target_buffer() const -> const std::string &
{
return (this->buffer_switch) ? this->buffer_a : this->buffer_b;
}
auto Object::get_texture_buffer() const -> const std::string &
{
return (this->buffer_switch) ? this->buffer_b : this->buffer_a;
}
Object::~Object()
{
if (this->model) delete this->model;
if (this->model_object) delete this->model_object;
if (this->combine_object) delete this->combine_object;
for (RenderPass *pass : this->passes)
{
delete pass;
}
for (Effect *effect : this->effects)
{
delete effect;
}
}
|