]> git.tdb.fi Git - libs/gl.git/blob - source/clipplane.cpp
Support animations on Placeables
[libs/gl.git] / source / clipplane.cpp
1 #include <msp/gl/extensions/msp_clipping.h>
2 #include <msp/gl/extensions/msp_legacy_features.h>
3 #include <msp/strings/format.h>
4 #include "clipplane.h"
5 #include "clipunit.h"
6 #include "gl.h"
7 #include "matrix.h"
8 #include "misc.h"
9 #include "programdata.h"
10
11 namespace Msp {
12 namespace GL {
13
14 ClipPlane::ClipPlane():
15         eq(0, 0, 0, 1)
16 { }
17
18 ClipPlane::ClipPlane(const Vector4 &e):
19         eq(e)
20 { }
21
22 ClipPlane::ClipPlane(const Vector3 &p, const Vector3 &d):
23         eq(compose(d, -dot(p, d)))
24 { }
25
26 void ClipPlane::update(unsigned index) const
27 {
28         double deq[4];
29         for(unsigned i=0; i<4; ++i)
30                 deq[i] = eq[i];
31         glClipPlane(GL_CLIP_PLANE0+index, deq);
32 }
33
34 void ClipPlane::set_equation(const Vector4 &e)
35 {
36         eq = e;
37         if(ClipUnit *unit = ClipUnit::find_unit(this))
38                 update(unit->get_index());
39 }
40
41 void ClipPlane::set_plane(const Vector3 &p, const Vector3 &d)
42 {
43         Vector3 nd = normalize(d);
44         set_equation(compose(nd, -dot(p, nd)));
45 }
46
47 void ClipPlane::update_shader_data(ProgramData &shdata, const Matrix &view_inverse, unsigned i) const
48 {
49         shdata.uniform(format("clip_planes[%d].equation", i), eq*view_inverse);
50 }
51
52 void ClipPlane::bind_to(unsigned i) const
53 {
54         Require _req(MSP_clipping);
55         Require _req2(MSP_legacy_features);
56
57         ClipUnit &unit = ClipUnit::get_unit(i);
58         if(unit.set_plane(this))
59         {
60                 enable(GL_CLIP_PLANE0+unit.get_index());
61                 update(unit.get_index());
62         }
63 }
64
65 void ClipPlane::unbind_from(unsigned i)
66 {
67         ClipUnit &unit = ClipUnit::get_unit(i);
68         if(unit.set_plane(0))
69                 disable(GL_CLIP_PLANE0+unit.get_index());
70 }
71
72 } // namespace GL
73 } // namespace Msp