2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 // compute a determinant using Sarrus rule
25 //++timo "inline" this with a macro
26 // NOTE : the three vec3_t are understood as columns of the matrix
27 vec_t SarrusDet( vec3_t a, vec3_t b, vec3_t c ){
28 return a[0] * b[1] * c[2] + b[0] * c[1] * a[2] + c[0] * a[1] * b[2]
29 - c[0] * b[1] * a[2] - a[1] * b[0] * c[2] - a[0] * b[2] * c[1];
32 // in many case we know three points A,B,C in two axis base B1 and B2
33 // and we want the matrix M so that A(B1) = T * A(B2)
34 // NOTE: 2D homogeneous space stuff
35 // NOTE: we don't do any check to see if there's a solution or we have a particular case .. need to make sure before calling
36 // NOTE: the third coord of the A,B,C point is ignored
37 // NOTE: see the commented out section to fill M and D
38 //++timo TODO: update the other members to use this when possible
39 void MatrixForPoints( vec3_t M[3], vec3_t D[2], brushprimit_texdef_t *T ){
40 // vec3_t M[3]; // columns of the matrix .. easier that way (the indexing is not standard! it's column-line .. later computations are easier that way)
43 M[2][0] = 1.0f; M[2][1] = 1.0f; M[2][2] = 1.0f;
45 // fill the data vectors
46 M[0][0] = A2[0]; M[0][1] = B2[0]; M[0][2] = C2[0];
47 M[1][0] = A2[1]; M[1][1] = B2[1]; M[1][2] = C2[1];
48 M[2][0] = 1.0f; M[2][1] = 1.0f; M[2][2] = 1.0f;
57 det = SarrusDet( M[0], M[1], M[2] );
58 T->coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
59 T->coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
60 T->coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
61 T->coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
62 T->coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
63 T->coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
66 //++timo replace everywhere texX by texS etc. ( ----> and in q3map !)
67 // NOTE : ComputeAxisBase here and in q3map code must always BE THE SAME !
68 // WARNING : special case behaviour of atan2(y,x) <-> atan(y/x) might not be the same everywhere when x == 0
69 // rotation by (0,RotY,RotZ) assigns X to normal
70 void ComputeAxisBase( vec3_t normal,vec3_t texS,vec3_t texT ){
73 if ( fabs( normal[0] ) < 1e-6 ) {
76 if ( fabs( normal[1] ) < 1e-6 ) {
79 if ( fabs( normal[2] ) < 1e-6 ) {
82 RotY = -atan2( normal[2],sqrt( normal[1] * normal[1] + normal[0] * normal[0] ) );
83 RotZ = atan2( normal[1],normal[0] );
84 // rotate (0,1,0) and (0,0,1) to compute texS and texT
85 texS[0] = -sin( RotZ );
86 texS[1] = cos( RotZ );
88 // the texT vector is along -Z ( T texture coorinates axis )
89 texT[0] = -sin( RotY ) * cos( RotZ );
90 texT[1] = -sin( RotY ) * sin( RotZ );
91 texT[2] = -cos( RotY );
94 void FaceToBrushPrimitFace( face_t *f ){
97 // ST of (0,0) (1,0) (0,1)
98 vec_t ST[3][5]; // [ point index ] [ xyz ST ]
99 //++timo not used as long as brushprimit_texdef and texdef are static
100 /* f->brushprimit_texdef.contents=f->texdef.contents;
101 f->brushprimit_texdef.flags=f->texdef.flags;
102 f->brushprimit_texdef.value=f->texdef.value;
103 strcpy(f->brushprimit_texdef.name,f->texdef.name); */
105 if ( f->plane.normal[0] == 0.0f && f->plane.normal[1] == 0.0f && f->plane.normal[2] == 0.0f ) {
106 Sys_Printf( "Warning : f->plane.normal is (0,0,0) in FaceToBrushPrimitFace\n" );
109 if ( !f->d_texture ) {
110 Sys_Printf( "Warning : f.d_texture is NULL in FaceToBrushPrimitFace\n" );
115 ComputeAxisBase( f->plane.normal,texX,texY );
116 // compute projection vector
117 VectorCopy( f->plane.normal,proj );
118 VectorScale( proj,f->plane.dist,proj );
119 // (0,0) in plane axis base is (0,0,0) in world coordinates + projection on the affine plane
120 // (1,0) in plane axis base is texX in world coordinates + projection on the affine plane
121 // (0,1) in plane axis base is texY in world coordinates + projection on the affine plane
122 // use old texture code to compute the ST coords of these points
123 VectorCopy( proj,ST[0] );
124 EmitTextureCoordinates( ST[0], f->d_texture, f );
125 VectorCopy( texX,ST[1] );
126 VectorAdd( ST[1],proj,ST[1] );
127 EmitTextureCoordinates( ST[1], f->d_texture, f );
128 VectorCopy( texY,ST[2] );
129 VectorAdd( ST[2],proj,ST[2] );
130 EmitTextureCoordinates( ST[2], f->d_texture, f );
131 // compute texture matrix
132 f->brushprimit_texdef.coords[0][2] = ST[0][3];
133 f->brushprimit_texdef.coords[1][2] = ST[0][4];
134 f->brushprimit_texdef.coords[0][0] = ST[1][3] - f->brushprimit_texdef.coords[0][2];
135 f->brushprimit_texdef.coords[1][0] = ST[1][4] - f->brushprimit_texdef.coords[1][2];
136 f->brushprimit_texdef.coords[0][1] = ST[2][3] - f->brushprimit_texdef.coords[0][2];
137 f->brushprimit_texdef.coords[1][1] = ST[2][4] - f->brushprimit_texdef.coords[1][2];
140 // compute texture coordinates for the winding points
141 void EmitBrushPrimitTextureCoordinates( face_t * f, winding_t * w ){
145 ComputeAxisBase( f->plane.normal,texX,texY );
146 // in case the texcoords matrix is empty, build a default one
147 // same behaviour as if scale[0]==0 && scale[1]==0 in old code
148 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 ) {
149 f->brushprimit_texdef.coords[0][0] = 1.0f;
150 f->brushprimit_texdef.coords[1][1] = 1.0f;
151 ConvertTexMatWithQTexture( &f->brushprimit_texdef, NULL, &f->brushprimit_texdef, f->d_texture );
154 for ( i = 0 ; i < w->numpoints ; i++ )
156 x = DotProduct( w->points[i],texX );
157 y = DotProduct( w->points[i],texY );
159 if ( g_qeglobals.bNeedConvert ) {
160 // check we compute the same ST as the traditional texture computation used before
161 vec_t S = f->brushprimit_texdef.coords[0][0] * x + f->brushprimit_texdef.coords[0][1] * y + f->brushprimit_texdef.coords[0][2];
162 vec_t T = f->brushprimit_texdef.coords[1][0] * x + f->brushprimit_texdef.coords[1][1] * y + f->brushprimit_texdef.coords[1][2];
163 if ( fabs( S - w->points[i][3] ) > 1e-2 || fabs( T - w->points[i][4] ) > 1e-2 ) {
164 if ( fabs( S - w->points[i][3] ) > 1e-4 || fabs( T - w->points[i][4] ) > 1e-4 ) {
165 Sys_Printf( "Warning : precision loss in brush -> brush primitive texture computation\n" );
168 Sys_Printf( "Warning : brush -> brush primitive texture computation bug detected\n" );
173 w->points[i][3] = f->brushprimit_texdef.coords[0][0] * x + f->brushprimit_texdef.coords[0][1] * y + f->brushprimit_texdef.coords[0][2];
174 w->points[i][4] = f->brushprimit_texdef.coords[1][0] * x + f->brushprimit_texdef.coords[1][1] * y + f->brushprimit_texdef.coords[1][2];
178 // compute a fake shift scale rot representation from the texture matrix
179 // these shift scale rot values are to be understood in the local axis base
180 void TexMatToFakeTexCoords( vec_t texMat[2][3], float shift[2], float *rot, float scale[2] ){
182 // check this matrix is orthogonal
183 if ( fabs( texMat[0][0] * 1.0L * texMat[0][1] + texMat[1][0] * 1.0L * texMat[1][1] ) > ZERO_EPSILON ) {
184 Sys_Printf( "Warning : non orthogonal texture matrix in TexMatToFakeTexCoords\n" );
187 scale[0] = sqrt( texMat[0][0] * 1.0L * texMat[0][0] + texMat[1][0] * 1.0L * texMat[1][0] );
188 scale[1] = sqrt( texMat[0][1] * 1.0L * texMat[0][1] + texMat[1][1] * 1.0L * texMat[1][1] );
190 if ( scale[0] < ZERO_EPSILON || scale[1] < ZERO_EPSILON ) {
191 Sys_Printf( "Warning : unexpected scale==0 in TexMatToFakeTexCoords\n" );
194 // compute rotate value
195 if ( fabs( texMat[0][0] ) < ZERO_EPSILON ) {
197 // check brushprimit_texdef[1][0] is not zero
198 if ( fabs( texMat[1][0] ) < ZERO_EPSILON ) {
199 Sys_Printf( "Warning : unexpected texdef[1][0]==0 in TexMatToFakeTexCoords\n" );
203 if ( texMat[1][0] > 0 ) {
211 *rot = RAD2DEG( atan2( texMat[1][0] * 1.0L, texMat[0][0] * 1.0L ) );
213 shift[0] = -texMat[0][2];
214 shift[1] = texMat[1][2];
217 // compute back the texture matrix from fake shift scale rot
218 // the matrix returned must be understood as a qtexture_t with width=2 height=2 ( the default one )
219 void FakeTexCoordsToTexMat( float shift[2], float rot, float scale[2], vec_t texMat[2][3] ){
220 texMat[0][0] = scale[0] * 1.0L * cos( DEG2RAD( 1.0L * rot ) );
221 texMat[1][0] = scale[0] * 1.0L * sin( DEG2RAD( 1.0L * rot ) );
222 texMat[0][1] = -scale[1] * 1.0L * sin( DEG2RAD( 1.0L * rot ) );
223 texMat[1][1] = scale[1] * 1.0L * cos( DEG2RAD( 1.0L * rot ) );
224 texMat[0][2] = -shift[0];
225 texMat[1][2] = shift[1];
228 // convert a texture matrix between two qtexture_t
229 // if NULL for qtexture_t, basic 2x2 texture is assumed ( straight mapping between s/t coordinates and geometric coordinates )
230 void ConvertTexMatWithQTexture( vec_t texMat1[2][3], qtexture_t *qtex1, vec_t texMat2[2][3], qtexture_t *qtex2 ){
232 s1 = ( qtex1 ? static_cast<float>( qtex1->width ) : 2.0f ) / ( qtex2 ? static_cast<float>( qtex2->width ) : 2.0f );
233 s2 = ( qtex1 ? static_cast<float>( qtex1->height ) : 2.0f ) / ( qtex2 ? static_cast<float>( qtex2->height ) : 2.0f );
234 texMat2[0][0] = s1 * texMat1[0][0];
235 texMat2[0][1] = s1 * texMat1[0][1];
236 texMat2[0][2] = s1 * texMat1[0][2];
237 texMat2[1][0] = s2 * texMat1[1][0];
238 texMat2[1][1] = s2 * texMat1[1][1];
239 texMat2[1][2] = s2 * texMat1[1][2];
242 void ConvertTexMatWithQTexture( brushprimit_texdef_t *texMat1, qtexture_t *qtex1, brushprimit_texdef_t *texMat2, qtexture_t *qtex2 ){
243 ConvertTexMatWithQTexture( texMat1->coords, qtex1, texMat2->coords, qtex2 );
246 // used for texture locking
247 // will move the texture according to a geometric vector
248 void ShiftTextureGeometric_BrushPrimit( face_t *f, vec3_t delta ){
251 vec3_t M[3]; // columns of the matrix .. easier that way
254 // compute plane axis base ( doesn't change with translation )
255 ComputeAxisBase( f->plane.normal, texS, texT );
256 // compute translation vector in plane axis base
257 tx = DotProduct( delta, texS );
258 ty = DotProduct( delta, texT );
259 // fill the data vectors
260 M[0][0] = tx; M[0][1] = 1.0f + tx; M[0][2] = tx;
261 M[1][0] = ty; M[1][1] = ty; M[1][2] = 1.0f + ty;
262 M[2][0] = 1.0f; M[2][1] = 1.0f; M[2][2] = 1.0f;
263 D[0][0] = f->brushprimit_texdef.coords[0][2];
264 D[0][1] = f->brushprimit_texdef.coords[0][0] + f->brushprimit_texdef.coords[0][2];
265 D[0][2] = f->brushprimit_texdef.coords[0][1] + f->brushprimit_texdef.coords[0][2];
266 D[1][0] = f->brushprimit_texdef.coords[1][2];
267 D[1][1] = f->brushprimit_texdef.coords[1][0] + f->brushprimit_texdef.coords[1][2];
268 D[1][2] = f->brushprimit_texdef.coords[1][1] + f->brushprimit_texdef.coords[1][2];
270 det = SarrusDet( M[0], M[1], M[2] );
271 f->brushprimit_texdef.coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
272 f->brushprimit_texdef.coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
273 f->brushprimit_texdef.coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
274 f->brushprimit_texdef.coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
275 f->brushprimit_texdef.coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
276 f->brushprimit_texdef.coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
279 // shift a texture (texture adjustments) along it's current texture axes
280 // x and y are geometric values, which we must compute as ST increments
281 // this depends on the texture size and the pixel/texel ratio
282 void ShiftTextureRelative_BrushPrimit( face_t *f, float x, float y ){
284 // as a ratio against texture size
285 // the scale of the texture is not relevant here (we work directly on a transformation from the base vectors)
286 s = ( x * 2.0 ) / (float)f->d_texture->width;
287 t = ( y * 2.0 ) / (float)f->d_texture->height;
288 f->brushprimit_texdef.coords[0][2] -= s;
289 f->brushprimit_texdef.coords[1][2] -= t;
292 // TTimo: FIXME: I don't like that, it feels broken
293 // (and it's likely that it's not used anymore)
294 // best fitted 2D vector is x.X+y.Y
295 void ComputeBest2DVector( vec3_t v, vec3_t X, vec3_t Y, int &x, int &y ){
297 sx = DotProduct( v, X );
298 sy = DotProduct( v, Y );
299 if ( fabs( sy ) > fabs( sx ) ) {
320 //++timo FIXME quick'n dirty hack, doesn't care about current texture settings (angle)
321 // can be improved .. bug #107311
322 // mins and maxs are the face bounding box
323 //++timo fixme: we use the face info, mins and maxs are irrelevant
324 void Face_FitTexture_BrushPrimit( face_t *f, vec3_t mins, vec3_t maxs, int nHeight, int nWidth ){
325 vec3_t BBoxSTMin, BBoxSTMax;
330 // vec3_t N[2],Mf[2];
331 brushprimit_texdef_t N;
335 // we'll be working on a standardized texture size
336 // ConvertTexMatWithQTexture( &f->brushprimit_texdef, f->d_texture, &f->brushprimit_texdef, NULL );
337 // compute the BBox in ST coords
338 EmitBrushPrimitTextureCoordinates( f, f->face_winding );
339 ClearBounds( BBoxSTMin, BBoxSTMax );
341 for ( i = 0 ; i < w->numpoints ; i++ )
343 // AddPointToBounds in 2D on (S,T) coordinates
344 for ( j = 0 ; j < 2 ; j++ )
346 val = w->points[i][j + 3];
347 if ( val < BBoxSTMin[j] ) {
350 if ( val > BBoxSTMax[j] ) {
355 // we have the three points of the BBox (BBoxSTMin[0].BBoxSTMin[1]) (BBoxSTMax[0],BBoxSTMin[1]) (BBoxSTMin[0],BBoxSTMax[1]) in ST space
356 // the BP matrix we are looking for gives (0,0) (nwidth,0) (0,nHeight) coordinates in (Sfit,Tfit) space to these three points
357 // we have A(Sfit,Tfit) = (0,0) = Mf * A(TexS,TexT) = N * M * A(TexS,TexT) = N * A(S,T)
358 // so we solve the system for N and then Mf = N * M
359 M[0][0] = BBoxSTMin[0]; M[0][1] = BBoxSTMax[0]; M[0][2] = BBoxSTMin[0];
360 M[1][0] = BBoxSTMin[1]; M[1][1] = BBoxSTMin[1]; M[1][2] = BBoxSTMax[1];
361 D[0][0] = 0.0f; D[0][1] = nWidth; D[0][2] = 0.0f;
362 D[1][0] = 0.0f; D[1][1] = 0.0f; D[1][2] = nHeight;
363 MatrixForPoints( M, D, &N );
366 // FIT operation gives coordinates of three points of the bounding box in (S',T'), our target axis base
367 // A(S',T')=(0,0) B(S',T')=(nWidth,0) C(S',T')=(0,nHeight)
368 // and we have them in (S,T) axis base: A(S,T)=(BBoxSTMin[0],BBoxSTMin[1]) B(S,T)=(BBoxSTMax[0],BBoxSTMin[1]) C(S,T)=(BBoxSTMin[0],BBoxSTMax[1])
369 // we compute the N transformation so that: A(S',T') = N * A(S,T)
370 VectorSet( N[0], ( BBoxSTMax[0] - BBoxSTMin[0] ) / (float)nWidth, 0.0f, BBoxSTMin[0] );
371 VectorSet( N[1], 0.0f, ( BBoxSTMax[1] - BBoxSTMin[1] ) / (float)nHeight, BBoxSTMin[1] );
374 // the final matrix is the product (Mf stands for Mfit)
375 Mf[0][0] = N.coords[0][0] * f->brushprimit_texdef.coords[0][0] + N.coords[0][1] * f->brushprimit_texdef.coords[1][0];
376 Mf[0][1] = N.coords[0][0] * f->brushprimit_texdef.coords[0][1] + N.coords[0][1] * f->brushprimit_texdef.coords[1][1];
377 Mf[0][2] = N.coords[0][0] * f->brushprimit_texdef.coords[0][2] + N.coords[0][1] * f->brushprimit_texdef.coords[1][2] + N.coords[0][2];
378 Mf[1][0] = N.coords[1][0] * f->brushprimit_texdef.coords[0][0] + N.coords[1][1] * f->brushprimit_texdef.coords[1][0];
379 Mf[1][1] = N.coords[1][0] * f->brushprimit_texdef.coords[0][1] + N.coords[1][1] * f->brushprimit_texdef.coords[1][1];
380 Mf[1][2] = N.coords[1][0] * f->brushprimit_texdef.coords[0][2] + N.coords[1][1] * f->brushprimit_texdef.coords[1][2] + N.coords[1][2];
382 VectorCopy( Mf[0], f->brushprimit_texdef.coords[0] );
383 VectorCopy( Mf[1], f->brushprimit_texdef.coords[1] );
384 // handle the texture size
385 // ConvertTexMatWithQTexture( &f->brushprimit_texdef, NULL, &f->brushprimit_texdef, f->d_texture );
388 void BrushPrimitFaceToFace( face_t *f ){
390 // we have parsed brush primitives and need conversion back to standard format
391 // NOTE: converting back is a quick hack, there's some information lost and we can't do anything about it
392 // FIXME: if we normalize the texture matrix to a standard 2x2 size, we end up with wrong scaling
393 // I tried various tweaks, no luck .. seems shifting is lost
394 brushprimit_texdef_t aux;
395 ConvertTexMatWithQTexture( &face->brushprimit_texdef, face->d_texture, &aux, NULL );
396 TexMatToFakeTexCoords( aux.coords, face->texdef.shift, &face->texdef.rotate, face->texdef.scale );
397 face->texdef.scale[0] /= 2.0;
398 face->texdef.scale[1] /= 2.0;
400 // new method by divVerent@alientrap.org: Shift and scale no longer get lost when opening a BP map in texdef mode.
405 ComputeAxisBase( f->plane.normal,texX,texY );
406 VectorCopy( f->plane.normal,proj );
407 VectorScale( proj,f->plane.dist,proj );
408 VectorCopy( proj,ST[0] );
409 VectorCopy( texX,ST[1] );
410 VectorAdd( ST[1],proj,ST[1] );
411 VectorCopy( texY,ST[2] );
412 VectorAdd( ST[2],proj,ST[2] );
414 ST[0][3] = f->brushprimit_texdef.coords[0][2];
415 ST[0][4] = f->brushprimit_texdef.coords[1][2];
416 ST[1][3] = f->brushprimit_texdef.coords[0][0] + ST[0][3];
417 ST[1][4] = f->brushprimit_texdef.coords[1][0] + ST[0][4];
418 ST[2][3] = f->brushprimit_texdef.coords[0][1] + ST[0][3];
419 ST[2][4] = f->brushprimit_texdef.coords[1][1] + ST[0][4];
421 Face_TexdefFromTextureCoordinates( ST[0], ST[1], ST[2], f->d_texture, f );
425 // TEXTURE LOCKING -----------------------------------------------------------------------------------------------------
426 // (Relevant to the editor only?)
428 // internally used for texture locking on rotation and flipping
429 // the general algorithm is the same for both lockings, it's only the geometric transformation part that changes
430 // so I wanted to keep it in a single function
431 // if there are more linear transformations that need the locking, going to a C++ or code pointer solution would be best
432 // (but right now I want to keep brush_primit.cpp striclty C)
434 qboolean txlock_bRotation;
436 // rotation locking params
441 // flip locking params
442 vec3_t txl_matrix[3];
445 void TextureLockTransformation_BrushPrimit( face_t *f ){
446 vec3_t Orig,texS,texT; // axis base of initial plane
447 // used by transformation algo
449 vec3_t vRotate; // rotation vector
451 vec3_t rOrig,rvecS,rvecT; // geometric transformation of (0,0) (1,0) (0,1) { initial plane axis base }
452 vec3_t rNormal,rtexS,rtexT; // axis base for the transformed plane
453 vec3_t lOrig,lvecS,lvecT; // [2] are not used ( but usefull for debugging )
458 // compute plane axis base
459 ComputeAxisBase( f->plane.normal, texS, texT );
460 VectorSet( Orig, 0.0f, 0.0f, 0.0f );
462 // compute coordinates of (0,0) (1,0) (0,1) ( expressed in initial plane axis base ) after transformation
463 // (0,0) (1,0) (0,1) ( expressed in initial plane axis base ) <-> (0,0,0) texS texT ( expressed world axis base )
464 // input: Orig, texS, texT (and the global locking params)
465 // ouput: rOrig, rvecS, rvecT, rNormal
466 if ( txlock_bRotation ) {
468 VectorSet( vRotate, 0.0f, 0.0f, 0.0f );
469 vRotate[txl_nAxis] = txl_fDeg;
470 VectorRotateOrigin( Orig, vRotate, txl_vOrigin, rOrig );
471 VectorRotateOrigin( texS, vRotate, txl_vOrigin, rvecS );
472 VectorRotateOrigin( texT, vRotate, txl_vOrigin, rvecT );
473 // compute normal of plane after rotation
474 VectorRotate( f->plane.normal, vRotate, rNormal );
478 VectorSubtract( Orig, txl_origin, temp );
479 for ( j = 0 ; j < 3 ; j++ )
480 rOrig[j] = DotProduct( temp, txl_matrix[j] ) + txl_origin[j];
481 VectorSubtract( texS, txl_origin, temp );
482 for ( j = 0 ; j < 3 ; j++ )
483 rvecS[j] = DotProduct( temp, txl_matrix[j] ) + txl_origin[j];
484 VectorSubtract( texT, txl_origin, temp );
485 for ( j = 0 ; j < 3 ; j++ )
486 rvecT[j] = DotProduct( temp, txl_matrix[j] ) + txl_origin[j];
487 // we also need the axis base of the target plane, apply the transformation matrix to the normal too..
488 for ( j = 0 ; j < 3 ; j++ )
489 rNormal[j] = DotProduct( f->plane.normal, txl_matrix[j] );
492 // compute rotated plane axis base
493 ComputeAxisBase( rNormal, rtexS, rtexT );
494 // compute S/T coordinates of the three points in rotated axis base ( in M matrix )
495 lOrig[0] = DotProduct( rOrig, rtexS );
496 lOrig[1] = DotProduct( rOrig, rtexT );
497 lvecS[0] = DotProduct( rvecS, rtexS );
498 lvecS[1] = DotProduct( rvecS, rtexT );
499 lvecT[0] = DotProduct( rvecT, rtexS );
500 lvecT[1] = DotProduct( rvecT, rtexT );
501 M[0][0] = lOrig[0]; M[1][0] = lOrig[1]; M[2][0] = 1.0f;
502 M[0][1] = lvecS[0]; M[1][1] = lvecS[1]; M[2][1] = 1.0f;
503 M[0][2] = lvecT[0]; M[1][2] = lvecT[1]; M[2][2] = 1.0f;
505 D[0][0] = f->brushprimit_texdef.coords[0][2];
506 D[0][1] = f->brushprimit_texdef.coords[0][0] + f->brushprimit_texdef.coords[0][2];
507 D[0][2] = f->brushprimit_texdef.coords[0][1] + f->brushprimit_texdef.coords[0][2];
508 D[1][0] = f->brushprimit_texdef.coords[1][2];
509 D[1][1] = f->brushprimit_texdef.coords[1][0] + f->brushprimit_texdef.coords[1][2];
510 D[1][2] = f->brushprimit_texdef.coords[1][1] + f->brushprimit_texdef.coords[1][2];
512 det = SarrusDet( M[0], M[1], M[2] );
513 f->brushprimit_texdef.coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
514 f->brushprimit_texdef.coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
515 f->brushprimit_texdef.coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
516 f->brushprimit_texdef.coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
517 f->brushprimit_texdef.coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
518 f->brushprimit_texdef.coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
522 // called before the points on the face are actually rotated
523 void RotateFaceTexture_BrushPrimit( face_t *f, int nAxis, float fDeg, vec3_t vOrigin ){
524 // this is a placeholder to call the general texture locking algorithm
525 txlock_bRotation = true;
528 VectorCopy( vOrigin, txl_vOrigin );
529 TextureLockTransformation_BrushPrimit( f );
532 // compute the new brush primit texture matrix for a transformation matrix and a flip order flag (change plane orientation)
533 // this matches the select_matrix algo used in select.cpp
534 // this needs to be called on the face BEFORE any geometric transformation
535 // it will compute the texture matrix that will represent the same texture on the face after the geometric transformation is done
536 void ApplyMatrix_BrushPrimit( face_t *f, vec3_t matrix[3], vec3_t origin ){
537 // this is a placeholder to call the general texture locking algorithm
538 txlock_bRotation = false;
539 VectorCopy( matrix[0], txl_matrix[0] );
540 VectorCopy( matrix[1], txl_matrix[1] );
541 VectorCopy( matrix[2], txl_matrix[2] );
542 VectorCopy( origin, txl_origin );
543 TextureLockTransformation_BrushPrimit( f );
547 void BPMatMul( vec_t A[2][3], vec_t B[2][3], vec_t C[2][3] ){
548 C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0];
549 C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0];
550 C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1];
551 C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1];
552 C[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2];
553 C[1][2] = A[1][0] * B[0][2] + A[1][1] * B[1][2] + A[1][2];
556 void BPMatDump( vec_t A[2][3] ){
557 Sys_Printf( "%g %g %g\n%g %g %g\n0 0 1\n", A[0][0], A[0][1], A[0][2], A[1][0], A[1][1], A[1][2] );
560 void BPMatRotate( vec_t A[2][3], float theta ){
563 memset( &m, 0, sizeof( vec_t ) * 6 );
564 m[0][0] = cos( theta * Q_PI / 180.0 );
565 m[0][1] = -sin( theta * Q_PI / 180.0 );
568 BPMatMul( A, m, aux );
572 // get the relative axes of the current texturing
573 void BrushPrimit_GetRelativeAxes( face_t *f, vec3_t vecS, vec3_t vecT ){
575 // first we compute them as expressed in plane axis base
576 // BP matrix has coordinates of plane axis base expressed in geometric axis base
577 // so we use the line vectors
578 vS[0] = f->brushprimit_texdef.coords[0][0];
579 vS[1] = f->brushprimit_texdef.coords[0][1];
580 vT[0] = f->brushprimit_texdef.coords[1][0];
581 vT[1] = f->brushprimit_texdef.coords[1][1];
582 // now compute those vectors in geometric space
583 vec3_t texS, texT; // axis base of the plane (geometric)
584 ComputeAxisBase( f->plane.normal, texS, texT );
585 // vecS[] = vS[0].texS[] + vS[1].texT[]
586 // vecT[] = vT[0].texS[] + vT[1].texT[]
587 vecS[0] = vS[0] * texS[0] + vS[1] * texT[0];
588 vecS[1] = vS[0] * texS[1] + vS[1] * texT[1];
589 vecS[2] = vS[0] * texS[2] + vS[1] * texT[2];
590 vecT[0] = vT[0] * texS[0] + vT[1] * texT[0];
591 vecT[1] = vT[0] * texS[1] + vT[1] * texT[1];
592 vecT[2] = vT[0] * texS[2] + vT[1] * texT[2];
595 // GL matrix 4x4 product (3D homogeneous matrix)
596 // NOTE: the crappy thing is that GL doesn't follow the standard convention [line][column]
597 // otherwise it's all good
598 void GLMatMul( vec_t M[4][4], vec_t A[4], vec_t B[4] ){
600 for ( i = 0; i < 4; i++ )
603 for ( j = 0; j < 4; j++ )
605 B[i] += M[j][i] * A[j];
610 qboolean IsBrushPrimitMode(){
611 return( g_qeglobals.m_bBrushPrimitMode );