]> git.tdb.fi Git - libs/gl.git/blob - source/texunit.cpp
Rework exceptions
[libs/gl.git] / source / texunit.cpp
1 #include <stdexcept>
2 #include "extension.h"
3 #include "gl.h"
4 #include "texunit.h"
5 #include "version_1_3.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 vector<TexUnit> TexUnit::units;
13 TexUnit *TexUnit::cur_unit = 0;
14
15 TexUnit::TexUnit():
16         texture(0),
17         texenv(0)
18 { }
19
20 bool TexUnit::set_texture(const Texture *tex)
21 {
22         bool result = (tex!=texture);
23         texture = tex;
24         return result;
25 }
26
27 bool TexUnit::set_texenv(const TexEnv *env)
28 {
29         bool result = (texenv!=env);
30         texenv = env;
31         return result;
32 }
33
34 unsigned TexUnit::get_n_units()
35 {
36         static int count = -1;
37         if(count<0)
38         {
39                 if(is_version_at_least(2, 0) || is_supported("ARB_vertex_shader"))
40                         glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &count);
41                 else if(is_version_at_least(1, 3))
42                         glGetIntegerv(GL_MAX_TEXTURE_UNITS, &count);
43                 else
44                         count = 1;
45         }
46         return count;
47 }
48
49 TexUnit &TexUnit::activate(unsigned n)
50 {
51         if(n>=get_n_units())
52                 throw out_of_range("TexUnit::activate");
53
54         if(units.size()<=n)
55                 units.resize(n+1);
56
57         if(cur_unit!=&units[n] && (cur_unit || n))
58                 glActiveTexture(GL_TEXTURE0+n);
59         cur_unit = &units[n];
60
61         return units[n];
62 }
63
64 TexUnit &TexUnit::current()
65 {
66         if(!cur_unit)
67                 return activate(0);
68         return *cur_unit;
69 }
70
71 } // namespace GL
72 } // namespace Msp