-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_image.cpp
80 lines (63 loc) · 2.35 KB
/
buffer_image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <algorithm>
#include "glviewer.h"
#include "buffer_image.h"
using namespace std;
using namespace MaxLib;
namespace GLViewer {
// Global counter for buffer
static uint m_Counter_ImageID = 0;
RenderableImage::RenderableImage(Viewer* viewer, ImageTexture texture, const glm::vec2& position2d, const glm::vec2& size, const glm::vec4& colour)
: m_Texture(texture), m_Position(position2d), m_Size(size), m_Colour(colour), m_Valid(true)
{
(void)viewer;
}
RenderableImage::RenderableImage(Viewer* viewer, ImageTexture texture, const glm::vec3& position3d, const glm::vec2& size, const glm::vec4& colour)
: m_Texture(texture), m_Size(size), m_Colour(colour)
{
// convert world coords to screen coords
if(std::optional<glm::vec2> screenCoords = viewer->WorldToScreenCoords(position3d)) {
// top left of image
m_Position = *screenCoords - (m_Size / 2.0f);
m_Valid = true;
}
}
void RenderableImage::Render()
{
if(!m_Valid) { return; }
// convert colour
ImU32 colour = ImGui::GetColorU32(ImVec4(m_Colour.r, m_Colour.g, m_Colour.b, m_Colour.a));
ImVec2 p_min = ImVec2(m_Position.x, m_Position.y);
ImVec2 p_max = ImVec2(m_Position.x + m_Size.x, m_Position.y + m_Size.y);
// add to ImGui draw list
ImGui::GetBackgroundDrawList()->AddImage((void*)(intptr_t)m_Texture.textureID, p_min, p_max, ImVec2(0, 0), ImVec2(1, 1), colour);
}
ImageBuffer::ImageBuffer(Viewer* viewer)
: m_Parent(viewer), m_ID(++m_Counter_ImageID)
{}
void ImageBuffer::Add2DImage(ImageTexture texture, const glm::vec2& position2d, const glm::vec2& size, const glm::vec4& colour)
{
m_Images.emplace_back(m_Parent, texture, position2d, size, colour);
}
void ImageBuffer::Add3DImage(ImageTexture texture, const glm::vec3& position3d, const glm::vec2& size, const glm::vec4& colour)
{
m_Images.emplace_back(m_Parent, texture, position3d, size, colour);
}
void ImageBuffer::ClearAll()
{
m_Images.erase(m_Images.begin(), m_Images.end());
}
void ImageBuffer::SetData(std::function<void(ImageBuffer&)> cb_AddData)
{ // Clear old data
ClearAll();
// Add vertices via callback
cb_AddData(*this);
}
void ImageBuffer::Render()
{
// add each image to imgui draw list
for(RenderableImage& image : m_Images) {
image.Render();
}
}
} // End namespace GLViewer