cgv
Loading...
Searching...
No Matches
render_data_base.h
1#pragma once
2
3#include <cgv/data/optional.h>
4
5#include "renderer.h"
6
8#define CGV_RDB_TRANSFER_ARRAY(NAME, DATA) \
9if(DATA.size() == super::size()) \
10 r.set_##NAME##_array(ctx, DATA); \
11else if(DATA.empty()) \
12 r.remove_##NAME##_array(ctx);
13
14namespace cgv {
15namespace render {
16
65template <class RendererType, class RenderStyleType, typename ColorType = rgb>
67private:
69 bool state_out_of_date = true;
72
73protected:
79 virtual RendererType& ref_renderer_singleton(context& ctx, int ref_count_change = 0) = 0;
80
85 virtual bool transfer(context& ctx, RendererType& r) {
86 state_out_of_date = false;
87
88 // The positions array determines the amount of stored vertices.
89 // All other arrays except indices must match its size.
90 if(!positions.empty()) {
91 r.set_position_array(ctx, positions);
92 if(colors.size() == size())
93 r.set_color_array(ctx, colors);
94 else if(colors.empty())
95 r.remove_color_array(ctx);
96
97 //if(colors.size() == size())
98 // r.set_color_array(ctx, colors);
99 if(!indices.empty())
100 r.set_indices(ctx, indices);
101 else
102 r.remove_indices(ctx);
103 return true;
104 } else {
105 // If no positions are set the render data is assumed to be empty. In this case the
106 // attribute array manager is destructed and reinitialized to clear the GPU buffers.
107 if(attribute_array.is_created()) {
108 attribute_array.destruct(ctx);
109 attribute_array.init(ctx);
110 }
111 // Return false since the attribute array manager holds no data anymore.
112 return false;
113 }
114 }
115
124 virtual void set_const_attributes(context& ctx, RendererType& r) {
125 if(colors.empty() && const_color)
126 r.set_color(ctx, const_color.value());
127 }
128
137 template<typename T>
138 void fill(std::vector<T>& vector, const T& value) {
139 if(vector.size() < size())
140 vector.resize(size(), value);
141 }
142
143public:
145 RenderStyleType style;
146
148 std::vector<uint32_t> indices;
150 std::vector<vec3> positions;
152 std::vector<ColorType> colors;
155
158 size_t size() {
159 return positions.size();
160 }
161
167 bool empty() const {
168 return positions.empty();
169 }
170
172 void clear() {
173 indices.clear();
174 positions.clear();
175 colors.clear();
176
177 state_out_of_date = true;
178 }
179
187 state_out_of_date = true;
188 }
189
196 size_t render_count() {
197 if(indices.empty())
198 return positions.size();
199 else
200 return indices.size();
201 }
202
211
214 bool init(context& ctx) {
216 return attribute_array.init(ctx);
217 }
218
221 void destruct(context& ctx) {
222 ref_renderer_singleton(ctx, -1);
223 attribute_array.destruct(ctx);
224 }
225
235 void early_transfer(context& ctx, RendererType& r) {
236 r.enable_attribute_array_manager(ctx, this->attribute_array);
237 if(this->state_out_of_date)
238 transfer(ctx, r);
239 r.disable_attribute_array_manager(ctx, this->attribute_array);
240 }
241
251 bool enable(context& ctx, RendererType& r, const RenderStyleType& s) {
252 if(this->size() > 0) {
253 r.set_render_style(s);
254 r.enable_attribute_array_manager(ctx, this->attribute_array);
255
256 if(this->state_out_of_date)
257 transfer(ctx, r);
258 this->set_const_attributes(ctx, r);
259
260 return r.validate_and_enable(ctx);
261 } else if(this->state_out_of_date) {
262 early_transfer(ctx, r);
263 }
264 return false;
265 }
266
271 bool disable(context& ctx, RendererType& r) {
272 bool res = r.disable(ctx);
273 r.disable_attribute_array_manager(ctx, this->attribute_array);
274 return res;
275 }
276
291 void draw(context& ctx, renderer& r, unsigned offset = 0, int count = -1) {
292 size_t draw_count = render_count();
293 offset = std::min(offset, static_cast<unsigned>(draw_count));
294 draw_count = std::min(offset + (count < 0 ? draw_count : count), draw_count) - offset;
295
296 r.draw(ctx, offset, draw_count);
297 }
298
306 void render(context& ctx, unsigned offset = 0, int count = -1) {
307 render(ctx, ref_renderer_singleton(ctx), style, offset, count);
308 }
309
318 void render(context& ctx, const RenderStyleType& s, unsigned offset = 0, int count = -1) {
319 render(ctx, ref_renderer_singleton(ctx), s, offset, count);
320 }
321
330 void render(context& ctx, RendererType& r, unsigned offset = 0, int count = -1) {
331 render(ctx, r, style, offset, count);
332 }
333
343 void render(context& ctx, RendererType& r, const RenderStyleType& s, unsigned offset = 0, int count = -1) {
344 if(enable(ctx, r, s)) {
345 this->draw(ctx, r, offset, count);
346 this->disable(ctx, r);
347 }
348 }
349
350 void add_index(const uint32_t index) {
351 indices.push_back(index);
352 }
353
354 void add_position(const vec3& position) {
355 positions.push_back(position);
356 }
357
358 void add_color(const ColorType& color) {
359 colors.push_back(color);
360 }
361
362 void add(const vec3& position, const ColorType& color) {
363 add_position(position);
364 add_color(color);
365 }
366
367 void fill_colors(const ColorType& color) {
368 fill(colors, color);
369 }
370};
371
372}
373}
A simple and naiive implementation of an optional value.
Definition optional.h:22
attribute array manager used to upload arrays to gpu
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
A base class for storing render data and style usable with the default renderers provided in the cgv:...
const attribute_array_manager & ref_attribute_array_manager() const
Constant access to the private attribute_array_manager.
bool disable(context &ctx, RendererType &r)
Disable the renderer and attribute_array.
void render(context &ctx, const RenderStyleType &s, unsigned offset=0, int count=-1)
Render the stored geometry using the given style.
void early_transfer(context &ctx, RendererType &r)
Perform a transfer of the stored data to the attribute_array right now.
cgv::data::optional< ColorType > const_color
optional constant color used for all elements
size_t render_count()
Return the number of vertices that will be rendered.
void render(context &ctx, unsigned offset=0, int count=-1)
Render the stored geometry.
size_t size()
Return the number of stored positions.
std::vector< ColorType > colors
array of colors
virtual bool transfer(context &ctx, RendererType &r)
Transfers the data stored in members to the attribute array.
std::vector< vec3 > positions
array of positions
std::vector< uint32_t > indices
array of indices used for optional indexed rendering
bool init(context &ctx)
Initialize the attribute array manager.
void render(context &ctx, RendererType &r, unsigned offset=0, int count=-1)
Render the stored geometry using the given renderer.
void destruct(context &ctx)
Destruct the attribute array manager and decrease the reference count of the used renderer.
void render(context &ctx, RendererType &r, const RenderStyleType &s, unsigned offset=0, int count=-1)
Render the stored geometry using the given renderer and style.
void clear()
Clear the stored data and set state out of date.
bool empty() const
Return whether this render data is empty.
void draw(context &ctx, renderer &r, unsigned offset=0, int count=-1)
Draw the stored geometry using the given renderer.
void fill(std::vector< T > &vector, const T &value)
Template for filling a member array to the size of the render data.
RenderStyleType style
the default render style
bool enable(context &ctx, RendererType &r, const RenderStyleType &s)
Enable the render data for rendering.
virtual void set_const_attributes(context &ctx, RendererType &r)
Set constant vertex attributes if present.
void set_out_of_date()
Notify the render data about state changes.
virtual RendererType & ref_renderer_singleton(context &ctx, int ref_count_change=0)=0
Manage the singleton of the used renderer.
abstract base class for all renderers that handles a shader program and position / color attribute
Definition renderer.h:22
virtual void draw(context &ctx, size_t start, size_t count, bool use_strips=false, bool use_adjacency=false, uint32_t strip_restart_index=-1)
Draw a range of vertices or indexed elements.
Definition renderer.cxx:337
the cgv namespace
Definition print.h:11
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:669