cgv
Loading...
Searching...
No Matches
color_scale_adapter.cxx
1#include "color_scale_adapter.h"
2
3namespace cgv {
4namespace render {
5
7 return uniform_buffer_.create(ctx);
8}
9
11 uniform_buffer_.destruct(ctx);
12 return texture_.destruct(ctx);
13}
14
15bool color_scale_adapter::enable(const context& ctx, int texture_unit) {
16 create_texture(ctx);
17
18 // Todo: Allow to set uniform block binding
19 // Todo: Update uniform buffer here if color scales changed.
20
21 //_uniform_buffer.replace(ctx, uniforms);
22 uniform_buffer_.bind(ctx, 0);
23
24 return texture_.enable(ctx, texture_unit);
25}
26
28 uniform_buffer_.unbind(ctx, 0);
29 return texture_.disable(ctx);
30}
31
33 bool was_enabled = prog.is_enabled();
34 if(!was_enabled)
35 prog.enable(ctx);
36 prog.set_uniform(ctx, "color_scale_texture", texture_unit);
37 prog.set_uniform_block_binding(ctx, "color_scale_argument_block", 0);
38
39 for(size_t i = 0; i < color_scales_.size(); ++i)
40 uniforms_[i] = color_scales_[i]->get_arguments();
41
42 uniform_buffer_.resize_or_replace(ctx, uniforms_);
43
44 if(!was_enabled)
45 prog.disable(ctx);
46}
47
48void color_scale_adapter::set_color_scale(std::shared_ptr<const device_color_scale> color_scale) {
49 if(!color_scales_.size() == 1 || color_scales_.front() != color_scale) {
50 auto g = cgv::data::time_point::min();
51 build_time_.reset();
52 color_scales_ = { color_scale };
53 uniforms_ = std::vector<device_color_scale_arguments>(1);
54 }
55}
56
57void color_scale_adapter::set_color_scales(const std::vector<std::shared_ptr<const device_color_scale>>& color_scales) {
58 if(color_scales_ != color_scales) {
59 build_time_.reset();
60 color_scales_ = color_scales;
61 if(color_scales_.size() > k_max_color_scale_count_)
62 color_scales_.resize(k_max_color_scale_count_);
63 uniforms_ = std::vector<device_color_scale_arguments>(color_scales_.size());
64 }
65}
66
68 create_texture(ctx);
69 return texture_;
70}
71
72cgv::data::time_point color_scale_adapter::get_latest_color_scale_modification_time() const {
73 cgv::data::time_point time;
74 for(const auto& color_scale : color_scales_)
75 time = std::max(time, color_scale->get_modified_time());
76 return time;
77}
78
79bool color_scale_adapter::create_texture(const context& ctx) {
80 if(color_scales_.empty())
81 return false;
82
83 if(!build_time_.is_valid() || get_latest_color_scale_modification_time() > build_time_.get_modified_time()) {
84 std::vector<cgv::rgba8> texture_data;
85
86 for(const auto& color_scale : color_scales_) {
87 size_t offset = texture_data.size();
88 texture_data.resize(texture_data.size() + k_texture_width_);
89 std::vector<rgba> colors = color_scale->get_texture_data(k_texture_width_);
90 std::transform(colors.begin(), colors.end(), texture_data.begin() + offset, [](const cgv::rgba& color) {
91 return cgv::rgba8(color);
92 });
93 }
94
95 cgv::data::data_format data_format(k_texture_width_, color_scales_.size(), cgv::type::info::TI_UINT8, cgv::data::CF_RGBA);
96 cgv::data::data_view data_view(&data_format, texture_data.data());
97
98 const cgv::render::TextureFilter filter = cgv::render::TF_LINEAR;
99 texture_.set_min_filter(filter);
100 texture_.set_mag_filter(filter);
101
102 build_time_.modified();
103 return texture_.create(ctx, data_view, 0);
104 }
105
106 return true;
107}
108
109} // namespace render
110} // namespace cgv
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
void set_color_scales(const std::vector< std::shared_ptr< const device_color_scale > > &color_scales)
Set a reference to multiple device_color_scales to be managed by this color_scale_adapter.
bool destruct(const context &ctx)
Destruct the color_scale_adapter.
bool disable(const context &ctx)
Disable the managed render resources after rendering.
bool enable(const context &ctx, int texture_unit)
Enable the managed render resources to prepare for rendering.
texture & get_texture(const context &ctx)
Get the texture containing the device_color_scales' color data.
void set_uniforms_in_program(context &ctx, shader_program &prog, int texture_unit)
Set texture unit and uniform buffer location in the given shader_program and prepare uniform buffer.
bool init(const context &ctx)
Initialize the color_scale_adapter.
void set_color_scale(std::shared_ptr< const device_color_scale > color_scale)
Set a reference to a single device_color_scale to be managed by this color_scale_adapter.
base class for all drawables, which is independent of the used rendering API.
Definition context.h:668
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool enable(context &ctx)
enable the shader program
bool disable(context &ctx)
disable shader program and restore fixed functionality
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
bool is_enabled() const
check whether program is currently enabled
the texture class encapsulates all functionality independent of the rendering api.
Definition texture.h:15
void set_mag_filter(TextureFilter _mag_filter)
set the magnification filter
Definition texture.cxx:143
bool create(const context &ctx, TextureType _tt=TT_UNDEF, unsigned width=-1, unsigned height=-1, unsigned depth=-1)
create the texture of dimension and resolution specified in the data format base class.
Definition texture.cxx:213
bool disable(const context &ctx)
disable texture and restore state from before last enable call
Definition texture.cxx:774
bool enable(const context &ctx, int tex_unit=-1)
enable this texture in the given texture unit, -1 corresponds to the current unit.
Definition texture.cxx:763
bool destruct(const context &ctx)
destruct the texture and free texture memory and handle
Definition texture.cxx:748
void set_min_filter(TextureFilter _min_filter, float _anisotropy=2.0f)
set the minification filters, if minification is set to TF_ANISOTROP, the second floating point param...
Definition texture.cxx:125
@ CF_RGBA
color format with components R, G and B
TextureFilter
different texture filter
Definition context.h:245
@ TI_UINT8
signed integer stored in 64 bits
Definition type_id.h:23
this header is dependency free
Definition print.h:11