# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

patchuv_locator.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/patchuv_locator.h"
00029 
00030 using namespace std;
00031 using namespace NLMISC;
00032 
00033 namespace NL3D 
00034 {
00035 
00036 
00037 // ***************************************************************************
00038 void    CPatchUVLocator::build(const CPatch *patchCenter, sint edgeCenter, CPatch::CBindInfo    &bindInfo)
00039 {
00040         nlassert(bindInfo.Zone);
00041         // copy basic. NB: NPatchs==0 means patchCenter is binded on a 1/X patch.
00042         _CenterPatch= const_cast<CPatch*>(patchCenter);
00043         _CenterPatchEdge= edgeCenter;
00044         _NPatchs= bindInfo.NPatchs;
00045 
00046 
00047         // set it to true. false-d if one of the neighbor patch does not have same number of tile.
00048         _SameEdgeOrder= true;
00049 
00050 
00051         // For all patchs binded to me.
00052         for(sint i=0; i<_NPatchs; i++)
00053         {
00054                 // The edge of the neihbor on which we are binded.
00055                 sint    edgeNeighbor= bindInfo.Edge[i];
00056                 CPatch  *paNeighbor= bindInfo.Next[i];
00057                 _NeighborPatch[i]= paNeighbor;
00058 
00059 
00060                 // Find uvI, uvJ, uvP such that: 
00061                 // uvOut= uvIn.x * uvI + uvIn.y * uvJ + uvP.
00062                 CVector2f               &uvI= _NeighborBasis[i].UvI;
00063                 CVector2f               &uvJ= _NeighborBasis[i].UvJ;
00064                 CVector2f               &uvP= _NeighborBasis[i].UvP;
00065 
00066 
00067                 // Find the basis MeToNeighbor.
00068                 //=============================
00069                 sint    rotation= (edgeCenter - edgeNeighbor + 4) & 3;
00070                 // Find scale to apply.
00071                 float   scX, scY;
00072                 // If our neighbor edge is a vertical edge
00073                 if( (edgeNeighbor&1)==0 )
00074                 {
00075                         scX= 1;
00076 
00077                         // Manage difference of Order at the edge.
00078                         scY= (float)paNeighbor->getOrderForEdge(edgeNeighbor) / (float)patchCenter->getOrderForEdge(edgeCenter);
00079                         // Manage bind on the edge.
00080                         // If patchCenter is binded on a bigger
00081                         if(bindInfo.MultipleBindNum!=0)
00082                                 scY/= bindInfo.MultipleBindNum;
00083                         if(_NPatchs>1)
00084                                 scY*= _NPatchs;
00085                         // same TileOrder on the edge??
00086                         if(scY!=1)
00087                                 _SameEdgeOrder= false;
00088                 }
00089                 else
00090                 {
00091                         scY= 1;
00092 
00093                         // Manage difference of Order at the edge.
00094                         scX= (float)paNeighbor->getOrderForEdge(edgeNeighbor) / (float)patchCenter->getOrderForEdge(edgeCenter);
00095                         // Manage bind on the edge.
00096                         // If patchCenter is binded on a bigger
00097                         if(bindInfo.MultipleBindNum!=0)
00098                                 scX/= bindInfo.MultipleBindNum;
00099                         if(_NPatchs>1)
00100                                 scX*= _NPatchs;
00101                         // same TileOrder on the edge??
00102                         if(scX!=1)
00103                                 _SameEdgeOrder= false;
00104                 }
00105                 // Find rotation to apply.
00106                 switch(rotation)
00107                 {
00108                 case 0: uvI.set(-scX, 0); uvJ.set(0, -scY);     break;
00109                 case 1: uvI.set(0, -scY); uvJ.set(scX, 0);      break;
00110                 case 2: uvI.set(scX, 0); uvJ.set(0, scY);       break;
00111                 case 3: uvI.set(0, scY); uvJ.set(-scX, 0);      break;
00112                 }
00113 
00114 
00115                 // Find the position.
00116                 //=============================
00117                 // Find the uv coord at start of the edge, for 2 patchs.
00118                 CVector2f               uvCenter;
00119                 CVector2f               uvNeighbor;
00120                 float   decal;
00121 
00122                 // find the uv at start of edgeCenter, + decal due to bind 1/X.
00123                 float   ocS= patchCenter->getOrderS();
00124                 float   ocT= patchCenter->getOrderT();
00125                 // Manage Bind 1/X.
00126                 if(_NPatchs>1)
00127                 {
00128                         // Move uvCenter, so it is near the position at start of edgeNeighbor.
00129                         decal= (float)i / _NPatchs;
00130                 }
00131                 else
00132                         decal= 0;
00133                 // Manage rotation.
00134                 switch(edgeCenter)
00135                 {
00136                 case 0: uvCenter.set(0, decal*ocT);             break;
00137                 case 1: uvCenter.set(decal*ocS, ocT);   break;
00138                 case 2: uvCenter.set(ocS, (1-decal)*ocT);       break;
00139                 case 3: uvCenter.set((1-decal)*ocS, 0); break;
00140                 };
00141 
00142                 // find the uv at start of edgeNeighbor, + decal due to bind X/1.
00143                 float   onS= paNeighbor->getOrderS();
00144                 float   onT= paNeighbor->getOrderT();
00145                 // Manage Bind X/1.
00146                 if(bindInfo.MultipleBindNum!=0)
00147                 {
00148                         // Must invert the id, because of mirror.... (make a draw).
00149                         sint    id= (bindInfo.MultipleBindNum-1) - bindInfo.MultipleBindId;
00150                         // Move uvNeighbor, so it is near the position at start of edgeCenter.
00151                         decal= (float)id / bindInfo.MultipleBindNum;
00152                 }
00153                 else
00154                         decal= 0;
00155                 // Manage rotation.
00156                 switch(edgeNeighbor)
00157                 {
00158                 case 0: uvNeighbor.set(0, (1-decal)*onT);               break;
00159                 case 1: uvNeighbor.set((1-decal)*onS, onT);     break;
00160                 case 2: uvNeighbor.set(onS, decal*onT);         break;
00161                 case 3: uvNeighbor.set(decal*onS, 0);           break;
00162                 };
00163 
00164 
00165 
00166                 // uvOut= uvIn.x * uvI + uvIn.y * uvJ + uvP.
00167                 // So uvP  = uvOut - uvIn.x * uvI - uvIn.y * uvJ
00168                 uvP= uvNeighbor - uvCenter.x * uvI - uvCenter.y * uvJ;
00169 
00170         }
00171 }
00172 
00173 
00174 // ***************************************************************************
00175 uint    CPatchUVLocator::selectPatch(const CVector2f &uvIn)
00176 {
00177         if(_NPatchs==1)
00178                 return 0;
00179         else
00180         {
00181                 // Choice before on which patch we must go.
00182                 float   selection=0.0;
00183                 uint    os= _CenterPatch->getOrderS();
00184                 uint    ot= _CenterPatch->getOrderT();
00185                 switch(_CenterPatchEdge)
00186                 {
00187                 case 0: selection= uvIn.y / ot; break;
00188                 case 1: selection= uvIn.x / os; break;
00189                 case 2: selection= (ot-uvIn.y) / ot; break;
00190                 case 3: selection= (os-uvIn.x) / os; break;
00191                 }
00192 
00193                 sint    sel= (sint)floor(selection*_NPatchs);
00194                 clamp(sel, 0, _NPatchs-1);
00195 
00196                 return sel;
00197         }
00198 }
00199 
00200 
00201 // ***************************************************************************
00202 void    CPatchUVLocator::locateUV(const CVector2f &uvIn, uint patch, CPatch *&patchOut, CVector2f &uvOut)
00203 {
00204         if(_NPatchs==1)
00205         {
00206                 // Change basis and select good patch.
00207                 _NeighborBasis[0].mulPoint(uvIn, uvOut);
00208                 patchOut= _NeighborPatch[0];
00209         }
00210         else
00211         {
00212                 // Change basis and select good patch.
00213                 _NeighborBasis[patch].mulPoint(uvIn, uvOut);
00214                 patchOut= _NeighborPatch[patch];
00215         }
00216 }
00217 
00218 
00219 
00220 } // NL3D