]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/clipping.cpp
Store implementation limits in a central struct
[libs/gl.git] / source / core / clipping.cpp
index 3bdeee3ca8b3367624504f9815338f1810c4dcb9..644a2434c6b783bda0f806012f23e841e9acd5b8 100644 (file)
@@ -1,6 +1,9 @@
+#include <msp/core/algorithm.h>
 #include <msp/gl/extensions/msp_clipping.h>
 #include "clipping.h"
 #include "clipplane.h"
+#include "deviceinfo.h"
+#include "error.h"
 #include "matrix.h"
 #include "misc.h"
 
@@ -11,40 +14,49 @@ namespace GL {
 
 unsigned Clipping::get_n_attach_points()
 {
-       static Require _req(MSP_clipping);
-       static int count = get_i(GL_MAX_CLIP_PLANES);
-       return count;
+       return Limits::get_global().max_clip_planes;
 }
 
-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_member(planes, &p, &AttachedPlane::plane)!=planes.end())
+               return;
+       if(planes.size()>=Limits::get_global().max_clip_planes)
+               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<AttachedPlane>::iterator i = find_member(planes, &p, &AttachedPlane::plane);
+       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::update_shader_data(ProgramData &shdata, const Matrix &view_matrix) const
+void Clipping::detach(unsigned i)
+{
+       if(i<planes.size())
+               detach(*planes[i].plane);
+}
+
+const ProgramData &Clipping::get_shader_data() 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);
+               if(planes[i].plane->get_generation()!=planes[i].generation)
+               {
+                       planes[i].plane->update_shader_data(shdata, i);
+                       planes[i].generation = planes[i].plane->get_generation();
+               }
+
+       return shdata;
 }
 
 void Clipping::bind() const
@@ -56,12 +68,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 +84,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