summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/assets.h17
-rw-r--r--src/context_glfw.cc1
-rw-r--r--src/context_x11.cc398
-rw-r--r--src/context_x11.h6
-rw-r--r--src/engine.cc98
-rw-r--r--src/engine.h41
-rw-r--r--src/gl.cc6
-rw-r--r--src/gl.h2
-rw-r--r--src/mauri.cc5
-rw-r--r--src/objects/effect.cc7
-rw-r--r--src/objects/effect.h2
-rw-r--r--src/objects/material.h1
-rw-r--r--src/objects/object.c1
-rw-r--r--src/objects/object.cc51
-rw-r--r--src/objects/pass.cc22
-rw-r--r--src/objects/pass.h4
-rw-r--r--src/objects/scene.cc2
-rw-r--r--src/shader.cc9
-rw-r--r--src/texture.cc2
-rw-r--r--src/texture.h4
20 files changed, 381 insertions, 298 deletions
diff --git a/src/assets.h b/src/assets.h
index 57fde5a..c6888ac 100644
--- a/src/assets.h
+++ b/src/assets.h
@@ -27,8 +27,9 @@ enum FileTypeHint
NONE
};
-struct Asset
+class Asset
{
+ public:
Asset();
Asset(std::string path, byte *begin, byte *end);
Asset(std::string path);
@@ -40,13 +41,8 @@ struct Asset
AssetType type;
u32 size;
- u32 pos;
- byte *data;
-
u64 hash;
- std::string_view str;
-
void seek(s32 n)
{
this->pos += n;
@@ -57,6 +53,11 @@ struct Asset
return &this->data[this->pos];
}
+ void reset()
+ {
+ this->pos = 0;
+ }
+
template <typename T>
T read()
{
@@ -83,6 +84,10 @@ struct Asset
std::stringstream(std::string((char *)this->ptr(), this->size)) >> root;
return root;
}
+
+ private:
+ u32 pos;
+ byte *data;
};
struct PkgFile
diff --git a/src/context_glfw.cc b/src/context_glfw.cc
index 21e2ef3..eca597e 100644
--- a/src/context_glfw.cc
+++ b/src/context_glfw.cc
@@ -74,7 +74,6 @@ ContextGLFW::ContextGLFW(int width, int height, std::string name, bool wallpaper
}
glEnable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
print_gl_info();
diff --git a/src/context_x11.cc b/src/context_x11.cc
index 70f8b9f..e08f7e7 100644
--- a/src/context_x11.cc
+++ b/src/context_x11.cc
@@ -1,7 +1,9 @@
#include "context_x11.h"
#include "context.h"
+#include "engine.h"
#include "log.h"
+#include <X11/Xlib.h>
using namespace Mauri;
@@ -22,262 +24,286 @@ void ContextX11::disable_input()
ContextX11::ContextX11(int width, int height, std::string name, bool wallpaper)
: Context(width, height, name, wallpaper)
{
- x11_d = XOpenDisplay(NULL);
+ x11_d = XOpenDisplay(NULL);
- ATOM_WM_DELETE_WINDOW = XInternAtom(x11_d, "WM_DELETE_WINDOW", true);
+ ATOM_WM_DELETE_WINDOW = XInternAtom(x11_d, "WM_DELETE_WINDOW", true);
- if (!x11_d)
- {
- log_error("%s", "could not open display");
- return;
- }
+ if (!x11_d)
+ {
+ log_error("%s", "could not open display");
+ return;
+ }
- int screen = XDefaultScreen(x11_d);
- Window root = RootWindow(x11_d, screen);
+ int screen = XDefaultScreen(x11_d);
+ Window root = RootWindow(x11_d, screen);
- static int gl_attrs[] = {
- GLX_X_RENDERABLE, True,
- GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
- GLX_RENDER_TYPE, GLX_RGBA_BIT,
- GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
- GLX_DOUBLEBUFFER, True,
- GLX_RED_SIZE, 8,
- GLX_GREEN_SIZE, 8,
- GLX_BLUE_SIZE, 8,
- GLX_ALPHA_SIZE, 8,
- None
- };
+ static int gl_attrs[] = {
+ GLX_X_RENDERABLE, True,
+ GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
+ GLX_RENDER_TYPE, GLX_RGBA_BIT,
+ GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
+ GLX_DOUBLEBUFFER, True,
+ GLX_RED_SIZE, 8,
+ GLX_GREEN_SIZE, 8,
+ GLX_BLUE_SIZE, 8,
+ GLX_ALPHA_SIZE, 8,
+ None
+ };
- GLint context_attrs[] = {
- GLX_CONTEXT_MAJOR_VERSION_ARB, GL_VERSION_MAJOR,
- GLX_CONTEXT_MINOR_VERSION_ARB, GL_VERSION_MINOR,
- None
- };
+ GLint context_attrs[] = {
+ GLX_CONTEXT_MAJOR_VERSION_ARB, GL_VERSION_MAJOR,
+ GLX_CONTEXT_MINOR_VERSION_ARB, GL_VERSION_MINOR,
+ None
+ };
- // https://github.com/jarcode-foss/glava/blob/master/glava/glx_wcb.c#L383
- int fb_sz, best = -1, samp = -1;
+ // https://github.com/jarcode-foss/glava/blob/master/glava/glx_wcb.c#L383
+ int fb_sz, best = -1, samp = -1;
- GLXFBConfig *fbc = glXChooseFBConfig(x11_d, screen, gl_attrs, &fb_sz);
- if (!fbc)
- return;
-
- for (int t = 0; t < fb_sz; ++t)
- {
- XVisualInfo* xvi = glXGetVisualFromFBConfig(x11_d, fbc[t]);
- if (xvi)
+ GLXFBConfig *fbc = glXChooseFBConfig(x11_d, screen, gl_attrs, &fb_sz);
+ if (!fbc)
+ return;
+
+ for (int t = 0; t < fb_sz; ++t)
{
- int samp_buf, samples;
- glXGetFBConfigAttrib(x11_d, fbc[t], GLX_SAMPLE_BUFFERS, &samp_buf);
- glXGetFBConfigAttrib(x11_d, fbc[t], GLX_SAMPLES, &samples);
- XRenderPictFormat* fmt = XRenderFindVisualFormat(x11_d, xvi->visual);
-
- if (!fmt) continue;
-
- if (best < 0 || (samp_buf && samples > samp))
- {
- best = t;
- samp = samples;
- }
- XFree(xvi);
+ XVisualInfo* xvi = glXGetVisualFromFBConfig(x11_d, fbc[t]);
+ if (xvi)
+ {
+ int samp_buf, samples;
+ glXGetFBConfigAttrib(x11_d, fbc[t], GLX_SAMPLE_BUFFERS, &samp_buf);
+ glXGetFBConfigAttrib(x11_d, fbc[t], GLX_SAMPLES, &samples);
+ XRenderPictFormat* fmt = XRenderFindVisualFormat(x11_d, xvi->visual);
+
+ if (!fmt) continue;
+
+ if (best < 0 || (samp_buf && samples > samp))
+ {
+ best = t;
+ samp = samples;
+ }
+ XFree(xvi);
+ }
+ }
+
+ if (best == -1)
+ {
+ log_error("%s", "XRender could not find suitable format");
+ return;
}
- }
-
- if (best == -1)
- {
- log_error("%s", "XRender could not find suitable format");
- return;
- }
-
- GLXFBConfig config = fbc[best];
- XFree(fbc);
-
- XVisualInfo *vi = glXGetVisualFromFBConfig(x11_d, config);
- Colormap cmap = XCreateColormap(x11_d, root, vi->visual, AllocNone);
+ GLXFBConfig config = fbc[best];
+ XFree(fbc);
- XSetWindowAttributes swa;
+ XVisualInfo *vi = glXGetVisualFromFBConfig(x11_d, config);
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask |
- PropertyChangeMask | VisibilityChangeMask;
+ Colormap cmap = XCreateColormap(x11_d, root, vi->visual, AllocNone);
- swa.background_pixmap = None;
- swa.border_pixel = 0;
-
- window = XCreateWindow(x11_d, root, 0, 0, width, height, 0, vi->depth, InputOutput,
- vi->visual, CWColormap | CWEventMask | CWBackPixmap | CWBorderPixel, &swa);
+ XSetWindowAttributes swa;
- XFree(vi);
+ swa.colormap = cmap;
+ swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask |
+ PropertyChangeMask | VisibilityChangeMask;
- //disable_input();
+ swa.background_pixmap = None;
+ swa.border_pixel = 0;
+
+ window = XCreateWindow(x11_d, root, 0, 0, width, height, 0, vi->depth, InputOutput,
+ vi->visual, CWColormap | CWEventMask | CWBackPixmap | CWBorderPixel, &swa);
- XStoreName(x11_d, window, name.c_str());
+ XFree(vi);
- glXCreateContextAttribsARBProc glXCreateContextAttribsARB = NULL;
- glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
- glXGetProcAddressARB((const GLubyte*) "glXCreateContextAttribsARB");
-
- if (!glXCreateContextAttribsARB)
- return;
-
- if (!(glc = glXCreateContextAttribsARB(x11_d, config, 0, True, context_attrs)))
- return;
-
- glXMakeCurrent(x11_d, window, glc);
+ //disable_input();
- if (!gladLoadGL((GLADloadfunc)glXGetProcAddressARB))
- {
- log_error("%s", "failed to load glad");
- return;
- }
+ XStoreName(x11_d, window, name.c_str());
- if (wallpaper)
- {
- //Atom desktop = XInternAtom(x11_d, "_NET_WM_DESKTOP", false);
+ XChangeProperty(x11_d, window,
+ XInternAtom(x11_d, "_NET_WM_NAME", False),
+ XInternAtom(x11_d, "UTF8_STRING", False),
+ 8, PropModeReplace, (unsigned char *)name.c_str(), name.length());
- //unsigned long all_desktops = XWIN_ALL_DESKTOPS;
- //XChangeProperty(x11_d, window, desktop,
- // XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&all_desktops, 1);
+ XClassHint class_hint;
- Atom desktop_type = XInternAtom(x11_d, "_NET_WM_WINDOW_TYPE_DESKTOP", false);
+ class_hint.res_class = const_cast<char *>(name.c_str());
+ class_hint.res_name = const_cast<char *>(name.c_str());
+
+ XSetClassHint(x11_d, window, &class_hint);
- XChangeProperty(x11_d, window, XInternAtom(x11_d, "_NET_WM_WINDOW_TYPE", false),
- XA_ATOM, 32, PropModeReplace, (unsigned char*)&desktop_type, 1);
+ glXCreateContextAttribsARBProc glXCreateContextAttribsARB = NULL;
+ glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
+ glXGetProcAddressARB((const GLubyte*) "glXCreateContextAttribsARB");
+
+ if (!glXCreateContextAttribsARB)
+ return;
+
+ if (!(glc = glXCreateContextAttribsARB(x11_d, config, 0, True, context_attrs)))
+ return;
+
+ glXMakeCurrent(x11_d, window, glc);
- /*
- XEvent event;
- event.xclient.type = ClientMessage;
- event.xclient.serial = 0;
- event.xclient.send_event = True;
- event.xclient.display = x11_d;
- event.xclient.window = window;
- event.xclient.message_type = XInternAtom(x11_d, "_NET_WM_STATE", false);
- event.xclient.format = 32;
+ if (!gladLoadGL((GLADloadfunc)glXGetProcAddressARB))
+ {
+ log_error("%s", "failed to load glad");
+ return;
+ }
- event.xclient.data.l[0] = _NET_WM_STATE_ADD;
- event.xclient.data.l[1] = XInternAtom(x11_d, "_NET_WM_STATE_BELOW", false);
- event.xclient.data.l[2] = 0; //unused.
- event.xclient.data.l[3] = 0;
- event.xclient.data.l[4] = 0;
+ if (wallpaper)
+ {
+ /*
+ Atom desktop = XInternAtom(x11_d, "_NET_WM_DESKTOP", false);
- XSendEvent(x11_d, root, false,
- SubstructureRedirectMask|SubstructureNotifyMask, &event);
+ unsigned long all_desktops = XWIN_ALL_DESKTOPS;
+ XChangeProperty(x11_d, window, desktop,
+ XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&all_desktops, 1);
+ */
- event.xclient.data.l[1] = XInternAtom(x11_d, "_NET_WM_STATE_STICKY", false);
+ Atom desktop_type = XInternAtom(x11_d, "_NET_WM_WINDOW_TYPE_DESKTOP", false);
- XSendEvent(x11_d, root, false,
- SubstructureRedirectMask|SubstructureNotifyMask, &event);
+ XChangeProperty(x11_d, window, XInternAtom(x11_d, "_NET_WM_WINDOW_TYPE", false),
+ XA_ATOM, 32, PropModeReplace, (unsigned char*)&desktop_type, 1);
- event.xclient.data.l[1] = XInternAtom(x11_d, "_NET_WM_STATE_FULLSCREEN", false);
- */
- }
+ /*
+ XEvent event;
+ event.xclient.type = ClientMessage;
+ event.xclient.serial = 0;
+ event.xclient.send_event = True;
+ event.xclient.display = x11_d;
+ event.xclient.window = window;
+ event.xclient.message_type = XInternAtom(x11_d, "_NET_WM_STATE", false);
+ event.xclient.format = 32;
- XSetWMProtocols(x11_d, window, &ATOM_WM_DELETE_WINDOW, 1);
+ event.xclient.data.l[0] = _NET_WM_STATE_ADD;
+ event.xclient.data.l[1] = XInternAtom(x11_d, "_NET_WM_STATE_BELOW", false);
+ event.xclient.data.l[2] = 0; //unused.
+ event.xclient.data.l[3] = 0;
+ event.xclient.data.l[4] = 0;
- XMoveWindow(x11_d, window, 0, 0);
+ XSendEvent(x11_d, root, false,
+ SubstructureRedirectMask|SubstructureNotifyMask, &event);
- XMapWindow(x11_d, window);
- XFlush(x11_d);
+ event.xclient.data.l[1] = XInternAtom(x11_d, "_NET_WM_STATE_STICKY", false);
- glEnable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_MULTISAMPLE);
+ XSendEvent(x11_d, root, false,
+ SubstructureRedirectMask|SubstructureNotifyMask, &event);
- print_gl_info();
+ event.xclient.data.l[1] = XInternAtom(x11_d, "_NET_WM_STATE_FULLSCREEN", false);
+ */
+ }
- start = system_clock::now();
+ XSetWMProtocols(x11_d, window, &ATOM_WM_DELETE_WINDOW, 1);
+
+ //XMoveWindow(x11_d, window, 1080, 0);
+
+ XMapWindow(x11_d, window);
+ XFlush(x11_d);
+
+ glEnable(GL_BLEND);
+ glEnable(GL_MULTISAMPLE);
+
+ print_gl_info();
+
+ start = system_clock::now();
}
void ContextX11::resize_window(int w, int h)
{
- width = w;
- height = h;
+ width = w;
+ height = h;
}
void ContextX11::swap_buffers()
{
- glXSwapBuffers(x11_d, window);
+ glXSwapBuffers(x11_d, window);
}
void ContextX11::raise() {
- XClientMessageEvent ev = {
- .type = ClientMessage,
- .serial = 0,
- .send_event = true,
- .display = x11_d,
- .window = window,
- .message_type = ATOM__NET_ACTIVE_WINDOW,
- .format = 32,
- .data = { .l = {
- 1, /* source indication -- `1` when coming from an application */
- 0, /* timestamp -- `0` to (attempt to) ignore */
- (long)window /* requestor's currently active window -- `0` for none */
+ XClientMessageEvent ev = {
+ .type = ClientMessage,
+ .serial = 0,
+ .send_event = true,
+ .display = x11_d,
+ .window = window,
+ .message_type = ATOM__NET_ACTIVE_WINDOW,
+ .format = 32,
+ .data = { .l = {
+ 1, /* source indication -- `1` when coming from an application */
+ 0, /* timestamp -- `0` to (attempt to) ignore */
+ (long)window /* requestor's currently active window -- `0` for none */
+ }
}
- }
- };
- /* Send the client message as defined by EWMH standards (usually works) */
- XSendEvent(x11_d, DefaultRootWindow(x11_d), false, StructureNotifyMask, (XEvent*) &ev);
- /* Raise the client in the X11 stacking order (sometimes works, can be blocked by the WM) */
- XRaiseWindow(x11_d, window);
- XFlush(x11_d);
+ };
+ /* Send the client message as defined by EWMH standards (usually works) */
+ XSendEvent(x11_d, DefaultRootWindow(x11_d), false, StructureNotifyMask, (XEvent*) &ev);
+ /* Raise the client in the X11 stacking order (sometimes works, can be blocked by the WM) */
+ XRaiseWindow(x11_d, window);
+ XFlush(x11_d);
}
void ContextX11::set_clickthrough()
{
- Window win = window, root = DefaultRootWindow(x11_d);
- while (win != None)
- {
- Region region;
- if ((region = XCreateRegion()))
+ Window win = window, root = DefaultRootWindow(x11_d);
+ while (win != None)
{
- XShapeCombineRegion(x11_d, window, ShapeInput, 0, 0, region, ShapeSet);
- XDestroyRegion(region);
- }
+ Region region;
+ if ((region = XCreateRegion()))
+ {
+ XShapeCombineRegion(x11_d, window, ShapeInput, 0, 0, region, ShapeSet);
+ XDestroyRegion(region);
+ }
- Window parent, *children = NULL;
- unsigned int num_children;
+ Window parent, *children = NULL;
+ unsigned int num_children;
- if (XQueryTree(x11_d, win, &root, &parent, &children, &num_children))
- {
- if (children)
- XFree(children);
- }
+ if (XQueryTree(x11_d, win, &root, &parent, &children, &num_children))
+ {
+ if (children)
+ XFree(children);
+ }
- win = (parent == root ? None : parent);
- }
+ win = (parent == root ? None : parent);
+ }
- XFlush(x11_d);
+ XFlush(x11_d);
}
void ContextX11::close_window()
{
this->_should_close = true;
+}
+
+void ContextX11::cursor_pos(float *x, float *y)
+{
+ u32 ret;
+ s32 root_return_x, root_return_y;
+ s32 win_return_x, win_return_y;
+ Window root_return, child_return;
+ XQueryPointer(x11_d, window, &root_return, &child_return,
+ &root_return_x, &root_return_y,
+ &win_return_x, &win_return_y, &ret);
+
+ *x = (f32)win_return_x;
+ *y = (f32)win_return_y;
}
void ContextX11::process_input()
{
- XEvent xev;
-
- while (XPending(x11_d) > 0)
- {
- XNextEvent(x11_d, &xev);
+ XEvent xev;
- switch (xev.type)
+ while (XPending(x11_d) > 0)
{
- case ClientMessage:
- if (xev.xclient.message_type == ATOM_WM_PROTOCOLS
- && xev.xclient.data.l[0] == ATOM_WM_DELETE_WINDOW)
+ XNextEvent(x11_d, &xev);
+
+ switch (xev.type)
{
- _should_close = true;
+ case ClientMessage:
+ if (xev.xclient.data.l[0] == ATOM_WM_DELETE_WINDOW)
+ _should_close = true;
+ break;
+ case MapNotify: // make window not clickable
+ //set_clickthrough();
+ XFlush(x11_d);
+ break;
}
- break;
- case MapNotify: // make window not clickable
- //set_clickthrough();
- XFlush(x11_d);
- break;
}
- }
}
ContextX11::~ContextX11()
diff --git a/src/context_x11.h b/src/context_x11.h
index c0fd015..a90bdc1 100644
--- a/src/context_x11.h
+++ b/src/context_x11.h
@@ -50,11 +50,7 @@ public:
void swap_buffers();
void process_input();
- void cursor_pos(float *x, float *y)
- {
- *x = 0.f;
- *y = 0.f;
- }
+ void cursor_pos(float *x, float *y);
double current_time()
{
diff --git a/src/engine.cc b/src/engine.cc
index 83bea7d..f20182d 100644
--- a/src/engine.cc
+++ b/src/engine.cc
@@ -8,35 +8,44 @@
#include "objects/scene.h"
#include <glm/ext.hpp>
+#include <glm/ext/matrix_transform.hpp>
#include <glm/fwd.hpp>
#include <thread>
using namespace Mauri;
-void View::center_scale(float w1, float h1)
+void View::center_scale_viewport()
{
- auto w0 = width;
- auto h0 = height;
-
- auto w_scale = w1 / w0;
- auto h_scale = h1 / h0;
-
- auto scale = (w_scale > h_scale) ? w_scale : h_scale;
-
- auto width = w0 * scale;
- auto height = h0 * scale;
+ glViewport(camera_pos[0], camera_pos[1], width / scale, height / scale);
+}
- auto wpad = (width - w1) / -2.f;
- auto hpad = (height - h1) / -2.f;
+void View::center_viewport(f32 w1, f32 h1)
+{
+ auto wpad = (width - w1) / 2.f;
+ auto hpad = (height - h1) / 2.f;
- glViewport(wpad, hpad, width, height);
+ glViewport(wpad, hpad, w1, h1);
}
-void View::default_viewport(float w0, float h0)
+void View::default_viewport(f32 w0, f32 h0)
{
glViewport(0, 0, w0, h0);
}
+void View::center(f32 w1, f32 h1)
+{
+ auto w_scale = width / w1;
+ auto h_scale = height / h1;
+
+ scale = (w_scale <= h_scale) ? w_scale : h_scale;
+
+ auto scaled_width = width / scale;
+ auto scaled_height = height / scale;
+
+ camera_pos[0] = (scaled_width - w1) / -2.f;
+ camera_pos[1] = (scaled_height - h1) / -2.f;
+}
+
Engine::Engine(Scene *scene, Context *context)
: context(context), scene(scene)
{
@@ -49,19 +58,35 @@ Engine::Engine(Scene *scene, Context *context)
view.texel_half_size[0] = .5f / scene->width;
view.texel_half_size[1] = .5f / scene->height;
- //texture_cache.reserve(100);
+ view.center(context->width, context->height);
- auto combine = std::string("_rt_FullFrameBuffer");
+ minimal_shader = new Shader("minimal");
+ minimal_shader->compile();
- new_framebuffer(combine, context->width, context->height, 1.f);
- combine_buffer = get_framebuffer(combine);
+ create_framebuffer(COMBINE_BUFFER, view.width, view.height, 1.f);
+ combine_buffer = get_framebuffer(COMBINE_BUFFER);
default_object = new RenderObject(DEFAULT);
- identity = mat4x4(1.f);
+
+ default_model = mat4x4(1.f);
+ default_model_flipped = glm::scale(default_model, (vec3){1.f, -1.f, 1.f});
scene->load(this);
}
+Texture *Engine::create_texture(Asset *asset)
+{
+ for (auto &texture : texture_cache)
+ {
+ if (texture->hash == asset->hash)
+ return texture;
+ }
+
+ texture_cache.emplace_back(new Texture(asset));
+
+ return texture_cache.back();
+}
+
Framebuffer *Engine::get_framebuffer(std::string name)
{
return framebuffers[name];
@@ -72,7 +97,7 @@ Texture *Engine::get_framebuffer_texture(std::string name)
return framebuffers[name]->texture;
}
-void Engine::new_framebuffer(std::string name, f32 width, f32 height, f32 scale)
+void Engine::create_framebuffer(std::string name, f32 width, f32 height, f32 scale)
{
framebuffers[name] = new Framebuffer(width, height, scale);
}
@@ -82,6 +107,20 @@ void Engine::update()
context->process_input();
time = context->current_time();
+ context->cursor_pos(&view.cursor_pos[0], &view.cursor_pos[1]);
+
+ view.cursor_pos[0] *= view.scale;
+ view.cursor_pos[1] *= view.scale;
+
+ view.cursor_pos[0] -= view.camera_pos[0];
+ view.cursor_pos[1] -= view.camera_pos[1];
+
+ view.cursor_pos[0] /= view.width;
+ view.cursor_pos[1] /= view.height;
+
+ view.cursor_pos[0] = (view.cursor_pos[0] / 2.f) + 0.25f;
+ view.cursor_pos[1] = ((1 - view.cursor_pos[1]) / 2.f) + 0.25f;
+
for (auto &object: scene->objects)
{
object->update(this);
@@ -97,11 +136,25 @@ void Engine::draw()
}
if (scene->clear_enabled)
+ {
combine_buffer->buffer->clear(scene->clear_color);
+ }
scene->draw(this);
- combine_buffer->buffer->blit(context->width, context->height);
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+
+ default_object->bind();
+ minimal_shader->bind(this, &default_model_flipped);
+
+ combine_buffer->texture->bind(0);
+
+ glBlendFunc(GL_ONE, GL_ZERO);
+ glColorMask(1.f, 1.f, 1.f, 1.f);
+
+ view.center_scale_viewport();
+
+ glDrawArrays(GL_TRIANGLES, 0, 6);
}
void Engine::run()
@@ -133,4 +186,5 @@ Engine::~Engine()
delete buffer.second;
}
delete default_object;
+ delete minimal_shader;
}
diff --git a/src/engine.h b/src/engine.h
index c663e2d..5ecdd22 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -20,20 +20,22 @@ class Object;
struct View
{
- float width;
- float height;
-
- float shake_y = 0.f;
- float shake_x = 0.f;
-
- bool shake_y_switch = false;
- bool shake_x_switch = false;
+ f32 width;
+ f32 height;
vec2 texel_size;
vec2 texel_half_size;
- void center_scale(float w0, float h0);
- void default_viewport(float w0, float h0);
+ f32 scale;
+
+ vec2 camera_pos;
+ vec2 cursor_pos;
+
+ void center(f32 w1, f32 h1);
+
+ void center_scale_viewport();
+ void center_viewport(f32 w0, f32 h0);
+ void default_viewport(f32 w0, f32 h0);
};
class Engine
@@ -42,22 +44,27 @@ class Engine
Engine(Scene *scene, Context *context);
~Engine();
- View view;
+ f32 time;
+ View view;
Context *context;
- float time;
-
RenderObject *default_object;
- mat4x4 identity;
- std::vector<Texture> texture_cache;
+ mat4x4 default_model;
+ mat4x4 default_model_flipped;
- std::unordered_map<std::string, Framebuffer *> framebuffers;
+ Shader *minimal_shader;
+
+ std::vector<Texture *> texture_cache;
+
+ Texture *create_texture(Asset *asset);
Framebuffer *combine_buffer;
- void new_framebuffer(std::string name, float width, float height, float scale);
+ std::unordered_map<std::string, Framebuffer *> framebuffers;
+
+ void create_framebuffer(std::string name, f32 width, f32 height, f32 scale);
Framebuffer *get_framebuffer(std::string name);
Texture *get_framebuffer_texture(std::string name);
diff --git a/src/gl.cc b/src/gl.cc
index 3a9f212..c5988a7 100644
--- a/src/gl.cc
+++ b/src/gl.cc
@@ -339,11 +339,11 @@ void RenderFramebuffer::bind()
glBindFramebuffer(GL_FRAMEBUFFER, id);
}
-void RenderFramebuffer::blit(f32 width, f32 height)
+void RenderFramebuffer::blit(u32 dest, f32 w0, f32 h0)
{
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dest);
glBindFramebuffer(GL_READ_FRAMEBUFFER, id);
- glBlitFramebuffer(0, height, width, 0, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
+ glBlitFramebuffer(0, h0, w0, 0, 0, 0, w0, h0, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
void RenderFramebuffer::clear(vec4 color)
diff --git a/src/gl.h b/src/gl.h
index 67e4649..f781b11 100644
--- a/src/gl.h
+++ b/src/gl.h
@@ -94,7 +94,7 @@ class RenderFramebuffer
RenderTexture *texture = nullptr;
void bind();
- void blit(f32 width, f32 height);
+ void blit(u32 dest, f32 w0, f32 h0);
void clear(vec4 color);
};
diff --git a/src/mauri.cc b/src/mauri.cc
index 5a969dd..cb6e840 100644
--- a/src/mauri.cc
+++ b/src/mauri.cc
@@ -23,9 +23,10 @@ s32 main(s32 argc, char *argv[])
gl_initialized = true;
#endif
- //Context *context = new ContextX11(1080, 1920, "mauri", true);
+ //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(810, 1440, "mauri", false);
+ Context *context = new ContextX11(1920, 1080, "mauri", false);
if (argc == 2)
{
diff --git a/src/objects/effect.cc b/src/objects/effect.cc
index a56bd75..cbe0b7f 100644
--- a/src/objects/effect.cc
+++ b/src/objects/effect.cc
@@ -49,17 +49,14 @@ Effect::Effect(Json::Value root)
void Effect::load(Engine *engine, Object *object, bool last_effect)
{
- if (!visible)
- return;
-
buffer_id = fmt::format("{}_{}", id, object->id);
for (auto &fbo : fbos)
{
- engine->new_framebuffer(fbo.name + buffer_id, object->size[0], object->size[1], fbo.scale);
+ engine->create_framebuffer(fbo.name + buffer_id, object->size[0], object->size[1], fbo.scale);
}
- for (int i = 0; i < passes0.size(); i++)
+ for (u32 i = 0; i < passes0.size(); i++)
{
Pass *pass = new Pass();
diff --git a/src/objects/effect.h b/src/objects/effect.h
index c3eddba..010634c 100644
--- a/src/objects/effect.h
+++ b/src/objects/effect.h
@@ -14,7 +14,7 @@ class RenderPass;
struct EffectBuffer
{
std::string name;
- float scale;
+ f32 scale;
};
class Effect
diff --git a/src/objects/material.h b/src/objects/material.h
index 23aa06b..24c2ea0 100644
--- a/src/objects/material.h
+++ b/src/objects/material.h
@@ -24,6 +24,7 @@ class Material
void load(Engine *engine, Object *object, MaterialType type, Pass *pass, bool no_effects);
+ private:
std::vector<Pass *> passes0;
};
diff --git a/src/objects/object.c b/src/objects/object.c
deleted file mode 100644
index 69c5411..0000000
--- a/src/objects/object.c
+++ /dev/null
@@ -1 +0,0 @@
-//#include "../engine.h"
diff --git a/src/objects/object.cc b/src/objects/object.cc
index 71a8b28..0a57adf 100644
--- a/src/objects/object.cc
+++ b/src/objects/object.cc
@@ -29,7 +29,7 @@ Object::Object(Json::Value root)
sscanf_vec3(json_user_value(root.get("angles", "")).asCString(), angles);
sscanf_vec3(json_user_value(root.get("colors", "")).asCString(), color);
- sscanf_vec2(json_user_value(root.get("size", "")).asCString(), size);
+ sscanf_vec2(json_user_value(root.get("size", "0 0")).asCString(), size);
sscanf_vec3(json_user_value(root.get("scale", "")).asCString(), scale);
sscanf_vec3(json_user_value(root.get("origin", "")).asCString(), origin);
@@ -58,14 +58,15 @@ void Object::get_model(f32 width, f32 height, bool flip)
model = mat4x4(1.f);
model = glm::translate(model, (vec3){-width, -height, 0.f});
+
model = glm::translate(model, origin * 2.f);
+ model = glm::scale(model, scale);
+
model = glm::rotate(model, angles[0], (vec3){1.f, 0.f, 0.f});
model = glm::rotate(model, angles[1], (vec3){0.f, 1.f, 0.f});
model = glm::rotate(model, angles[2], (vec3){0.f, 0.f, 1.f});
- model = glm::scale(model, scale);
-
mat4x4 _ortho = glm::ortho(-width, width, height, -height, 0.f, 1.f);
if (flip)
@@ -78,17 +79,35 @@ void Object::get_model(f32 width, f32 height, bool flip)
void Object::load(Engine *engine)
{
- if (loaded || !visible)
+ if (loaded)
return;
- f32 width = size[0];
- f32 height = size[1];
+ if (image != nullptr)
+ {
+ if (image->passthrough)
+ passthrough = true;
+
+ if (image->fullscreen || passthrough)
+ {
+ if (size[0] == 0 && size[1] == 0)
+ {
+ size[0] = engine->view.width;
+ size[1] = engine->view.height;
+ origin[0] = size[0] / 2.f;
+ origin[1] = size[1] / 2.f;
+ }
+ fullscreen = true;
+ }
+ }
+
+ auto width = size[0];
+ auto height = size[1];
abuffer = fmt::format("_rt_imageLayerComposite_{}_a", this->id);
bbuffer = fmt::format("_rt_imageLayerComposite_{}_b", this->id);
- engine->new_framebuffer(abuffer, width, height, 1.f);
- engine->new_framebuffer(bbuffer, width, height, 1.f);
+ engine->create_framebuffer(abuffer, width, height, 1.f);
+ engine->create_framebuffer(bbuffer, width, height, 1.f);
get_model(engine->view.width, engine->view.height, effects.size() == 0);
@@ -98,23 +117,9 @@ void Object::load(Engine *engine)
gl_uv_object = new RenderObject(UV_SCALE, width, height);
if (image != nullptr)
- {
image->load(engine, this, effects.size() == 0);
- if (image->fullscreen)
- {
- fullscreen = true;
- size[0] = engine->view.width;
- size[1] = engine->view.height;
- origin[0] = size[0] / 2.f;
- origin[1] = size[1] / 2.f;
- }
-
- if (image->passthrough)
- passthrough = true;
- }
-
- for (int i = 0; i < effects.size(); i++)
+ for (u32 i = 0; i < effects.size(); i++)
{
effects[i]->load(engine, this, i == (effects.size() - 1));
}
diff --git a/src/objects/pass.cc b/src/objects/pass.cc
index 6334641..f4c237a 100644
--- a/src/objects/pass.cc
+++ b/src/objects/pass.cc
@@ -2,6 +2,7 @@
#include <glm/fwd.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <thread>
+#include <type_traits>
#include "assets.h"
#include "engine.h"
@@ -32,8 +33,8 @@ Pass::Pass(Json::Value root, PassStage stage)
uniform.name = name;
- json_number(json_user_value(_uniforms[name]),
- glm::value_ptr(uniform.value));
+ auto value = json_user_value(_uniforms[name]);
+ json_number(value, glm::value_ptr(uniform.value));
uniforms.push_back(uniform);
}
@@ -185,7 +186,7 @@ RenderPass::RenderPass(Engine *engine, Pass *pass)
auto asset = asset_manager()->get_file(pass->textures[i], TEXTURE);
if (asset->type != ASSET_VOID)
- textures[i] = new Texture(asset);
+ textures[i] = engine->create_texture(asset);
else
textures[i] = engine->get_framebuffer_texture(pass->textures[i]);
}
@@ -268,7 +269,7 @@ RenderPass::RenderPass(Engine *engine, Pass *pass)
else
{
robject = engine->default_object;
- model = &engine->identity;
+ model = &engine->default_model;
}
}
}
@@ -305,15 +306,12 @@ void RenderPass::draw(Engine *engine)
break;
}
- if (combine)
- engine->view.center_scale(engine->context->width, engine->context->height);
- else
- engine->view.default_viewport(target->buffer->width, target->buffer->height);
+ engine->view.default_viewport(target->buffer->width, target->buffer->height);
glDrawArrays(GL_TRIANGLES, 0, 6);
#if 0
- engine->combine_buffer->buffer->blit(context->width, context->height);
+ target->buffer->blit(0, engine->context->width, engine->context->height);
engine->context->swap_buffers();
std::this_thread::sleep_for(std::chrono::seconds(1));
#endif
@@ -323,11 +321,5 @@ RenderPass::~RenderPass()
{
if (shader != nullptr)
delete shader;
-
- for (int i = 0; i < 8; i++)
- {
- if (textures[i] != nullptr && !textures[i]->wrapped)
- delete textures[i];
- }
}
diff --git a/src/objects/pass.h b/src/objects/pass.h
index 563900f..92aa3e5 100644
--- a/src/objects/pass.h
+++ b/src/objects/pass.h
@@ -56,13 +56,13 @@ class Pass
class RenderPass
{
-public:
+ public:
RenderPass(Engine *engine, Pass *pass);
~RenderPass();
void draw(Engine *engine);
-private:
+ private:
Shader *shader;
Framebuffer *target;
diff --git a/src/objects/scene.cc b/src/objects/scene.cc
index 83b7be6..3cab05c 100644
--- a/src/objects/scene.cc
+++ b/src/objects/scene.cc
@@ -68,7 +68,7 @@ void Scene::load(Engine *engine)
for (auto &dep : object->deps)
{
auto dep_object = get_object_by_id(dep);
- if (dep_object)
+ if (dep_object != nullptr)
dep_object->load(engine);
}
diff --git a/src/shader.cc b/src/shader.cc
index 6a27954..57a34d8 100644
--- a/src/shader.cc
+++ b/src/shader.cc
@@ -19,9 +19,9 @@ static const char *gl_compat = "#define highp\n"
"#define lowp\n"
"#define mul(x, y) (y * x)\n"
"#define frac fract\n"
- "#define CAST2(x) vec2(x)\n"
- "#define CAST3(x) vec3(x)\n"
- "#define CAST4(x) vec4(x)\n"
+ "#define CAST2(x) (vec2(x))\n"
+ "#define CAST3(x) (vec3(x))\n"
+ "#define CAST4(x) (vec4(x))\n"
"#define CAST3X3(x) (mat3(x))\n"
"#define saturate(x) (clamp(x, 0.0, 1.0))\n"
"#define texSample2D texture2D\n"
@@ -213,7 +213,6 @@ void Shader::bind(Engine *engine, mat4x4 *model)
{
this->rshader->bind();
-
for (auto &uniform : this->uniforms)
{
if (uniform.uname.find('[') != std::string::npos)
@@ -227,6 +226,8 @@ void Shader::bind(Engine *engine, mat4x4 *model)
this->rshader->set_uniform("g_TexelSize", TYPE_VEC2, glm::value_ptr(engine->view.texel_size));
this->rshader->set_uniform("g_TexelSizeHalf", TYPE_VEC2, glm::value_ptr(engine->view.texel_half_size));
+ this->rshader->set_uniform("g_PointerPosition", TYPE_VEC2, glm::value_ptr(engine->view.cursor_pos));
+
mat4x4 _model, _inverse;
if (!model)
diff --git a/src/texture.cc b/src/texture.cc
index debbac3..bd8996f 100644
--- a/src/texture.cc
+++ b/src/texture.cc
@@ -159,7 +159,7 @@ Texture::Texture(Asset *asset)
asset->seek(byte_count);
}
- asset->pos = 0;
+ asset->reset();
}
Texture::~Texture()
diff --git a/src/texture.h b/src/texture.h
index d70b423..52bf197 100644
--- a/src/texture.h
+++ b/src/texture.h
@@ -49,6 +49,8 @@ class Texture
s32 width;
s32 height;
+ u64 hash;
+
bool wrapped = false;
vec4 texture_resolution;
@@ -64,8 +66,6 @@ class Texture
s32 texture_width;
s32 texture_height;
- u64 hash;
-
union {
TextureFlags map;
unsigned int integer;