]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/brush_primit.cpp
Remove some dead/debug code
[xonotic/netradiant.git] / radiant / brush_primit.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "brush_primit.h"
23
24 #include "debugging/debugging.h"
25
26 #include "itexdef.h"
27 #include "itextures.h"
28
29 #include <algorithm>
30
31 #include "stringio.h"
32 #include "texturelib.h"
33 #include "math/matrix.h"
34 #include "math/plane.h"
35 #include "math/aabb.h"
36
37 #include "winding.h"
38 #include "preferences.h"
39
40
41 /*!
42    \brief Construct a transform from XYZ space to ST space (3d to 2d).
43    This will be one of three axis-aligned spaces, depending on the surface normal.
44    NOTE: could also be done by swapping values.
45  */
46 void Normal_GetTransform( const Vector3& normal, Matrix4& transform ){
47         switch ( projectionaxis_for_normal( normal ) )
48         {
49         case eProjectionAxisZ:
50                 transform[0]  =  1;
51                 transform[1]  =  0;
52                 transform[2]  =  0;
53
54                 transform[4]  =  0;
55                 transform[5]  =  1;
56                 transform[6]  =  0;
57
58                 transform[8]  =  0;
59                 transform[9]  =  0;
60                 transform[10] =  1;
61                 break;
62         case eProjectionAxisY:
63                 transform[0]  =  1;
64                 transform[1]  =  0;
65                 transform[2]  =  0;
66
67                 transform[4]  =  0;
68                 transform[5]  =  0;
69                 transform[6]  = -1;
70
71                 transform[8]  =  0;
72                 transform[9]  =  1;
73                 transform[10] =  0;
74                 break;
75         case eProjectionAxisX:
76                 transform[0]  =  0;
77                 transform[1]  =  0;
78                 transform[2]  =  1;
79
80                 transform[4]  =  1;
81                 transform[5]  =  0;
82                 transform[6]  =  0;
83
84                 transform[8]  =  0;
85                 transform[9]  =  1;
86                 transform[10] =  0;
87                 break;
88         }
89         transform[3] = transform[7] = transform[11] = transform[12] = transform[13] = transform[14] = 0;
90         transform[15] = 1;
91 }
92
93 /*!
94    \brief Construct a transform in ST space from the texdef.
95    Transforms constructed from quake's texdef format are (-shift)*(1/scale)*(-rotate) with x translation sign flipped.
96    This would really make more sense if it was inverseof(shift*rotate*scale).. oh well.
97  */
98 inline void Texdef_toTransform( const texdef_t& texdef, float width, float height, Matrix4& transform ){
99         double inverse_scale[2];
100
101         // transform to texdef shift/scale/rotate
102         inverse_scale[0] = 1 / ( texdef.scale[0] * width );
103         inverse_scale[1] = 1 / ( texdef.scale[1] * -height );
104         transform[12] = texdef.shift[0] / width;
105         transform[13] = -texdef.shift[1] / -height;
106         double c = cos( degrees_to_radians( -texdef.rotate ) );
107         double s = sin( degrees_to_radians( -texdef.rotate ) );
108         transform[0] = static_cast<float>( c * inverse_scale[0] );
109         transform[1] = static_cast<float>( s * inverse_scale[1] );
110         transform[4] = static_cast<float>( -s * inverse_scale[0] );
111         transform[5] = static_cast<float>( c * inverse_scale[1] );
112         transform[2] = transform[3] = transform[6] = transform[7] = transform[8] = transform[9] = transform[11] = transform[14] = 0;
113         transform[10] = transform[15] = 1;
114 }
115
116 inline void BPTexdef_toTransform( const brushprimit_texdef_t& bp_texdef, Matrix4& transform ){
117         transform = g_matrix4_identity;
118         transform.xx() = bp_texdef.coords[0][0];
119         transform.yx() = bp_texdef.coords[0][1];
120         transform.tx() = bp_texdef.coords[0][2];
121         transform.xy() = bp_texdef.coords[1][0];
122         transform.yy() = bp_texdef.coords[1][1];
123         transform.ty() = bp_texdef.coords[1][2];
124 }
125
126 inline void Texdef_toTransform( const TextureProjection& projection, float width, float height, Matrix4& transform ){
127         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
128                 BPTexdef_toTransform( projection.m_brushprimit_texdef, transform );
129         }
130         else
131         {
132                 Texdef_toTransform( projection.m_texdef, width, height, transform );
133         }
134 }
135
136 // handles degenerate cases, just in case library atan2 doesn't
137 inline double arctangent_yx( double y, double x ){
138         if ( fabs( x ) > 1.0E-6 ) {
139                 return atan2( y, x );
140         }
141         else if ( y > 0 ) {
142                 return c_half_pi;
143         }
144         else
145         {
146                 return -c_half_pi;
147         }
148 }
149
150 inline void Texdef_fromTransform( texdef_t& texdef, float width, float height, const Matrix4& transform ){
151         texdef.scale[0] = static_cast<float>( ( 1.0 / vector2_length( Vector2( transform[0], transform[4] ) ) ) / width );
152         texdef.scale[1] = static_cast<float>( ( 1.0 / vector2_length( Vector2( transform[1], transform[5] ) ) ) / height );
153
154         texdef.rotate = static_cast<float>( -radians_to_degrees( arctangent_yx( -transform[4], transform[0] ) ) );
155
156         if ( texdef.rotate == -180.0f ) {
157                 texdef.rotate = 180.0f;
158         }
159
160         texdef.shift[0] = transform[12] * width;
161         texdef.shift[1] = transform[13] * height;
162
163         // If the 2d cross-product of the x and y axes is positive, one of the axes has a negative scale.
164         if ( vector2_cross( Vector2( transform[0], transform[4] ), Vector2( transform[1], transform[5] ) ) > 0 ) {
165                 if ( texdef.rotate >= 180.0f ) {
166                         texdef.rotate -= 180.0f;
167                         texdef.scale[0] = -texdef.scale[0];
168                 }
169                 else
170                 {
171                         texdef.scale[1] = -texdef.scale[1];
172                 }
173         }
174         //globalOutputStream() << "fromTransform: " << texdef.shift[0] << " " << texdef.shift[1] << " " << texdef.scale[0] << " " << texdef.scale[1] << " " << texdef.rotate << "\n";
175 }
176
177 inline void BPTexdef_fromTransform( brushprimit_texdef_t& bp_texdef, const Matrix4& transform ){
178         bp_texdef.coords[0][0] = transform.xx();
179         bp_texdef.coords[0][1] = transform.yx();
180         bp_texdef.coords[0][2] = transform.tx();
181         bp_texdef.coords[1][0] = transform.xy();
182         bp_texdef.coords[1][1] = transform.yy();
183         bp_texdef.coords[1][2] = transform.ty();
184 }
185
186 inline void Texdef_fromTransform( TextureProjection& projection, float width, float height, const Matrix4& transform ){
187         ASSERT_MESSAGE( ( transform[0] != 0 || transform[4] != 0 )
188                                         && ( transform[1] != 0 || transform[5] != 0 ), "invalid texture matrix" );
189
190         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
191                 BPTexdef_fromTransform( projection.m_brushprimit_texdef, transform );
192         }
193         else
194         {
195                 Texdef_fromTransform( projection.m_texdef, width, height, transform );
196         }
197 }
198
199 inline void Texdef_normalise( texdef_t& texdef, float width, float height ){
200         // it may be useful to also normalise the rotation here, if this function is used elsewhere.
201         texdef.shift[0] = float_mod( texdef.shift[0], width );
202         texdef.shift[1] = float_mod( texdef.shift[1], height );
203         //globalOutputStream() << "normalise: " << texdef.shift[0] << " " << texdef.shift[1] << " " << texdef.scale[0] << " " << texdef.scale[1] << " " << texdef.rotate << "\n";
204 }
205
206 inline void BPTexdef_normalise( brushprimit_texdef_t& bp_texdef, float width, float height ){
207         bp_texdef.coords[0][2] = float_mod( bp_texdef.coords[0][2], width );
208         bp_texdef.coords[1][2] = float_mod( bp_texdef.coords[1][2], height );
209 }
210
211 /// \brief Normalise \p projection for a given texture \p width and \p height.
212 ///
213 /// All texture-projection translation (shift) values are congruent modulo the dimensions of the texture.
214 /// This function normalises shift values to the smallest positive congruent values.
215 void Texdef_normalise( TextureProjection& projection, float width, float height ){
216         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
217                 BPTexdef_normalise( projection.m_brushprimit_texdef, width, height );
218         }
219         else
220         {
221                 Texdef_normalise( projection.m_texdef, width, height );
222         }
223 }
224
225 void ComputeAxisBase( const Vector3& normal, Vector3& texS, Vector3& texT );
226
227 inline void DebugAxisBase( const Vector3& normal ){
228         Vector3 x, y;
229         ComputeAxisBase( normal, x, y );
230         globalOutputStream() << "BP debug: " << x << y << normal << "\n";
231 }
232
233 void Texdef_basisForNormal( const TextureProjection& projection, const Vector3& normal, Matrix4& basis ){
234         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
235                 basis = g_matrix4_identity;
236                 ComputeAxisBase( normal, vector4_to_vector3( basis.x() ), vector4_to_vector3( basis.y() ) );
237                 vector4_to_vector3( basis.z() ) = normal;
238                 matrix4_transpose( basis );
239                 //DebugAxisBase(normal);
240         }
241         else if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_HALFLIFE ) {
242                 basis = g_matrix4_identity;
243                 vector4_to_vector3( basis.x() ) = projection.m_basis_s;
244                 vector4_to_vector3( basis.y() ) = vector3_negated( projection.m_basis_t );
245                 vector4_to_vector3( basis.z() ) = vector3_normalised( vector3_cross( vector4_to_vector3( basis.x() ), vector4_to_vector3( basis.y() ) ) );
246                 matrix4_multiply_by_matrix4( basis, matrix4_rotation_for_z_degrees( -projection.m_texdef.rotate ) );
247                 //globalOutputStream() << "debug: " << projection.m_basis_s << projection.m_basis_t << normal << "\n";
248                 matrix4_transpose( basis );
249         }
250         else
251         {
252                 Normal_GetTransform( normal, basis );
253         }
254 }
255
256 void Texdef_EmitTextureCoordinates( const TextureProjection& projection, std::size_t width, std::size_t height, Winding& w, const Vector3& normal, const Matrix4& localToWorld ){
257         if ( w.numpoints < 3 ) {
258                 return;
259         }
260
261         Matrix4 local2tex;
262         Texdef_toTransform( projection, (float)width, (float)height, local2tex );
263
264         {
265                 Matrix4 xyz2st;
266                 // we don't care if it's not normalised...
267                 Texdef_basisForNormal( projection, matrix4_transformed_direction( localToWorld, normal ), xyz2st );
268                 matrix4_multiply_by_matrix4( local2tex, xyz2st );
269         }
270
271         Vector3 tangent( vector3_normalised( vector4_to_vector3( matrix4_transposed( local2tex ).x() ) ) );
272         Vector3 bitangent( vector3_normalised( vector4_to_vector3( matrix4_transposed( local2tex ).y() ) ) );
273
274         matrix4_multiply_by_matrix4( local2tex, localToWorld );
275
276         for ( Winding::iterator i = w.begin(); i != w.end(); ++i )
277         {
278                 Vector3 texcoord = matrix4_transformed_point( local2tex, ( *i ).vertex );
279                 ( *i ).texcoord[0] = texcoord[0];
280                 ( *i ).texcoord[1] = texcoord[1];
281
282                 ( *i ).tangent = tangent;
283                 ( *i ).bitangent = bitangent;
284         }
285 }
286
287 /*!
288    \brief Provides the axis-base of the texture ST space for this normal,
289    as they had been transformed to world XYZ space.
290  */
291 void TextureAxisFromNormal( const Vector3& normal, Vector3& s, Vector3& t ){
292         switch ( projectionaxis_for_normal( normal ) )
293         {
294         case eProjectionAxisZ:
295                 s[0]  =  1;
296                 s[1]  =  0;
297                 s[2]  =  0;
298
299                 t[0]  =  0;
300                 t[1]  = -1;
301                 t[2]  =  0;
302
303                 break;
304         case eProjectionAxisY:
305                 s[0]  =  1;
306                 s[1]  =  0;
307                 s[2]  =  0;
308
309                 t[0]  =  0;
310                 t[1]  =  0;
311                 t[2]  = -1;
312
313                 break;
314         case eProjectionAxisX:
315                 s[0]  =  0;
316                 s[1]  =  1;
317                 s[2]  =  0;
318
319                 t[0]  =  0;
320                 t[1]  =  0;
321                 t[2]  = -1;
322
323                 break;
324         }
325 }
326
327 void Texdef_Assign( texdef_t& td, const texdef_t& other ){
328         td = other;
329 }
330
331 void Texdef_Shift( texdef_t& td, float s, float t ){
332         td.shift[0] += s;
333         td.shift[1] += t;
334 }
335
336 void Texdef_Scale( texdef_t& td, float s, float t ){
337         td.scale[0] += s;
338         td.scale[1] += t;
339 }
340
341 void Texdef_Rotate( texdef_t& td, float angle ){
342         td.rotate += angle;
343         td.rotate = static_cast<float>( float_to_integer( td.rotate ) % 360 );
344 }
345
346 // NOTE: added these from Ritual's Q3Radiant
347 void ClearBounds( Vector3& mins, Vector3& maxs ){
348         mins[0] = mins[1] = mins[2] = 99999;
349         maxs[0] = maxs[1] = maxs[2] = -99999;
350 }
351
352 void AddPointToBounds( const Vector3& v, Vector3& mins, Vector3& maxs ){
353         int i;
354         float val;
355
356         for ( i = 0 ; i < 3 ; i++ )
357         {
358                 val = v[i];
359                 if ( val < mins[i] ) {
360                         mins[i] = val;
361                 }
362                 if ( val > maxs[i] ) {
363                         maxs[i] = val;
364                 }
365         }
366 }
367
368 template<typename Element>
369 inline BasicVector3<Element> vector3_inverse( const BasicVector3<Element>& self ){
370         return BasicVector3<Element>(
371                            Element( 1.0 / self.x() ),
372                            Element( 1.0 / self.y() ),
373                            Element( 1.0 / self.z() )
374                            );
375 }
376
377 // low level functions .. put in mathlib?
378 #define BPMatCopy( a,b ) {b[0][0] = a[0][0]; b[0][1] = a[0][1]; b[0][2] = a[0][2]; b[1][0] = a[1][0]; b[1][1] = a[1][1]; b[1][2] = a[1][2]; }
379 // apply a scale transformation to the BP matrix
380 #define BPMatScale( m,sS,sT ) {m[0][0] *= sS; m[1][0] *= sS; m[0][1] *= sT; m[1][1] *= sT; }
381 // apply a translation transformation to a BP matrix
382 #define BPMatTranslate( m,s,t ) {m[0][2] += m[0][0] * s + m[0][1] * t; m[1][2] += m[1][0] * s + m[1][1] * t; }
383 // 2D homogeneous matrix product C = A*B
384 void BPMatMul( float A[2][3], float B[2][3], float C[2][3] );
385 // apply a rotation (degrees)
386 void BPMatRotate( float A[2][3], float theta );
387
388
389
390 bp_globals_t g_bp_globals;
391 float g_texdef_default_scale;
392
393 // compute a determinant using Sarrus rule
394 //++timo "inline" this with a macro
395 // NOTE : the three vectors are understood as columns of the matrix
396 inline float SarrusDet( const Vector3& a, const Vector3& b, const Vector3& c ){
397         return a[0] * b[1] * c[2] + b[0] * c[1] * a[2] + c[0] * a[1] * b[2]
398                    - c[0] * b[1] * a[2] - a[1] * b[0] * c[2] - a[0] * b[2] * c[1];
399 }
400
401 // in many case we know three points A,B,C in two axis base B1 and B2
402 // and we want the matrix M so that A(B1) = T * A(B2)
403 // NOTE: 2D homogeneous space stuff
404 // 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
405 // NOTE: the third coord of the A,B,C point is ignored
406 // NOTE: see the commented out section to fill M and D
407 //++timo TODO: update the other members to use this when possible
408 void MatrixForPoints( Vector3 M[3], Vector3 D[2], brushprimit_texdef_t *T ){
409         float det;
410         M[2][0] = 1.0f; M[2][1] = 1.0f; M[2][2] = 1.0f;
411
412         // solve
413         det = SarrusDet( M[0], M[1], M[2] );
414         T->coords[0][0] = SarrusDet( D[0], M[1], M[2] ) / det;
415         T->coords[0][1] = SarrusDet( M[0], D[0], M[2] ) / det;
416         T->coords[0][2] = SarrusDet( M[0], M[1], D[0] ) / det;
417         T->coords[1][0] = SarrusDet( D[1], M[1], M[2] ) / det;
418         T->coords[1][1] = SarrusDet( M[0], D[1], M[2] ) / det;
419         T->coords[1][2] = SarrusDet( M[0], M[1], D[1] ) / det;
420 }
421
422 //++timo replace everywhere texX by texS etc. ( ----> and in q3map !)
423 // NOTE : ComputeAxisBase here and in q3map code must always BE THE SAME !
424 // WARNING : special case behaviour of atan2(y,x) <-> atan(y/x) might not be the same everywhere when x == 0
425 // rotation by (0,RotY,RotZ) assigns X to normal
426 void ComputeAxisBase( const Vector3& normal, Vector3& texS, Vector3& texT ){
427         const Vector3 up( 0, 0, 1 );
428         const Vector3 down( 0, 0, -1 );
429
430         if ( vector3_equal_epsilon( normal, up, float(1e-6) ) ) {
431                 texS = Vector3( 0, 1, 0 );
432                 texT = Vector3( 1, 0, 0 );
433         }
434         else if ( vector3_equal_epsilon( normal, down, float(1e-6) ) ) {
435                 texS = Vector3( 0, 1, 0 );
436                 texT = Vector3( -1, 0, 0 );
437         }
438         else
439         {
440                 texS = vector3_normalised( vector3_cross( normal, up ) );
441                 texT = vector3_normalised( vector3_cross( normal, texS ) );
442                 vector3_negate( texS );
443         }
444 }
445
446 typedef float texmat_t[2][3];
447
448 void TexMat_Scale( texmat_t texmat, float s, float t ){
449         texmat[0][0] *= s;
450         texmat[0][1] *= s;
451         texmat[0][2] *= s;
452         texmat[1][0] *= t;
453         texmat[1][1] *= t;
454         texmat[1][2] *= t;
455 }
456
457 void TexMat_Assign( texmat_t texmat, const texmat_t other ){
458         texmat[0][0] = other[0][0];
459         texmat[0][1] = other[0][1];
460         texmat[0][2] = other[0][2];
461         texmat[1][0] = other[1][0];
462         texmat[1][1] = other[1][1];
463         texmat[1][2] = other[1][2];
464 }
465
466 void ConvertTexMatWithDimensions( const texmat_t texmat1, std::size_t w1, std::size_t h1,
467                                                                   texmat_t texmat2, std::size_t w2, std::size_t h2 ){
468         TexMat_Assign( texmat2, texmat1 );
469         TexMat_Scale( texmat2, static_cast<float>( w1 ) / static_cast<float>( w2 ), static_cast<float>( h1 ) / static_cast<float>( h2 ) );
470 }
471
472 // compute a fake shift scale rot representation from the texture matrix
473 // these shift scale rot values are to be understood in the local axis base
474 // Note: this code looks similar to Texdef_fromTransform, but the algorithm is slightly different.
475
476 void TexMatToFakeTexCoords( const brushprimit_texdef_t& bp_texdef, texdef_t& texdef ){
477         texdef.scale[0] = static_cast<float>( 1.0 / vector2_length( Vector2( bp_texdef.coords[0][0], bp_texdef.coords[1][0] ) ) );
478         texdef.scale[1] = static_cast<float>( 1.0 / vector2_length( Vector2( bp_texdef.coords[0][1], bp_texdef.coords[1][1] ) ) );
479
480         texdef.rotate = -static_cast<float>( radians_to_degrees( arctangent_yx( bp_texdef.coords[1][0], bp_texdef.coords[0][0] ) ) );
481
482         texdef.shift[0] = -bp_texdef.coords[0][2];
483         texdef.shift[1] = bp_texdef.coords[1][2];
484
485         // determine whether or not an axis is flipped using a 2d cross-product
486         double cross = vector2_cross( Vector2( bp_texdef.coords[0][0], bp_texdef.coords[0][1] ), Vector2( bp_texdef.coords[1][0], bp_texdef.coords[1][1] ) );
487         if ( cross < 0 ) {
488                 // This is a bit of a compromise when using BPs--since we don't know *which* axis was flipped,
489                 // we pick one (rather arbitrarily) using the following convention: If the X-axis is between
490                 // 0 and 180, we assume it's the Y-axis that flipped, otherwise we assume it's the X-axis and
491                 // subtract out 180 degrees to compensate.
492                 if ( texdef.rotate >= 180.0f ) {
493                         texdef.rotate -= 180.0f;
494                         texdef.scale[0] = -texdef.scale[0];
495                 }
496                 else
497                 {
498                         texdef.scale[1] = -texdef.scale[1];
499                 }
500         }
501 }
502
503 // compute back the texture matrix from fake shift scale rot
504 void FakeTexCoordsToTexMat( const texdef_t& texdef, brushprimit_texdef_t& bp_texdef ){
505         double r = degrees_to_radians( -texdef.rotate );
506         double c = cos( r );
507         double s = sin( r );
508         double x = 1.0f / texdef.scale[0];
509         double y = 1.0f / texdef.scale[1];
510         bp_texdef.coords[0][0] = static_cast<float>( x * c );
511         bp_texdef.coords[1][0] = static_cast<float>( x * s );
512         bp_texdef.coords[0][1] = static_cast<float>( y * -s );
513         bp_texdef.coords[1][1] = static_cast<float>( y * c );
514         bp_texdef.coords[0][2] = -texdef.shift[0];
515         bp_texdef.coords[1][2] = texdef.shift[1];
516 }
517
518 // TTimo: FIXME: I don't like that, it feels broken
519 //   (and it's likely that it's not used anymore)
520 // best fitted 2D vector is x.X+y.Y
521 void ComputeBest2DVector( Vector3& v, Vector3& X, Vector3& Y, int &x, int &y ){
522         double sx,sy;
523         sx = vector3_dot( v, X );
524         sy = vector3_dot( v, Y );
525         if ( fabs( sy ) > fabs( sx ) ) {
526                 x = 0;
527                 if ( sy > 0.0 ) {
528                         y =  1;
529                 }
530                 else{
531                         y = -1;
532                 }
533         }
534         else
535         {
536                 y = 0;
537                 if ( sx > 0.0 ) {
538                         x =  1;
539                 }
540                 else{
541                         x = -1;
542                 }
543         }
544 }
545
546 // don't do C==A!
547 void BPMatMul( float A[2][3], float B[2][3], float 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];
554 }
555
556 void BPMatRotate( float A[2][3], float theta ){
557         float m[2][3];
558         float aux[2][3];
559         memset( &m, 0, sizeof( float ) * 6 );
560         m[0][0] = static_cast<float>( cos( degrees_to_radians( theta ) ) );
561         m[0][1] = static_cast<float>( -sin( degrees_to_radians( theta ) ) );
562         m[1][0] = -m[0][1];
563         m[1][1] = m[0][0];
564         BPMatMul( A, m, aux );
565         BPMatCopy( aux,A );
566 }
567
568 void BPTexdef_Assign( brushprimit_texdef_t& bp_td, const brushprimit_texdef_t& bp_other ){
569         bp_td = bp_other;
570 }
571
572 void BPTexdef_Shift( brushprimit_texdef_t& bp_td, float s, float t ){
573         // shift a texture (texture adjustments) along it's current texture axes
574         // x and y are geometric values, which we must compute as ST increments
575         // this depends on the texture size and the pixel/texel ratio
576         // as a ratio against texture size
577         // the scale of the texture is not relevant here (we work directly on a transformation from the base vectors)
578         bp_td.coords[0][2] -= s;
579         bp_td.coords[1][2] += t;
580 }
581
582 void BPTexdef_Scale( brushprimit_texdef_t& bp_td, float s, float t ){
583         // apply same scale as the spinner button of the surface inspector
584         texdef_t texdef;
585         // compute fake shift scale rot
586         TexMatToFakeTexCoords( bp_td, texdef );
587         // update
588         texdef.scale[0] += s;
589         texdef.scale[1] += t;
590         // compute new normalized texture matrix
591         FakeTexCoordsToTexMat( texdef, bp_td );
592 }
593
594 void BPTexdef_Rotate( brushprimit_texdef_t& bp_td, float angle ){
595         // apply same scale as the spinner button of the surface inspector
596         texdef_t texdef;
597         // compute fake shift scale rot
598         TexMatToFakeTexCoords( bp_td, texdef );
599         // update
600         texdef.rotate += angle;
601         // compute new normalized texture matrix
602         FakeTexCoordsToTexMat( texdef, bp_td );
603 }
604
605 void BPTexdef_Construct( brushprimit_texdef_t& bp_td, std::size_t width, std::size_t height ){
606         bp_td.coords[0][0] = 1.0f;
607         bp_td.coords[1][1] = 1.0f;
608         ConvertTexMatWithDimensions( bp_td.coords, 2, 2, bp_td.coords, width, height );
609 }
610
611 void Texdef_Assign( TextureProjection& projection, const TextureProjection& other ){
612         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
613                 BPTexdef_Assign( projection.m_brushprimit_texdef, other.m_brushprimit_texdef );
614         }
615         else
616         {
617                 Texdef_Assign( projection.m_texdef, other.m_texdef );
618                 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_HALFLIFE ) {
619                         projection.m_basis_s = other.m_basis_s;
620                         projection.m_basis_t = other.m_basis_t;
621                 }
622         }
623 }
624
625 void Texdef_Shift( TextureProjection& projection, float s, float t ){
626         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
627                 BPTexdef_Shift( projection.m_brushprimit_texdef, s, t );
628         }
629         else
630         {
631                 Texdef_Shift( projection.m_texdef, s, t );
632         }
633 }
634
635 void Texdef_Scale( TextureProjection& projection, float s, float t ){
636         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
637                 BPTexdef_Scale( projection.m_brushprimit_texdef, s, t );
638         }
639         else
640         {
641                 Texdef_Scale( projection.m_texdef, s, t );
642         }
643 }
644
645 void Texdef_Rotate( TextureProjection& projection, float angle ){
646         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
647                 BPTexdef_Rotate( projection.m_brushprimit_texdef, angle );
648         }
649         else
650         {
651                 Texdef_Rotate( projection.m_texdef, angle );
652         }
653 }
654
655 void Texdef_FitTexture( TextureProjection& projection, std::size_t width, std::size_t height, const Vector3& normal, const Winding& w, float s_repeat, float t_repeat ){
656         if ( w.numpoints < 3 ) {
657                 return;
658         }
659
660         Matrix4 st2tex;
661         Texdef_toTransform( projection, (float)width, (float)height, st2tex );
662
663         // the current texture transform
664         Matrix4 local2tex = st2tex;
665         {
666                 Matrix4 xyz2st;
667                 Texdef_basisForNormal( projection, normal, xyz2st );
668                 matrix4_multiply_by_matrix4( local2tex, xyz2st );
669         }
670
671         // the bounds of the current texture transform
672         AABB bounds;
673         for ( Winding::const_iterator i = w.begin(); i != w.end(); ++i )
674         {
675                 Vector3 texcoord = matrix4_transformed_point( local2tex, ( *i ).vertex );
676                 aabb_extend_by_point_safe( bounds, texcoord );
677         }
678         bounds.origin.z() = 0;
679         bounds.extents.z() = 1;
680
681         // the bounds of a perfectly fitted texture transform
682         AABB perfect( Vector3( s_repeat * 0.5, t_repeat * 0.5, 0 ), Vector3( s_repeat * 0.5, t_repeat * 0.5, 1 ) );
683
684         // the difference between the current texture transform and the perfectly fitted transform
685         Matrix4 matrix( matrix4_translation_for_vec3( bounds.origin - perfect.origin ) );
686         matrix4_pivoted_scale_by_vec3( matrix, bounds.extents / perfect.extents, perfect.origin );
687         matrix4_affine_invert( matrix );
688
689         // apply the difference to the current texture transform
690         matrix4_premultiply_by_matrix4( st2tex, matrix );
691
692         Texdef_fromTransform( projection, (float)width, (float)height, st2tex );
693         Texdef_normalise( projection, (float)width, (float)height );
694 }
695
696 float Texdef_getDefaultTextureScale(){
697         return g_texdef_default_scale;
698 }
699
700 void TexDef_Construct_Default( TextureProjection& projection ){
701         projection.m_texdef.scale[0] = Texdef_getDefaultTextureScale();
702         projection.m_texdef.scale[1] = Texdef_getDefaultTextureScale();
703
704         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
705                 FakeTexCoordsToTexMat( projection.m_texdef, projection.m_brushprimit_texdef );
706         }
707 }
708
709
710
711 void ShiftScaleRotate_fromFace( texdef_t& shiftScaleRotate, const TextureProjection& projection ){
712         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
713                 TexMatToFakeTexCoords( projection.m_brushprimit_texdef, shiftScaleRotate );
714         }
715         else
716         {
717                 shiftScaleRotate = projection.m_texdef;
718         }
719 }
720
721 void ShiftScaleRotate_toFace( const texdef_t& shiftScaleRotate, TextureProjection& projection ){
722         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
723                 // compute texture matrix
724                 // the matrix returned must be understood as a qtexture_t with width=2 height=2
725                 FakeTexCoordsToTexMat( shiftScaleRotate, projection.m_brushprimit_texdef );
726         }
727         else
728         {
729                 projection.m_texdef = shiftScaleRotate;
730         }
731 }
732
733
734 inline void print_vector3( const Vector3& v ){
735         globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )\n";
736 }
737
738 inline void print_3x3( const Matrix4& m ){
739         globalOutputStream() << "( " << m.xx() << " " << m.xy() << " " << m.xz() << " ) "
740                                                  << "( " << m.yx() << " " << m.yy() << " " << m.yz() << " ) "
741                                                  << "( " << m.zx() << " " << m.zy() << " " << m.zz() << " )\n";
742 }
743
744
745 inline Matrix4 matrix4_rotation_for_vector3( const Vector3& x, const Vector3& y, const Vector3& z ){
746         return Matrix4(
747                            x.x(), x.y(), x.z(), 0,
748                            y.x(), y.y(), y.z(), 0,
749                            z.x(), z.y(), z.z(), 0,
750                            0, 0, 0, 1
751                            );
752 }
753
754 inline Matrix4 matrix4_swap_axes( const Vector3& from, const Vector3& to ){
755         if ( from.x() != 0 && to.y() != 0 ) {
756                 return matrix4_rotation_for_vector3( to, from, g_vector3_axis_z );
757         }
758
759         if ( from.x() != 0 && to.z() != 0 ) {
760                 return matrix4_rotation_for_vector3( to, g_vector3_axis_y, from );
761         }
762
763         if ( from.y() != 0 && to.z() != 0 ) {
764                 return matrix4_rotation_for_vector3( g_vector3_axis_x, to, from );
765         }
766
767         if ( from.y() != 0 && to.x() != 0 ) {
768                 return matrix4_rotation_for_vector3( from, to, g_vector3_axis_z );
769         }
770
771         if ( from.z() != 0 && to.x() != 0 ) {
772                 return matrix4_rotation_for_vector3( from, g_vector3_axis_y, to );
773         }
774
775         if ( from.z() != 0 && to.y() != 0 ) {
776                 return matrix4_rotation_for_vector3( g_vector3_axis_x, from, to );
777         }
778
779         ERROR_MESSAGE( "unhandled axis swap case" );
780
781         return g_matrix4_identity;
782 }
783
784 inline Matrix4 matrix4_reflection_for_plane( const Plane3& plane ){
785         return Matrix4(
786                            static_cast<float>( 1 - ( 2 * plane.a * plane.a ) ),
787                            static_cast<float>( -2 * plane.a * plane.b ),
788                            static_cast<float>( -2 * plane.a * plane.c ),
789                            0,
790                            static_cast<float>( -2 * plane.b * plane.a ),
791                            static_cast<float>( 1 - ( 2 * plane.b * plane.b ) ),
792                            static_cast<float>( -2 * plane.b * plane.c ),
793                            0,
794                            static_cast<float>( -2 * plane.c * plane.a ),
795                            static_cast<float>( -2 * plane.c * plane.b ),
796                            static_cast<float>( 1 - ( 2 * plane.c * plane.c ) ),
797                            0,
798                            static_cast<float>( -2 * plane.d * plane.a ),
799                            static_cast<float>( -2 * plane.d * plane.b ),
800                            static_cast<float>( -2 * plane.d * plane.c ),
801                            1
802                            );
803 }
804
805 inline Matrix4 matrix4_reflection_for_plane45( const Plane3& plane, const Vector3& from, const Vector3& to ){
806         Vector3 first = from;
807         Vector3 second = to;
808
809         if ( vector3_dot( from, plane.normal() ) > 0 == vector3_dot( to, plane.normal() ) > 0 ) {
810                 first = vector3_negated( first );
811                 second = vector3_negated( second );
812         }
813
814
815         Matrix4 swap = matrix4_swap_axes( first, second );
816
817         Matrix4 tmp = matrix4_reflection_for_plane( plane );
818
819         swap.tx() = -static_cast<float>( -2 * plane.a * plane.d );
820         swap.ty() = -static_cast<float>( -2 * plane.b * plane.d );
821         swap.tz() = -static_cast<float>( -2 * plane.c * plane.d );
822
823         return swap;
824 }
825
826 void Texdef_transformLocked( TextureProjection& projection, std::size_t width, std::size_t height, const Plane3& plane, const Matrix4& identity2transformed ){
827         //globalOutputStream() << "identity2transformed: " << identity2transformed << "\n";
828
829         //globalOutputStream() << "plane.normal(): " << plane.normal() << "\n";
830
831         Vector3 normalTransformed( matrix4_transformed_direction( identity2transformed, plane.normal() ) );
832
833         //globalOutputStream() << "normalTransformed: " << normalTransformed << "\n";
834
835         // identity: identity space
836         // transformed: transformation
837         // stIdentity: base st projection space before transformation
838         // stTransformed: base st projection space after transformation
839         // stOriginal: original texdef space
840
841         // stTransformed2stOriginal = stTransformed -> transformed -> identity -> stIdentity -> stOriginal
842
843         Matrix4 identity2stIdentity;
844         Texdef_basisForNormal( projection, plane.normal(), identity2stIdentity );
845         //globalOutputStream() << "identity2stIdentity: " << identity2stIdentity << "\n";
846
847         if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_HALFLIFE ) {
848                 matrix4_transform_direction( identity2transformed, projection.m_basis_s );
849                 matrix4_transform_direction( identity2transformed, projection.m_basis_t );
850         }
851
852         Matrix4 transformed2stTransformed;
853         Texdef_basisForNormal( projection, normalTransformed, transformed2stTransformed );
854
855         Matrix4 stTransformed2identity( matrix4_affine_inverse( matrix4_multiplied_by_matrix4( transformed2stTransformed, identity2transformed ) ) );
856
857         Vector3 originalProjectionAxis( vector4_to_vector3( matrix4_affine_inverse( identity2stIdentity ).z() ) );
858
859         Vector3 transformedProjectionAxis( vector4_to_vector3( stTransformed2identity.z() ) );
860
861         Matrix4 stIdentity2stOriginal;
862         Texdef_toTransform( projection, (float)width, (float)height, stIdentity2stOriginal );
863         Matrix4 identity2stOriginal( matrix4_multiplied_by_matrix4( stIdentity2stOriginal, identity2stIdentity ) );
864
865         //globalOutputStream() << "originalProj: " << originalProjectionAxis << "\n";
866         //globalOutputStream() << "transformedProj: " << transformedProjectionAxis << "\n";
867         double dot = vector3_dot( originalProjectionAxis, transformedProjectionAxis );
868         //globalOutputStream() << "dot: " << dot << "\n";
869         if ( dot == 0 ) {
870                 // The projection axis chosen for the transformed normal is at 90 degrees
871                 // to the transformed projection axis chosen for the original normal.
872                 // This happens when the projection axis is ambiguous - e.g. for the plane
873                 // 'X == Y' the projection axis could be either X or Y.
874
875                 Matrix4 identityCorrected = matrix4_reflection_for_plane45( plane, originalProjectionAxis, transformedProjectionAxis );
876
877                 identity2stOriginal = matrix4_multiplied_by_matrix4( identity2stOriginal, identityCorrected );
878         }
879
880         Matrix4 stTransformed2stOriginal = matrix4_multiplied_by_matrix4( identity2stOriginal, stTransformed2identity );
881
882         Texdef_fromTransform( projection, (float)width, (float)height, stTransformed2stOriginal );
883         Texdef_normalise( projection, (float)width, (float)height );
884 }
885
886 #if 1
887 void Q3_to_matrix( const texdef_t& texdef, float width, float height, const Vector3& normal, Matrix4& matrix ){
888         Normal_GetTransform( normal, matrix );
889
890         Matrix4 transform;
891
892         Texdef_toTransform( texdef, width, height, transform );
893
894         matrix4_multiply_by_matrix4( matrix, transform );
895 }
896
897 void BP_from_matrix( brushprimit_texdef_t& bp_texdef, const Vector3& normal, const Matrix4& transform ){
898         Matrix4 basis;
899         basis = g_matrix4_identity;
900         ComputeAxisBase( normal, vector4_to_vector3( basis.x() ), vector4_to_vector3( basis.y() ) );
901         vector4_to_vector3( basis.z() ) = normal;
902         matrix4_transpose( basis );
903         matrix4_affine_invert( basis );
904
905         Matrix4 basis2texture = matrix4_multiplied_by_matrix4( basis, transform );
906
907         BPTexdef_fromTransform( bp_texdef, basis2texture );
908 }
909
910 void Q3_to_BP( const texdef_t& texdef, float width, float height, const Vector3& normal, brushprimit_texdef_t& bp_texdef ){
911         Matrix4 matrix;
912         Q3_to_matrix( texdef, width, height, normal, matrix );
913         BP_from_matrix( bp_texdef, normal, matrix );
914 }
915 #endif