]> git.tdb.fi Git - libs/gl.git/blob - source/clipping.cpp
Fully unbind Clipping when legacy mode changes
[libs/gl.git] / source / clipping.cpp
1 #include <msp/gl/extensions/msp_legacy_features.h>
2 #include "clipping.h"
3 #include "clipplane.h"
4 #include "clipunit.h"
5 #include "matrix.h"
6 #include "misc.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 bool Clipping::bound_with_legacy = false;
14
15 void Clipping::attach(unsigned i, const ClipPlane &p)
16 {
17         if(i>=ClipUnit::get_n_units())
18                 throw out_of_range("Clipping::attach");
19
20         if(i>=planes.size())
21                 planes.resize(i+1);
22
23         planes[i] = &p;
24         if(current()==this)
25         {
26                 if(bound_with_legacy)
27                         p.bind_to(i);
28                 else
29                         glEnable(GL_CLIP_PLANE0+i);
30         }
31 }
32
33 void Clipping::detach(unsigned i)
34 {
35         if(i>=planes.size())
36                 return;
37
38         planes[i] = 0;
39         if(current()==this)
40         {
41                 if(bound_with_legacy)
42                         ClipPlane::unbind_from(i);
43                 else
44                         disable(GL_CLIP_PLANE0+i);
45         }
46 }
47
48 void Clipping::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
49 {
50         Matrix view_inverse = invert(view_matrix);
51         for(unsigned i=0; i<planes.size(); ++i)
52                 if(planes[i])
53                         planes[i]->update_shader_data(shdata, view_inverse, i);
54 }
55
56 void Clipping::bind(bool legacy) const
57 {
58         if(legacy)
59                 static Require _req(MSP_legacy_features);
60         
61         if(legacy!=bound_with_legacy)
62                 unbind();
63
64         const Clipping *old = current();
65         if(!set_current(this))
66                 return;
67
68         bound_with_legacy = legacy;
69         if(legacy)
70         {
71                 for(unsigned i=0; i<planes.size(); ++i)
72                 {
73                         if(planes[i])
74                                 planes[i]->bind_to(i);
75                         else
76                                 ClipPlane::unbind_from(i);
77                 }
78
79                 if(old)
80                 {
81                         for(unsigned i=planes.size(); i<old->planes.size(); ++i)
82                                 ClipPlane::unbind_from(i);
83                 }
84         }
85         else
86         {
87                 for(unsigned i=0; i<planes.size(); ++i)
88                 {
89                         if(planes[i])
90                                 enable(GL_CLIP_PLANE0+i);
91                         else
92                                 disable(GL_CLIP_PLANE0+i);
93                 }
94
95                 if(old)
96                 {
97                         for(unsigned i=planes.size(); i<old->planes.size(); ++i)
98                                 disable(GL_CLIP_PLANE0+i);
99                 }
100         }
101 }
102
103 void Clipping::unbind()
104 {
105         const Clipping *old = current();
106         if(!set_current(0))
107                 return;
108
109         if(bound_with_legacy)
110         {
111                 for(unsigned i=0; i<old->planes.size(); ++i)
112                         if(old->planes[i])
113                                 ClipPlane::unbind_from(i);
114         }
115         else
116         {
117                 for(unsigned i=0; i<old->planes.size(); ++i)
118                         if(old->planes[i])
119                                 disable(GL_CLIP_PLANE0+i);
120         }
121 }
122
123 } // namespace GL
124 } // namespace Msp