Skip to content

Commit

Permalink
frame_buffer: updated aspect ratio handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Nov 5, 2024
1 parent 62dc3fe commit f3f75f0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/app/panels/scene_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ScenePanel::ScenePanel(const std::string& panel_name) : GlWidget(panel_name) {

void ScenePanel::Draw() {
ImVec2 content_size = ImGui::GetContentRegionAvail();

// Orthographic projection for a top-down view
float aspect_ratio =
static_cast<float>(content_size.x) / static_cast<float>(content_size.y);
Expand Down
4 changes: 3 additions & 1 deletion src/imview/include/imview/component/opengl/frame_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FrameBuffer {
FrameBuffer& operator=(FrameBuffer&&) = delete;

// public methods
void Bind() const;
void Bind(bool lock_aspect_ratio = true) const;
void Unbind() const;
void Clear(float r = 0.0, float g = 0.0, float b = 0.0, float a = 1.0) const;
uint32_t GetTextureId() const { return texture_buffer_; }
Expand All @@ -37,6 +37,8 @@ class FrameBuffer {
void CreateBuffers();
void DestroyBuffers();

float aspect_ratio_ = 1.0;
bool lock_aspect_ratio_ = true;
uint32_t width_;
uint32_t height_;
uint32_t frame_buffer_;
Expand Down
17 changes: 15 additions & 2 deletions src/imview/src/component/opengl/frame_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FrameBuffer::FrameBuffer(uint32_t width, uint32_t height)
frame_buffer_(0),
texture_buffer_(0),
render_buffer_(0) {
aspect_ratio_ = static_cast<float>(width) / static_cast<float>(height);
CreateBuffers();
}

Expand Down Expand Up @@ -63,9 +64,21 @@ void FrameBuffer::DestroyBuffers() {
render_buffer_ = 0;
}

void FrameBuffer::Bind() const {
void FrameBuffer::Bind(bool lock_aspect_ratio) const {
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);
glViewport(0, 0, width_, height_);

if (lock_aspect_ratio) {
float expected_width = height_ * aspect_ratio_;
if (expected_width > width_) {
float actual_height = width_ / aspect_ratio_;
glViewport(0, (height_ - actual_height) / 2, width_, actual_height);
} else {
float actual_width = expected_width;
glViewport((width_ - actual_width) / 2, 0, actual_width, height_);
}
} else {
glViewport(0, 0, width_, height_);
}
glEnable(GL_DEPTH_TEST);
}

Expand Down
34 changes: 18 additions & 16 deletions src/imview/src/widget/gl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ GlWidget::GlWidget(const std::string& widget_name) : Panel(widget_name) {
this->SetAutoLayout(false);
this->SetWindowNoMenuButton();
this->SetNoBackground(true);

frame_buffer_ = std::make_unique<FrameBuffer>(100, 50);
}

void GlWidget::AddOpenGLObject(const std::string& name,
Expand Down Expand Up @@ -42,21 +40,25 @@ void GlWidget::Draw() {
float width = content_size.x;
float height = content_size.y;

// render to frame buffer
frame_buffer_->Resize(width, height);
frame_buffer_->Bind();
for (auto& obj : drawable_objects_) {
obj.second->OnDraw(projection_, view_);
if (frame_buffer_ != nullptr) {
// render to frame buffer
frame_buffer_->Resize(width, height);
frame_buffer_->Bind();
for (auto& obj : drawable_objects_) {
obj.second->OnDraw(projection_, view_);
}
frame_buffer_->Unbind();

// render frame buffer to ImGui
ImVec2 uv0 = ImVec2(0, 1);
ImVec2 uv1 = ImVec2(1, 0);
ImVec4 tint_col = ImVec4(1, 1, 1, 1);
ImVec4 border_col = ImVec4(0, 0, 0, 0);
ImGui::Image((void*)(intptr_t)frame_buffer_->GetTextureId(),
ImVec2(width, height), uv0, uv1, tint_col, border_col);
} else {
frame_buffer_ = std::make_unique<FrameBuffer>(width, height);
}
frame_buffer_->Unbind();

// render frame buffer to ImGui
ImVec2 uv0 = ImVec2(0, 1);
ImVec2 uv1 = ImVec2(1, 0);
ImVec4 tint_col = ImVec4(1, 1, 1, 1);
ImVec4 border_col = ImVec4(0, 0, 0, 0);
ImGui::Image((void*)(intptr_t)frame_buffer_->GetTextureId(),
ImVec2(width, height), uv0, uv1, tint_col, border_col);
}
End();
}
Expand Down

0 comments on commit f3f75f0

Please sign in to comment.