zoukankan      html  css  js  c++  java
  • 空间变换代码,相当简洁优美

    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 );
    }

  • 相关阅读:
    在纪念中国人民抗日战争暨世界反法西斯战争胜利70周年大会上的讲话
    ConcurrentHashMap 的实现原理
    聊聊并发(四)——深入分析ConcurrentHashMap
    Mybatis 动态 SQL
    Mybatis Mapper XML 文件
    MySQL的语句执行顺序
    Java 集合细节(二):asList 的缺陷
    java中 列表,集合,数组之间的转换
    将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)
    把Java数组转换为List时的注意事项
  • 原文地址:https://www.cnblogs.com/len3d/p/180753.html
Copyright © 2011-2022 走看看