Skip to content

Commit

Permalink
widget: added gl widget but not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Oct 29, 2024
1 parent f4f7fd2 commit 9fa77fa
Show file tree
Hide file tree
Showing 19 changed files with 360 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/app/panels/config_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace quickviz {
ConfigPanel::ConfigPanel(std::string name) : Panel(name) {
this->SetAutoLayout(false);
this->SetNoResize(true);
this->SetNoMove(true);
// this->SetNoMove(true);
this->SetWindowNoMenuButton();
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/panels/console_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace quickviz {
ConsolePanel::ConsolePanel(std::string name) : Panel(name) {
this->SetAutoLayout(false);
this->SetNoResize(true);
this->SetNoMove(true);
// this->SetNoMove(true);
this->SetWindowNoMenuButton();
}

Expand Down
12 changes: 11 additions & 1 deletion src/app/panels/scene_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

#include "panels/scene_panel.hpp"

#include "glad/glad.h"
#include "imview/fonts.hpp"

namespace quickviz {
ScenePanel::ScenePanel(std::string name) : Panel(name) {
this->SetAutoLayout(false);
this->SetNoResize(true);
this->SetNoMove(true);
// this->SetNoMove(true);
this->SetWindowNoMenuButton();
}

Expand All @@ -27,6 +28,15 @@ void ScenePanel::Draw() {
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::PopStyleColor();
ImGui::PopFont();

glEnable(GL_SCISSOR_TEST);
glViewport(x_, y_, width_, height_);
glScissor(x_, y_, width_, height_);

glClearColor(0.5, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glDisable(GL_SCISSOR_TEST);
}
End();
}
Expand Down
6 changes: 4 additions & 2 deletions src/imview/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ add_library(imview
src/widget/cv_image_widget.cpp
src/widget/buffered_cv_image_widget.cpp
src/widget/cairo_widget.cpp
src/widget/cairo/cairo_context.cpp
src/widget/cairo/cairo_draw.cpp
src/widget/details/cairo_context.cpp
src/widget/details/cairo_draw.cpp
src/widget/rt_line_plot_widget.cpp
src/widget/gl_scene_widget.cpp
src/widget/details/gl_frame_buffer.cpp
# data buffer
src/buffer/buffer_registry.cpp
# event handling
Expand Down
4 changes: 2 additions & 2 deletions src/imview/include/imview/widget/cairo_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "imgui.h"

#include "imview/panel.hpp"
#include "imview/widget/cairo/cairo_context.hpp"
#include "imview/widget/cairo/cairo_draw.hpp"
#include "imview/widget/details/cairo_context.hpp"
#include "imview/widget/details/cairo_draw.hpp"

namespace quickviz {
class CairoWidget : public Panel {
Expand Down
4 changes: 1 addition & 3 deletions src/imview/include/imview/widget/cv_image_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#ifndef QUICKVIZ_CV_IMAGE_WIDGET_HPP
#define QUICKVIZ_CV_IMAGE_WIDGET_HPP

#include "glad/glad.h"

#include <mutex>
#include <functional>

Expand All @@ -32,7 +30,7 @@ class CvImageWidget : public Panel {
private:
std::mutex image_mutex_;
cv::Mat image_mat_;
GLuint image_texture_;
uint32_t image_texture_;
bool keep_aspect_ratio_ = false;
};
} // namespace quickviz
Expand Down
49 changes: 49 additions & 0 deletions src/imview/include/imview/widget/details/gl_frame_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @file gl_frame_buffer.hpp
* @date 10/29/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef XMOTION_GL_FRAME_BUFFER_HPP
#define XMOTION_GL_FRAME_BUFFER_HPP

#include <cstdint>

namespace quickviz {
class GlFrameBuffer {
public:
GlFrameBuffer(uint32_t width, uint32_t height);
~GlFrameBuffer();

// do not allow copy or move
GlFrameBuffer(const GlFrameBuffer&) = delete;
GlFrameBuffer& operator=(const GlFrameBuffer&) = delete;
GlFrameBuffer(GlFrameBuffer&&) = delete;
GlFrameBuffer& operator=(GlFrameBuffer&&) = delete;

// public methods
void Bind() const;
void Unbind() const;
void Clear() const;
uint32_t GetTextureId() const { return texture_id_; }

uint32_t GetWidth() const { return width_; }
uint32_t GetHeight() const { return height_; }
void Resize(uint32_t width, uint32_t height);

private:
void CreateBuffers();
void DestroyBuffers();

uint32_t width_;
uint32_t height_;
uint32_t texture_id_;
uint32_t frame_buffer_;
uint32_t texture_buffer_;
uint32_t render_buffer_;
};
} // namespace quickviz

#endif // XMOTION_GL_FRAME_BUFFER_HPP
35 changes: 35 additions & 0 deletions src/imview/include/imview/widget/gl_scene_widget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* @file gl_scene_widget.hpp
* @date 10/29/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef XMOTION_GL_SCENE_WIDGET_HPP
#define XMOTION_GL_SCENE_WIDGET_HPP

#include <memory>
#include <functional>

#include "imview/panel.hpp"
#include "imview/widget/details/gl_frame_buffer.hpp"

namespace quickviz {
class GlSceneWidget : public Panel {
public:
GlSceneWidget(const std::string& widget_name);
~GlSceneWidget() = default;

// public methods
using GlRenderFunction = std::function<void(const GlFrameBuffer&)>;
void SetGlRenderFunction(GlRenderFunction func);
void Draw() override;

private:
GlRenderFunction render_function_;
std::unique_ptr<GlFrameBuffer> frame_buffer_;
};
} // namespace quickviz

#endif // XMOTION_GL_SCENE_WIDGET_HPP
1 change: 1 addition & 0 deletions src/imview/src/widget/cv_image_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "imview/widget/cv_image_widget.hpp"

#include "glad/glad.h"
#include "imview/utils/image_utils.hpp"

namespace quickviz {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (c) 2021 Ruixiang Du (rdu)
*/

#include "imview/widget/cairo//cairo_context.hpp"
#include "imview/widget/details/cairo_context.hpp"

#include <iostream>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (c) 2021 Ruixiang Du (rdu)
*/

#include "imview/widget/cairo/cairo_draw.hpp"
#include "imview/widget/details/cairo_draw.hpp"

#include <cmath>

Expand Down
91 changes: 91 additions & 0 deletions src/imview/src/widget/details/gl_frame_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* @file gl_frame_buffer.cpp
* @date 10/29/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#include "imview/widget/details/gl_frame_buffer.hpp"

#include <iostream>

#include "glad/glad.h"

namespace quickviz {
GlFrameBuffer::GlFrameBuffer(uint32_t width, uint32_t height)
: width_(width),
height_(height),
frame_buffer_(0),
texture_buffer_(0),
render_buffer_(0) {
CreateBuffers();
}

GlFrameBuffer::~GlFrameBuffer() { DestroyBuffers(); }

void GlFrameBuffer::CreateBuffers() {
// generate and bind the framebuffer
glGenFramebuffers(1, &frame_buffer_);
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);

// create a color attachment texture
glGenTextures(1, &texture_buffer_);
glBindTexture(GL_TEXTURE_2D, texture_buffer_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB,
GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture_buffer_, 0);

// create a renderbuffer object for depth and stencil attachment
glGenRenderbuffers(1, &render_buffer_);
glBindRenderbuffer(GL_RENDERBUFFER, render_buffer_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width_, height_);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, render_buffer_);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw std::runtime_error("Framebuffer initialization unsuccessful!");
}

// Unbind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void GlFrameBuffer::DestroyBuffers() {
glDeleteFramebuffers(1, &frame_buffer_);
glDeleteTextures(1, &texture_buffer_);
glDeleteRenderbuffers(1, &render_buffer_);
frame_buffer_ = 0;
texture_buffer_ = 0;
render_buffer_ = 0;
}

void GlFrameBuffer::Bind() const {
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);
glViewport(0, 0, width_, height_);
glEnable(GL_DEPTH_TEST);
}

void GlFrameBuffer::Unbind() const { glBindFramebuffer(GL_FRAMEBUFFER, 0); }

void GlFrameBuffer::Clear() const {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void GlFrameBuffer::Resize(uint32_t width, uint32_t height) {
if (width == width_ && height == height_) return;

width_ = width;
height_ = height;

// delete old buffers and create new ones
DestroyBuffers();
CreateBuffers();

std::cout << "new frame buffer id: " << frame_buffer_ << std::endl;
}
} // namespace quickviz
46 changes: 46 additions & 0 deletions src/imview/src/widget/gl_scene_widget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* @file gl_scene_widget.cpp
* @date 10/29/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#include "imview/widget/gl_scene_widget.hpp"

namespace quickviz {
GlSceneWidget::GlSceneWidget(const std::string& widget_name)
: Panel(widget_name) {
this->SetAutoLayout(false);
this->SetWindowNoMenuButton();
this->SetNoBackground(true);

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

void GlSceneWidget::SetGlRenderFunction(GlSceneWidget::GlRenderFunction func) {
render_function_ = func;
}

void GlSceneWidget::Draw() {
Begin();
{
ImVec2 contentSize = ImGui::GetContentRegionAvail();
float width = contentSize.x;
float height = contentSize.y;

frame_buffer_->Resize(width, height);
frame_buffer_->Bind();
if (render_function_ != nullptr) render_function_(*frame_buffer_.get());
frame_buffer_->Unbind();

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();
}
} // namespace quickviz
3 changes: 3 additions & 0 deletions src/imview/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ target_link_libraries(test_event PRIVATE imview)
add_executable(test_async_event test_async_event.cpp)
target_link_libraries(test_async_event PRIVATE imview)

add_executable(test_framebuffer test_framebuffer.cpp)
target_link_libraries(test_framebuffer PRIVATE imview)

find_package(OpenCV QUIET)
if (OpenCV_FOUND)
add_executable(test_double_buffer test_double_buffer.cpp)
Expand Down
3 changes: 3 additions & 0 deletions src/imview/test/feature/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ target_link_libraries(test_cairo_widget PRIVATE imview)

add_executable(test_implot_widget test_implot_widget.cpp)
target_link_libraries(test_implot_widget PRIVATE imview)

add_executable(test_gl_scene_widget test_gl_scene_widget.cpp)
target_link_libraries(test_gl_scene_widget PRIVATE imview)
Loading

0 comments on commit 9fa77fa

Please sign in to comment.