Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: Add Framebuffer for render-to-texture support

esengine6d349151a6fd
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 337417c9a..94a0e92fd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,6 +34,7 @@ set(ESENGINE_SOURCES
src/esengine/renderer/Shader.cpp
src/esengine/renderer/Buffer.cpp
src/esengine/renderer/Texture.cpp
+ src/esengine/renderer/Framebuffer.cpp
src/esengine/renderer/stb_image_impl.cpp
src/esengine/platform/input/Input.cpp
# UI System
@@ -89,6 +90,7 @@ set(ESENGINE_HEADERS
src/esengine/renderer/Shader.hpp
src/esengine/renderer/Buffer.hpp
src/esengine/renderer/Texture.hpp
+ src/esengine/renderer/Framebuffer.hpp
src/esengine/renderer/RenderCommand.hpp
src/esengine/platform/Platform.hpp
src/esengine/platform/input/Input.hpp
diff --git a/src/esengine/renderer/Framebuffer.cpp b/src/esengine/renderer/Framebuffer.cpp
new file mode 100644
index 000000000..66080e6b4
--- /dev/null
+++ b/src/esengine/renderer/Framebuffer.cpp
@@ -0,0 +1,193 @@
+/**
+ * @file Framebuffer.cpp
+ * @brief Framebuffer implementation for OpenGL/WebGL
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "Framebuffer.hpp"
+#include "../core/Log.hpp"
+
+#ifdef ES_PLATFORM_WEB
+ #include <GLES3/gl3.h>
+#else
+ #ifdef _WIN32
+ #include <windows.h>
+ #endif
+ #include <GL/gl.h>
+ #ifndef GL_FRAMEBUFFER
+ #define GL_FRAMEBUFFER 0x8D40
+ #define GL_COLOR_ATTACHMENT0 0x8CE0
+ #define GL_DEPTH_ATTACHMENT 0x8D00
+ #define GL_DEPTH_STENCIL_ATTACHMENT 0x821A
+ #define GL_FRAMEBUFFER_COMPLETE 0x8CD5
+ #define GL_TEXTURE_2D 0x0DE1
+ #define GL_DEPTH_COMPONENT 0x1902
+ #define GL_DEPTH_COMPONENT24 0x81A6
+ #define GL_DEPTH24_STENCIL8 0x88F0
+ #define GL_UNSIGNED_INT_24_8 0x84FA
+ #define GL_RGB 0x1907
+ #define GL_RGBA 0x1908
+ #define GL_RGBA8 0x8058
+ #define GL_UNSIGNED_BYTE 0x1401
+ #define GL_NEAREST 0x2600
+ #define GL_CLAMP_TO_EDGE 0x812F
+ #define GL_TEXTURE_MIN_FILTER 0x2801
+ #define GL_TEXTURE_MAG_FILTER 0x2800
+ #define GL_TEXTURE_WRAP_S 0x2802
+ #define GL_TEXTURE_WRAP_T 0x2803
+ #endif
+
+ extern "C" {
+ void glGenFramebuffers(int, unsigned int*);
+ void glDeleteFramebuffers(int, const unsigned int*);
+ void glBindFramebuffer(unsigned int, unsigned int);
+ void glFramebufferTexture2D(unsigned int, unsigned int, unsigned int, unsigned int, int);
+ unsigned int glCheckFramebufferStatus(unsigned int);
+ void glGenTextures(int, unsigned int*);
+ void glDeleteTextures(int, const unsigned int*);
+ void glBindTexture(unsigned int, unsigned int);
+ void glTexImage2D(unsigned int, int, int, int, int, int, unsigned int, unsigned int, const void*);
+ void glTexParameteri(unsigned int, unsigned int, int);
+ }
+#endif
+
+namespace esengine {
+
+// =============================================================================
+// Constructor / Destructor
+// =============================================================================
+
+Framebuffer::~Framebuffer() {
+ cleanup();
+}
+
+Framebuffer::Framebuffer(Framebuffer&& other) noexcept
+ : spec_(other.spec_),
+ framebufferId_(other.framebufferId_),
+ colorAttachment_(other.colorAttachment_),
+ depthAttachment_(other.depthAttachment_) {
+ other.framebufferId_ = 0;
+ other.colorAttachment_ = 0;
+ other.depthAttachment_ = 0;
+}
+
+Framebuffer& Framebuffer::operator=(Framebuffer&& other) noexcept {
+ if (this != &other) {
+ cleanup();
+ spec_ = other.spec_;
+ framebufferId_ = other.framebufferId_;
+ colorAttachment_ = other.colorAttachment_;
+ depthAttachment_ = other.depthAttachment_;
+ other.framebufferId_ = 0;
+ other.colorAttachment_ = 0;
+ other.depthAttachment_ = 0;
+ }
+ return *this;
+}
+
+// =============================================================================
+// Creation
+// =============================================================================
+
+Unique<Framebuffer> Framebuffer::create(const FramebufferSpec& spec) {
+ auto framebuffer = makeUnique<Framebuffer>();
+ framebuffer->spec_ = spec;
+
+ if (!framebuffer->initialize()) {
+ ES_LOG_ERROR("Failed to create framebuffer");
+ return nullptr;
+ }
+
+ return framebuffer;
+}
+
+// =============================================================================
+// Operations
+// =============================================================================
+
+void Framebuffer::bind() const {
+ glBindFramebuffer(GL_FRAMEBUFFER, framebufferId_);
+}
+
+void Framebuffer::unbind() const {
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+}
+
+void Framebuffer::resize(u32 width, u32 height) {
+ if (width == 0 || height == 0 || width > 8192 || height > 8192) {
+ ES_LOG_WARN("Invalid framebuffer size: {}x{}", width, height);
+ return;
+ }
+
+ spec_.width = width;
+ spec_.height = height;
+
+ cleanup();
+ initialize();
+}
+
+// =============================================================================
+// Private Methods
+// =============================================================================
+
+bool Framebuffer::initialize() {
+ glGenFramebuffers(1, &framebufferId_);
+ glBindFramebuffer(GL_FRAMEBUFFER, framebufferId_);
+
+ glGenTextures(1, &colorAttachment_);
+ glBindTexture(GL_TEXTURE_2D, colorAttachment_);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, spec_.width, spec_.height, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+ GL_TEXTURE_2D, colorAttachment_, 0);
+
+ if (spec_.depthStencil) {
+ glGenTextures(1, &depthAttachment_);
+ glBindTexture(GL_TEXTURE_2D, depthAttachment_);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, spec_.width, spec_.height, 0,
+ GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8, nullptr);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
+ GL_TEXTURE_2D, depthAttachment_, 0);
+ }
+
+ GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
+ if (status != GL_FRAMEBUFFER_COMPLETE) {
+ ES_LOG_ERROR("Framebuffer is incomplete! Status: 0x{:X}", status);
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ return false;
+ }
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ ES_LOG_INFO("Framebuffer created: {}x{}", spec_.width, spec_.height);
+ return true;
+}
+
+void Framebuffer::cleanup() {
+ if (colorAttachment_) {
+ glDeleteTextures(1, &colorAttachment_);
+ colorAttachment_ = 0;
+ }
+
+ if (depthAttachment_) {
+ glDeleteTextures(1, &depthAttachment_);
+ depthAttachment_ = 0;
+ }
+
+ if (framebufferId_) {
+ glDeleteFramebuffers(1, &framebufferId_);
+ framebufferId_ = 0;
+ }
+}
+
+} // namespace esengine
diff --git a/src/esengine/renderer/Framebuffer.hpp b/src/esengine/renderer/Framebuffer.hpp
new file mode 100644
index 000000000..c458beb91
--- /dev/null
+++ b/src/esengine/renderer/Framebuffer.hpp
@@ -0,0 +1,170 @@
+/**
+ * @file Framebuffer.hpp
+ * @brief GPU framebuffer abstraction for render-to-texture
+ * @details Provides off-screen rendering targets with color and depth attachments
+ * for OpenGL ES/WebGL.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+#pragma once
+
+// =============================================================================
+// Includes
+// =============================================================================
+
+#include "../core/Types.hpp"
+#include "Texture.hpp"
+
+namespace esengine {
+
+// =============================================================================
+// Framebuffer Specification
+// =============================================================================
+
+/**
+ * @brief Framebuffer creation parameters
+ *
+ * @details Specifies the dimensions and attachment configuration
+ * for an off-screen rendering target.
+ *
+ * @code
+ * FramebufferSpec spec;
+ * spec.width = 1280;
+ * spec.height = 720;
+ * spec.samples = 1; // No multisampling
+ * auto fbo = Framebuffer::create(spec);
+ * @endcode
+ */
+struct FramebufferSpec {
+ /** @brief Framebuffer width in pixels */
+ u32 width = 1280;
+ /** @brief Framebuffer height in pixels */
+ u32 height = 720;
+ /** @brief Number of samples for MSAA (1 = no multisampling) */
+ u32 samples = 1;
+ /** @brief Whether to create depth/stencil attachment */
+ bool depthStencil = true;
+};
+
+// =============================================================================
+// Framebuffer Class
+// =============================================================================
+
+/**
+ * @brief Off-screen rendering target
+ *
+ * @details Encapsulates an OpenGL/WebGL framebuffer object with
+ * color and optional depth attachments. Supports render-to-texture
+ * for scene views, post-processing, and shadow maps.
+ *
+ * @code
+ * // Create framebuffer
+ * FramebufferSpec spec;
+ * spec.width = 800;
+ * spec.height = 600;
+ * auto fbo = Framebuffer::create(spec);
+ *
+ * // Render to texture
+ * fbo->bind();
+ * RenderCommand::clear();
+ * // ... render scene ...
+ * fbo->unbind();
+ *
+ * // Use color texture
+ * u32 textureId = fbo->getColorAttachment();
+ * @endcode
+ */
+class Framebuffer {
+public:
+ Framebuffer() = default;
+ ~Framebuffer();
+
+ // Non-copyable, movable
+ Framebuffer(const Framebuffer&) = delete;
+ Framebuffer& operator=(const Framebuffer&) = delete;
+ Framebuffer(Framebuffer&& other) noexcept;
+ Framebuffer& operator=(Framebuffer&& other) noexcept;
+
+ // =========================================================================
+ // Creation
+ // =========================================================================
+
+ /**
+ * @brief Creates a framebuffer from specification
+ * @param spec Framebuffer parameters
+ * @return Unique pointer to the framebuffer
+ *
+ * @details Creates framebuffer with color attachment and optional
+ * depth/stencil attachment based on spec.
+ */
+ static Unique<Framebuffer> create(const FramebufferSpec& spec);
+
+ // =========================================================================
+ // Operations
+ // =========================================================================
+
+ /**
+ * @brief Binds the framebuffer for rendering
+ * @details All subsequent draw calls will render to this framebuffer
+ * until unbind() is called.
+ */
+ void bind() const;
+
+ /**
+ * @brief Unbinds the framebuffer
+ * @details Restores rendering to the default framebuffer (screen).
+ */
+ void unbind() const;
+
+ /**
+ * @brief Resizes the framebuffer
+ * @param width New width in pixels
+ * @param height New height in pixels
+ *
+ * @details Recreates all attachments with new dimensions.
+ * Existing content is lost.
+ */
+ void resize(u32 width, u32 height);
+
+ // =========================================================================
+ // Properties
+ // =========================================================================
+
+ /** @brief Gets the color attachment texture ID */
+ u32 getColorAttachment() const { return colorAttachment_; }
+
+ /** @brief Gets the depth attachment texture ID (0 if none) */
+ u32 getDepthAttachment() const { return depthAttachment_; }
+
+ /** @brief Gets the framebuffer width in pixels */
+ u32 getWidth() const { return spec_.width; }
+
+ /** @brief Gets the framebuffer height in pixels */
+ u32 getHeight() const { return spec_.height; }
+
+ /** @brief Gets the framebuffer specification */
+ const FramebufferSpec& getSpecification() const { return spec_; }
+
+private:
+ /**
+ * @brief Initializes the framebuffer on GPU
+ * @return True on success
+ */
+ bool initialize();
+
+ /**
+ * @brief Releases GPU resources
+ */
+ void cleanup();
+
+ FramebufferSpec spec_;
+ u32 framebufferId_ = 0;
+ u32 colorAttachment_ = 0;
+ u32 depthAttachment_ = 0;
+};
+
+} // namespace esengine