-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
widget: added gl widget but not working yet
- Loading branch information
Showing
19 changed files
with
360 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
49 changes: 49 additions & 0 deletions
49
src/imview/include/imview/widget/details/gl_frame_buffer.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.