]> git.tdb.fi Git - libs/gl.git/blob - source/core/clipping.cpp
18b706e78b1b34e9205879acd51cf83d10ad9cbb
[libs/gl.git] / source / core / clipping.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/gl/extensions/msp_clipping.h>
3 #include "clipping.h"
4 #include "clipplane.h"
5 #include "error.h"
6 #include "matrix.h"
7 #include "misc.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 unsigned Clipping::get_n_attach_points()
15 {
16         static Require _req(MSP_clipping);
17         static int count = get_i(GL_MAX_CLIP_PLANES);
18         return count;
19 }
20
21 void Clipping::attach(const ClipPlane &p)
22 {
23         if(find_member(planes, &p, &AttachedPlane::plane)!=planes.end())
24                 return;
25         if(planes.size()>=get_n_attach_points())
26                 throw invalid_operation("Clipping::attach");
27
28         planes.push_back(&p);
29         if(current()==this)
30                 glEnable(GL_CLIP_PLANE0+planes.size()-1);
31 }
32
33 void Clipping::detach(const ClipPlane &p)
34 {
35         vector<AttachedPlane>::iterator i = find_member(planes, &p, &AttachedPlane::plane);
36         if(i!=planes.end())
37         {
38                 planes.erase(i);
39                 if(current()==this)
40                         disable(GL_CLIP_PLANE0+planes.size());
41
42         }
43 }
44
45 void Clipping::detach(unsigned i)
46 {
47         if(i<planes.size())
48                 detach(*planes[i].plane);
49 }
50
51 const ProgramData &Clipping::get_shader_data() const
52 {
53         for(unsigned i=0; i<planes.size(); ++i)
54                 if(planes[i].plane->get_generation()!=planes[i].generation)
55                 {
56                         planes[i].plane->update_shader_data(shdata, i);
57                         planes[i].generation = planes[i].plane->get_generation();
58                 }
59
60         return shdata;
61 }
62
63 void Clipping::bind() const
64 {
65         static Require _req(MSP_clipping);
66
67         const Clipping *old = current();
68         if(!set_current(this))
69                 return;
70
71         for(unsigned i=0; i<planes.size(); ++i)
72                 enable(GL_CLIP_PLANE0+i);
73
74         if(old)
75         {
76                 for(unsigned i=planes.size(); i<old->planes.size(); ++i)
77                         disable(GL_CLIP_PLANE0+i);
78         }
79 }
80
81 void Clipping::unbind()
82 {
83         const Clipping *old = current();
84         if(!set_current(0))
85                 return;
86
87         for(unsigned i=0; i<old->planes.size(); ++i)
88                 disable(GL_CLIP_PLANE0+i);
89 }
90
91 } // namespace GL
92 } // namespace Msp