Skip to content

Commit

Permalink
component: added shader and shader_program class
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Nov 2, 2024
1 parent 9ffce16 commit 318a070
Show file tree
Hide file tree
Showing 29 changed files with 402 additions and 107 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
- name: Checkout submodules
run: git submodule update --init --recursive
- name: Install Dependencies
run: sudo apt-get update && sudo apt-get install -y libgl1-mesa-dev libglfw3-dev libcairo2-dev libopencv-dev
run: |
sudo apt-get update && sudo apt-get install -y libgl1-mesa-dev \
libglfw3-dev libcairo2-dev libopencv-dev libglm-dev
- name: Create Build Environment
run: cmake -E make_directory ${{runner.workspace}}/build
- name: Configure CMake (Ubuntu 20.04)
Expand Down
6 changes: 6 additions & 0 deletions data/glsl/fragment_shader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.2, 0.5, 0.8, 1.0);
}
6 changes: 6 additions & 0 deletions data/glsl/vertex_shader.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0);
}
2 changes: 1 addition & 1 deletion src/app/panels/main_docking_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include "imview/panel.hpp"

#include "imview/widget/gl_scene_widget.hpp"
#include "imview/widget/gl_widget.hpp"

#include "panels/menu_bar.hpp"
#include "panels/config_panel.hpp"
Expand Down
16 changes: 8 additions & 8 deletions src/imview/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ add_library(imview
src/panel.cpp
src/layer.cpp
${AUTO_LAYOUT_SRC}
# src/popup.cpp
# utils
src/utils/image_utils.cpp
# widgets
src/widget/cv_image_widget.cpp
src/widget/buffered_cv_image_widget.cpp
src/widget/cairo_widget.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
src/widget/details/shader.cpp
src/widget/gl_widget.cpp
# components
src/component/cairo_context.cpp
src/component/cairo_draw.cpp
src/component/image_utils.cpp
src/component/shader.cpp
src/component/shader_program.cpp
src/component/frame_buffer.cpp
# data buffer
src/buffer/buffer_registry.cpp
src/buffer/scrolling_plot_buffer.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* @file gl_frame_buffer.hpp
* @file 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
#ifndef XMOTION_FRAME_BUFFER_HPP
#define XMOTION_FRAME_BUFFER_HPP

#include <cstdint>

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

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

// public methods
void Bind(bool keep_aspect_ratio = true) const;
Expand All @@ -45,4 +45,4 @@ class GlFrameBuffer {
};
} // namespace quickviz

#endif // XMOTION_GL_FRAME_BUFFER_HPP
#endif // XMOTION_FRAME_BUFFER_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@
#ifndef XMOTION_SHADER_HPP
#define XMOTION_SHADER_HPP

#include <cstdint>
#include <string>
#include <unordered_map>

#include <glm/glm.hpp>

#include "glad/glad.h"

