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