]> git.tdb.fi Git - libs/gl.git/blob - source/texturing.cpp
Rework exceptions
[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         TexUnit::activate(i);
51         if(attch.tex)
52                 attch.tex->bind();
53         else
54                 Texture::unbind();
55         if(attch.env)
56                 attch.env->bind();
57         else
58                 TexEnv::unbind();
59 }
60
61 void Texturing::unbind_attachment(unsigned i)
62 {
63         TexUnit::activate(i);
64         Texture::unbind();
65         TexEnv::unbind();
66 }
67
68 void Texturing::bind() const
69 {
70         const Texturing *old = current();
71         if(set_current(this))
72         {
73                 for(unsigned i=0; i<attachments.size(); ++i)
74                         bind_attachment(i);
75                 if(old)
76                 {
77                         for(unsigned i=attachments.size(); i<old->attachments.size(); ++i)
78                                 unbind_attachment(i);
79                 }
80         }
81 }
82
83 void Texturing::unbind()
84 {
85         const Texturing *old = current();
86         if(set_current(0))
87         {
88                 for(unsigned i=old->attachments.size(); i--;)
89                         unbind_attachment(i);
90         }
91 }
92
93
94 Texturing::Attachment::Attachment():
95         tex(0),
96         env(0)
97 { }
98
99 } // namespace GL
100 } // namespace Msp;