]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Drop Id tags and copyright notices from files
[libs/gl.git] / source / texturing.cpp
1 #include "texenv.h"
2 #include "texture.h"
3 #include "texturing.h"
4 #include "texunit.h"
5
6 namespace Msp {
7 namespace GL {
8
9 Texturing::~Texturing()
10 {
11         if(current()==this)
12                 unbind();
13 }
14
15 void Texturing::attach(unsigned attch, const Texture &tex)
16 {
17         set_attachment(attch, &tex, 0);
18 }
19
20 void Texturing::attach(unsigned attch, const Texture &tex, const TexEnv &env)
21 {
22         set_attachment(attch, &tex, &env);
23 }
24
25 void Texturing::detach(unsigned attch)
26 {
27         set_attachment(attch, 0, 0);
28 }
29
30 void Texturing::set_attachment(unsigned attch, const Texture *tex, const TexEnv *env)
31 {
32         if(attch>=TexUnit::get_n_units())
33                 throw InvalidParameterValue("Invalid texture attachment");
34
35         if(attachments.size()<=attch)
36                 attachments.resize(attch+1);
37
38         attachments[attch].tex = tex;
39         attachments[attch].env = env;
40
41         if(current()==this)
42                 bind_attachment(attch);
43 }
44
45 void Texturing::bind_attachment(unsigned i) const
46 {
47         const Attachment &attch = attachments[i];
48         TexUnit::activate(i);
49         if(attch.tex)
50                 attch.tex->bind();
51         else
52                 Texture::unbind();
53         if(attch.env)
54                 attch.env->bind();
55         else
56                 TexEnv::unbind();
57 }
58
59 void Texturing::unbind_attachment(unsigned i)
60 {
61         TexUnit::activate(i);
62         Texture::unbind();
63         TexEnv::unbind();
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;