]> git.tdb.fi Git - libs/gl.git/blob - source/clipping.cpp
Binding fixes for Clipping when mixing legacy and modern mode
[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         const Clipping *old = current();
62         if(!set_current(this) && !(legacy && !bound_with_legacy))
63                 return;
64
65         bound_with_legacy = legacy;
66         if(legacy)
67         {
68                 for(unsigned i=0; i<planes.size(); ++i)
69                 {
70                         if(planes[i])
71                                 planes[i]->bind_to(i);
72                         else
73                                 ClipPlane::unbind_from(i);
74                 }
75
76                 if(old)
77                 {
78                         for(unsigned i=planes.size(); i<old->planes.size(); ++i)
79                                 ClipPlane::unbind_from(i);
80                 }
81         }
82         else
83         {
84                 for(unsigned i=0; i<planes.size(); ++i)
85                 {
86                         if(planes[i])
87                                 enable(GL_CLIP_PLANE0+i);
88                         else
89                                 disable(GL_CLIP_PLANE0+i);
90                 }
91
92                 if(old)
93                 {
94                         for(unsigned i=planes.size(); i<old->planes.size(); ++i)
95                                 disable(GL_CLIP_PLANE0+i);
96                 }
97         }
98 }
99
100 void Clipping::unbind()
101 {
102         const Clipping *old = current();
103         if(!set_current(0))
104                 return;
105
106         if(bound_with_legacy)
107         {
108                 for(unsigned i=0; i<old->planes.size(); ++i)
109                         if(old->planes[i])
110                                 ClipPlane::unbind_from(i);
111         }
112         else
113         {
114                 for(unsigned i=0; i<old->planes.size(); ++i)
115                         if(old->planes[i])
116                                 disable(GL_CLIP_PLANE0+i);
117         }
118 }
119
120 } // namespace GL
121 } // namespace Msp