]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Always bind textures in a specific texture unit
[libs/gl.git] / source / texturing.cpp
1 #include "texenv.h"
2 #include "texture.h"
3 #include "texturing.h"
4 #include "texunit.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Texturing::~Texturing()
12 {
13         if(current()==this)
14                 unbind();
15 }
16
17 void Texturing::attach(unsigned attch, const Texture &tex)
18 {
19         set_attachment(attch, &tex, 0);
20 }
21
22 void Texturing::attach(unsigned attch, const Texture &tex, const TexEnv &env)
23 {
24         set_attachment(attch, &tex, &env);
25 }
26
27 void Texturing::detach(unsigned attch)
28 {
29         set_attachment(attch, 0, 0);
30 }
31
32 void Texturing::set_attachment(unsigned attch, const Texture *tex, const TexEnv *env)
33 {
34         if(attch>=TexUnit::get_n_units())
35                 throw out_of_range("Texturing::set_attachment");
36
37         if(attachments.size()<=attch)
38                 attachments.resize(attch+1);
39
40         attachments[attch].tex = tex;
41         attachments[attch].env = env;
42
43         if(current()==this)
44                 bind_attachment(attch);
45 }
46
47 void Texturing::bind_attachment(unsigned i) const
48 {
49         const Attachment &attch = attachments[i];
50         if(attch.tex)
51                 attch.tex->bind_to(i);
52         else
53                 Texture::unbind_from(i);
54         if(attch.env)
55                 attch.env->bind_to(i);
56         else
57                 TexEnv::unbind_from(i);
58 }
59
60 void Texturing::unbind_attachment(unsigned i)
61 {
62         Texture::unbind_from(i);
63         TexEnv::unbind_from(i);
64 }
65
66 void Texturing::bind() const
67 {
68         const Texturing *old = current();
69         if(set_current(this))
70         {
71                 for(unsigned i=0; i<attachments.size(); ++i)
72                         bind_attachment(i);
73                 if(old)
74                 {
75                         for(unsigned i=attachments.size(); i<old->attachments.size(); ++i)
76                                 unbind_attachment(i);
77                 }
78         }
79 }
80
81 void Texturing::unbind()
82 {
83         const Texturing *old = current();
84         if(set_current(0))
85         {
86                 for(unsigned i=old->attachments.size(); i--;)
87                         unbind_attachment(i);
88         }
89 }
90
91
92 Texturing::Attachment::Attachment():
93         tex(0),
94         env(0)
95 { }
96
97 } // namespace GL
98 } // namespace Msp;