• 空间变换代码,相当简洁优美


    class eiObject
    {
    public:
     eiString    name;

     eiObjectType   type;

     eiMaterial    *material;
     eiUInt     mtl_id;

     eiBound     box;
     eiFloat     width;  //delta x
     eiFloat     height; //delta y
     eiFloat     length; //delta z
     eiFloat     radius;
      
    public:
     eiObject();
     ~eiObject();

     virtual eiVoid inherit_transform(const eiMatrix &);

     virtual eiVoid triangulate();
    };
    //-------------------------------------------------
    class eiPolygon : public eiObject
    {
    public:
     std::vector<eiTri>  triList;
     std::vector<eiVtx>  vtxList;
     std::vector<eiEdge> edgeList;

     ......

    public:
     eiPolygon();
     ~eiPolygon();

     eiVoid inherit_transform(const eiMatrix &);
    };
    //-------------------------------------------------
    class eiGroup
    {
    public:
     eiString    name;

     eiVector    pos;
     eiVector    x_axis,y_axis,z_axis;

     std::list<eiGroup>  children;
     eiGroup     *parent;
       
     eiObject    *object;

    public:
     eiGroup();
     ~eiGroup();

     virtual eiVoid translate(const eiVector &);
     virtual eiVoid rotate(const eiVector &);
     virtual eiVoid scale(const eiVector &);

     virtual eiVoid apply_transform();
     virtual eiVoid inherit_transform(const eiMatrix &);
    };


    /**************************************************************************
            Transform
     **************************************************************************/

    #include "stdafx.h"
    #include "head.h"

    //-------------------------------------------------------------------------
    eiVoid eiObject::inherit_transform(const eiMatrix & mx)
    {
     return;
    }
    //-------------------------------------------------------------------------
    eiVoid eiObject::triangulate()
    {
     return;
    }
    //-------------------------------------------------------------------------
    eiVoid eiPolygon::inherit_transform(const eiMatrix & mx)
    {
     if( ! vtxList.empty() )
     {
      for(eiInt i = 0 ; i < vtxList.size() ; i++)
      {
       vtxList[i].v_pos = mulvm( vtxList[i].pos, inverse( mx ) );
      }
     }
    }
    //-------------------------------------------------------------------------
    eiVoid eiGroup::eiGroup()
    {
     pos = newvec();
     x_axis = newvec( 1.0, 0.0, 0.0 );
     y_axis = newvec( 0.0, 1.0, 0.0 );
     z_axis = newvec( 0.0, 0.0, 1.0 );

     parent = NULL;
     object = NULL;
    }
    //-------------------------------------------------------------------------
    eiVoid eiGroup::apply_transform()
    {
     eiMatrix tm,rm;

     tm = newmx( 1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        - pos.x, - pos.y, - pos.z, 1.0 );

     rm = newmx( x_axis.x, y_axis.x, z_axis.x, 0.0 ,
              x_axis.y, y_axis.y, z_axis.y, 0.0 ,
        x_axis.z, y_axis.z, z_axis.z, 0.0 ,
        0.0 , 0.0 , 0.0 , 1.0 );

     tm = mulmm( tm, rm );

     if(object != NULL)
     {
      object->inherit_transform( tm );
     }

     if( ! children.empty() )
     {
      for( std::list<eiGroup>::iterator iter = children.begin() ; iter != children.end() ; ++ iter )
      {
       iter->inherit_transform( tm );
      }
     }
    }
    //-------------------------------------------------------------------------
    eiVoid eiGroup::inherit_transform(const eiMatrix & mx)
    {
     eiMatrix tm,rm;

     tm = newmx( 1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        - pos.x, - pos.y, - pos.z, 1.0 );

     rm = newmx( x_axis.x, y_axis.x, z_axis.x, 0.0 ,
              x_axis.y, y_axis.y, z_axis.y, 0.0 ,
        x_axis.z, y_axis.z, z_axis.z, 0.0 ,
        0.0 , 0.0 , 0.0 , 1.0 );

     tm = mulmm( tm, rm );
     tm = mulmm( mx, tm );

     if(object != NULL)
     {
      object->inherit_transform( tm );
     }

     if( ! children.empty() )
     {
      for( std::list<eiGroup>::iterator iter = children.begin() ; iter != children.end() ; ++ iter )
      {
       iter->inherit_transform( tm );
      }
     }
    }
    //-------------------------------------------------------------------------
    eiVoid eiGroup::translate(const eiVector & vec)
    {
     pos = add( pos, vec );
    }
    //-------------------------------------------------------------------------
    eiVoid eiGroup::rotate(const eiVector & vec)
    {
     if(vec.y != 0.0f)
     {
      x_axis = mulvm( x_axis, roty(vec.y) );
      y_axis = mulvm( y_axis, roty(vec.y) );
      z_axis = mulvm( z_axis, roty(vec.y) );
     }

     if(vec.x != 0.0f)
     {
      x_axis = mulvm( x_axis, rotx(vec.x) );
      y_axis = mulvm( y_axis, rotx(vec.x) );
      z_axis = mulvm( z_axis, rotx(vec.x) );
     }

     if(vec.z != 0.0f)
     {
      x_axis = mulvm( x_axis, rotz(vec.z) );
      y_axis = mulvm( y_axis, rotz(vec.z) );
      z_axis = mulvm( z_axis, rotz(vec.z) );
     }
    }
    //-------------------------------------------------------------------------
    eiVoid eiGroup::scale(const eiVector & vec)
    {
     x_axis = mulvf( x_axis, vec.x );
     y_axis = mulvf( y_axis, vec.y );
     z_axis = mulvf( z_axis, vec.z );
    }

  • 相关阅读:
    实习第一周小记------生活不易
    第一次实习面试感受----苦逼程序员生活初体验
    第一次实习面试感受----苦逼程序员生活初体验 分类: 程序人生 2013-07-28 14:13 2395人阅读 评论(0) 收藏
    Java-基于JDK的动态代理
    JS字符串去替换元素再转换成数组
    byte溢出栗子
    Java链式写法
    Scala笔记
    Java反射使用方法
    Java数组3种创建方式
  • 原文地址:https://www.cnblogs.com/len3d/p/180753.html
Copyright © 2020-2023  润新知