]> git.tdb.fi Git - libs/gl.git/blobdiff - source/clipplane.cpp
Restore user clip planes in a form compatible with modern OpenGL
[libs/gl.git] / source / clipplane.cpp
diff --git a/source/clipplane.cpp b/source/clipplane.cpp
new file mode 100644 (file)
index 0000000..04bd6ac
--- /dev/null
@@ -0,0 +1,68 @@
+#include <msp/strings/format.h>
+#include "clipplane.h"
+#include "clipunit.h"
+#include "gl.h"
+#include "matrix.h"
+#include "misc.h"
+#include "programdata.h"
+
+namespace Msp {
+namespace GL {
+
+ClipPlane::ClipPlane():
+       eq(0, 0, 0, 1)
+{ }
+
+ClipPlane::ClipPlane(const Vector4 &e):
+       eq(e)
+{ }
+
+ClipPlane::ClipPlane(const Vector3 &p, const Vector3 &d):
+       eq(compose(d, -dot(p, d)))
+{ }
+
+void ClipPlane::update(unsigned index) const
+{
+       double deq[4];
+       for(unsigned i=0; i<4; ++i)
+               deq[i] = eq[i];
+       glClipPlane(GL_CLIP_PLANE0+index, deq);
+}
+
+void ClipPlane::set_equation(const Vector4 &e)
+{
+       eq = e;
+       if(ClipUnit *unit = ClipUnit::find_unit(this))
+               update(unit->get_index());
+}
+
+void ClipPlane::set_plane(const Vector3 &p, const Vector3 &d)
+{
+       Vector3 nd = normalize(d);
+       set_equation(compose(nd, -dot(p, nd)));
+}
+
+void ClipPlane::update_shader_data(ProgramData &shdata, const Matrix &view_inverse, unsigned i) const
+{
+       shdata.uniform(format("clip_planes[%d].equation", i), eq*view_inverse);
+}
+
+void ClipPlane::bind_to(unsigned i) const
+{
+       ClipUnit &unit = ClipUnit::get_unit(i);
+       if(unit.set_plane(this))
+       {
+               enable(GL_CLIP_PLANE0+unit.get_index());
+               update(unit.get_index());
+       }
+}
+
+void ClipPlane::unbind_from(unsigned i)
+{
+       ClipUnit &unit = ClipUnit::get_unit(i);
+       if(unit.set_plane(0))
+               disable(GL_CLIP_PLANE0+unit.get_index());
+}
+
+} // namespace GL
+} // namespace Msp