]> git.tdb.fi Git - libs/gl.git/blob - source/texunit.cpp
Remove useless using declarations
[libs/gl.git] / source / texunit.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/arb_multitexture.h>
3 #include <msp/gl/extensions/arb_vertex_shader.h>
4 #include <msp/gl/extensions/msp_legacy_features.h>
5 #include "gl.h"
6 #include "misc.h"
7 #include "texunit.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 vector<TexUnit> TexUnit::units;
15 TexUnit *TexUnit::cur_unit = 0;
16
17 TexUnit::TexUnit():
18         legacy(false),
19         texture(0),
20         tex_legacy(false)
21 { }
22
23 bool TexUnit::set_texture(const Texture *tex, bool lgc)
24 {
25         lgc = (lgc && legacy && tex);
26         bool result = (tex!=texture || lgc!=tex_legacy);
27         texture = tex;
28         tex_legacy = lgc;
29         return result;
30 }
31
32 void TexUnit::bind()
33 {
34         if(cur_unit!=this && (cur_unit || index))
35                 glActiveTexture(GL_TEXTURE0+index);
36         cur_unit = this;
37 }
38
39 unsigned TexUnit::get_n_units()
40 {
41         static int count = -1;
42         if(count<0)
43         {
44                 if(ARB_vertex_shader)
45                         count = get_i(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
46                 else if(ARB_multitexture)
47                         count = get_i(GL_MAX_TEXTURE_UNITS);
48                 else
49                         count = 1;
50         }
51         return count;
52 }
53
54 unsigned TexUnit::get_n_legacy_units()
55 {
56         static int count = -1;
57         if(count<0)
58         {
59                 if(MSP_legacy_features)
60                         count = get_i(GL_MAX_TEXTURE_UNITS);
61                 else
62                         count = 0;
63         }
64         return count;
65 }
66
67 TexUnit &TexUnit::get_unit(unsigned n)
68 {
69         if(n>0)
70                 static Require _req(ARB_multitexture);
71         if(n>=get_n_units())
72                 throw out_of_range("TexUnit::get_unit");
73
74         if(units.size()<=n)
75         {
76                 unsigned i = units.size();
77                 unsigned n_legacy = get_n_legacy_units();
78                 units.resize(n+1, TexUnit());
79                 for(; i<units.size(); ++i)
80                 {
81                         units[i].index = i;
82                         units[i].legacy = (i<n_legacy);
83                 }
84         }
85
86         return units[n];
87 }
88
89 TexUnit &TexUnit::current()
90 {
91         if(!cur_unit)
92                 get_unit(0).bind();
93         return *cur_unit;
94 }
95
96 TexUnit *TexUnit::find_unit(const Texture *tex)
97 {
98         for(vector<TexUnit>::iterator i=units.begin(); i!=units.end(); ++i)
99                 if(i->texture==tex)
100                         return &*i;
101         return 0;
102 }
103
104 } // namespace GL
105 } // namespace Msp