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