2#include "frame_buffer.h"
4#include <cgv/math/integer.h>
5#include <cgv/media/image/image_reader.h>
6#include <cgv/media/image/image_writer.h>
7#include <cgv/utils/tokenizer.h>
8#include <cgv/utils/file.h>
9#include <cgv/utils/statistics.h>
14using namespace cgv::utils::file;
30 mag_filter = _mag_filter;
31 min_filter = _min_filter;
35 state_out_of_date =
true;
49 std::cerr <<
"could not destruct texture properly" << std::endl;
57 state_out_of_date =
true;
66 state_out_of_date =
true;
74 state_out_of_date =
true;
81 state_out_of_date =
true;
87 state_out_of_date =
true;
93 state_out_of_date =
true;
117 state_out_of_date =
true;
128 min_filter = _min_filter;
129 anisotropy = _anisotropy;
130 state_out_of_date =
true;
146 mag_filter = _mag_filter;
147 state_out_of_date =
true;
157 priority = _priority;
158 state_out_of_date =
true;
168 use_compare_function = _use_compare_function;
169 state_out_of_date =
true;
174 return use_compare_function;
180 compare_function = _compare_function;
181 state_out_of_date =
true;
187 return compare_function;
199 void* tmp = internal_format;
201 internal_format = tmp;
206 if (state_out_of_date) {
207 ctx.texture_set_state(*
this);
208 state_out_of_date =
false;
226 if (!internal_format)
228 return complete_create(ctx, ctx.texture_create(*
this, *
this));
233 nr_multi_samples = _nr_samples;
238 fixed_sample_locations = use;
242 const std::string& file_name,
unsigned char* clear_color_ptr,
int level,
int cube_side)
244 bool ensure_power_of_two = clear_color_ptr != 0;
245 std::string fn = file_name;
252 std::vector<data_format> palette_formats;
253 std::vector<data_view> palettes;
263 for (
unsigned i=0; i<palette_formats.size(); ++i) {
276 if (ensure_power_of_two && (!cgv::math::is_power_of_two(w) || !cgv::math::is_power_of_two(h))) {
277 W = cgv::math::next_power_of_two(df.
get_width());
278 H = cgv::math::next_power_of_two(df.
get_height());
285 const unsigned char* src_ptr = dv.
get_ptr<
unsigned char>();
286 unsigned char* dest_ptr = dv1.
get_ptr<
unsigned char>();
287 unsigned char* dest_ptr_end = dest_ptr + entry_size * W*H;
288 for (
unsigned y = 0; y < h; ++y) {
289 dest_ptr_end -= W * entry_size;
290 memcpy(dest_ptr_end, src_ptr, w*entry_size);
291 if (clear_color_ptr) {
292 for (
size_t x = w; x < W; ++x)
293 memcpy(dest_ptr_end + x * entry_size, clear_color_ptr, entry_size);
296 std::fill(dest_ptr_end + w * entry_size, dest_ptr_end + W*entry_size, 0);
297 src_ptr += w * entry_size;
300 size_t N = (H - h)*W;
301 if (clear_color_ptr) {
302 for (
size_t i = 0; i < N; ++i)
303 memcpy(dest_ptr + i * entry_size, clear_color_ptr, entry_size);
306 std::fill(dest_ptr, dest_ptr + N*entry_size, 0);
308 return create(ctx, dv1, level, cube_side,
false, &palettes);
314 const std::string& file_name,
int* image_width_ptr,
int* image_height_ptr,
315 unsigned char* clear_color_ptr,
int level,
int cube_side)
323 if (image_height_ptr)
330 if (std::find(file_names.begin(), file_names.end(),
'{') != file_names.end()) {
331 std::vector<token> toks;
332 deduced_names.resize(6);
335 for (
unsigned i=0; i<toks.size(); ++i) {
336 if (toks[i] ==
"{") {
337 if (selection != -1) {
338 std::cerr <<
"warning: nested {} not allowed in cubemap file names specification " << file_names << std::endl;
343 else if (toks[i] ==
"}") {
344 if (selection == -1) {
345 std::cerr <<
"warning: } without opening { in cubemap file names specification " << file_names << std::endl;
348 if (selection != 5) {
349 std::cerr <<
"warning: no 6 file names specified for creating cubemap from images " << file_names << std::endl;
354 else if (toks[i] ==
",") {
355 if (selection == -1) {
356 std::cerr <<
"warning: , arising outside {} in cubemap file names specification " << file_names << std::endl;
362 if (selection == -1) {
363 for (
unsigned j=0; j<6; ++j)
367 if (selection == 6) {
368 std::cerr <<
"warning: more than 6 files names specified for cubemap creation " << file_names << std::endl;
371 deduced_names[selection] +=
to_string(toks[i]);
377 std::string path_prefix = get_path(file_names);
378 if (!path_prefix.empty())
380 void* handle = find_first(file_names);
381 while (handle != 0) {
382 deduced_names.push_back(path_prefix+find_name(handle));
383 handle = find_next(handle);
385 if (deduced_names.size() != 6) {
386 std::cerr <<
"warning: not exactly 6 files names specified for cubemap creation " << file_names << std::endl;
395 std::vector<std::string> deduced_names;
399 for (
unsigned i=0; success && i<6; ++i)
402 std::cerr <<
"could not create cubemap side " << i <<
" from image " << deduced_names[i] << std::endl;
410bool texture::write_to_file(
context& ctx,
const std::string& file_name,
unsigned int z_or_cube_size,
float depth_map_gamma,
const std::string& options)
const
414 last_error =
"texture must be created for write_to_file";
419 last_error =
"could not create frame buffer object for write_to_file";
422 if (z_or_cube_size != -1) {
423 if (!fb.
attach(ctx,*
this,z_or_cube_size,0,0)) {
424 last_error =
"could not attach texture for write_to_file";
429 if (!fb.
attach(ctx,*
this)) {
430 last_error =
"could not attach texture for write_to_file";
440 if (!fb.
attach(ctx, rb)) {
441 last_error =
"could not attach color buffer for write_to_file";
446 last_error =
"frame buffer object not complete for write_to_file due to\n";
460 for (i = 0; i < n; ++i)
466 for (i = 0; i < n; ++i) {
496 return ctx.texture_create_mipmaps(*
this, *
this);
506bool texture::complete_create(
const context& ctx,
bool created)
508 state_out_of_date =
true;
532 if (level == -1 && !internal_format)
534 return complete_create(ctx, ctx.texture_create_from_buffer(*
this, *
this, x, y, level));
557 }
else if(num_array_layers != 0) {
558 if(num_array_layers < 0) {
567 case TT_1D: tt = TT_1D_ARRAY;
break;
568 case TT_2D: tt = TT_2D_ARRAY;
break;
569 case TT_3D: tt = TT_2D_ARRAY;
break;
574 if ((tt == TT_1D || tt == TT_2D || tt == TT_3D) &&
is_created()) {
575 bool replace_allowed = tt == this->tt;
578 replace_allowed =
false;
579 if (replace_allowed && level < 1) {
581 case TT_1D :
return replace(ctx, 0, data, level, palettes);
582 case TT_2D :
return replace(ctx, 0, 0, data, level, palettes);
583 case TT_3D :
return replace(ctx, 0, 0, 0, data, level, palettes);
595 if (level == -1 || !internal_format)
598 return complete_create(ctx, ctx.texture_create(*
this, *
this, data, level, cube_side, num_array_layers, palettes));
614 return ctx.texture_replace(*
this,x,-1,-1,data,level, palettes);
630 return ctx.texture_replace(*
this,x,y,-1,data,level, palettes);
643 bool res = ctx.texture_replace(*
this,x,y,z_or_cube_side,data,level, palettes);
644 if (res && level == -1 && !have_mipmaps)
651 int y_buffer,
int width,
int height,
int level)
661 return ctx.texture_replace_from_buffer(*
this,x,y,-1,x_buffer,y_buffer,width,height,level);
666 int y_buffer,
int width,
int height,
int level)
672 if (tt != TT_3D && tt != TT_CUBEMAP) {
676 return ctx.texture_replace_from_buffer(*
this,x,y,z_or_cube_side,x_buffer,y_buffer,width,height,level);
690 const std::string& file_name,
int x,
int y,
int z_or_cube_side,
693 std::string fn = file_name;
697 std::vector<data_format> palette_formats;
701 std::vector<data_view> palettes;
702 for (
unsigned i=0; i<palette_formats.size(); ++i) {
707 replace(ctx, x, y, z_or_cube_side, dv, level, &palettes);
722 return ctx.texture_copy_back(*
this, level, dv);
728 state_out_of_date =
true;
731 return ctx.texture_destruct(*
this);
748 tex_unit = _tex_unit;
764 tex_unit = _tex_unit;
767 return ctx.texture_bind_as_image(*
this, tex_unit, level, bind_array, layer, access);
778 return user_data != 0;
void multi_set(const std::string &property_assignments, bool report_error=true)
set several properties
complete implementation of method actions that only call one method when entering a node
The const_data_view has the functionality of the data_view but uses a const pointer and therefore doe...
void manage_format(bool enable=true)
whether to manage the data format pointer
const data_format * get_format() const
return the component format
cgv::type::func::transfer_const< P, S * >::type get_ptr() const
return a data pointer to type S
the data view gives access to a data array of one, two, three or four dimensions.
base class for all drawables, which is independent of the used rendering API.
virtual bool read_frame_buffer(data::data_view &dv, unsigned int x=0, unsigned int y=0, FrameBufferType buffer_type=FB_BACK, cgv::type::info::TypeId type=cgv::type::info::TI_UINT8, data::ComponentFormat cf=data::CF_RGB, int w=-1, int h=-1)=0
read the current frame buffer or a rectangular region of it into the given data view.
virtual bool make_current() const =0
make the current context current if possible
this class encapsulate frame buffers that live on the GPU and can be used as destination for the rend...
bool create(const context &ctx, int _width=-1, int _height=-1)
create framebuffer if extension is supported, otherwise return false.
bool is_complete(const context &ctx) const
check for completeness, if not complete, get the reason in last_error
bool attach(const context &ctx, const render_buffer &rb, int i=0)
attach render buffer to depth buffer if it is a depth buffer, to stencil if it is a stencil buffer or...
bool enable(context &ctx, int i0=-1, int i1=-1, int i2=-1, int i3=-1, int i4=-1, int i5=-1, int i6=-1, int i7=-1, int i8=-1, int i9=-1, int i10=-1, int i11=-1, int i12=-1, int i13=-1, int i14=-1, int i15=-1)
enable the framebuffer either with all color attachments if no arguments are given or if arguments ar...
bool disable(context &ctx)
disable the framebuffer object
std::string last_error
a string that contains the last error, which is only set by the init method
this class encapsulate render buffers that live on the GPU which must support frame buffer objects fo...
bool create(const context &ctx, int width=-1, int height=-1)
create a render buffer.
virtual bool is_created() const
return whether component has been created
const context * ctx_ptr
keep pointer to my context
std::string last_error
a string that contains the last error
base interface for a texture
bool generate_mipmaps(const context &ctx)
generate mipmaps automatically, only supported if framebuffer objects are supported by the GPU
void set_mag_filter(TextureFilter _mag_filter)
set the magnification filter
TextureWrap get_wrap_s() const
return the texture wrap behaviour in s direction
TextureFilter get_mag_filter() const
return the magnification filter
bool set_data_format(const std::string &description)
change the data format and clear internal format
TextureFilter get_min_filter() const
return the minification filter
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.
~texture()
destruct texture, the destructor can be called without context if the destruct method has been called...
void set_compare_function(CompareFunction compare_function)
set the texture compare function
int get_tex_unit() const
return the currently used texture unit and -1 for current
bool create_from_image(const context &ctx, const std::string &file_name="", int *image_width_ptr=0, int *image_height_ptr=0, unsigned char *clear_color_ptr=0, int level=-1, int cube_side=-1)
create the texture from an image file.
TextureWrap get_wrap_r() const
return the texture wrap behaviour in r direction
bool create_from_buffer(const context &ctx, int x, int y, int width, int height, int level=-1)
create texture from the currently set read buffer, where x, y, width and height define the to be used...
static bool deduce_file_names(const std::string &file_names, std::vector< std::string > &deduced_names)
Helper function that determins the individual file names for a cubemap.
bool replace_from_buffer(const context &ctx, int x, int y, int x_buffer, int y_buffer, int width, int height, int level=-1)
replace a block within a 2d texture from the current read buffer.
void set_priority(float _priority)
set priority with which texture is kept in GPU memory
float get_priority() const
return the priority with which texture is kept in GPU memory
void set_fixed_sample_locations(bool use)
set whether multi sampling uses fixed sample locations
texture(const std::string &description="uint8[R,G,B,A]", TextureFilter _mag_filter=TF_LINEAR, TextureFilter _min_filter=TF_LINEAR, TextureWrap _wrap_s=TW_CLAMP_TO_EDGE, TextureWrap _wrap_t=TW_CLAMP_TO_EDGE, TextureWrap _wrap_r=TW_CLAMP_TO_EDGE)
construct from description string (which defaults to rgba format) and most commonly used texture para...
bool replace_from_image(const context &ctx, const std::string &file_name, int x, int y, int z_or_cube_side, int level)
replace within a slice of a volume or a side of a cube map from the given image
void set_nr_multi_samples(unsigned _nr_samples)
set the number of multi samples for textures of type TT_MULTISAMPLE_2D and TT_MULTISAMPLE_2D_ARRAY
bool disable(const context &ctx)
disable texture and restore state from before last enable call
void ensure_state(const context &ctx) const
ensure the the texture state is synchronized with the GPU settings
bool enable(const context &ctx, int tex_unit=-1)
enable this texture in the given texture unit, -1 corresponds to the current unit.
void set_wrap_t(TextureWrap _wrap_t)
set the texture wrap behaviour in t direction
bool destruct(const context &ctx)
destruct the texture and free texture memory and handle
std::string last_error
a string that contains the last error
CompareFunction get_compare_function() const
get the texture compare function
bool get_compare_mode() const
get the texture compare mode and function
bool write_to_file(context &ctx, const std::string &file_name, unsigned int z_or_cube_side=-1, float depth_map_gamma=1.0f, const std::string &options="") const
write the content of the texture to a file. This method needs support for frame buffer objects.
bool mipmaps_created() const
check whether mipmaps have been created
void set_wrap_s(TextureWrap _wrap_s)
set the texture wrap behaviour in s direction
bool replace(const context &ctx, int x, const cgv::data::const_data_view &data, int level=-1, const std::vector< cgv::data::data_view > *palettes=0)
replace a block within a 1d texture with the given data.
cgv::vec4 get_border_color() const
return the texture border color
void find_best_format(const context &ctx, const std::vector< cgv::data::data_view > *palettes=0)
find the format that matches the one specified in the component format best
void set_component_format(const component_format &cf)
change component format and clear internal format
void set_border_color(const float *rgba)
set the border color
bool is_enabled() const
check whether textue is enabled
TextureWrap get_wrap_t() const
return the texture wrap behaviour in t direction
bool create_mipmaps(const context &ctx)
create storage for mipmaps without computing the mipmap contents
bool copy(const context &ctx, cgv::data::data_view &dv, int level=-1) const
read back a texture level to CPU memory storing the content in a data view whose data format will mat...
void set_wrap_r(TextureWrap _wrap_r)
set the texture wrap behaviour in r direction
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...
void set_compare_mode(bool use_compare_function)
set the texture compare mode and function
bool create_from_images(const context &ctx, const std::string &file_names, int level=-1)
Create a cube map from six files specified in the file_names parameter.
bool bind_as_image(const context &ctx, int tex_unit, int level=0, bool bind_array=false, int layer=0, AccessType access=AT_WRITE_ONLY)
Binds this texture as an image texture to the given texture unit.
float get_anisotropy() const
return the currently set anisotropy
incrementally accumulate statistical information
void update(const double &v)
consider another value
double get_max() const
get the maximum of the considered variables
double get_min() const
get the minimum of the considered variables
the tokenizer allows to split text into tokens in a convenient way.
tokenizer & set_sep(const std::string &sep, bool merge)
set the list of separators and specify whether succeeding separators are merged into single tokens
tokenizer & set_ws(const std::string &ws)
set the list of white spaces, that separate tokens and are skipped
namespace for data management components
@ CF_RGB
color format with two components R and G
@ CF_D
color format with components B, G, R and A
AccessType
different access types
TextureFilter
different texture filter
TextureWrap
different texture wrap modes
TextureType
different texture types
CompareFunction
different comparison functions used for depth testing or texture comparisons
@ TI_UINT8
signed integer stored in 64 bits
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
namespace that holds tools that dont fit any other namespace
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
this header is dependency free
cgv::math::fvec< float, 4 > vec4
declare type of 4d single precision floating point vectors (used for homogeneous coordinates)