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