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