cgv
Loading...
Searching...
No Matches
textured_material.cxx
1#include <cgv/base/base.h>
2#include "textured_material.h"
3
4namespace cgv {
5 namespace render {
6
9{
10 ctx_ptr = 0;
11 alpha_test_func = AT_GREATER;
12 alpha_threshold = 0.0f;
13
14}
15
18 : media::illum::textured_surface_material(mtl)
19{
20 alpha_test_func = AT_GREATER;
21 alpha_threshold = 0.0f;
22 ctx_ptr = 0;
23}
24
27{
28 if (ctx_ptr && ctx_ptr->make_current())
29 destruct_textures(*ctx_ptr);
30}
31
34{
35 while (textures.size() < image_file_names.size())
36 textures.push_back(0);
37 textures.push_back(&tex);
38 return (int)textures.size() - 1;
39}
40
43{
44 bool success = true;
45 while (textures.size() < image_file_names.size())
46 textures.push_back(0);
47
48 for (size_t ti = 0; ti < image_file_names.size(); ++ti) {
49 texture*& T = textures[ti];
50 if (!T) {
51 T = new texture();
52 if (!T->create_from_image(ctx, image_file_names[ti])) {
53 std::cerr << "could not create texture from file '" << image_file_names[ti] << "': " << T->get_last_error() << std::endl;
54 delete T;
55 T = 0;
56 success = false;
57 }
58 else {
59 T->set_wrap_s(TW_REPEAT);
60 T->set_wrap_t(TW_REPEAT);
61 }
62 }
63 }
64 return success;
65}
66
68void textured_material::set_alpha_test(AlphaTestFunc _alpha_test_func, float _alpha_threshold)
69{
70 alpha_test_func = _alpha_test_func;
71 alpha_threshold = _alpha_threshold;
72}
73
74
80
81
84{
85 return alpha_threshold;
86}
87
95{
96 return alpha_threshold;
97}
98
99void destruct_texture(context& ctx, texture*& T)
100{
101 if (!T)
102 return;
103 T->destruct(ctx);
104 delete T;
105 T = 0;
106}
107
110{
111 for (size_t ti = 0; ti < textures.size(); ++ti) {
112 if (textures[ti]) {
113 textures[ti]->destruct(ctx);
114 delete textures[ti];
115 textures[ti] = 0;
116 }
117 }
118 textures.clear();
119}
120
123{
124 for (unsigned ti = 0; ti < textures.size(); ++ti) {
125 if (textures[ti])
126 textures[ti]->enable(ctx, ti);
127 }
128}
129
132{
133 for (unsigned ti = 0; ti < textures.size(); ++ti) {
134 if (textures[ti])
135 textures[ti]->disable(ctx);
136 }
137}
138
141{
142 assert(ti < (int)textures.size());
143 return textures[ti];
144}
145
146 }
147}
148
static const std::string & get_last_error()
returns an error string after parsing of description string has failed
simple class to hold the material properties of a phong material
std::vector< std::string > image_file_names
vector of image file names
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
virtual bool make_current() const =0
make the current context current if possible
the texture class encapsulates all functionality independent of the rendering api.
Definition texture.h:15
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.
Definition texture.cxx:330
void set_wrap_t(TextureWrap _wrap_t)
set the texture wrap behaviour in t direction
Definition texture.cxx:83
bool destruct(const context &ctx)
destruct the texture and free texture memory and handle
Definition texture.cxx:730
void set_wrap_s(TextureWrap _wrap_s)
set the texture wrap behaviour in s direction
Definition texture.cxx:77
AlphaTestFunc & ref_alpha_test_func()
return reference to currently set alpha test function
texture * get_texture(int texture_index) const
return pointer to ambient texture or 0 if non created
void disable_textures(context &ctx)
disable material textures
virtual ~textured_material()
ensure that textures are destructed
AlphaTestFunc
different test functions for alpha test
float get_alpha_threshold() const
return the currently used alpha threshold used by the comparison alpha test functions
float & ref_alpha_threshold()
return reference to currently used alpha threshold used by the comparison alpha test functions
void set_alpha_test(AlphaTestFunc _alpha_test_func=AT_GREATER, float _alpha_threshold=0.0f)
configure the alpha test that is performed in case alpha values are given in the textures
void destruct_textures(context &ctx)
destruct textures
AlphaTestFunc get_alpha_test_func() const
return the currently set alpha test function
int add_texture_reference(cgv::render::texture &tex)
add a reference to a new texture that is managed outside of this class and return its index
textured_material()
initialize textures
void enable_textures(context &ctx)
enable all textures with their indices as texture unit
bool ensure_textures(context &ctx)
call this to ensure that the textures specified by image files are loaded - typically done in the ini...
the cgv namespace
Definition print.h:11