3 #include "texturestate.h"
7 TexImageState::TexImageState():
14 void TexImageState::set_1d(GLenum ifmt, unsigned wd)
16 set_3d(ifmt, wd, 0, 0);
19 void TexImageState::set_2d(GLenum ifmt, unsigned wd, unsigned ht)
21 set_3d(ifmt, wd, ht, 0);
24 void TexImageState::set_3d(GLenum ifmt, unsigned wd, unsigned ht, unsigned dp)
29 internal_format = ifmt;
32 string TexImageState::describe() const
34 string descr = strformat("%d", width);
37 descr += strformat("x%d", height);
39 descr += strformat("x%d", height);
41 descr += strformat(", %s", describe_enum(internal_format, "PixelFormat"));
46 GLenum texture_cube_faces[6] =
48 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
49 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
50 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
51 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
52 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
53 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
56 TextureState::TextureState():
59 min_filter(GL_LINEAR_MIPMAP_LINEAR),
60 mag_filter(GL_LINEAR),
64 compare_mode(GL_NONE),
65 compare_func(GL_LEQUAL),
66 generate_mipmap(false)
69 TexImageState &TextureState::get_image(unsigned level)
71 if(images.size()<=level)
72 images.resize(level+1);
76 void TextureState::set_image_1d(unsigned level, GLenum ifmt, unsigned wd)
78 get_image(level).set_1d(ifmt, wd);
79 if(level==0 && generate_mipmap)
83 void TextureState::set_image_2d(unsigned level, GLenum ifmt, unsigned wd, unsigned ht)
85 get_image(level).set_2d(ifmt, wd, ht);
86 if(level==0 && generate_mipmap)
90 void TextureState::set_face_image_2d(GLenum face, unsigned level, GLenum ifmt, unsigned wd, unsigned ht)
93 for(; (index<6 && texture_cube_faces[index]!=face); ++index) ;
97 set_image_2d(level*6+index, ifmt, wd, ht);
100 void TextureState::set_image_3d(unsigned level, GLenum ifmt, unsigned wd, unsigned ht, unsigned dp)
102 get_image(level).set_3d(ifmt, wd, ht, dp);
103 if(level==0 && generate_mipmap)
107 void TextureState::create_mipmaps(unsigned face)
109 unsigned stride = (target==GL_TEXTURE_CUBE_MAP ? 6 : 1);
113 unsigned wd = images[face].width;
114 unsigned ht = images[face].height;
115 unsigned dp = images[face].depth;
116 for(unsigned i=1;; ++i)
118 if(wd<=1 && ht<=1 && dp<=1)
128 get_image(i*stride+face).set_3d(images[0].internal_format, wd, ht, dp);
132 void TextureState::set_parameter(GLenum pname, const int *values)
134 if(pname==GL_TEXTURE_MIN_FILTER)
135 min_filter = values[0];
136 else if(pname==GL_TEXTURE_MAG_FILTER)
137 mag_filter = values[0];
138 else if(pname==GL_TEXTURE_WRAP_S)
140 else if(pname==GL_TEXTURE_WRAP_T)
142 else if(pname==GL_TEXTURE_WRAP_R)
144 else if(pname==GL_TEXTURE_COMPARE_MODE)
145 compare_mode = values[0];
146 else if(pname==GL_TEXTURE_COMPARE_FUNC)
147 compare_func = values[0];
148 else if(pname==GL_GENERATE_MIPMAP)
149 generate_mipmap = values[0];
152 string TextureState::describe() const
154 string descr = describe_enum(target, "TextureTarget");
156 descr += ", undefined";
157 else if(target==GL_TEXTURE_CUBE_MAP)
159 bool homogeneous = true;
160 for(unsigned i=1; (homogeneous && i<6); ++i)
161 homogeneous = (images[i].width==images[0].width && images[i].height==images[0].height &&
162 images[i].internal_format==images[0].internal_format);
164 descr += ", "+images.front().describe();
166 descr += ", heterogeneous faces";
169 descr += ", "+images.front().describe();
174 TexUnitState::Binding *TexUnitState::get_binding(GLenum target)
176 if(target==GL_TEXTURE_1D)
178 else if(target==GL_TEXTURE_2D)
180 else if(target==GL_TEXTURE_3D)
182 else if(target==GL_TEXTURE_CUBE_MAP)
183 return &binding_cube;
188 const TexUnitState::Binding *TexUnitState::get_binding(GLenum target) const
190 return const_cast<TexUnitState *>(this)->get_binding(target);
193 void TexUnitState::set_current_texture(GLenum target, TextureState *tex)
195 if(Binding *binding = get_binding(target))
196 binding->current = tex;
199 TextureState *TexUnitState::get_current_texture(GLenum target)
201 if(Binding *binding = get_binding(target))
202 return binding->current;
207 const TextureState *TexUnitState::get_current_texture(GLenum target) const
209 return const_cast<TexUnitState *>(this)->get_current_texture(target);
212 bool TexUnitState::is_enabled(GLenum target) const
214 if(const Binding *binding = get_binding(target))
215 return binding->enabled;
220 string TexUnitState::describe_binding(GLenum target) const
222 if(const Binding *binding = get_binding(target))
223 return binding->describe();
229 TexUnitState::Binding::Binding():
234 string TexUnitState::Binding::describe() const
238 string descr = strformat("%d ", current->id);
239 if(current->images.empty())
240 descr += "(undefined)";
242 descr += "("+current->images.front().describe()+")";
244 descr += ", enabled";
248 return "None, enabled";