]> git.tdb.fi Git - libs/gl.git/commitdiff
Change Clipping to use index-less attaching
authorMikko Rasa <tdb@tdb.fi>
Sat, 8 May 2021 13:40:23 +0000 (16:40 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 8 May 2021 13:49:40 +0000 (16:49 +0300)
source/core/clipping.cpp
source/core/clipping.h

index 3bdeee3ca8b3367624504f9815338f1810c4dcb9..c286b7e57677e7880a56908e3dda0f3ff90a15be 100644 (file)
@@ -1,6 +1,8 @@
+#include <msp/core/algorithm.h>
 #include <msp/gl/extensions/msp_clipping.h>
 #include "clipping.h"
 #include "clipplane.h"
+#include "error.h"
 #include "matrix.h"
 #include "misc.h"
 
@@ -16,35 +18,41 @@ unsigned Clipping::get_n_attach_points()
        return count;
 }
 
-void Clipping::attach(unsigned i, const ClipPlane &p)
+void Clipping::attach(const ClipPlane &p)
 {
-       if(i>=get_n_attach_points())
-               throw out_of_range("Clipping::attach");
-
-       if(i>=planes.size())
-               planes.resize(i+1);
+       if(find(planes, &p)!=planes.end())
+               return;
+       if(planes.size()>=get_n_attach_points())
+               throw invalid_operation("Clipping::attach");
 
-       planes[i] = &p;
+       planes.push_back(&p);
        if(current()==this)
-               glEnable(GL_CLIP_PLANE0+i);
+               glEnable(GL_CLIP_PLANE0+planes.size()-1);
 }
 
-void Clipping::detach(unsigned i)
+void Clipping::detach(const ClipPlane &p)
 {
-       if(i>=planes.size())
-               return;
+       vector<const ClipPlane *>::iterator i = find(planes, &p);
+       if(i!=planes.end())
+       {
+               planes.erase(i);
+               if(current()==this)
+                       disable(GL_CLIP_PLANE0+planes.size());
 
-       planes[i] = 0;
-       if(current()==this)
-               disable(GL_CLIP_PLANE0+i);
+       }
+}
+
+void Clipping::detach(unsigned i)
+{
+       if(i<planes.size())
+               detach(*planes[i]);
 }
 
 void Clipping::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
 {
        Matrix view_inverse = invert(view_matrix);
        for(unsigned i=0; i<planes.size(); ++i)
-               if(planes[i])
-                       planes[i]->update_shader_data(shdata, view_inverse, i);
+               planes[i]->update_shader_data(shdata, view_inverse, i);
 }
 
 void Clipping::bind() const
@@ -56,12 +64,7 @@ void Clipping::bind() const
                return;
 
        for(unsigned i=0; i<planes.size(); ++i)
-       {
-               if(planes[i])
-                       enable(GL_CLIP_PLANE0+i);
-               else
-                       disable(GL_CLIP_PLANE0+i);
-       }
+               enable(GL_CLIP_PLANE0+i);
 
        if(old)
        {
@@ -77,8 +80,7 @@ void Clipping::unbind()
                return;
 
        for(unsigned i=0; i<old->planes.size(); ++i)
-               if(old->planes[i])
-                       disable(GL_CLIP_PLANE0+i);
+               disable(GL_CLIP_PLANE0+i);
 }
 
 } // namespace GL
index e42c28e6394b6c1c519afc26f182957b1f75be20..319a8d98ff765a9cfb4e1c7b3fd57ba825b60b75 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_CLIPPING_H_
 
 #include <vector>
+#include <msp/core/attributes.h>
 #include "bindable.h"
 
 namespace Msp {
@@ -19,8 +20,11 @@ private:
 public:
        static unsigned get_n_attach_points();
 
-       void attach(unsigned, const ClipPlane &);
-       void detach(unsigned);
+       void attach(const ClipPlane &);
+       void detach(const ClipPlane &);
+
+       DEPRECATED void attach(unsigned, const ClipPlane &p) { attach(p); }
+       DEPRECATED void detach(unsigned);
 
        void update_shader_data(ProgramData &, const Matrix &) const;