namespace quickviz {
class Shader {
public:
enum class Type {
enum class Type : uint32_t {
kUnknown = 0,
kVertex,
kFragment,
};

public:
Shader(const std::string& source, Type type);
Shader(const std::string& source_file, Type type);
~Shader();

// do not allow copy
Shader(const Shader&) = delete;
Shader& operator=(const Shader&) = delete;

// public methods
void Compile();
GLuint GetShaderID() const { return shader_id_; }
void Print() const;
bool Compile();
uint32_t GetShaderID() const { return shader_id_; }

private:
std::string LoadSourceFile(const std::string& file_path);

GLuint shader_id_;
std::string source_file_;
Type type_;
std::string source_code_;
uint32_t shader_id_;
};
} // namespace quickviz

Expand Down
45 changes: 45 additions & 0 deletions src/imview/include/imview/component/shader_program.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @file shader_program.hpp
* @date 11/2/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#ifndef QUICKVIZ_SHADER_PROGRAM_HPP
#define QUICKVIZ_SHADER_PROGRAM_HPP

#include <glm/glm.hpp>

#include <cstdint>
#include <unordered_map>

#include "imview/component/shader.hpp"

namespace quickviz {
class ShaderProgram {
public:
ShaderProgram();
~ShaderProgram();

// public methods
void AttachShader(const Shader& shader);
bool LinkProgram();
void Use() const;

// Uniform setting functions
void SetUniform(const std::string& name, bool value);
void SetUniform(const std::string& name, int value);
void SetUniform(const std::string& name, float value);
void SetUniform(const std::string& name, const glm::vec3& vector);
void SetUniform(const std::string& name, const glm::mat4& matrix);

private:
uint32_t GetUniformLocation(const std::string& name);

uint32_t program_id_;
std::unordered_map<std::string, uint32_t> uniform_location_cache_;
};
} // namespace quickviz

#endif // QUICKVIZ_SHADER_PROGRAM_HPP
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/details/cairo_context.hpp"
#include "imview/widget/details/cairo_draw.hpp"
#include "imview/component/cairo_context.hpp"
#include "imview/component/cairo_draw.hpp"

namespace quickviz {
class CairoWidget : public Panel {
Expand Down
35 changes: 0 additions & 35 deletions src/imview/include/imview/widget/gl_scene_widget.hpp

This file was deleted.

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

#ifndef XMOTION_GL_WIDGET_HPP
#define XMOTION_GL_WIDGET_HPP

#include <memory>
#include <functional>

#include "imview/panel.hpp"
#include "imview/component/frame_buffer.hpp"

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

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

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

#endif // XMOTION_GL_WIDGET_HPP
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/details/cairo_context.hpp"
#include "imview/component/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/details/cairo_draw.hpp"
#include "imview/component/cairo_draw.hpp"

#include <cmath>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* @file gl_frame_buffer.cpp
* @file frame_buffer.cpp
* @date 10/29/24
* @brief
*
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

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

#include <iostream>

#include "glad/glad.h"

namespace quickviz {
GlFrameBuffer::GlFrameBuffer(uint32_t width, uint32_t height)
FrameBuffer::FrameBuffer(uint32_t width, uint32_t height)
: width_(width),
height_(height),
frame_buffer_(0),
Expand All @@ -22,9 +22,9 @@ GlFrameBuffer::GlFrameBuffer(uint32_t width, uint32_t height)
CreateBuffers();
}

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

void GlFrameBuffer::CreateBuffers() {
void FrameBuffer::CreateBuffers() {
// generate and bind the framebuffer
glGenFramebuffers(1, &frame_buffer_);
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);
Expand Down Expand Up @@ -54,7 +54,7 @@ void GlFrameBuffer::CreateBuffers() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void GlFrameBuffer::DestroyBuffers() {
void FrameBuffer::DestroyBuffers() {
glDeleteFramebuffers(1, &frame_buffer_);
glDeleteTextures(1, &texture_buffer_);
glDeleteRenderbuffers(1, &render_buffer_);
Expand All @@ -63,7 +63,7 @@ void GlFrameBuffer::DestroyBuffers() {
render_buffer_ = 0;
}

void GlFrameBuffer::Bind(bool keep_aspect_ratio) const {
void FrameBuffer::Bind(bool keep_aspect_ratio) const {
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);
if (keep_aspect_ratio) {
float square_size = std::min(width_, height_);
Expand All @@ -76,14 +76,14 @@ void GlFrameBuffer::Bind(bool keep_aspect_ratio) const {
glEnable(GL_DEPTH_TEST);
}

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

void GlFrameBuffer::Clear(float r, float g, float b, float a) const {
void FrameBuffer::Clear(float r, float g, float b, float a) const {
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

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

width_ = width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Copyright (c) 2024 Ruixiang Du (rdu)
*/

#include "imview/utils/image_utils.hpp"
#include "imview/component/image_utils.hpp"

namespace quickviz {
cv::Mat GenerateRandomMat(int width, int height) {
Expand Down
Loading

0 comments on commit 318a070

Please sign in to comment.