我想使用简单的正交投影在平面上变换或投影多边形点的位置 - 用于 UV 映射并被卡住。编码:
CPolygonFaceRefArray lPolygons = lPolygonMesh.GetPolygons();
for( long f=0; f < lPolygons.GetCount(); f++ )
{
PolygonFace lFace = lPolygons[ f ];
CPointRefArray lPoints = lFace.GetPoints();
Point lPoint1 = lPoints[ 0 ];
Point lPoint2 = lPoints[ 1 ];
Point lPoint3 = lPoints[ 2 ];
CVector3 lPoint1Position = lPoint1.GetPosition();
CVector3 lPoint2Position = lPoint2.GetPosition();
CVector3 lPoint3Position = lPoint3.GetPosition();
_LogValue2( f, L"--------------------------------------" );
// Vector A->B, that becomes the X axis of the local plane.
CVector3 lLocalX = lLocalX.Sub( lPoint1Position, lPoint2Position );
lLocalX.NormalizeInPlace();
// Vector A->C
CVector3 lVector13 = lVector13.Sub( lPoint1Position, lPoint3Position );
lVector13.NormalizeInPlace();
// Determine the Z vector of the local plane:.
CVector3 lLocalZ = lLocalZ.Cross( lLocalX, lVector13 );
lLocalZ.NormalizeInPlace();
// Determine Y vector of the local plane, assuming that it’s perpendicular to already known X and Z.
CVector3 lLocalY = lLocalY.Cross( lLocalZ, lLocalX );
lLocalY.NormalizeInPlace();
// In result we got three axis of the local plane:
// lLocalX.Dot( lLocalY ) == lLocalX.Dot( lLocalZ ) == lLocalY.Dot( lLocalZ ) == 0;
/*
Now use system of equations to determine the positions of all points in reference to the local plane.
double GetBase(const double& a1, const double& b1, const double& c1, const double& a2, const double& b2, const double& c2, const double& a3, const double& b3, const double& c3 )
{return (a1*b2*c3)+(a3*b1*c2)+(a2*b3*c1)-((a3*b2*c1)+(a1*b3*c2)+(a2*b1*c3));}
double GetX(const double& a1, const double& b1, const double& c1, const double& d1, const double& a2, const double& b2, const double& c2, const double& d2, const double& a3, const double& b3, const double& c3, const double& d3, const double& in_Base)
{return ( (d1*b2*c3)+(d2*b1*c2)+(d2*b3*c1)-((d3*b2*c1)+(d1*b3*c2)+(d2*b1*c3)) ) / in_Base;}
double GetY(const double& a1, const double& b1, const double& c1, const double& d1, const double& a2, const double& b2, const double& c2, const double& d2, const double& a3, const double& b3, const double& c3, const double& d3, const double& in_Base)
{return ( (a1*d2*c3)+(a3*d1*c2)+(a2*d3*c1)-((a3*d2*c1)+(a1*d3*c2)+(a2*b1*c3)) ) / in_Base;}
double GetZ(const double& a1, const double& b1, const double& c1, const double& d1, const double& a2, const double& b2, const double& c2, const double& d2, const double& a3, const double& b3, const double& c3, const double& d3, const double& in_Base)
{return ( (a1*b2*d3)+(a3*b1*d2)+(a2*b3*d1)-((a3*b2*d1)+(a1*b3*d2)+(a2*b1*d3)) ) / in_Base;}
*/
double fU;
double fV;
double fW;
double fBase;
// For each point, except the first one (lPoint1Position), which we aleady know.
for( long p=1; p < lPoints.GetCount(); p++ )
{
Point lPoint = lPoints[ p ];
CVector3 lPointPosition = lPoint.GetPosition();
CVector3 lPointVector = lPointVector.Sub( lPoint1Position, lPointPosition );
fBase = GetBase(
lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(),
lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(),
lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ() );
fU = GetX( lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(), lPointVector.GetX(),
lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(), lPointVector.GetY(),
lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ(), lPointVector.GetZ()
,fBase );
fV = GetY( lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(), lPointVector.GetX(),
lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(), lPointVector.GetY(),
lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ(), lPointVector.GetZ()
,fBase );
fW = GetZ( lLocalX.GetX(), lLocalY.GetX(), lLocalZ.GetX(), lPointVector.GetX(),
lLocalX.GetY(), lLocalY.GetY(), lLocalZ.GetY(), lPointVector.GetY(),
lLocalX.GetZ(), lLocalY.GetZ(), lLocalZ.GetZ(), lPointVector.GetZ()
,fBase );
_LogRoundValue3( p, fU, fV, fW );
}
// Result for a cube with the center in 0,0,0 (global coords) is:
// INFO : Values: 0, --------------------------------------
// INFO : 1 = 0, 0, 0
// INFO : 2 = 0, 1, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 1, --------------------------------------
// INFO : 1 = 1, 1, 0
// INFO : 2 = 1, 1, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 2, --------------------------------------
// INFO : 1 = 1, 0, 0
// INFO : 2 = 1, 1, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 3, --------------------------------------
// INFO : 1 = 1, 0, 0
// INFO : 2 = 1, 1, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 4, --------------------------------------
// INFO : 1 = 1, 0, 0
// INFO : 2 = 1, 1, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 5, --------------------------------------
// INFO : 1 = 0, 0, 0
// INFO : 2 = 0, 1, 0
// INFO : 3 = 0, 1, 0
// and for a rotated cube at the same place:
// INFO : Values: 0, --------------------------------------
// INFO : 1 = 0.027506, 0.033672, 0
// INFO : 2 = 0.487751, 0.969746, 0
// INFO : 3 = 0.460245, 1, 0
// INFO : Values: 1, --------------------------------------
// INFO : 1 = 1.15549, 0.753083, 0
// INFO : 2 = 1.03428, 1, 0
// INFO : 3 = -0.121205, 1, 0
// INFO : Values: 2, --------------------------------------
// INFO : 1 = 1, 0.04442, 0
// INFO : 2 = 1, 1.04442, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 3, --------------------------------------
// INFO : 1 = 1, -0.093859, 0
// INFO : 2 = 1, 0.906141, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 4, --------------------------------------
// INFO : 1 = 1, -0.04442, 0
// INFO : 2 = 1, 0.95558, 0
// INFO : 3 = 0, 1, 0
// INFO : Values: 5, --------------------------------------
// INFO : 1 = 0.027506, 0.09418, 0
// INFO : 2 = -0.43274, 1.03025, 0
// INFO : 3 = -0.460245, 1, 0
}
它只是每个多边形投影,我假设 - 并非总是如此,但这并不重要 - 所有多边形点都在同一个局部平面上。
我从确定局部平面开始,然后使用方程系统试图找到局部平面上点的坐标。然而结果是错误的。
该代码使用 XSI 的 API,矢量类文档在这里:
- 第一个问题是:这是一个正确的方法吗?
- 如果是,那它有什么问题?
…是的,我已经阅读了http://en.wikipedia.org/wiki/3D_projection :)
我会很感激任何建议。