mouse picking

This commit is contained in:
Erris
2026-02-22 16:35:27 +01:00
parent cb064bdb46
commit 577b52be28
19 changed files with 412 additions and 124 deletions

View File

@@ -1,9 +1,11 @@
#ifndef OPENGL_FRAMEBUFFER_HPP
#define OPENGL_FRAMEBUFFER_HPP
#include "core.hpp"
#include <renderer/framebuffer.hpp>
#include <cstdint>
#include <vector>
namespace OpenEngine {
class OpenGLFramebuffer : public FrameBuffer
@@ -18,15 +20,24 @@ namespace OpenEngine {
virtual void Unbind() override;
virtual void Resize(uint32_t width, uint32_t height) override;
virtual int ReadPixel(uint32_t index, int x, int y) override;
virtual uint32_t GetColorAttachmentRendererID() const override { return color_attachment; }
virtual uint32_t GetColorAttachmentRendererID(uint32_t index = 0) const override {
OE_CORE_ASSERT(index < color_attachment_ids.size(), "Index is greater than color attachment count.");
return color_attachment_ids[index];
}
virtual const FramebufferSpecification& GetSpecification() const override { return specs; }
private:
uint32_t id = 0;
uint32_t color_attachment = 0, depth_attachment = 0;
FramebufferSpecification specs;
std::vector<FramebufferTextureSpecification> color_attachment_specs;
FramebufferTextureSpecification depth_attachment_specs = FramebufferTextureFormat::None;
std::vector<uint32_t> color_attachment_ids;
uint32_t depth_attachment_id;
};
}

View File

@@ -13,6 +13,7 @@ namespace OpenEngine {
virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) override;
virtual void SetClearColor(const glm::vec4& color) override;
virtual void ClearBufferI(int buffer, int value) override;
virtual void Clear() override;
virtual void DrawIndexed(const Ref<VertexArray>& vertex_array, uint32_t index_count = 0) override;

View File

