]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/texturestate.cpp
Replace per-file license notices with License.txt
[gldbg.git] / flavors / gl / source / texturestate.cpp
1 #include "enums.h"
2 #include "strformat.h"
3 #include "texturestate.h"
4
5 using namespace std;
6
7 TexImageState::TexImageState():
8         width(0),
9         height(0),
10         depth(0),
11         internal_format(0)
12 { }
13
14 void TexImageState::set_2d(GLenum ifmt, unsigned wd, unsigned ht)
15 {
16         width = wd;
17         height = ht;
18         depth = 0;
19         internal_format = ifmt;
20 }
21
22 string TexImageState::describe() const
23 {
24         string descr = strformat("%d", width);
25         if(height)
26         {
27                 descr += strformat("x%d", height);
28                 if(depth)
29                         descr += strformat("x%d", height);
30         }
31         descr += strformat(", %s", describe_enum(internal_format, "PixelFormat"));
32         return descr;
33 }
34
35
36 TextureState::TextureState():
37         id(0),
38         target(0),
39         min_filter(GL_LINEAR_MIPMAP_LINEAR),
40         mag_filter(GL_LINEAR),
41         wrap_s(GL_REPEAT),
42         wrap_t(GL_REPEAT),
43         wrap_r(GL_REPEAT),
44         compare_mode(GL_NONE),
45         compare_func(GL_LEQUAL),
46         generate_mipmap(false)
47 { }
48
49 void TextureState::set_image_2d(unsigned level, GLenum ifmt, unsigned wd, unsigned ht)
50 {
51         while(1)
52         {
53                 if(images.size()<=level)
54                         images.resize(level+1);
55                 images[level].set_2d(ifmt, wd, ht);
56
57                 if(generate_mipmap)
58                 {
59                         if(wd==1 && ht==1)
60                                 break;
61                         ++level;
62                         if(wd>1)
63                                 wd /= 2;
64                         if(ht>1)
65                                 ht /= 2;
66                 }
67                 else
68                         break;
69         }
70 }
71
72 void TextureState::set_parameter(GLenum pname, const int *values)
73 {
74         if(pname==GL_TEXTURE_MIN_FILTER)
75                 min_filter = values[0];
76         else if(pname==GL_TEXTURE_MAG_FILTER)
77                 mag_filter = values[0];
78         else if(pname==GL_TEXTURE_WRAP_S)
79                 wrap_s = values[0];
80         else if(pname==GL_TEXTURE_WRAP_T)
81                 wrap_t = values[0];
82         else if(pname==GL_TEXTURE_WRAP_R)
83                 wrap_r = values[0];
84         else if(pname==GL_TEXTURE_COMPARE_MODE)
85                 compare_mode = values[0];
86         else if(pname==GL_TEXTURE_COMPARE_FUNC)
87                 compare_func = values[0];
88         else if(pname==GL_GENERATE_MIPMAP)
89                 generate_mipmap = values[0];
90 }
91
92 string TextureState::describe() const
93 {
94         string descr = describe_enum(target, "TextureTarget");
95         if(images.empty())
96                 descr += ", undefined";
97         else
98                 descr += ", "+images.front().describe();
99         return descr;
100 }
101
102
103 TexUnitState::TexUnitState():
104         current_2d(0),
105         current_3d(0)
106 { }
107
108 void TexUnitState::set_current_texture(GLenum target, TextureState *tex)
109 {
110         if(target==GL_TEXTURE_2D)
111                 current_2d = tex;
112         else if(target==GL_TEXTURE_3D)
113                 current_3d = tex;
114 }
115
116 TextureState *TexUnitState::get_current_texture(GLenum target)
117 {
118         if(target==GL_TEXTURE_2D)
119                 return current_2d;
120         else if(target==GL_TEXTURE_3D)
121                 return current_3d;
122         else
123                 return 0;
124 }
125
126 const TextureState *TexUnitState::get_current_texture(GLenum target) const
127 {
128         return const_cast<TexUnitState *>(this)->get_current_texture(target);
129 }
130
131 string TexUnitState::describe_binding(GLenum target) const
132 {
133         if(const TextureState *tex = get_current_texture(target))
134         {
135                 string descr = strformat("%d ", tex->id);
136                 if(tex->images.empty())
137                         descr += "(undefined)";
138                 else
139                         descr += "("+tex->images.front().describe()+")";
140                 return descr;
141         }
142         else
143                 return "None";
144 }