From 2ed9a495bfef193ad5f9f614ae6c52d38260a231 Mon Sep 17 00:00:00 2001 From: Slendi Date: Tue, 30 Sep 2025 16:36:41 +0300 Subject: [PATCH] 2D rendering (kinda, hud brokey) Signed-off-by: Slendi --- src/LunarWM_core.c | 5 +- src/LunarWM_render.c | 33 ++++++------ src/LunarWM_render.h | 4 +- src/LunarWM_wayland.c | 115 +++++++++++++++++++++++++++++++++--------- 4 files changed, 112 insertions(+), 45 deletions(-) diff --git a/src/LunarWM_core.c b/src/LunarWM_core.c index 1163e4a..c662f08 100644 --- a/src/LunarWM_core.c +++ b/src/LunarWM_core.c @@ -417,10 +417,7 @@ void LunarWM_run(LunarWM *wm) EndDrawing(); } else { - - ClearBackground(RED); - - EndDrawing(); + wm->renderer.camera.fovy = 90; } } } diff --git a/src/LunarWM_render.c b/src/LunarWM_render.c index ac3889c..deb1ae8 100644 --- a/src/LunarWM_render.c +++ b/src/LunarWM_render.c @@ -349,24 +349,24 @@ void LunarWM_render_windows(LunarWM *this, bool alpha_check) } } -static void render_3d(LunarWM *this, float /*dt*/) +void LunarWM_render_3d(LunarWM *this, float /*dt*/) { LunarWM_render_windows(this, true); for (int h = 0; this->xr.hand_tracking_enabled && h < 2; ++h) { - auto *hand_info = &this->xr.hands[h]; - if (hand_info->hand_tracker == XR_NULL_HANDLE) - continue; - for (size_t k = 0; k < XR_HAND_JOINT_COUNT_EXT; ++k) { - auto const *jl = &hand_info->joint_locations[k]; - Vector3 pos = { - jl->pose.position.x, - jl->pose.position.y, - jl->pose.position.z, - }; - pos = RecenterPoint(this, pos); - DrawSphere(pos, jl->radius, (Color) { 255, 0, 0, 255 }); - } + auto *hand_info = &this->xr.hands[h]; + if (hand_info->hand_tracker == XR_NULL_HANDLE) + continue; + for (size_t k = 0; k < XR_HAND_JOINT_COUNT_EXT; ++k) { + auto const *jl = &hand_info->joint_locations[k]; + Vector3 pos = { + jl->pose.position.x, + jl->pose.position.y, + jl->pose.position.z, + }; + pos = RecenterPoint(this, pos); + DrawSphere(pos, jl->radius, (Color) { 255, 0, 0, 255 }); + } } Skybox_draw(this->renderer.skybox, this->renderer.camera.position); @@ -432,7 +432,8 @@ static void render_3d(LunarWM *this, float /*dt*/) } } -bool LunarWM_render_layer(LunarWM *this, LunarWM_RenderLayerInfo *info, float dt) +bool LunarWM_render_layer( + LunarWM *this, LunarWM_RenderLayerInfo *info, float dt) { auto const view_count = (uint32_t)this->xr.view_configuration_views_count; assert(view_count == 2); @@ -592,7 +593,7 @@ bool LunarWM_render_layer(LunarWM *this, LunarWM_RenderLayerInfo *info, float dt BeginMode3D(this->renderer.camera); { ClearBackground(RED); - render_3d(this, dt); + LunarWM_render_3d(this, dt); } EndMode3D(); diff --git a/src/LunarWM_render.h b/src/LunarWM_render.h index f09a880..2d7f345 100644 --- a/src/LunarWM_render.h +++ b/src/LunarWM_render.h @@ -5,7 +5,7 @@ void LunarWM_render_hud(LunarWM *wm, float dt, int hud_size); void LunarWM_render_windows(LunarWM *wm, bool alpha_check); -bool LunarWM_render_layer( - LunarWM *wm, LunarWM_RenderLayerInfo *info, float dt); +bool LunarWM_render_layer(LunarWM *wm, LunarWM_RenderLayerInfo *info, float dt); +void LunarWM_render_3d(LunarWM *this, float dt); #endif diff --git a/src/LunarWM_wayland.c b/src/LunarWM_wayland.c index aa5bee5..f80e545 100644 --- a/src/LunarWM_wayland.c +++ b/src/LunarWM_wayland.c @@ -1,10 +1,12 @@ #include "LunarWM_wayland.h" +#include "LunarWM_render.h" #include "common.h" +#include "raylib.h" #include "vec.h" -#include #include +#include #include #include @@ -18,6 +20,8 @@ #include #include +#include + static void handle_new_output(struct wl_listener *listener, void *data); static void handle_output_frame(struct wl_listener *listener, void *data); static void handle_output_destroy(struct wl_listener *listener, void *data); @@ -1488,7 +1492,8 @@ static void setup_xwayland(LunarWM *this) wl_signal_add(&this->wayland.xwayland->events.new_surface, &this->wayland.xwayland_new_surface); } -static void destroy_output(LunarWM_Output *output) { +static void destroy_output(LunarWM_Output *output) +{ if (!output) { return; } @@ -1503,7 +1508,8 @@ static void destroy_output(LunarWM_Output *output) { free(output); } -static void handle_output_destroy(struct wl_listener *listener, void *data) { +static void handle_output_destroy(struct wl_listener *listener, void *data) +{ (void)data; LunarWM_Output *output = wl_container_of(listener, output, destroy); LunarWM *wm = output->wm; @@ -1520,7 +1526,8 @@ static void handle_output_destroy(struct wl_listener *listener, void *data) { destroy_output(output); } -static void handle_output_frame(struct wl_listener *listener, void *data) { +static void handle_output_frame(struct wl_listener *listener, void *data) +{ (void)data; LunarWM_Output *output = wl_container_of(listener, output, frame); LunarWM *wm = output->wm; @@ -1534,6 +1541,9 @@ static void handle_output_frame(struct wl_listener *listener, void *data) { struct wlr_output_state state; wlr_output_state_init(&state); + int width, height; + wlr_output_effective_resolution(wlr_output, &width, &height); + struct wlr_render_pass *pass = wlr_output_begin_render_pass(wlr_output, &state, NULL); if (pass == NULL) { @@ -1544,21 +1554,16 @@ static void handle_output_frame(struct wl_listener *listener, void *data) { return; } - struct wlr_box box = { - .x = 0, - .y = 0, - .width = wlr_output->width, - .height = wlr_output->height, - }; - wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){ - .box = box, - .color = { - .r = 1.0f, - .g = 0.0f, - .b = 0.0f, - .a = 1.0f, - }, - }); + GLint drawFboId = 0, readFboId = 0; + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId); + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId); + + rlMatrixMode(RL_MODELVIEW); + rlLoadIdentity(); + + // if (!IsTextureValid(wm->renderer.main_rt.texture)) { + // wm->renderer.main_rt = LoadRenderTexture(width, height); + // } if (!wlr_render_pass_submit(pass)) { wlr_output_state_finish(&state); @@ -1576,17 +1581,82 @@ static void handle_output_frame(struct wl_listener *listener, void *data) { return; } + wm->renderer.tmp_rt.id = drawFboId; + wm->renderer.tmp_rt.texture = (Texture) { + .id = drawFboId, + .width = width, + .height = height, + .mipmaps = 1, + .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, + }; + if (!IsRenderTextureValid(wm->renderer.tmp_rt)) { + wm->renderer.tmp_rt.depth.id = rlLoadTextureDepth(width, height, true); + wm->renderer.tmp_rt.depth.width = width; + wm->renderer.tmp_rt.depth.height = height; + wm->renderer.tmp_rt.depth.format = 19; // DEPTH_COMPONENT_24BIT? + wm->renderer.tmp_rt.depth.mipmaps = 1; + } + rlFramebufferAttach(wm->renderer.tmp_rt.id, wm->renderer.tmp_rt.depth.id, + RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0); + + if (!IsTextureValid(wm->renderer.main_rt.texture)) { + wm->renderer.main_rt = LoadRenderTexture(width, height); + } + int const hud_size = wm->cman->cfg.displays.hud.size; + if (!IsTextureValid(wm->renderer.hud_rt.texture)) { + wm->renderer.hud_rt = LoadRenderTexture(hud_size, hud_size); + } + BeginTextureMode(wm->renderer.hud_rt); + { + LunarWM_render_hud(wm, GetFrameTime(), 1); + } + EndTextureMode(); + BeginTextureMode(wm->renderer.main_rt); + { + BeginMode3D(wm->renderer.camera); + { + ClearBackground(BLACK); + LunarWM_render_3d(wm, GetFrameTime()); + } + EndMode3D(); + + rlDrawRenderBatchActive(); + } + EndTextureMode(); + + BeginTextureMode(wm->renderer.tmp_rt); + { + DrawTexturePro(wm->renderer.main_rt.texture, + (Rectangle) { + 0, + 0, + wm->renderer.main_rt.texture.width, + wm->renderer.main_rt.texture.height, + }, + (Rectangle) { + 0, + 0, + wm->renderer.main_rt.texture.width, + -wm->renderer.main_rt.texture.height, + }, + (Vector2) { 0, 0 }, 0, WHITE); + } + EndTextureMode(); + wlr_output_state_finish(&state); } -static void handle_new_output(struct wl_listener *listener, void *data) { +static void handle_new_output(struct wl_listener *listener, void *data) +{ struct wlr_output *wlr_output = data; LunarWM *wm = wl_container_of(listener, wm, wayland.new_output_listener); + wlr_log(WLR_INFO, "Got new output."); + if (!wlr_output_init_render( wlr_output, wm->wayland.allocator, wm->wayland.renderer)) { - wlr_log(WLR_ERROR, "Failed to init render for output %s", - wlr_output->name); + wlr_log( + WLR_ERROR, "Failed to init render for output %s", wlr_output->name); return; } @@ -1626,7 +1696,6 @@ static void handle_new_output(struct wl_listener *listener, void *data) { wlr_output_schedule_frame(wlr_output); } - bool LunarWM_wayland_init(LunarWM *this) { wlr_log_init(WLR_DEBUG, nullptr);