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