home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / brush_primit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  15.0 KB  |  441 lines

  1. #include "stdafx.h"
  2. #include "qe3.h"
  3.  
  4. // compute a determinant using Sarrus rule
  5. //++timo "inline" this with a macro
  6. // NOTE : the three vec3_t are understood as columns of the matrix
  7. vec_t SarrusDet(vec3_t a, vec3_t b, vec3_t c)
  8. {
  9.     return a[0]*b[1]*c[2]+b[0]*c[1]*a[2]+c[0]*a[1]*b[2]
  10.         -c[0]*b[1]*a[2]-a[1]*b[0]*c[2]-a[0]*b[2]*c[1];
  11. }
  12.  
  13. //++timo replace everywhere texX by texS etc. ( ----> and in q3map !) 
  14. // NOTE : ComputeAxisBase here and in q3map code must always BE THE SAME !
  15. // WARNING : special case behaviour of atan2(y,x) <-> atan(y/x) might not be the same everywhere when x == 0
  16. // rotation by (0,RotY,RotZ) assigns X to normal
  17. void ComputeAxisBase(vec3_t normal,vec3_t texS,vec3_t texT )
  18. {
  19.     vec_t RotY,RotZ;
  20.     // do some cleaning
  21.     if (fabs(normal[0])<1e-6)
  22.         normal[0]=0.0f;
  23.     if (fabs(normal[1])<1e-6)
  24.         normal[1]=0.0f;
  25.     if (fabs(normal[2])<1e-6)
  26.         normal[2]=0.0f;
  27.     RotY=-atan2(normal[2],sqrt(normal[1]*normal[1]+normal[0]*normal[0]));
  28.     RotZ=atan2(normal[1],normal[0]);
  29.     // rotate (0,1,0) and (0,0,1) to compute texS and texT
  30.     texS[0]=-sin(RotZ);
  31.     texS[1]=cos(RotZ);
  32.     texS[2]=0;
  33.     // the texT vector is along -Z ( T texture coorinates axis )
  34.     texT[0]=-sin(RotY)*cos(RotZ);
  35.     texT[1]=-sin(RotY)*sin(RotZ);
  36.     texT[2]=-cos(RotY);
  37. }
  38.  
  39. void FaceToBrushPrimitFace(face_t *f)
  40. {
  41.     vec3_t texX,texY;
  42.     vec3_t proj;
  43.     // ST of (0,0) (1,0) (0,1)
  44.     vec_t ST[3][5]; // [ point index ] [ xyz ST ]
  45.     //++timo not used as long as brushprimit_texdef and texdef are static
  46. /*    f->brushprimit_texdef.contents=f->texdef.contents;
  47.     f->brushprimit_texdef.flags=f->texdef.flags;
  48.     f->brushprimit_texdef.value=f->texdef.value;
  49.     strcpy(f->brushprimit_texdef.name,f->texdef.name); */
  50. #ifdef _DEBUG
  51.     if ( f->plane.normal[0]==0.0f && f->plane.normal[1]==0.0f && f->plane.normal[2]==0.0f )
  52.     {
  53.         Sys_Printf("Warning : f->plane.normal is (0,0,0) in FaceToBrushPrimitFace\n");
  54.     }
  55.     // check d_texture
  56.     if (!f->d_texture)
  57.     {
  58.         Sys_Printf("Warning : f.d_texture is NULL in FaceToBrushPrimitFace\n");
  59.         return;
  60.     }
  61. #endif
  62.     // compute axis base
  63.     ComputeAxisBase(f->plane.normal,texX,texY);
  64.     // compute projection vector
  65.     VectorCopy(f->plane.normal,proj);
  66.     VectorScale(proj,f->plane.dist,proj);
  67.     // (0,0) in plane axis base is (0,0,0) in world coordinates + projection on the affine plane
  68.     // (1,0) in plane axis base is texX in world coordinates + projection on the affine plane
  69.     // (0,1) in plane axis base is texY in world coordinates + projection on the affine plane
  70.     // use old texture code to compute the ST coords of these points
  71.     VectorCopy(proj,ST[0]);
  72.     EmitTextureCoordinates(ST[0], f->d_texture, f);
  73.     VectorCopy(texX,ST[1]);
  74.     VectorAdd(ST[1],proj,ST[1]);
  75.     EmitTextureCoordinates(ST[1], f->d_texture, f);
  76.     VectorCopy(texY,ST[2]);
  77.     VectorAdd(ST[2],proj,ST[2]);
  78.     EmitTextureCoordinates(ST[2], f->d_texture, f);
  79.     // compute texture matrix
  80.     f->brushprimit_texdef.coords[0][2]=ST[0][3];
  81.     f->brushprimit_texdef.coords[1][2]=ST[0][4];
  82.     f->brushprimit_texdef.coords[0][0]=ST[1][3]-f->brushprimit_texdef.coords[0][2];
  83.     f->brushprimit_texdef.coords[1][0]=ST[1][4]-f->brushprimit_texdef.coords[1][2];
  84.     f->brushprimit_texdef.coords[0][1]=ST[2][3]-f->brushprimit_texdef.coords[0][2];
  85.     f->brushprimit_texdef.coords[1][1]=ST[2][4]-f->brushprimit_texdef.coords[1][2];
  86. }
  87.  
  88. // compute texture coordinates for the winding points
  89. void EmitBrushPrimitTextureCoordinates(face_t * f, winding_t * w)
  90. {
  91.     vec3_t texX,texY;
  92.     vec_t x,y;
  93.     // compute axis base
  94.     ComputeAxisBase(f->plane.normal,texX,texY);
  95.     // in case the texcoords matrix is empty, build a default one
  96.     // same behaviour as if scale[0]==0 && scale[1]==0 in old code
  97.     if (f->brushprimit_texdef.coords[0][0]==0 && f->brushprimit_texdef.coords[1][0]==0 && f->brushprimit_texdef.coords[0][1]==0 && f->brushprimit_texdef.coords[1][1]==0)
  98.     {
  99.         f->brushprimit_texdef.coords[0][0] = 1.0f;
  100.         f->brushprimit_texdef.coords[1][1] = 1.0f;
  101.         ConvertTexMatWithQTexture( &f->brushprimit_texdef, NULL, &f->brushprimit_texdef, f->d_texture );
  102.     }
  103.     int i;
  104.     for (i=0 ; i<w->numpoints ; i++)
  105.     {
  106.         x=DotProduct(w->points[i],texX);
  107.         y=DotProduct(w->points[i],texY);
  108. #ifdef _DEBUG
  109.         if (g_qeglobals.bNeedConvert)
  110.         {
  111.             // check we compute the same ST as the traditional texture computation used before
  112.             vec_t S=f->brushprimit_texdef.coords[0][0]*x+f->brushprimit_texdef.coords[0][1]*y+f->brushprimit_texdef.coords[0][2];
  113.             vec_t T=f->brushprimit_texdef.coords[1][0]*x+f->brushprimit_texdef.coords[1][1]*y+f->brushprimit_texdef.coords[1][2];
  114.             if ( fabs(S-w->points[i][3])>1e-2 || fabs(T-w->points[i][4])>1e-2 )
  115.             {
  116.                 if ( fabs(S-w->points[i][3])>1e-4 || fabs(T-w->points[i][4])>1e-4 )
  117.                     Sys_Printf("Warning : precision loss in brush -> brush primitive texture computation\n");
  118.                 else
  119.                     Sys_Printf("Warning : brush -> brush primitive texture computation bug detected\n");
  120.             }
  121.         }
  122. #endif
  123.         w->points[i][3]=f->brushprimit_texdef.coords[0][0]*x+f->brushprimit_texdef.coords[0][1]*y+f->brushprimit_texdef.coords[0][2];
  124.         w->points[i][4]=f->brushprimit_texdef.coords[1][0]*x+f->brushprimit_texdef.coords[1][1]*y+f->brushprimit_texdef.coords[1][2];
  125.     }
  126. }
  127.  
  128. // parse a brush in brush primitive format
  129. void BrushPrimit_Parse(brush_t    *b)
  130. {
  131.     epair_t        *ep;
  132.     face_t        *f;
  133.     int            i,j;
  134.     GetToken (true);
  135.     if (strcmp (token, "{"))
  136.     {
  137.         Warning ("parsing brush primitive");
  138.         return;
  139.     }
  140.     do
  141.     {
  142.         if (!GetToken (true))
  143.             break;
  144.         if (!strcmp (token, "}") )
  145.             break;
  146.         // reading of b->epairs if any
  147.         if (strcmp (token, "(") )
  148.         {
  149.             ep = ParseEpair();
  150.             ep->next = b->epairs;
  151.             b->epairs = ep;
  152.         }
  153.         else
  154.         // it's a face
  155.         {
  156.             f = Face_Alloc();
  157.             f->next = NULL;
  158.             if (!b->brush_faces)
  159.                   b->brush_faces = f;
  160.               else
  161.             {
  162.                 face_t *scan;
  163.                 for (scan=b->brush_faces ; scan->next ; scan=scan->next)
  164.                     ;
  165.                 scan->next = f;
  166.               }
  167.  
  168.             // read the three point plane definition
  169.             for (i=0 ; i<3 ; i++)
  170.             {
  171.                 if (i != 0)
  172.                     GetToken (true);
  173.                 if (strcmp (token, "(") )
  174.                 {
  175.                     Warning ("parsing brush");
  176.                     return;
  177.                 }
  178.                 for (j=0 ; j<3 ; j++)
  179.                 {
  180.                     GetToken (false);
  181.                     f->planepts[i][j] = atof(token);
  182.                 }
  183.                 GetToken (false);
  184.                 if (strcmp (token, ")") )
  185.                 {
  186.                     Warning ("parsing brush");
  187.                     return;
  188.                 }
  189.             }
  190.             // texture coordinates
  191.             GetToken (false);
  192.             if (strcmp(token, "("))
  193.             {
  194.                 Warning ("parsing brush primitive");
  195.                 return;
  196.             }
  197.             GetToken (false);
  198.             if (strcmp(token, "("))
  199.             {
  200.                 Warning ("parsing brush primitive");
  201.                 return;
  202.             }
  203.             for (j=0;j<3;j++)
  204.             {
  205.                 GetToken(false);
  206.                 f->brushprimit_texdef.coords[0][j]=atof(token);
  207.             }
  208.             GetToken (false);
  209.             if (strcmp(token, ")"))
  210.             {
  211.                 Warning ("parsing brush primitive");
  212.                 return;
  213.             }
  214.             GetToken (false);
  215.             if (strcmp(token, "("))
  216.             {
  217.                 Warning ("parsing brush primitive");
  218.                 return;
  219.             }
  220.             for (j=0;j<3;j++)
  221.             {
  222.                 GetToken(false);
  223.                 f->brushprimit_texdef.coords[1][j]=atof(token);
  224.             }
  225.             GetToken (false);
  226.             if (strcmp(token, ")"))
  227.             {
  228.                 Warning ("parsing brush primitive");
  229.                 return;
  230.             }
  231.             GetToken (false);
  232.             if (strcmp(token, ")"))
  233.             {
  234.                 Warning ("parsing brush primitive");
  235.                 return;
  236.             }
  237.             // read the texturedef
  238.             GetToken (false);
  239.             //strcpy(f->texdef.name, token);
  240.             f->texdef.SetName(token);
  241.             if (TokenAvailable ())
  242.             {
  243.                 GetToken (false);
  244.                 f->texdef.contents = atoi(token);
  245.         GetToken (false);
  246.                 f->texdef.flags = atoi(token);
  247.                 GetToken (false);
  248.                 f->texdef.value = atoi(token);
  249.             }
  250.         }
  251.     } while (1);
  252. }
  253.  
  254. // compute a fake shift scale rot representation from the texture matrix
  255. // these shift scale rot values are to be understood in the local axis base
  256. void TexMatToFakeTexCoords( vec_t texMat[2][3], float shift[2], float *rot, float scale[2] )
  257. {
  258. #ifdef _DEBUG
  259.     // check this matrix is orthogonal
  260.     if (fabs(texMat[0][0]*texMat[0][1]+texMat[1][0]*texMat[1][1])>ZERO_EPSILON)
  261.         Sys_Printf("Warning : non orthogonal texture matrix in TexMatToFakeTexCoords\n");
  262. #endif
  263.     scale[0]=sqrt(texMat[0][0]*texMat[0][0]+texMat[1][0]*texMat[1][0]);
  264.     scale[1]=sqrt(texMat[0][1]*texMat[0][1]+texMat[1][1]*texMat[1][1]);
  265. #ifdef _DEBUG
  266.     if (scale[0]<ZERO_EPSILON || scale[1]<ZERO_EPSILON)
  267.         Sys_Printf("Warning : unexpected scale==0 in TexMatToFakeTexCoords\n");
  268. #endif
  269.     // compute rotate value
  270.     if (fabs(texMat[0][0])<ZERO_EPSILON)
  271.     {
  272. #ifdef _DEBUG
  273.         // check brushprimit_texdef[1][0] is not zero
  274.         if (fabs(texMat[1][0])<ZERO_EPSILON)
  275.             Sys_Printf("Warning : unexpected texdef[1][0]==0 in TexMatToFakeTexCoords\n");
  276. #endif
  277.         // rotate is +-90
  278.         if (texMat[1][0]>0)
  279.             *rot=90.0f;
  280.         else
  281.             *rot=-90.0f;
  282.     }
  283.     else
  284.     *rot = RAD2DEG( atan2( texMat[1][0], texMat[0][0] ) );
  285.     shift[0] = -texMat[0][2];
  286.     shift[1] = texMat[1][2];
  287. }
  288.  
  289. // compute back the texture matrix from fake shift scale rot
  290. // the matrix returned must be understood as a qtexture_t with width=2 height=2 ( the default one )
  291. void FakeTexCoordsToTexMat( float shift[2], float rot, float scale[2], vec_t texMat[2][3] )
  292. {
  293.     texMat[0][0] = scale[0] * cos( DEG2RAD( rot ) );
  294.     texMat[1][0] = scale[0] * sin( DEG2RAD( rot ) );
  295.     texMat[0][1] = -1.0f * scale[1] * sin( DEG2RAD( rot ) );
  296.     texMat[1][1] = scale[1] * cos( DEG2RAD( rot ) );
  297.     texMat[0][2] = -shift[0];
  298.     texMat[1][2] = shift[1];
  299. }
  300.  
  301. // convert a texture matrix between two qtexture_t
  302. // if NULL for qtexture_t, basic 2x2 texture is assumed ( straight mapping between s/t coordinates and geometric coordinates )
  303. void ConvertTexMatWithQTexture( brushprimit_texdef_t *texMat1, qtexture_t *qtex1, brushprimit_texdef_t *texMat2, qtexture_t *qtex2 )
  304. {
  305.     float s1,s2;
  306.     s1 = ( qtex1 ? static_cast<float>( qtex1->width ) : 2.0f ) / ( qtex2 ? static_cast<float>( qtex2->width ) : 2.0f );
  307.     s2 = ( qtex1 ? static_cast<float>( qtex1->height ) : 2.0f ) / ( qtex2 ? static_cast<float>( qtex2->height ) : 2.0f );
  308.     texMat2->coords[0][0]=s1*texMat1->coords[0][0];
  309.     texMat2->coords[0][1]=s1*texMat1->coords[0][1];
  310.     texMat2->coords[0][2]=s1*texMat1->coords[0][2];
  311.     texMat2->coords[1][0]=s2*texMat1->coords[1][0];
  312.     texMat2->coords[1][1]=s2*texMat1->coords[1][1];
  313.     texMat2->coords[1][2]=s2*texMat1->coords[1][2];
  314. }
  315.  
  316. // texture locking
  317. void Face_MoveTexture_BrushPrimit(face_t *f, vec3_t delta)
  318. {
  319.     vec3_t texS,texT;
  320.     vec_t tx,ty;
  321.     vec3_t M[3]; // columns of the matrix .. easier that way
  322.     vec_t det;
  323.     vec3_t D[2];
  324.     // compute plane axis base ( doesn't change with translation )
  325.     ComputeAxisBase( f->plane.normal, texS, texT );
  326.     // compute translation vector in plane axis base
  327.     tx = DotProduct( delta, texS );
  328.     ty = DotProduct( delta, texT );
  329.     // fill the data vectors
  330.     M[0][0]=tx; M[0][1]=1.0f+tx; M[0][2]=tx;
  331.     M[1][0]=ty; M[1][1]=ty; M[1][2]=1.0f+ty;
  332.     M[2][0]=1.0f; M[2][1]=1.0f; M[2][2]=1.0f;
  333.     D[0][0]=f->brushprimit_texdef.coords[0][2];
  334.     D[0][1]=f->brushprimit_texdef.coords[0][0]+f->brushprimit_texdef.coords[0][2];
  335.     D[0][2]=f->brushprimit_texdef.coords[0][1]+f->brushprimit_texdef.coords[0][2];
  336.     D[1][0]=f->brushprimit_texdef.coords[1][2];
  337.     D[1][1]=f->brushprimit_texdef.coords[1][0]+f->brushprimit_texdef.coords[1][2];
  338.     D[1][2]=f->brushprimit_texdef.coords[1][1]+f->brushprimit_texdef.coords[1][2];
  339.     // solve
  340.     det = SarrusDet( M[0], M[1], M[2] );
  341.     f->brushprimit_texdef.coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
  342.     f->brushprimit_texdef.coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
  343.     f->brushprimit_texdef.coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
  344.     f->brushprimit_texdef.coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
  345.     f->brushprimit_texdef.coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
  346.     f->brushprimit_texdef.coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
  347. }
  348.  
  349. // call Face_MoveTexture_BrushPrimit after vec3_t computation
  350. void Select_ShiftTexture_BrushPrimit( face_t *f, int x, int y )
  351. {
  352.     vec3_t texS,texT;
  353.     vec3_t delta;
  354.     ComputeAxisBase( f->plane.normal, texS, texT );
  355.     VectorScale( texS, static_cast<float>(x), texS );
  356.     VectorScale( texT, static_cast<float>(y), texT );
  357.     VectorCopy( texS, delta );
  358.     VectorAdd( delta, texT, delta );
  359.     Face_MoveTexture_BrushPrimit( f, delta );
  360. }
  361.  
  362. // texture locking
  363. // called before the points on the face are actually rotated
  364. void RotateFaceTexture_BrushPrimit(face_t *f, int nAxis, float fDeg, vec3_t vOrigin )
  365. {
  366.     vec3_t texS,texT;            // axis base of the initial plane
  367.     vec3_t vRotate;                // rotation vector
  368.     vec3_t Orig;
  369.     vec3_t rOrig,rvecS,rvecT;    // (0,0) (1,0) (0,1) ( initial plane axis base ) after rotation ( world axis base )
  370.     vec3_t rNormal;                // normal of the plane after rotation
  371.     vec3_t rtexS,rtexT;            // axis base of the rotated plane
  372.     vec3_t lOrig,lvecS,lvecT;    // [2] are not used ( but usefull for debugging )
  373.     vec3_t M[3];
  374.     vec_t det;
  375.     vec3_t D[2];
  376.     // compute plane axis base
  377.     ComputeAxisBase( f->plane.normal, texS, texT );
  378.     // compute coordinates of (0,0) (1,0) (0,1) ( initial plane axis base ) after rotation
  379.     // (0,0) (1,0) (0,1) ( initial plane axis base ) <-> (0,0,0) texS texT ( world axis base )
  380.     // rotation vector
  381.     VectorSet( vRotate, 0.0f, 0.0f, 0.0f );
  382.     vRotate[nAxis]=fDeg;
  383.     VectorSet( Orig, 0.0f, 0.0f, 0.0f );
  384.     VectorRotate( Orig, vRotate, vOrigin, rOrig );
  385.     VectorRotate( texS, vRotate, vOrigin, rvecS );
  386.     VectorRotate( texT, vRotate, vOrigin, rvecT );
  387.     // compute normal of plane after rotation
  388.     VectorRotate( f->plane.normal, vRotate, rNormal );
  389.     // compute rotated plane axis base
  390.     ComputeAxisBase( rNormal, rtexS, rtexT );
  391.     // compute S/T coordinates of the three points in rotated axis base ( in M matrix )
  392.     lOrig[0] = DotProduct( rOrig, rtexS );
  393.     lOrig[1] = DotProduct( rOrig, rtexT );
  394.     lvecS[0] = DotProduct( rvecS, rtexS );
  395.     lvecS[1] = DotProduct( rvecS, rtexT );
  396.     lvecT[0] = DotProduct( rvecT, rtexS );
  397.     lvecT[1] = DotProduct( rvecT, rtexT );
  398.     M[0][0] = lOrig[0]; M[1][0] = lOrig[1]; M[2][0] = 1.0f;
  399.     M[0][1] = lvecS[0]; M[1][1] = lvecS[1]; M[2][1] = 1.0f;
  400.     M[0][2] = lvecT[0]; M[1][2] = lvecT[1]; M[2][2] = 1.0f;
  401.     // fill data vector
  402.     D[0][0]=f->brushprimit_texdef.coords[0][2];
  403.     D[0][1]=f->brushprimit_texdef.coords[0][0]+f->brushprimit_texdef.coords[0][2];
  404.     D[0][2]=f->brushprimit_texdef.coords[0][1]+f->brushprimit_texdef.coords[0][2];
  405.     D[1][0]=f->brushprimit_texdef.coords[1][2];
  406.     D[1][1]=f->brushprimit_texdef.coords[1][0]+f->brushprimit_texdef.coords[1][2];
  407.     D[1][2]=f->brushprimit_texdef.coords[1][1]+f->brushprimit_texdef.coords[1][2];
  408.     // solve
  409.     det = SarrusDet( M[0], M[1], M[2] );
  410.     f->brushprimit_texdef.coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
  411.     f->brushprimit_texdef.coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
  412.     f->brushprimit_texdef.coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
  413.     f->brushprimit_texdef.coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
  414.     f->brushprimit_texdef.coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
  415.     f->brushprimit_texdef.coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
  416. }
  417.  
  418. // best fitted 2D vector is x.X+y.Y
  419. void ComputeBest2DVector( vec3_t v, vec3_t X, vec3_t Y, int &x, int &y )
  420. {
  421.     double sx,sy;
  422.     sx = DotProduct( v, X );
  423.     sy = DotProduct( v, Y );
  424.     if ( fabs(sy) > fabs(sx) )
  425.     {
  426.         x = 0;
  427.         if ( sy > 0.0 )
  428.             y =  1;
  429.         else
  430.             y = -1;
  431.     }
  432.     else
  433.     {
  434.         y = 0;
  435.         if ( sx > 0.0 )
  436.             x =  1;
  437.         else
  438.             x = -1;
  439.     }
  440. }
  441.