summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2020-10-29 16:49:28 -0400
committerAndrew Opalach <andrew@akon.city> 2020-10-29 16:49:28 -0400
commit96d9a4f8b0b252a185e7e685fbb7efb446699778 (patch)
tree7b82e7c5a897ab73e7c1252d84d5df5b81d2c0d8
parent99c22028164d423377a37873572cb1654756591f (diff)
downloadmauri-96d9a4f8b0b252a185e7e685fbb7efb446699778.tar.gz
mauri-96d9a4f8b0b252a185e7e685fbb7efb446699778.tar.bz2
mauri-96d9a4f8b0b252a185e7e685fbb7efb446699778.zip
fix alternating framebuffers on objects, add toggleable drawing
-rw-r--r--TODO18
-rw-r--r--src/assets.cc29
-rw-r--r--src/assets.h4
-rw-r--r--src/context.cc22
-rw-r--r--src/context.h18
-rw-r--r--src/context_glfw.cc20
-rw-r--r--src/context_glfw.h2
-rw-r--r--src/context_null.h13
-rw-r--r--src/context_x11.cc2
-rw-r--r--src/context_x11.h6
-rw-r--r--src/engine.cc26
-rw-r--r--src/engine.h3
-rw-r--r--src/gl.cc138
-rw-r--r--src/gl.h1
-rw-r--r--src/mauri.cc17
-rw-r--r--src/objects/object.cc25
-rw-r--r--src/objects/object.h3
-rw-r--r--src/objects/pass.cc57
-rw-r--r--src/objects/pass.h7
-rw-r--r--src/objects/scene.cc4
-rw-r--r--src/texture.cc2
-rw-r--r--src/texture.h1
22 files changed, 231 insertions, 187 deletions
diff --git a/TODO b/TODO
index 3bfa9c4..e058572 100644
--- a/TODO
+++ b/TODO
@@ -1,24 +1,20 @@
-[x] better (cleaner) json handling
+[ ] start commiting in a more structured way
+ - most of the bulk work is done so this will make things easier going forward
-[x] pass parser as pointer everywhere
+[ ] make gl_enabled toggleable in code (i.e. toggleable at runtime)
-[x] cleanup visualizer code
+[ ] cleanup pass on non-objects (asset, texture, shader, etc)
-[X] cleanup build system
+[ ] objects with blendmode other than 0 have extra pass using blendpassthrough shader
-[ ] start commiting in a more structured way
- - most of the bulk work is done so this will make things easier going forward
+[ ] really analyze the structue of a pass/object and try to simplify the code
+ - verify if the model/robject order is correct (renderdoc)
[ ] more acurate visualizer
-[ ] seperate program to extract scene.pkg
-
[ ] gifs
- https://github.com/notscuffed/repkg/blob/master/RePKG.Application/Texture/TexFrameInfoContainerReader.cs
-[ ] RG88 & R8 textures
- - i think it works need to test more
-
Broken:
[ ] ./mauri ../../../c/workshop_wallpapers/1703298496/scene.pkg
- xray only works in the middle
diff --git a/src/assets.cc b/src/assets.cc
index fdfdd0d..7251ceb 100644
--- a/src/assets.cc
+++ b/src/assets.cc
@@ -9,20 +9,20 @@
#include <iostream>
#include <math.h>
-using namespace Mauri;
-
namespace Mauri
{
-static AssetManager manager;
+static AssetManager _asset_manager;
AssetManager *asset_manager()
{
- return &manager;
+ return &_asset_manager;
}
} // namespace Mauri
+using namespace Mauri;
+
Asset::Asset()
{
type = ASSET_VOID;
@@ -149,6 +149,18 @@ bool AssetManager::load_package(const std::string &path)
return true;
}
+void AssetManager::unload_package()
+{
+ for (auto &asset : files)
+ {
+ delete asset;
+ }
+
+ files.clear();
+
+ delete pkg;
+}
+
Asset *AssetManager::get_file(const std::string &part, FileTypeHint hint)
{
std::string path = part;
@@ -274,12 +286,3 @@ void AssetManager::write_files(const std::string &output_path)
}
}
}
-
-AssetManager::~AssetManager()
-{
- for (auto &file : files)
- {
- delete file;
- }
- delete pkg;
-}
diff --git a/src/assets.h b/src/assets.h
index e3fc47c..1217d95 100644
--- a/src/assets.h
+++ b/src/assets.h
@@ -102,13 +102,15 @@ class AssetManager
{
public:
AssetManager() {};
- ~AssetManager();
+ ~AssetManager() {};
std::vector<Asset *> files;
Asset *get_file(const std::string &part, FileTypeHint hint);
bool load_package(const std::string &path);
+ void unload_package();
+
void write_files(const std::string &output_path);
private:
diff --git a/src/context.cc b/src/context.cc
index 6de03a9..9f20b54 100644
--- a/src/context.cc
+++ b/src/context.cc
@@ -1,6 +1,28 @@
#include "context.h"
#include "log.h"
+namespace Mauri {
+
+static Context *_context = nullptr;
+
+Context *context()
+{
+ return _context;
+}
+
+void set_context(Context *c)
+{
+ if (_context != nullptr)
+ {
+ _context->close_window();
+ delete _context;
+ }
+
+ _context = c;
+}
+
+} // namespace Mauri
+
using namespace Mauri;
void Context::print_gl_info()
diff --git a/src/context.h b/src/context.h
index 8732631..215bbfa 100644
--- a/src/context.h
+++ b/src/context.h
@@ -20,7 +20,17 @@ class Context
int width, height;
- virtual void resize_window(int width, int height) = 0;
+ bool draw_enabled() const
+ {
+ return _draw_enabled;
+ }
+
+ void set_draw_enabled(bool enabled)
+ {
+ _draw_enabled = enabled;
+ }
+
+ virtual void resize_window(int w, int h) = 0;
virtual void close_window() = 0;
virtual bool should_close() const = 0;
@@ -35,8 +45,14 @@ class Context
protected:
GLenum check_error();
void print_gl_info();
+
+ private:
+ bool _draw_enabled = false;
};
+extern Context *context();
+extern void set_context(Context *c);
+
} // namespace Mauri
#endif // _CONTEXT_H
diff --git a/src/context_glfw.cc b/src/context_glfw.cc
index 136d124..8f3d847 100644
--- a/src/context_glfw.cc
+++ b/src/context_glfw.cc
@@ -11,10 +11,10 @@ void ContextGLFW::close_window()
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
-void ContextGLFW::resize_window(int nwidth, int nheight)
+void ContextGLFW::resize_window(int w, int h)
{
- width = nwidth;
- height = nheight;
+ width = w;
+ height = h;
glfwSetWindowSize(window, width, height);
}
@@ -36,6 +36,14 @@ void ContextGLFW::swap_buffers()
glfwSwapBuffers(window);
}
+static void key_callback(GLFWwindow *window, s32 key, s32 scancode, s32 action, s32 mods)
+{
+ if (key == GLFW_KEY_B && action == GLFW_PRESS)
+ {
+ context()->set_draw_enabled(!context()->draw_enabled());
+ }
+}
+
void ContextGLFW::process_input()
{
glfwPollEvents();
@@ -63,7 +71,7 @@ ContextGLFW::ContextGLFW(int width, int height, const std::string &name, bool wa
glfwWindowHint(GLFW_SAMPLES, 4);
- this->window = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr);
+ window = glfwCreateWindow(width, height, name.c_str(), nullptr, nullptr);
glfwMakeContextCurrent(window);
@@ -77,6 +85,10 @@ ContextGLFW::ContextGLFW(int width, int height, const std::string &name, bool wa
glEnable(GL_MULTISAMPLE);
print_gl_info();
+
+ set_draw_enabled(true);
+
+ glfwSetKeyCallback(window, key_callback);
}
ContextGLFW::~ContextGLFW()
diff --git a/src/context_glfw.h b/src/context_glfw.h
index de9f1d5..2f407e3 100644
--- a/src/context_glfw.h
+++ b/src/context_glfw.h
@@ -17,7 +17,7 @@ class ContextGLFW : public Context
~ContextGLFW();
void close_window();
- void resize_window(int width, int height);
+ void resize_window(int w, int h);
bool should_close() const;
void process_input();
diff --git a/src/context_null.h b/src/context_null.h
index 6267c04..80a7593 100644
--- a/src/context_null.h
+++ b/src/context_null.h
@@ -9,13 +9,18 @@ class ContextNull : public Context
{
public:
ContextNull(int width, int height, const std::string &name, bool wallpaper)
- : Context(width, height, name, wallpaper) {}
+ : Context(width, height, name, wallpaper)
+ {
+ set_draw_enabled(false);
+ }
~ContextNull() {}
- int width, height;
-
- void resize_window(int width, int height) {}
+ void resize_window(int w, int h)
+ {
+ width = w;
+ height = h;
+ }
void close_window()
{
diff --git a/src/context_x11.cc b/src/context_x11.cc
index 24a3271..3108b8c 100644
--- a/src/context_x11.cc
+++ b/src/context_x11.cc
@@ -203,6 +203,8 @@ ContextX11::ContextX11(int width, int height, const std::string &name, bool wall
print_gl_info();
start = system_clock::now();
+
+ set_draw_enabled(true);
}
void ContextX11::resize_window(int w, int h)
diff --git a/src/context_x11.h b/src/context_x11.h
index a1a45a5..a5c88dc 100644
--- a/src/context_x11.h
+++ b/src/context_x11.h
@@ -33,13 +33,13 @@ using std::chrono::system_clock;
namespace Mauri {
class ContextX11 : public Context {
-public:
+ public:
ContextX11(int width, int height, const std::string &name, bool wallpaper);
~ContextX11();
void close_window();
- void resize_window(int width, int height);
+ void resize_window(int w, int h);
bool should_close() const
{
@@ -57,7 +57,7 @@ public:
return diff.count() * 0.5;
}
-private:
+ private:
bool _should_close = false;
time_point<system_clock> start;
diff --git a/src/engine.cc b/src/engine.cc
index c52a6de..f882646 100644
--- a/src/engine.cc
+++ b/src/engine.cc
@@ -19,7 +19,7 @@ using namespace Mauri;
void View::center_scale_viewport()
{
- glViewport(camera_pos[0], camera_pos[1], width / scale, height / scale);
+ Render::set_viewport(camera_pos[0], camera_pos[1], width / scale, height / scale);
}
void View::center_viewport(f32 w1, f32 h1)
@@ -27,12 +27,12 @@ void View::center_viewport(f32 w1, f32 h1)
auto wpad = (width - w1) / 2.f;
auto hpad = (height - h1) / 2.f;
- glViewport(wpad, hpad, w1, h1);
+ Render::set_viewport(wpad, hpad, w1, h1);
}
void View::default_viewport(f32 w0, f32 h0)
{
- glViewport(0, 0, w0, h0);
+ Render::set_viewport(0, 0, w0, h0);
}
void View::center(f32 w1, f32 h1)
@@ -49,8 +49,8 @@ void View::center(f32 w1, f32 h1)
camera_pos[1] = (scaled_height - h1) / -2.f;
}
-Engine::Engine(Scene *scene, Context *context, Parser *parser, bool audio)
- : scene(scene), parser(parser), context(context), audio_enabled(audio)
+Engine::Engine(Scene *scene, Parser *parser, bool audio)
+ : scene(scene), parser(parser), audio_enabled(audio)
{
view.width = scene->width;
view.height = scene->height;
@@ -61,7 +61,7 @@ Engine::Engine(Scene *scene, Context *context, Parser *parser, bool audio)
view.texel_half_size[0] = .5f / view.width;
view.texel_half_size[1] = .5f / view.height;
- view.center(context->width, context->height);
+ view.center(context()->width, context()->height);
minimal_shader = new Shader(parser, "minimal");
minimal_shader->compile();
@@ -116,9 +116,9 @@ void Engine::create_framebuffer(const std::string &name, f32 width, f32 height,
void Engine::update()
{
- context->process_input();
+ context()->process_input();
- context->cursor_pos(&view.cursor_pos[0], &view.cursor_pos[1]);
+ context()->cursor_pos(&view.cursor_pos[0], &view.cursor_pos[1]);
view.cursor_pos[0] *= view.scale;
view.cursor_pos[1] *= view.scale;
@@ -172,21 +172,21 @@ void Engine::run()
{
const f32 TARGET_FPS = 60.f;
- time = context->current_time();
+ time = context()->current_time();
- while (!context->should_close())
+ while (!context()->should_close())
{
update();
draw();
- while (context->current_time() < time + 1.f / TARGET_FPS)
+ while (context()->current_time() < time + 1.f / TARGET_FPS)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
- time = context->current_time();
+ time = context()->current_time();
- context->swap_buffers();
+ context()->swap_buffers();
}
}
diff --git a/src/engine.h b/src/engine.h
index e46e5a2..45b441d 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -46,7 +46,7 @@ struct View
class Engine
{
public:
- Engine(Scene *scene, Context *context, Parser *parser, bool audio);
+ Engine(Scene *scene, Parser *parser, bool audio);
~Engine();
View view;
@@ -54,7 +54,6 @@ class Engine
Scene *scene;
Parser *parser;
- Context *context;
#ifdef BUILD_AUDIO
Visualizer *visualizer;
diff --git a/src/gl.cc b/src/gl.cc
index 14bc6f7..9c35e1d 100644
--- a/src/gl.cc
+++ b/src/gl.cc
@@ -7,11 +7,10 @@
#include "log.h"
#include "texture.h"
#include "util.h"
+#include "context.h"
using namespace Mauri;
-static bool gl_initialized = true;
-
static u32 pack_rgba(byte r, byte g, byte b, byte a)
{
return (a << 24 | b << 16 | g << 8 | r);
@@ -29,9 +28,8 @@ static u32 pack_red(byte r)
void RenderObject::set_data(f32 *vertices, s32 vertex_count)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
@@ -50,9 +48,8 @@ void RenderObject::set_data(f32 *vertices, s32 vertex_count)
void RenderObject::bind()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
@@ -60,18 +57,16 @@ void RenderObject::bind()
void RenderObject::draw()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glDrawArrays(GL_TRIANGLES, 0, 6);
}
RenderObject::RenderObject(RenderObjType type, f32 width, f32 height)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
switch (type)
{
@@ -132,9 +127,8 @@ RenderObject::RenderObject(RenderObjType type, f32 width, f32 height)
RenderObject::~RenderObject()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
@@ -142,9 +136,8 @@ RenderObject::~RenderObject()
bool RenderShader::compile(u32 program, const char *source, char *error)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return true;
-#endif
+ if (!context()->draw_enabled())
+ return true;
glShaderSource(program, 1, &source, 0);
glCompileShader(program);
@@ -164,9 +157,8 @@ bool RenderShader::compile(u32 program, const char *source, char *error)
RenderShader::RenderShader(const std::string &vs_source, const std::string &fs_source)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
uniform_locations.clear();
program = glCreateProgram();
@@ -195,18 +187,16 @@ RenderShader::RenderShader(const std::string &vs_source, const std::string &fs_s
void RenderShader::bind()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glUseProgram(program);
}
RenderShader::~RenderShader()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
if (program != 0)
glDeleteShader(program);
@@ -214,9 +204,8 @@ RenderShader::~RenderShader()
s32 RenderShader::uniform_loc(const std::string &name)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return 0;
-#endif
+ if (!context()->draw_enabled())
+ return 0;
auto loc = uniform_locations.find(name);
@@ -231,9 +220,8 @@ s32 RenderShader::uniform_loc(const std::string &name)
void RenderShader::set_uniform(const std::string &name, UniformType type, f32 *value)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
auto loc = uniform_loc(name);
@@ -264,9 +252,8 @@ void RenderShader::set_uniform(const std::string &name, UniformType type, f32 *v
void RenderTexture::bind(s32 index)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, id);
@@ -274,9 +261,8 @@ void RenderTexture::bind(s32 index)
RenderTexture::RenderTexture(bool no_interp, bool clamp_uv, bool filtering, s32 mm_count)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glGenTextures(1, &id);
@@ -319,16 +305,11 @@ RenderTexture::RenderTexture(bool no_interp, bool clamp_uv, bool filtering, s32
void RenderTexture::upload(byte *data, s32 width, s32 height, s32 level, bool dxt, bool scale)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
-
- bind(0);
-
// Save pixels in RGBA format as a simple utility.
+ // NOTE: this is pretty wasteful on memory
if (data != nullptr && level == 0)
{
- pixels.reserve(width * height);
+ pixels.resize(width * height);
switch (format)
{
@@ -362,6 +343,11 @@ void RenderTexture::upload(byte *data, s32 width, s32 height, s32 level, bool dx
}
}
+ if (!context()->draw_enabled())
+ return;
+
+ bind(0);
+
s32 iformat = GL_UNSIGNED_BYTE;
if (dxt)
@@ -389,9 +375,8 @@ void RenderTexture::upload(byte *data, s32 width, s32 height, s32 level, bool dx
RenderTexture::~RenderTexture()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glDeleteTextures(1, &id);
}
@@ -399,9 +384,8 @@ RenderTexture::~RenderTexture()
RenderFramebuffer::RenderFramebuffer(f32 width0, f32 height0, f32 scale)
: width(width0 / scale), height(height0 / scale)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
texture = new RenderTexture(false, true, false, 1);
@@ -419,18 +403,16 @@ RenderFramebuffer::RenderFramebuffer(f32 width0, f32 height0, f32 scale)
void RenderFramebuffer::bind()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glBindFramebuffer(GL_FRAMEBUFFER, id);
}
void RenderFramebuffer::blit(u32 dest, f32 w0, f32 h0)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dest);
glBindFramebuffer(GL_READ_FRAMEBUFFER, id);
@@ -439,9 +421,8 @@ void RenderFramebuffer::blit(u32 dest, f32 w0, f32 h0)
void RenderFramebuffer::clear(vec4 color)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
glBindFramebuffer(GL_FRAMEBUFFER, id);
glClearBufferfv(GL_COLOR, 0, glm::value_ptr(color));
@@ -449,9 +430,8 @@ void RenderFramebuffer::clear(vec4 color)
RenderFramebuffer::~RenderFramebuffer()
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
delete texture;
glDeleteFramebuffers(1, &id);
@@ -459,25 +439,33 @@ RenderFramebuffer::~RenderFramebuffer()
void Render::blend_func(s32 sfactor, s32 dfactor)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
+
glBlendFunc(sfactor, dfactor);
}
void Render::color_mask(f32 r, f32 g, f32 b, f32 a)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
+
glColorMask(r, g, b, a);
}
void Render::bind_framebuffer(s32 id)
{
-#ifdef _DEBUG_
- if (!gl_initialized) return;
-#endif
+ if (!context()->draw_enabled())
+ return;
+
glBindFramebuffer(GL_FRAMEBUFFER, id);
}
+void Render::set_viewport(u32 wpad, u32 hpad, u32 width, u32 height)
+{
+ if (!context()->draw_enabled())
+ return;
+
+ glViewport(wpad, hpad, width, height);
+}
+
diff --git a/src/gl.h b/src/gl.h
index a0f044e..97edd4b 100644
--- a/src/gl.h
+++ b/src/gl.h
@@ -110,6 +110,7 @@ class Render
static void blend_func(s32 sfactor, s32 dfactor);
static void color_mask(f32 r, f32 g, f32 b, f32 a);
static void bind_framebuffer(s32 id);
+ static void set_viewport(u32 wpad, u32 hpad, u32 width, u32 height);
};
} // namespace Mauri
diff --git a/src/mauri.cc b/src/mauri.cc
index 7871a52..04934c8 100644
--- a/src/mauri.cc
+++ b/src/mauri.cc
@@ -6,6 +6,7 @@
#include "context.h"
#include "context_null.h"
+#include <math.h>
#ifdef BUILD_GLFW
#include "context_glfw.h"
@@ -31,18 +32,14 @@ s32 main(s32 argc, char *argv[])
gst_init(&argc, &argv);
#endif
- //Context *context = new ContextX11(2560, 1440, "mauri", true);
- //Context *context = new ContextGLFW(720, 1280, "mauri", false);
- Context *context = new ContextGLFW(1920, 1080, "mauri", false);
- //Context *context = new ContextGLFW(720, 1280, "mauri", false);
- //Context *context = new ContextX11(1920, 1080, "mauri", false);
- //Context *context = new ContextNull(1920, 1080, "mauri", false);
+ set_context(new ContextGLFW(1920, 1080, "mauri", false));
+ //set_context(new ContextNull(1920, 1080, "mauri", false));
if (argc == 2)
{
if (!asset_manager()->load_package(std::string(argv[1])))
{
- delete context;
+ set_context(nullptr);
return 1;
}
@@ -52,7 +49,7 @@ s32 main(s32 argc, char *argv[])
//context->resize_window(scene->width / 2.f, scene->height / 2.f);
- Engine engine(scene, context, &parser, true);
+ Engine engine(scene, &parser, true);
//asset_manager()->write_files("./output");
@@ -61,9 +58,9 @@ s32 main(s32 argc, char *argv[])
delete scene;
}
- context->close_window();
+ asset_manager()->unload_package();
- delete context;
+ set_context(nullptr);
return 0;
}
diff --git a/src/objects/object.cc b/src/objects/object.cc
index ab6d7d1..d7d7aaa 100644
--- a/src/objects/object.cc
+++ b/src/objects/object.cc
@@ -27,11 +27,13 @@ Object::Object(Parser &p, json &root)
for (auto &id : root["dependencies"])
deps.push_back(id);
- // Filter deps that are either the same as this objects
- // id or repeated. This could be handled be sorting objects
+ // 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.
- for (auto it = deps.begin(); it != deps.end();)
+ std::vector<int>::const_iterator it = deps.begin();
+
+ while (it != deps.end())
{
bool remove = false;
@@ -50,7 +52,7 @@ Object::Object(Parser &p, json &root)
}
if (remove) deps.erase(it);
- else it++;
+ else ++it;
}
p.get_value<vec3>(root, "angles", &angles, vec3(0.f));
@@ -79,6 +81,7 @@ Object::Object(Parser &p, json &root)
for (auto &effect : root["effects"])
{
effects.emplace_back(new Effect(p, effect));
+
}
}
@@ -107,7 +110,7 @@ void Object::get_model(f32 width, f32 height, bool flip)
void Object::load(Engine *engine)
{
- if (loaded_as_dep)
+ if (loaded_as_dep || loaded)
return;
for (auto &dep : deps)
@@ -144,7 +147,6 @@ void Object::load(Engine *engine)
origin[1] = size[1] / 2.f;
}
-
std::stringstream _buffer_a, _buffer_b;
_buffer_a << "_rt_imageLayerComposite_" << id << "_a";
@@ -195,9 +197,11 @@ void Object::load(Engine *engine)
{
// If there are effects but no visible effects, make sure
// combine is set on the background pass.
- this->passes.back()->combine = true;
+ passes.back()->combine = true;
}
}
+
+ loaded = true;
}
void Object::update(Engine *engine)
@@ -208,13 +212,18 @@ void Object::draw(Engine *engine)
{
for (auto &dep : deps)
{
- //engine->scene->get_object_by_id(dep)->draw(engine);
+ engine->scene->get_object_by_id(dep)->draw(engine);
}
#if FRAME_STEP
std::cout << name << " (model):\n";
#endif
+ // Reset this every frame so avoid passes that target one of
+ // this objects buffers to get a different result every other
+ // frame.
+ buffer_switch = true;
+
for (auto &pass : passes)
{
#if FRAME_STEP
diff --git a/src/objects/object.h b/src/objects/object.h
index 138c6e0..3250b31 100644
--- a/src/objects/object.h
+++ b/src/objects/object.h
@@ -69,6 +69,7 @@ class Object
std::vector<Effect *> effects;
+ bool loaded = false;
bool loaded_as_dep = false;
void load(Engine *engine);
@@ -83,7 +84,7 @@ class Object
std::string buffer_a;
std::string buffer_b;
- bool buffer_switch = false;
+ bool buffer_switch;
Model *image = nullptr;
};
diff --git a/src/objects/pass.cc b/src/objects/pass.cc
index 00a5eae..f3a3509 100644
--- a/src/objects/pass.cc
+++ b/src/objects/pass.cc
@@ -62,6 +62,8 @@ Pass::Pass(Parser &p, json &root, PassStage stage)
if (_source.is_string())
source = _source;
+ binds[0] = "previous";
+
for (auto &bind : root["bind"])
{
u32 index = bind["index"];
@@ -78,13 +80,6 @@ Pass::Pass(Parser &p, json &root, PassStage stage)
auto _textures = root["textures"];
- /*
- if (_textures.is_array())
- {
- textures.fill("null");
- }
- */
-
for (int i = 0; i < _textures.size(); i++)
{
auto texture = _textures[i];
@@ -92,14 +87,6 @@ Pass::Pass(Parser &p, json &root, PassStage stage)
{
textures[i] = texture;
}
- else
- {
- // "null" in this case is different that empty
- // because it means that this texture was listed
- // in the pass, whereas empty strings where not.
- // "null" means use default and apply combos
- textures[i] = "null";
- }
}
for (auto &combo : root["combos"].items())
@@ -115,10 +102,6 @@ void Pass::load(Engine *engine, PassStage stage, Pass *pass)
if (textures[i].empty())
continue;
- // Only set pass->textures[i] to "null" if its not already set.
- if (textures[i] == "null" && !pass->textures[i].empty())
- continue;
-
pass->textures[i] = textures[i];
}
@@ -149,10 +132,10 @@ void Pass::load(Engine *engine, PassStage stage, Pass *pass)
if (binds[i].empty())
continue;
- if (binds[i] == "previous")
- pass->textures[i] = "";
- else
- pass->textures[i] = binds[i] + pass->effect->buffer_id;
+ pass->textures[i] = binds[i];
+
+ if (pass->textures[i] != "previous")
+ pass->textures[i] += pass->effect->buffer_id;
}
if (material != nullptr)
@@ -209,8 +192,8 @@ RenderPass::RenderPass(Engine *engine, Pass *pass)
if (!shader->textures[i].empty())
{
// If there is no texture at "i" use default shader from the texture.
- if (pass->textures[i] == "null")
- pass->textures[i] = shader->textures[i];
+ //if (pass->textures[i].empty())
+ // pass->textures[i] = shader->textures[i];
if (!shader->texture_combos[i].empty() && !pass->textures[i].empty())
pass->combos.push_back(Combo { shader->texture_combos[i], 1 });
@@ -219,6 +202,14 @@ RenderPass::RenderPass(Engine *engine, Pass *pass)
if (pass->textures[i].empty())
continue;
+ if (pass->textures[i] == "previous")
+ {
+ texture_types[i] = PREVIOUS;
+ continue;
+ }
+
+ texture_types[i] = STATIC;
+
// Check if texture is referencing a framebuffer.
textures[i] = engine->get_framebuffer_texture(pass->textures[i]);
@@ -367,15 +358,9 @@ void RenderPass::draw(Engine *engine)
for (int i = 0; i < shader->texture_count; i++)
{
- if (textures[i] != nullptr && !textures[i]->dynamic)
- continue;
-
- auto texture = engine->get_framebuffer_texture(object->get_texture_buffer());
-
- if (texture != nullptr)
+ if (texture_types[i] == PREVIOUS)
{
- texture->dynamic = true;
- textures[i] = texture;
+ textures[i] = engine->get_framebuffer_texture(object->get_texture_buffer());
}
}
@@ -401,9 +386,9 @@ void RenderPass::draw(Engine *engine)
robject->draw();
#if FRAME_STEP
- _target->buffer->blit(0, engine->context->width, engine->context->height);
- engine->context->swap_buffers();
- std::this_thread::sleep_for(std::chrono::seconds(1));
+ _target->buffer->blit(0, context()->width, context()->height);
+ context()->swap_buffers();
+ std::this_thread::sleep_for(std::chrono::seconds(2));
#endif
break;
diff --git a/src/objects/pass.h b/src/objects/pass.h
index ad645bf..cf31ca8 100644
--- a/src/objects/pass.h
+++ b/src/objects/pass.h
@@ -31,6 +31,12 @@ enum PassCommand
COPY
};
+enum TextureType
+{
+ STATIC,
+ PREVIOUS
+};
+
struct UniformOverride
{
vec4 value;
@@ -82,6 +88,7 @@ class RenderPass
Shader *shader = nullptr;
std::array<Texture *, MAX_TEXTURES> textures = {0};
+ std::array<TextureType, MAX_TEXTURES> texture_types;
void draw(Engine *engine);
diff --git a/src/objects/scene.cc b/src/objects/scene.cc
index b39921c..97b9f58 100644
--- a/src/objects/scene.cc
+++ b/src/objects/scene.cc
@@ -75,8 +75,8 @@ void Scene::draw(Engine *engine)
{
for (auto &object : objects)
{
- //if (object->loaded_as_dep)
- // continue;
+ if (object->loaded_as_dep)
+ continue;
object->draw(engine);
}
}
diff --git a/src/texture.cc b/src/texture.cc
index 04ebcce..36bd1d2 100644
--- a/src/texture.cc
+++ b/src/texture.cc
@@ -74,7 +74,7 @@ Texture::Texture(Asset *asset)
log_error("%s", "unknown texture version");
}
- textures.reserve(image_count);
+ textures.resize(image_count);
for (int s = 0; s < image_count; s++)
{
diff --git a/src/texture.h b/src/texture.h
index 058c947..0f19406 100644
--- a/src/texture.h
+++ b/src/texture.h
@@ -69,7 +69,6 @@ class Texture
u64 hash;
bool wrapped = false;
- bool dynamic = false;
bool is_gif;