00001
00002
00003 #include "RAS_TexMatrix.h"
00004
00005
00006 void RAS_CalcTexMatrix(RAS_TexVert p[3],MT_Point3& origin,MT_Vector3& udir,MT_Vector3& vdir)
00007 {
00008
00009
00010 MT_Vector3 vec1 = p[1].xyz()-p[0].xyz();
00011 MT_Vector3 vec2 = p[2].xyz()-p[0].xyz();
00012 MT_Vector3 normal = vec1.cross(vec2);
00013 normal.normalize();
00014
00015
00016
00017
00018 int ZCOORD = normal.closestAxis();
00019 int XCOORD = (ZCOORD+1)%3;
00020 int YCOORD = (ZCOORD+2)%3;
00021
00022
00023 MT_Scalar d = -p[0].xyz().dot(normal);
00024
00025
00026 MT_Matrix3x3 mat3( p[0].getUV1()[0],p[0].getUV1()[1], 1,
00027 p[1].getUV1()[0],p[1].getUV1()[1], 1,
00028 p[2].getUV1()[0],p[2].getUV1()[1], 1);
00029
00030
00031 MT_Matrix3x3 mat3inv = mat3.inverse();
00032
00033 MT_Vector3 p123x(p[0].xyz()[XCOORD],p[1].xyz()[XCOORD],p[2].xyz()[XCOORD]);
00034 MT_Vector3 resultx = mat3inv*p123x;
00035 MT_Vector3 p123y(p[0].xyz()[YCOORD],p[1].xyz()[YCOORD],p[2].xyz()[YCOORD]);
00036 MT_Vector3 resulty = mat3inv*p123y;
00037
00038
00039
00040
00041
00042 MT_Scalar droppedu = -(resultx.x()*normal[XCOORD]+resulty.x()*normal[YCOORD])/normal[ZCOORD];
00043 udir[XCOORD] = resultx.x();
00044 udir[YCOORD] = resulty.x();
00045 udir[ZCOORD] = droppedu;
00046 MT_Scalar droppedv = -(resultx.y()*normal[XCOORD]+resulty.y()*normal[YCOORD])/normal[ZCOORD];
00047 vdir[XCOORD] = resultx.y();
00048 vdir[YCOORD] = resulty.y();
00049 vdir[ZCOORD] = droppedv;
00050
00051 MT_Scalar droppedvalue = -((resultx.z()*normal[XCOORD] + resulty.z()*normal[YCOORD]+d))/normal[ZCOORD];
00052 origin[XCOORD] = resultx.z();
00053 origin[YCOORD] = resulty.z();
00054 origin[ZCOORD] = droppedvalue;
00055
00056
00057 }
00058
00059 #ifdef _TEXOWNMAIN
00060
00061 int main()
00062 {
00063
00064 MT_Point2 puv0={0,0};
00065 MT_Point3 pxyz0 (0,0,128);
00066
00067 MT_Scalar puv1[2]={1,0};
00068 MT_Point3 pxyz1(128,0,128);
00069
00070 MT_Scalar puv2[2]={1,1};
00071 MT_Point3 pxyz2(128,0,0);
00072
00073 RAS_TexVert p0(pxyz0,puv0);
00074 RAS_TexVert p1(pxyz1,puv1);
00075 RAS_TexVert p2(pxyz2,puv2);
00076
00077 RAS_TexVert vertices[3] =
00078 {
00079 p0,
00080 p1,
00081 p2
00082 };
00083
00084 MT_Vector3 udir,vdir;
00085 MT_Point3 origin;
00086 CalcTexMatrix(vertices,origin,udir,vdir);
00087
00088 MT_Point3 testpoint(128,32,64);
00089
00090 MT_Scalar lenu = udir.length2();
00091 MT_Scalar lenv = vdir.length2();
00092
00093 MT_Scalar testu=((pxyz2-origin).dot(udir))/lenu;
00094 MT_Scalar testv=((pxyz2-origin).dot(vdir))/lenv;
00095
00096
00097
00098
00099 return 0;
00100 }
00101
00102 #endif // _TEXOWNMAIN