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