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