]> git.tdb.fi Git - libs/gl.git/blob - source/texgen.cpp
Enhance the extension generator to support different APIs
[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         {
55                 unit.bind();
56                 disable(GL_TEXTURE_GEN_S+coord_index(c));
57         }
58 }
59
60 unsigned TexGen::coord_index(TexCoordComponent c)
61 {
62         switch(c)
63         {
64         case SCOORD: return 0;
65         case TCOORD: return 1;
66         case RCOORD: return 2;
67         case QCOORD: return 3;
68         default: throw invalid_argument("TexGen::coord_index");
69         }
70 }
71
72 } // namespace GL
73 } // namespace Msp