   3D clipping problems
  Hi all!I recently started working on my long dormant 3D engine project written entirely in software. As things started coming together I noticed that my 3D clipping simply does not work the way it's supposed to (not at all, in other words).I based the algorithm on the one I found in this article. I have tried finding the problem myself, but with no success.My code is presented below. The result of the clipping seems to be a completely unchanged triangle (as far as I know). I believe that the error lies somewhere in the creation of the plane or in the actual clipping.namespace i2{  namespace Clip{    class Plane3    {    private: i2::Vec3f normal; floatx distance;    public: template < typename VERT_t > void CreatePlane(const i2::Geom::Tri<VERT_t*> &_tri); template < typename VERT_t > void Clip(const i2::Geom::Poly<VERT_t*> &_subject, i2::Geom::Poly<VERT_t> &_OUT);    };  }}// Creates a plane from three points in spacetemplate < typename VERT_t >void i2::Clip::Plane3::CreatePlane(const i2::Geom::Tri<VERT_t*> &_tri){  i2::Vec3f q = (_tri.y->position -_tri.x->position), v = (_tri.y->position -_tri.z->position);  normal = i2::Cross3(q,v);  i2::Normal3(normal);  distance = i2::Dot3(normal, _tri.x->position);}// Clips a polygon (_subject) and outputs another polygon (_OUT)template < typename VERT_t >void i2::Clip::Plane3::Clip(const i2::Geom::Poly<VERT_t*> &_subject, i2::Geom::Poly<VERT_t> &_OUT){  _OUT.clear();  floatx bdist = i2::Dot3(_subject[_subject.size()-1]->position, normal); //previous distance  for (int32 i = 0, j = _subject.size()-1; i < _subject.size(); j=i, ++i){    floatx adist = i2::Dot3(_subject->position, normal); // current distance    if (adist > 0.f){ if (bdist > 0.f){   // a inside   // b inside - output a   _OUT.push_back(*_subject); } else {   // a inside   // b outside - output x & a   floatx s = adist/(adist-bdist);   _OUT.push_back(*_subject + ((*_subject[j]-*_subject) *s));   _OUT.push_back(*_subject); }    } else if (bdist > 0.f){ // a outside // b inside - output x floatx s = adist/(adist-bdist); //this might be wrong (adist/(bdist-adist)) _OUT.push_back(*_subject + ((*_subject[j]-*_subject) *s));    }    // a outside    // b outside - do nothing    bdist = adist; // do this to avoid recalculating same values every loop  }}* A brief explanation of the code: as you can see there are a few overloaded operators here. Just assume that they are component-wise addition/ subtraction/ multiplication/ division. Also, the i2::Geom::Tri class contains three vertices called x, y, z.This code is what I assume is causing the problem. Does anyone know what I have done wrong?Another question that came to mind when reading the article was; is it really faster to do clipping in 3D than in 2D?Thank you in advance!/JNT[Edited by - JNT on May 8, 2010 5:34:46 PM]
  I found help with my first question elsewhere.Basically all I needed to do was to store one of the three positional values used to create the plane, and then subtract this positional value from the subject polygons' positional values when calculating the distance using the dot product in the clipping function, like so:// in Plane3 class declaration...private:  i2::Vec3f position;...// in CreatePlane...position = _tri.x->position; // could just as well be position of vertex Y or Z...// in Clip...floatx bdist = i2::Dot3(_subject[_subject.size()-1]->position -position, normal);for (int32 i = 0, j = _subject.size()-1; i < _subject.size(); j=i, ++i){  floatx adist = i2::Dot3(_subject->position -position, normal);