-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffers.h
116 lines (99 loc) · 4.51 KB
/
buffers.h
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* GLViewer is a simple interface for opengl by handling the GLCore library
*
*/
#pragma once
#include "buffer_geometry.h"
#include "buffer_image.h"
#include "buffer_text.h"
#include "glviewer_common.h"
namespace GLViewer {
// Forward declare
class Viewer;
// A factory for creating, managing & storing all Geometry AllBuffers (static, dynamic & stream)
// Example: GeometryBufferID buffer = buffers.CreateBuffer<StaticBuffer>([](StaticBuffer& buffer){ buffer.Addxxx() });
// Example: GeometryBufferID buffer = buffers.CreateBuffer<DynamicBuffer>();
// Example: GeometryBufferID buffer = buffers.CreateBuffer<StreamBuffer>();
// Example: GeometryBufferID buffer = buffers.CreateBuffer<ImageBuffer>();
// Example: GeometryBufferID buffer = buffers.CreateBuffer<TextBuffer>();
class Buffers
{
public:
// Create a new static buffer
GeometryBufferID CreateStaticBuffer(std::function<void(StaticBuffer&)> cb_AddData = {});
// Create a new dynamic buffer
GeometryBufferID CreateDynamicBuffer();
// Create a new Stream buffer
GeometryBufferID CreateStreamBuffer();
// Create a new Text buffer
TextBufferID CreateTextBuffer();
// Create a new Image buffer
ImageBufferID CreateImageBuffer();
// Deletes a buffer, do not try to access this buffer after deletion
void DeleteGeometryBuffer(GeometryBufferID bufferID);
void DeleteTextBuffer(TextBufferID bufferID);
void DeleteImageBuffer(ImageBufferID bufferID);
// Deletes all geometry, text & image buffers
void DeleteAllBuffers();
void SetBufferData(TextBufferID bufferID, std::function<void(TextBuffer&)> cb_AddData) {
// Get buffer from id & Replace data using callback
GetTextBufferByID(bufferID.id).SetData(cb_AddData);
}
void SetBufferData(ImageBufferID bufferID, std::function<void(ImageBuffer&)> cb_AddData) {
// Get buffer from id & Replace data using callback
GetImageBufferByID(bufferID.id).SetData(cb_AddData);
}
void SetBufferData(GeometryBufferID bufferID, std::function<void(DynamicBuffer&)> cb_AddData) {
// Get buffer from id & replace data with new vertices in callback
GetGeometryBufferByID<DynamicBuffer>(bufferID.id).SetVertices(cb_AddData);
}
void SetBufferData(GeometryBufferID bufferID, std::function<void(StreamBuffer&)> cb_AddData) {
// Get buffer from id & replace data with new vertices in callback
GetGeometryBufferByID<StreamBuffer>(bufferID.id).SetVertices(cb_AddData);
}
private:
// Constructor
Buffers(Viewer* viewer) : m_Parent(viewer) {}
Viewer* m_Parent;
// A array of buffers (static, dynamic or stream)
MaxLib::Vector::Vector_Ptrs<GeometryBuffer> m_GeometryBuffers;
MaxLib::Vector::Vector_Ptrs<TextBuffer> m_TextBuffers;
MaxLib::Vector::Vector_Ptrs<ImageBuffer> m_ImageBuffers;
// Renders each of the buffers per frame
void RenderBuffers(const glm::mat4& proj, const glm::mat4& view);
TextBuffer& GetTextBufferByID(TextBufferID bufferID) {
// Go through each buffer
for(size_t i = 0; i < m_TextBuffers.Size(); i++) {
// Check if id's match, then return
if(m_TextBuffers[i].m_ID.id == bufferID.id) { return m_TextBuffers[i]; }
}
assert(0 && "TextBuffer ID does not exist");
}
ImageBuffer& GetImageBufferByID(ImageBufferID bufferID) {
// Go through each buffer
for(size_t i = 0; i < m_ImageBuffers.Size(); i++) {
// Check if id's match, then return
if(m_ImageBuffers[i].m_ID.id == bufferID.id) { return m_ImageBuffers[i]; }
}
assert(0 && "ImageBuffer ID does not exist");
}
// T: StaticBuffer, DynamicBuffer, StreamBuffer
template<typename T>
T& GetGeometryBufferByID(GeometryBufferID bufferID) {
// Go through each buffer
for(size_t i = 0; i < m_GeometryBuffers.Size(); i++) {
// Check if id's match
if(m_GeometryBuffers[i].m_ID.id == bufferID.id) {
// Convert to inherited class type and return
return *(m_GeometryBuffers.CastItem<T>(i));
//~ if(StaticBuffer* b = m_GeometryBuffers.CastItem<StaticBuffer>(i)) { return *b; }
//~ else if(DynamicBuffer* b = m_GeometryBuffers.CastItem<DynamicBuffer>(i)) { return *b; }
//~ else if(StreamBuffer* b = m_GeometryBuffers.CastItem<StreamBuffer>(i)) { return *b; }
//~ else { assert(0 && "Cannot case buffer type"); }
}
}
assert(0 && "GeometryBuffer ID does not exist");
}
friend class Viewer;
};
} // End namespace GLViewer