空間變換代碼,相當簡潔優美

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.