cgv
Loading...
Searching...
No Matches
generic_render_data.h
1#pragma once
2
3#include <cgv/render/context.h>
4#include <cgv/render/shader_program.h>
5#include <cgv/render/attribute_array_binding.h>
6
7#include "attribute_array_manager.h"
8
9// defines are prefixed with GRD (generic render data) to prevent conflicts with other libraries
10#define GRD_EXPAND(x) x
11
12#define GRD_VAR_TO_STRING_(s) #s
13#define GRD_VAR_TO_STRING(s) GRD_VAR_TO_STRING_(s)
14
15#define GRD_DECL_VEC_MEMBER(t, i) std::vector<t> i;
16#define GRD_DECL_VEC_MEMBER_CONST_REF(t, i) const std::vector<t>& ref_##i() const { return i; }
17#define GRD_CALL_CLEAR_FUNC(t, i) i.clear();
18#define GRD_CALL_SIZE_FUNC(t, i) i.size();
19#define GRD_CALL_PUSH_BACK_FUNC(t, i) this->i.push_back(i);
20#define GRD_SET_ATTRIB_ARRAY(t, i) success &= set_attribute_array(ctx, prog, GRD_VAR_TO_STRING(i), i);
21#define GRD_DEF_CONST_REF_PARAM(t, i) const t& i
22
23#define GRD_SEP_NULL(x)
24#define GRD_SEP_COMMA(x) ,
25
26#define GRD_APPLY_FUNC1(f, s, t0, i0) f(t0, i0)
27
28#define GRD_APPLY_FUNC2(f, s, t0, i0, t1, i1)\
29 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
30 GRD_APPLY_FUNC1(f, s, t1, i1)
31
32#define GRD_APPLY_FUNC3(f, s, t0, i0, t1, i1, t2, i2)\
33 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
34 GRD_APPLY_FUNC2(f, s, t1, i1, t2, i2)
35
36#define GRD_APPLY_FUNC4(f, s, t0, i0, t1, i1, t2, i2, t3, i3)\
37 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
38 GRD_APPLY_FUNC3(f, s, t1, i1, t2, i2, t3, i3)
39
40#define GRD_APPLY_FUNC5(f, s, t0, i0, t1, i1, t2, i2, t3, i3, t4, i4)\
41 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
42 GRD_APPLY_FUNC4(f, s, t1, i1, t2, i2, t3, i3, t4, i4)
43
44#define GRD_APPLY_FUNC6(f, s, t0, i0, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5)\
45 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
46 GRD_APPLY_FUNC5(f, s, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5)
47
48#define GRD_APPLY_FUNC7(f, s, t0, i0, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6)\
49 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
50 GRD_APPLY_FUNC6(f, s, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6)
51
52#define GRD_APPLY_FUNC8(f, s, t0, i0, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6, t7, i7)\
53 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
54 GRD_APPLY_FUNC7(f, s, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6, t7, i7)
55
56#define GRD_APPLY_FUNC9(f, s, t0, i0, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6, t7, i7, t8, i8)\
57 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
58 GRD_APPLY_FUNC8(f, s, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6, t7, i7, t8, i8)
59
60#define GRD_APPLY_FUNC10(f, s, t0, i0, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6, t7, i7, t8, i8, t9, i9)\
61 GRD_APPLY_FUNC1(f, s, t0, i0) s(0)\
62 GRD_APPLY_FUNC9(f, s, t1, i1, t2, i2, t3, i3, t4, i4, t5, i5, t6, i6, t7, i7, t8, i8, t9, i9)
63
64#define GRD_APPLY_FUNC_N(n, f, s, ...) GRD_EXPAND( GRD_APPLY_FUNC##n(f, s, __VA_ARGS__) )
65
66#define GRD_GET_FIRST_PAIR_(f, t, i, ...) f(t, i)
67#define GRD_GET_FIRST_PAIR(f, ...) GRD_EXPAND( GRD_GET_FIRST_PAIR_(f, __VA_ARGS__) )
68
69namespace cgv {
70
72namespace g2d {
73class generic_2d_renderer;
74}
75
76namespace render {
77
79class generic_renderer;
80
82 friend class generic_renderer;
83 friend class cgv::g2d::generic_2d_renderer;
84private:
86
87protected:
88 bool state_out_of_date = true;
89 std::vector<unsigned> idx;
90
91 template <typename T>
92 bool set_attribute_array(const context& ctx, const shader_program& prog, const std::string& name, const T& array) {
93 int loc = prog.get_attribute_location(ctx, name);
94 if(loc > -1)
95 return aam.set_attribute_array(ctx, loc, array);
96 return false;
97 }
98
99 void clear_attribute_arrays(context& ctx) {
100 if(aam.is_created()) {
101 aam.destruct(ctx);
102 aam.init(ctx);
103 }
104 }
105
106 bool set_indices(const context& ctx) {
107 return aam.set_indices(ctx, idx);
108 }
109
110 void remove_indices(const context& ctx) {
111 return aam.remove_indices(ctx);
112 }
113
114 virtual bool transfer(context& ctx, shader_program& prog) = 0;
115
116public:
117 void destruct(const context& ctx) {
118 aam.destruct(ctx);
119 }
120
121 bool init(context& ctx) {
122 return aam.init(ctx);
123 }
124
125 void add_idx(const unsigned int i) { idx.push_back(i); }
126
127 std::vector<unsigned>& ref_idx() { return idx; }
128 const std::vector<unsigned>& ref_idx() const { return idx; }
129
130 void set_out_of_date() {
131 state_out_of_date = true;
132 }
133
134 virtual size_t render_count() const = 0;
135
136 bool has_indices() const {
137 return aam.has_index_buffer();
138 }
139
140 bool enable(context& ctx, shader_program& prog) {
141 if(!aam.is_created())
142 aam.init(ctx);
143
144 if(render_count() > 0) {
145 bool res = true;
146 if(state_out_of_date)
147 res = transfer(ctx, prog);
148 if(!res)
149 return false;
150 return aam.enable(ctx);
151 } else if(state_out_of_date) {
152 transfer(ctx, prog);
153 }
154
155 return false;
156 }
157
158 bool disable(context& ctx) {
159 return aam.disable(ctx);
160 }
161};
162
163}
164}
165
233#define DEFINE_GENERIC_RENDER_DATA_CLASS(name, attrib_count, ...)\
234class name : public cgv::render::generic_render_data {\
235protected:\
236 bool transfer(cgv::render::context& ctx, cgv::render::shader_program& prog) {\
237 state_out_of_date = false;\
238 if(render_count() > 0) {\
239 bool success = true;\
240 GRD_APPLY_FUNC_N(attrib_count, GRD_SET_ATTRIB_ARRAY, GRD_SEP_NULL, __VA_ARGS__)\
241 if(idx.size() > 0) set_indices(ctx); \
242 else remove_indices(ctx); \
243 return success; \
244 } else {\
245 clear_attribute_arrays(ctx);\
246 return false;\
247 }\
248 }\
249public:\
250 GRD_APPLY_FUNC_N(attrib_count, GRD_DECL_VEC_MEMBER, GRD_SEP_NULL, __VA_ARGS__)\
251 GRD_APPLY_FUNC_N(attrib_count, GRD_DECL_VEC_MEMBER_CONST_REF, GRD_SEP_NULL, __VA_ARGS__)\
252 size_t render_count() const {\
253 if(idx.empty()) return GRD_GET_FIRST_PAIR(GRD_CALL_SIZE_FUNC, __VA_ARGS__)\
254 else return idx.size();\
255 }\
256 bool empty() const {\
257 return render_count() == 0;\
258 }\
259 void clear() {\
260 idx.clear();\
261 GRD_APPLY_FUNC_N(attrib_count, GRD_CALL_CLEAR_FUNC, GRD_SEP_NULL, __VA_ARGS__)\
262 state_out_of_date = true;\
263 }\
264 void add(GRD_APPLY_FUNC_N(attrib_count, GRD_DEF_CONST_REF_PARAM, GRD_SEP_COMMA, __VA_ARGS__)) {\
265 GRD_APPLY_FUNC_N(attrib_count, GRD_CALL_PUSH_BACK_FUNC, GRD_SEP_NULL, __VA_ARGS__)\
266 }\
267};
268
269/* Define some presets */
270namespace cgv {
271namespace render {
272
275DEFINE_GENERIC_RENDER_DATA_CLASS(generic_render_data_vec3, 1, vec3, position);
276
280DEFINE_GENERIC_RENDER_DATA_CLASS(generic_render_data_vec3_rgb, 2, vec3, position, rgb, color);
281
285DEFINE_GENERIC_RENDER_DATA_CLASS(generic_render_data_vec3_rgba, 2, vec3, position, rgba, color);
286
287} // namespace cgv
288} // namespace g2d
attribute array manager used to upload arrays to gpu
bool has_index_buffer() const
whether aam contains an index buffer
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
a shader program combines several shader code fragments to a complete definition of the shading pipel...
int get_attribute_location(const context &ctx, const std::string &name) const
query location index of an attribute
the cgv namespace
Definition print.h:11