]> git.tdb.fi Git - libs/gl.git/blob - source/texgen.cpp
Unbind things if they are deleted while current
[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 TexGen::~TexGen()
16 {
17         // TODO unbind
18 }
19
20 void TexGen::set_mode(TexGenMode m)
21 {
22         mode = m;
23 }
24
25 void TexGen::set_plane(const Vector4 &p)
26 {
27         plane = p;
28 }
29
30 void TexGen::bind_to(unsigned i, TexCoordComponent c) const
31 {
32         TexUnit &unit = TexUnit::get_unit(i);
33         if(unit.set_texgen(coord_index(c), this))
34         {
35                 unit.bind();
36                 glTexGeni(c, GL_TEXTURE_GEN_MODE, mode);
37                 if(mode==EYE_LINEAR)
38                         glTexGenfv(c, GL_EYE_PLANE, &plane.x);
39                 else if(mode==OBJECT_LINEAR)
40                         glTexGenfv(c, GL_OBJECT_PLANE, &plane.x);
41                 enable(GL_TEXTURE_GEN_S+coord_index(c));
42         }
43 }
44
45 const TexGen *TexGen::current(unsigned i, TexCoordComponent c)
46 {
47         return TexUnit::get_unit(i).get_texgen(coord_index(c));
48 }
49
50 void TexGen::unbind_from(unsigned i, TexCoordComponent c)
51 {
52         TexUnit &unit = TexUnit::get_unit(i);
53         if(unit.set_texgen(coord_index(c), 0))
54                 disable(GL_TEXTURE_GEN_S+coord_index(c));
55 }
56
57 unsigned TexGen::coord_index(TexCoordComponent c)
58 {
59         switch(c)
60         {
61         case SCOORD: return 0;
62         case TCOORD: return 1;
63         case RCOORD: return 2;
64         case QCOORD: return 3;
65         default: throw invalid_argument("TexGen::coord_index");
66         }
67 }
68
69 } // namespace GL
70 } // namespace Msp