]> git.tdb.fi Git - libs/gl.git/blob - source/texgen.cpp
Always bind textures in a specific texture unit
[libs/gl.git] / source / texgen.cpp
1 #include <stdexcept>
2 #include "misc.h"
3 #include "texgen.h"
4 #include "texunit.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 TexGen::TexGen():
12         mode(EYE_LINEAR)
13 { }
14
15 void TexGen::set_mode(TexGenMode m)
16 {
17         mode = m;
18 }
19
20 void TexGen::set_plane(const Vector4 &p)
21 {
22         plane = p;
23 }
24
25 void TexGen::bind_to(unsigned i, TexCoordComponent c) const
26 {
27         TexUnit &unit = TexUnit::get_unit(i);
28         if(unit.set_texgen(coord_index(c), this))
29         {
30                 unit.bind();
31                 glTexGeni(c, GL_TEXTURE_GEN_MODE, mode);
32                 if(mode==EYE_LINEAR)
33                         glTexGenfv(c, GL_EYE_PLANE, &plane.x);
34                 else if(mode==OBJECT_LINEAR)
35                         glTexGenfv(c, GL_OBJECT_PLANE, &plane.x);
36                 enable(GL_TEXTURE_GEN_S+coord_index(c));
37         }
38 }
39
40 const TexGen *TexGen::current(unsigned i, TexCoordComponent c)
41 {
42         return TexUnit::get_unit(i).get_texgen(coord_index(c));
43 }
44
45 void TexGen::unbind_from(unsigned i, TexCoordComponent c)
46 {
47         TexUnit &unit = TexUnit::get_unit(i);
48         if(unit.set_texgen(coord_index(c), 0))
49                 disable(GL_TEXTURE_GEN_S+coord_index(c));
50 }
51
52 unsigned TexGen::coord_index(TexCoordComponent c)
53 {
54         switch(c)
55         {
56         case SCOORD: return 0;
57         case TCOORD: return 1;
58         case RCOORD: return 2;
59         case QCOORD: return 3;
60         default: throw invalid_argument("TexGen::coord_index");
61         }
62 }
63
64 } // namespace GL
65 } // namespace Msp