Blender V2.61 - r43446

RAS_texmatrix.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "RAS_TexMatrix.h"
00034 
00035 void RAS_CalcTexMatrix(RAS_TexVert p[3],MT_Point3& origin,MT_Vector3& udir,MT_Vector3& vdir)
00036 {
00037 // precondition: 3 vertices are non-colinear
00038 
00039     MT_Vector3 vec1 = p[1].xyz()-p[0].xyz();
00040     MT_Vector3 vec2 = p[2].xyz()-p[0].xyz();
00041     MT_Vector3 normal = vec1.cross(vec2);
00042     normal.normalize();
00043 
00044     // determine which coordinate we drop, ie. max coordinate in the normal
00045     
00046 
00047     int ZCOORD = normal.closestAxis();
00048     int XCOORD = (ZCOORD+1)%3;
00049     int YCOORD = (ZCOORD+2)%3;
00050         
00051     // ax+by+cz+d=0
00052     MT_Scalar d = -p[0].xyz().dot(normal);
00053     
00054 
00055     MT_Matrix3x3 mat3(  p[0].getUV1()[0],p[0].getUV1()[1],  1,
00056                         p[1].getUV1()[0],p[1].getUV1()[1],  1,
00057                         p[2].getUV1()[0],p[2].getUV1()[1],  1);
00058 
00059 
00060     MT_Matrix3x3 mat3inv = mat3.inverse();
00061 
00062     MT_Vector3 p123x(p[0].xyz()[XCOORD],p[1].xyz()[XCOORD],p[2].xyz()[XCOORD]);
00063     MT_Vector3 resultx = mat3inv*p123x;
00064     MT_Vector3 p123y(p[0].xyz()[YCOORD],p[1].xyz()[YCOORD],p[2].xyz()[YCOORD]);
00065     MT_Vector3 resulty = mat3inv*p123y;
00066 
00067     // normal[ZCOORD] is not zero, because it's chosen to be maximal (absolute), and normal has length 1, 
00068     // so at least on of the coords is <> 0
00069 
00070     //droppedvalue udir.dot(normal) =0
00071     MT_Scalar droppedu = -(resultx.x()*normal[XCOORD]+resulty.x()*normal[YCOORD])/normal[ZCOORD];
00072     udir[XCOORD] = resultx.x();
00073     udir[YCOORD] = resulty.x();
00074     udir[ZCOORD] = droppedu;
00075     MT_Scalar droppedv = -(resultx.y()*normal[XCOORD]+resulty.y()*normal[YCOORD])/normal[ZCOORD];
00076     vdir[XCOORD] = resultx.y();
00077     vdir[YCOORD] = resulty.y();
00078     vdir[ZCOORD] = droppedv;
00079     // droppedvalue b = -(ax+cz+d)/y;
00080     MT_Scalar droppedvalue = -((resultx.z()*normal[XCOORD] + resulty.z()*normal[YCOORD]+d))/normal[ZCOORD];
00081     origin[XCOORD] = resultx.z();
00082     origin[YCOORD] = resulty.z();
00083     origin[ZCOORD] = droppedvalue;
00084     
00085 
00086 }
00087 
00088 #ifdef _TEXOWNMAIN
00089 
00090 int main()
00091 {
00092 
00093     MT_Point2 puv0={0,0};
00094     MT_Point3 pxyz0 (0,0,128);
00095 
00096     MT_Scalar puv1[2]={1,0};
00097     MT_Point3 pxyz1(128,0,128);
00098 
00099     MT_Scalar puv2[2]={1,1};
00100     MT_Point3 pxyz2(128,0,0);
00101 
00102     RAS_TexVert p0(pxyz0,puv0);
00103     RAS_TexVert p1(pxyz1,puv1);
00104     RAS_TexVert p2(pxyz2,puv2);
00105 
00106     RAS_TexVert vertices[3] = 
00107     {
00108         p0,
00109         p1,
00110         p2
00111     };
00112 
00113     MT_Vector3 udir,vdir;
00114     MT_Point3 origin;
00115     CalcTexMatrix(vertices,origin,udir,vdir);
00116 
00117     MT_Point3 testpoint(128,32,64);
00118 
00119     MT_Scalar lenu = udir.length2();
00120     MT_Scalar lenv = vdir.length2();
00121 
00122     MT_Scalar testu=((pxyz2-origin).dot(udir))/lenu;
00123     MT_Scalar testv=((pxyz2-origin).dot(vdir))/lenv;
00124 
00125 
00126 
00127 
00128     return 0;
00129 }
00130 
00131 #endif // _TEXOWNMAIN