@@ -34,6 +34,9 @@ namespace OpenEngine {
float GetPitch() const { return pitch; }
float GetYaw() const { return yaw; }
bool GetMoving() { return moving; };
private:
void UpdateProjection();
void UpdateView();
@@ -49,6 +52,7 @@ namespace OpenEngine {
std::pair<float, float> PanSpeed() const;
float RotationSpeed() const;
float ZoomSpeed() const;
private:
float fov = 45.0f, aspect_ratio = 1.778f, near_clip = 0.1f, far_clip = 1000.0f;
@@ -61,6 +65,8 @@ namespace OpenEngine {
float distance = 10.0f;
float pitch = 0.0f, yaw = 0.0f;
bool moving = false;
float viewport_width = 1280, viewport_height = 720;
};

View File

@@ -4,11 +4,50 @@
#include "open_engine/ref_scope.hpp"
#include <cstdint>
#include <initializer_list>
#include <vector>
namespace OpenEngine {
enum class FramebufferTextureFormat
{
None = 0,
// Color
RGBA8,
RED_INTEGER,
// Depth/Stencil
DEPTH24STENCIL8,
// Default
Depth = DEPTH24STENCIL8
};
struct FramebufferTextureSpecification
{
FramebufferTextureSpecification() = default;
FramebufferTextureSpecification(FramebufferTextureFormat format)
: texture_format(format)
{};
FramebufferTextureFormat texture_format = FramebufferTextureFormat::None;
};
struct FramebufferAttachmentSpecification
{
FramebufferAttachmentSpecification() = default;
FramebufferAttachmentSpecification(std::initializer_list<FramebufferTextureSpecification> attachments)
: attachments(attachments)
{}
std::vector<FramebufferTextureSpecification> attachments;
};
struct FramebufferSpecification
{
uint32_t width, height;
FramebufferAttachmentSpecification attachments;
uint32_t samples = 1;
bool swap_chain_target = false;
@@ -22,8 +61,9 @@ namespace OpenEngine {
virtual void Unbind() = 0;
virtual void Resize(uint32_t width, uint32_t height) = 0;
virtual int ReadPixel(uint32_t index, int x, int y) = 0;
virtual uint32_t GetColorAttachmentRendererID() const = 0;
virtual uint32_t GetColorAttachmentRendererID(uint32_t index = 0) const = 0;
virtual const FramebufferSpecification& GetSpecification() const = 0;

View File

@@ -24,6 +24,11 @@ namespace OpenEngine {
api->SetClearColor(color);
};
inline static void ClearBufferI(int buffer, int value)
{
api->ClearBufferI(buffer, value);
};
inline static void Clear()
{
api->Clear();

View File

@@ -39,13 +39,23 @@ namespace OpenEngine {
static void EndScene();
static void Flush();
static void DrawQuad(const Transform& transform_data, const glm::vec4& color);
static void DrawQuad(const Transform& transform_data, const Ref<Texture2D>& texture, float tiling_factor = 1.0f);
static void DrawQuad(const Transform& transform_data, const Ref<Subtexture2D>& subtexture, float tiling_factor = 1.0f);
static void DrawQuad(const Transform& transform_data,
const glm::vec4& color, int entity_id);
static void DrawQuad(const Transform& transform_data,
const Ref<Texture2D>& texture, int entity_id,
float tiling_factor = 1.0f);
static void DrawQuad(const Transform& transform_data,
const Ref<Subtexture2D>& subtexture, int entity_id,
float tiling_factor = 1.0f);
static void DrawQuad(const glm::mat4& transform, const glm::vec4& color);
static void DrawQuad(const glm::mat4& transform, const Ref<Texture2D>& texture, float tiling_factor = 1.0f);
static void DrawQuad(const glm::mat4& transform, const Ref<Subtexture2D>& subtexture, float tiling_factor = 1.0f);
static void DrawQuad(const glm::mat4& transform,
const glm::vec4& color, int entity_id);
static void DrawQuad(const glm::mat4& transform,
const Ref<Texture2D>& texture, int entity_id,
float tiling_factor = 1.0f);
static void DrawQuad(const glm::mat4& transform,
const Ref<Subtexture2D>& subtexture, int entity_id,
float tiling_factor = 1.0f);
static void ResetStats();
static const Statistics& GetStats();

View File

@@ -21,6 +21,7 @@ namespace OpenEngine {
virtual void SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) = 0;
virtual void SetClearColor(const glm::vec4& color) = 0;
virtual void ClearBufferI(int buffer, int value) = 0;
virtual void Clear() = 0;
virtual void DrawIndexed(const Ref<VertexArray>& vertex_array, uint32_t count = 0) = 0;

163
open_engine/src/open_engine/opengl/opengl_framebuffer.cpp Normal file → Executable file
View File

@@ -1,49 +1,166 @@
#include "logging.hpp"
#include <cstdint>
#include <pch.hpp>
#include <core.hpp>
#include <opengl/opengl_framebuffer.hpp>
#include <glad/glad.h>
#include <opengl/opengl_framebuffer.hpp>
#include <renderer/framebuffer.hpp>
namespace OpenEngine {
static const uint32_t max_framebuffer_size = 8192;
namespace Utils {
static bool IsDepthFormat(FramebufferTextureFormat format)
{
switch (format) {
case FramebufferTextureFormat::DEPTH24STENCIL8: return true;
}
return false;
}
static GLenum TextureTarget(bool multisampled)
{
return multisampled ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
}
static void CreateTexture(bool multisampled, uint32_t* out_id,
uint32_t count)
{
glCreateTextures(TextureTarget(multisampled), count, out_id);
}
static void BindTexture(bool multisampled, uint32_t id)
{
glBindTexture(TextureTarget(multisampled), id);
}
static void AttachColorTexture(uint32_t id, int samples,
GLenum internal_format, GLenum format,
uint32_t width, uint32_t height, int index)
{
bool multisampled = samples > 1;
if (multisampled) {
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples,
internal_format, width, height, GL_FALSE);
} else {
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height,
0, format, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
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 + index,
TextureTarget(multisampled), id, 0);
}
static void AttachDepthTexture(uint32_t id, int samples, GLenum format,
GLenum attachment_type, uint32_t width, uint32_t height)
{
bool multisampled = samples > 1;
if (multisampled) {
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples,
format, width, height, GL_FALSE);
} else {
glTexStorage2D(GL_TEXTURE_2D, 1, format, width, height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
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, attachment_type,
TextureTarget(multisampled), id, 0);
}
}
OpenGLFramebuffer::OpenGLFramebuffer(const FramebufferSpecification& spec)
: specs(spec)
{
for (auto format : specs.attachments.attachments) {
if (!Utils::IsDepthFormat(format.texture_format))
color_attachment_specs.emplace_back(format);
else
depth_attachment_specs = format.texture_format;
}
Invalidate();
}
OpenGLFramebuffer::~OpenGLFramebuffer()
{
glDeleteFramebuffers(1, &id);
glDeleteTextures(1, &color_attachment);
glDeleteTextures(1, &depth_attachment);
glDeleteTextures(color_attachment_ids.size(), color_attachment_ids.data());
glDeleteTextures(1, &depth_attachment_id);
}
void OpenGLFramebuffer::Invalidate()
{
if (id) {
glDeleteFramebuffers(1, &id);
glDeleteTextures(1, &color_attachment);
glDeleteTextures(1, &depth_attachment);
glDeleteTextures(color_attachment_ids.size(), color_attachment_ids.data());
glDeleteTextures(1, &depth_attachment_id);
color_attachment_ids.clear();
depth_attachment_id = 0;
}
glCreateFramebuffers(1, &id);
glBindFramebuffer(GL_FRAMEBUFFER, id);
glCreateTextures(GL_TEXTURE_2D, 1, &color_attachment);
glBindTexture(GL_TEXTURE_2D, color_attachment);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, specs.width, specs.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
bool multisample = specs.samples > 1;
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_attachment, 0);
// Attachments
if (color_attachment_specs.size()) {
color_attachment_ids.resize(color_attachment_specs.size());
Utils::CreateTexture(multisample, color_attachment_ids.data(), color_attachment_ids.size());
glCreateTextures(GL_TEXTURE_2D, 1, &depth_attachment);
glBindTexture(GL_TEXTURE_2D, depth_attachment);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, specs.width, specs.height);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depth_attachment, 0);
for (size_t i = 0; i < color_attachment_specs.size(); i++) {
Utils::BindTexture(multisample, color_attachment_ids[i]);
switch (color_attachment_specs[i].texture_format) {
case FramebufferTextureFormat::RGBA8:
Utils::AttachColorTexture(color_attachment_ids[i],
specs.samples, GL_RGBA8, GL_RGBA,
specs.width, specs.height, i);
break;
case FramebufferTextureFormat::RED_INTEGER:
Utils::AttachColorTexture(color_attachment_ids[i],
specs.samples, GL_R32I, GL_RED_INTEGER,
specs.width, specs.height, i);
break;
}
}
}
if (depth_attachment_specs.texture_format != FramebufferTextureFormat::None) {
Utils::CreateTexture(multisample, &depth_attachment_id, 1);
Utils::BindTexture(multisample, depth_attachment_id);
switch (depth_attachment_specs.texture_format) {
case OpenEngine::FramebufferTextureFormat::DEPTH24STENCIL8:
Utils::AttachDepthTexture(depth_attachment_id, specs.samples,
GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL_ATTACHMENT,
specs.width, specs.height);
break;
}
}
if (color_attachment_ids.size() > 1) {
OE_CORE_ASSERT(color_attachment_ids.size() < 4, "Only 4 color attachments supported.");
GLenum buffers[4] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(color_attachment_ids.size(), buffers);
} else if (color_attachment_ids.empty()) {
glDrawBuffer(GL_NONE);
}
OE_CORE_ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Framebuffer is incomplete!");
@@ -72,4 +189,16 @@ namespace OpenEngine {
Invalidate();
}
int OpenGLFramebuffer::ReadPixel(uint32_t index, int x, int y)
{
OE_CORE_ASSERT(index < color_attachment_ids.size(), "Index is greater than attachment count.");
glReadBuffer(GL_COLOR_ATTACHMENT0 + index);
int pixel_data;
glReadPixels(x, y, 1, 1, GL_RED_INTEGER, GL_INT, &pixel_data);
return pixel_data;
}
}

View File

@@ -57,6 +57,11 @@ namespace OpenEngine {
glClearColor(color.r, color.g, color.b, color.a);
}
void OpenGLRendererAPI::ClearBufferI(int buffer, int value)
{
glClearBufferiv(GL_COLOR, buffer, &value);
}
void OpenGLRendererAPI::Clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@@ -1,3 +1,4 @@
#include "renderer/buffer.hpp"
#include <pch.hpp>
#include <core.hpp>
@@ -64,12 +65,22 @@ namespace OpenEngine {
const auto& layout = vertex_buffer->GetLayout();
for (const auto& element : layout) {
glEnableVertexAttribArray(index);
glVertexAttribPointer(index,
element.GetComponentCount(),
ShaderDataTypeToOpenGLBaseType(element.type),
element.normalized? GL_TRUE : GL_FALSE,
layout.GetStride(),
(const void*)(intptr_t)element.offset);
if (element.type == ShaderDataType::Int) {
glVertexAttribIPointer(index,
element.GetComponentCount(),
ShaderDataTypeToOpenGLBaseType(element.type),
layout.GetStride(),
(const void*)(intptr_t)element.offset);
} else {
glVertexAttribPointer(index,
element.GetComponentCount(),
ShaderDataTypeToOpenGLBaseType(element.type),
element.normalized? GL_TRUE : GL_FALSE,
layout.GetStride(),
(const void*)(intptr_t)element.offset);
}
index++;
}

View File

@@ -69,13 +69,22 @@ namespace OpenEngine {
glm::vec2 delta = (mouse - initial_mouse_position) * 0.003f;
initial_mouse_position = mouse;
if (Input::IsMouseButtonPressed(Mouse::ButtonMiddle))
if (Input::IsMouseButtonPressed(Mouse::ButtonMiddle)) {
MousePan(delta);
else if (Input::IsMouseButtonPressed(Mouse::ButtonLeft))
moving = true;
}
else if (Input::IsMouseButtonPressed(Mouse::ButtonLeft)) {
MouseRotate(delta);
else if (Input::IsMouseButtonPressed(Mouse::ButtonRight))
moving = true;
}
else if (Input::IsMouseButtonPressed(Mouse::ButtonRight)) {
MouseZoom(delta.y);
}
moving = true;
}
} else {
moving = false;
}
UpdateView();
}

View File

@@ -22,6 +22,7 @@ namespace OpenEngine {
glm::vec2 tex_coord;
float tex_index;
float tiling_factor;
int id = -1;
};
struct Renderer2DData
@@ -55,14 +56,15 @@ namespace OpenEngine {
OE_PROFILE_FUNCTION();
renderer_data.vertex_array = VertexArray::Create();
renderer_data.vertex_buffer = VertexBuffer::Create(renderer_data.max_indices * sizeof(QuadVertex));
renderer_data.vertex_buffer = VertexBuffer::Create(renderer_data.max_vertices * sizeof(QuadVertex));
BufferLayout layout = {
{ ShaderDataType::Float3, "a_Position" },
{ ShaderDataType::Float4, "a_Color" },
{ ShaderDataType::Float2, "a_TexCoords" },
{ ShaderDataType::Float, "a_TexIndex" },
{ ShaderDataType::Float, "a_TilingFactor" }
{ ShaderDataType::Float, "a_TilingFactor" },
{ ShaderDataType::Int, "a_EntityID"}
};
renderer_data.vertex_buffer->SetLayout(layout);
@@ -183,7 +185,8 @@ namespace OpenEngine {
renderer_data.stats.draw_calls++;
}
void Renderer2D::DrawQuad(const Transform& transform_data, const Ref<Texture2D>& texture, float tiling_factor)
void Renderer2D::DrawQuad(const Transform& transform_data,
const Ref<Texture2D>& texture, int entity_id, float tiling_factor)
{
OE_PROFILE_FUNCTION();
@@ -219,10 +222,12 @@ namespace OpenEngine {
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), transform_data.size);
DrawQuad(transform, texture);
DrawQuad(transform, texture, entity_id);
}
void Renderer2D::DrawQuad(const Transform& transform_data, const Ref<Subtexture2D>& subtexture, float tiling_factor)
void Renderer2D::DrawQuad(const Transform& transform_data,
const Ref<Subtexture2D>& subtexture, int entity_id,
float tiling_factor)
{
OE_PROFILE_FUNCTION();
@@ -259,10 +264,11 @@ namespace OpenEngine {
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
* glm::scale(glm::mat4(1.0f), transform_data.size);
DrawQuad(transform, subtexture);
DrawQuad(transform, subtexture, entity_id);
}
void Renderer2D::DrawQuad(const Transform& transform_data, const glm::vec4& color)
void Renderer2D::DrawQuad(const Transform& transform_data,
const glm::vec4& color, int entity_id)
{
OE_PROFILE_FUNCTION();
@@ -280,10 +286,11 @@ namespace OpenEngine {
* glm::scale(glm::mat4(1.0f), transform_data.size);
DrawQuad(transform, color);
DrawQuad(transform, color, entity_id);
}
void Renderer2D::DrawQuad(const glm::mat4& transform, const Ref<Texture2D>& texture, float tiling_factor)
void Renderer2D::DrawQuad(const glm::mat4& transform,
const Ref<Texture2D>& texture, int entity_id, float tiling_factor)
{
OE_PROFILE_FUNCTION();
@@ -318,6 +325,7 @@ namespace OpenEngine {
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
renderer_data.quad_vertex_ptr->tex_index = texture_index;
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
renderer_data.quad_vertex_ptr->id = entity_id;
renderer_data.quad_vertex_ptr++;
}
@@ -325,7 +333,9 @@ namespace OpenEngine {
renderer_data.stats.quad_count++;
}
void Renderer2D::DrawQuad(const glm::mat4& transform, const Ref<Subtexture2D>& subtexture, float tiling_factor)
void Renderer2D::DrawQuad(const glm::mat4& transform,
const Ref<Subtexture2D>& subtexture, int entity_id,
float tiling_factor)
{
OE_PROFILE_FUNCTION();
@@ -361,6 +371,7 @@ namespace OpenEngine {
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
renderer_data.quad_vertex_ptr->tex_index = texture_index;
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
renderer_data.quad_vertex_ptr->id = entity_id;
renderer_data.quad_vertex_ptr++;
}
@@ -368,7 +379,8 @@ namespace OpenEngine {
renderer_data.stats.quad_count++;
}
void Renderer2D::DrawQuad(const glm::mat4& transform, const glm::vec4& color)
void Renderer2D::DrawQuad(const glm::mat4& transform,
const glm::vec4& color, int entity_id)
{
OE_PROFILE_FUNCTION();
@@ -384,6 +396,7 @@ namespace OpenEngine {
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
renderer_data.quad_vertex_ptr->tex_index = 0;
renderer_data.quad_vertex_ptr->tiling_factor = 1.0f;
renderer_data.quad_vertex_ptr->id = entity_id;
renderer_data.quad_vertex_ptr++;
}

View File

@@ -1,3 +1,4 @@
#include "logging.hpp"
#include <pch.hpp>
#include <scene/scene.hpp>
@@ -60,7 +61,7 @@ namespace OpenEngine {
for (const auto& entity : view) {
auto [transform, sprite] = view.get<TransformComponent, SpriteRendererComponent>(entity);
Renderer2D::DrawQuad(transform.GetTransform(), sprite.color);
Renderer2D::DrawQuad(transform.GetTransform(), sprite.color, (int)entity);
}
Renderer2D::EndScene();
@@ -77,7 +78,7 @@ namespace OpenEngine {
{
auto [transform, sprite] = group.get<TransformComponent, SpriteRendererComponent>(entity);
Renderer2D::DrawQuad(transform.GetTransform(), sprite.color);
Renderer2D::DrawQuad(transform.GetTransform(), sprite.color, (int)entity);
}
Renderer2D::EndScene();