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