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