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