2 Copyright (C) 1999-2006 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
22 #include "brush_primit.h"
24 #include "debugging/debugging.h"
27 #include "itextures.h"
32 #include "texturelib.h"
33 #include "math/matrix.h"
34 #include "math/plane.h"
35 #include "math/aabb.h"
38 #include "preferences.h"
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.
46 void Normal_GetTransform( const Vector3& normal, Matrix4& transform ){
47 switch ( projectionaxis_for_normal( normal ) )
49 case eProjectionAxisZ:
62 case eProjectionAxisY:
75 case eProjectionAxisX:
89 transform[3] = transform[7] = transform[11] = transform[12] = transform[13] = transform[14] = 0;
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.
98 inline void Texdef_toTransform( const texdef_t& texdef, float width, float height, Matrix4& transform ){
99 double inverse_scale[2];
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;
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];
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 );
132 Texdef_toTransform( projection.m_texdef, width, height, transform );
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 );
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 );
154 texdef.rotate = static_cast<float>( -radians_to_degrees( arctangent_yx( -transform[4], transform[0] ) ) );
156 if ( texdef.rotate == -180.0f ) {
157 texdef.rotate = 180.0f;
160 texdef.shift[0] = transform[12] * width;
161 texdef.shift[1] = transform[13] * height;
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];
171 texdef.scale[1] = -texdef.scale[1];
174 //globalOutputStream() << "fromTransform: " << texdef.shift[0] << " " << texdef.shift[1] << " " << texdef.scale[0] << " " << texdef.scale[1] << " " << texdef.rotate << "\n";
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();
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" );
190 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
191 BPTexdef_fromTransform( projection.m_brushprimit_texdef, transform );
195 Texdef_fromTransform( projection.m_texdef, width, height, transform );
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";
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 );
211 /// \brief Normalise \p projection for a given texture \p width and \p height.
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 );
221 Texdef_normalise( projection.m_texdef, width, height );
225 void ComputeAxisBase( const Vector3& normal, Vector3& texS, Vector3& texT );
227 inline void DebugAxisBase( const Vector3& normal ){
229 ComputeAxisBase( normal, x, y );
230 globalOutputStream() << "BP debug: " << x << y << normal << "\n";
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);
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 );
252 Normal_GetTransform( normal, basis );
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 ) {
262 Texdef_toTransform( projection, (float)width, (float)height, local2tex );
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 );
271 Vector3 tangent( vector3_normalised( vector4_to_vector3( matrix4_transposed( local2tex ).x() ) ) );
272 Vector3 bitangent( vector3_normalised( vector4_to_vector3( matrix4_transposed( local2tex ).y() ) ) );
274 matrix4_multiply_by_matrix4( local2tex, localToWorld );
276 for ( Winding::iterator i = w.begin(); i != w.end(); ++i )
278 Vector3 texcoord = matrix4_transformed_point( local2tex, ( *i ).vertex );
279 ( *i ).texcoord[0] = texcoord[0];
280 ( *i ).texcoord[1] = texcoord[1];
282 ( *i ).tangent = tangent;
283 ( *i ).bitangent = bitangent;
288 \brief Provides the axis-base of the texture ST space for this normal,
289 as they had been transformed to world XYZ space.
291 void TextureAxisFromNormal( const Vector3& normal, Vector3& s, Vector3& t ){
292 switch ( projectionaxis_for_normal( normal ) )
294 case eProjectionAxisZ:
304 case eProjectionAxisY:
314 case eProjectionAxisX:
327 void Texdef_Assign( texdef_t& td, const texdef_t& other ){
331 void Texdef_Shift( texdef_t& td, float s, float t ){
336 void Texdef_Scale( texdef_t& td, float s, float t ){
341 void Texdef_Rotate( texdef_t& td, float angle ){
343 td.rotate = static_cast<float>( float_to_integer( td.rotate ) % 360 );
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;
352 void AddPointToBounds( const Vector3& v, Vector3& mins, Vector3& maxs ){
356 for ( i = 0 ; i < 3 ; i++ )
359 if ( val < mins[i] ) {
362 if ( val > maxs[i] ) {
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() )
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 );
390 bp_globals_t g_bp_globals;
391 float g_texdef_default_scale;
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];
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 ){
410 M[2][0] = 1.0f; M[2][1] = 1.0f; M[2][2] = 1.0f;
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;
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 );
430 if ( vector3_equal_epsilon( normal, up, float(1e-6) ) ) {
431 texS = Vector3( 0, 1, 0 );
432 texT = Vector3( 1, 0, 0 );
434 else if ( vector3_equal_epsilon( normal, down, float(1e-6) ) ) {
435 texS = Vector3( 0, 1, 0 );
436 texT = Vector3( -1, 0, 0 );
440 texS = vector3_normalised( vector3_cross( normal, up ) );
441 texT = vector3_normalised( vector3_cross( normal, texS ) );
442 vector3_negate( texS );
446 typedef float texmat_t[2][3];
448 void TexMat_Scale( texmat_t texmat, float s, float t ){
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];
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 ) );
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.
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] ) ) );
480 texdef.rotate = -static_cast<float>( radians_to_degrees( arctangent_yx( bp_texdef.coords[1][0], bp_texdef.coords[0][0] ) ) );
482 texdef.shift[0] = -bp_texdef.coords[0][2];
483 texdef.shift[1] = bp_texdef.coords[1][2];
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] ) );
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];
498 texdef.scale[1] = -texdef.scale[1];
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 );
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];
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 ){
523 sx = vector3_dot( v, X );
524 sy = vector3_dot( v, Y );
525 if ( fabs( sy ) > fabs( sx ) ) {
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];
556 void BPMatRotate( float A[2][3], float theta ){
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 ) ) );
564 BPMatMul( A, m, aux );
568 void BPTexdef_Assign( brushprimit_texdef_t& bp_td, const brushprimit_texdef_t& bp_other ){
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;
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
585 // compute fake shift scale rot
586 TexMatToFakeTexCoords( bp_td, texdef );
588 texdef.scale[0] += s;
589 texdef.scale[1] += t;
590 // compute new normalized texture matrix
591 FakeTexCoordsToTexMat( texdef, bp_td );
594 void BPTexdef_Rotate( brushprimit_texdef_t& bp_td, float angle ){
595 // apply same scale as the spinner button of the surface inspector
597 // compute fake shift scale rot
598 TexMatToFakeTexCoords( bp_td, texdef );
600 texdef.rotate += angle;
601 // compute new normalized texture matrix
602 FakeTexCoordsToTexMat( texdef, bp_td );
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 );
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 );
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;
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 );
631 Texdef_Shift( projection.m_texdef, s, t );
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 );
641 Texdef_Scale( projection.m_texdef, s, t );
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 );
651 Texdef_Rotate( projection.m_texdef, angle );
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 ) {
661 Texdef_toTransform( projection, (float)width, (float)height, st2tex );
663 // the current texture transform
664 Matrix4 local2tex = st2tex;
667 Texdef_basisForNormal( projection, normal, xyz2st );
668 matrix4_multiply_by_matrix4( local2tex, xyz2st );
671 // the bounds of the current texture transform
673 for ( Winding::const_iterator i = w.begin(); i != w.end(); ++i )
675 Vector3 texcoord = matrix4_transformed_point( local2tex, ( *i ).vertex );
676 aabb_extend_by_point_safe( bounds, texcoord );
678 bounds.origin.z() = 0;
679 bounds.extents.z() = 1;
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 ) );
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 );
689 // apply the difference to the current texture transform
690 matrix4_premultiply_by_matrix4( st2tex, matrix );
692 Texdef_fromTransform( projection, (float)width, (float)height, st2tex );
693 Texdef_normalise( projection, (float)width, (float)height );
696 float Texdef_getDefaultTextureScale(){
697 return g_texdef_default_scale;
700 void TexDef_Construct_Default( TextureProjection& projection ){
701 projection.m_texdef.scale[0] = Texdef_getDefaultTextureScale();
702 projection.m_texdef.scale[1] = Texdef_getDefaultTextureScale();
704 if ( g_bp_globals.m_texdefTypeId == TEXDEFTYPEID_BRUSHPRIMITIVES ) {
705 FakeTexCoordsToTexMat( projection.m_texdef, projection.m_brushprimit_texdef );
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 );
717 shiftScaleRotate = projection.m_texdef;
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 );
729 projection.m_texdef = shiftScaleRotate;
734 inline void print_vector3( const Vector3& v ){
735 globalOutputStream() << "( " << v.x() << " " << v.y() << " " << v.z() << " )\n";
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";
745 inline Matrix4 matrix4_rotation_for_vector3( const Vector3& x, const Vector3& y, const Vector3& z ){
747 x.x(), x.y(), x.z(), 0,
748 y.x(), y.y(), y.z(), 0,
749 z.x(), z.y(), z.z(), 0,
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 );
759 if ( from.x() != 0 && to.z() != 0 ) {
760 return matrix4_rotation_for_vector3( to, g_vector3_axis_y, from );
763 if ( from.y() != 0 && to.z() != 0 ) {
764 return matrix4_rotation_for_vector3( g_vector3_axis_x, to, from );
767 if ( from.y() != 0 && to.x() != 0 ) {
768 return matrix4_rotation_for_vector3( from, to, g_vector3_axis_z );
771 if ( from.z() != 0 && to.x() != 0 ) {
772 return matrix4_rotation_for_vector3( from, g_vector3_axis_y, to );
775 if ( from.z() != 0 && to.y() != 0 ) {
776 return matrix4_rotation_for_vector3( g_vector3_axis_x, from, to );
779 ERROR_MESSAGE( "unhandled axis swap case" );
781 return g_matrix4_identity;
784 inline Matrix4 matrix4_reflection_for_plane( const Plane3& plane ){
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 ),
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 ),
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 ) ),
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 ),
805 inline Matrix4 matrix4_reflection_for_plane45( const Plane3& plane, const Vector3& from, const Vector3& to ){
806 Vector3 first = from;
809 if ( vector3_dot( from, plane.normal() ) > 0 == vector3_dot( to, plane.normal() ) > 0 ) {
810 first = vector3_negated( first );
811 second = vector3_negated( second );
815 Matrix4 swap = matrix4_swap_axes( first, second );
817 Matrix4 tmp = matrix4_reflection_for_plane( plane );
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 );
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";
829 //globalOutputStream() << "plane.normal(): " << plane.normal() << "\n";
831 Vector3 normalTransformed( matrix4_transformed_direction( identity2transformed, plane.normal() ) );
833 //globalOutputStream() << "normalTransformed: " << normalTransformed << "\n";
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
841 // stTransformed2stOriginal = stTransformed -> transformed -> identity -> stIdentity -> stOriginal
843 Matrix4 identity2stIdentity;
844 Texdef_basisForNormal( projection, plane.normal(), identity2stIdentity );
845 //globalOutputStream() << "identity2stIdentity: " << identity2stIdentity << "\n";
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 );
852 Matrix4 transformed2stTransformed;
853 Texdef_basisForNormal( projection, normalTransformed, transformed2stTransformed );
855 Matrix4 stTransformed2identity( matrix4_affine_inverse( matrix4_multiplied_by_matrix4( transformed2stTransformed, identity2transformed ) ) );
857 Vector3 originalProjectionAxis( vector4_to_vector3( matrix4_affine_inverse( identity2stIdentity ).z() ) );
859 Vector3 transformedProjectionAxis( vector4_to_vector3( stTransformed2identity.z() ) );
861 Matrix4 stIdentity2stOriginal;
862 Texdef_toTransform( projection, (float)width, (float)height, stIdentity2stOriginal );
863 Matrix4 identity2stOriginal( matrix4_multiplied_by_matrix4( stIdentity2stOriginal, identity2stIdentity ) );
865 //globalOutputStream() << "originalProj: " << originalProjectionAxis << "\n";
866 //globalOutputStream() << "transformedProj: " << transformedProjectionAxis << "\n";
867 double dot = vector3_dot( originalProjectionAxis, transformedProjectionAxis );
868 //globalOutputStream() << "dot: " << dot << "\n";
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.
875 Matrix4 identityCorrected = matrix4_reflection_for_plane45( plane, originalProjectionAxis, transformedProjectionAxis );
877 identity2stOriginal = matrix4_multiplied_by_matrix4( identity2stOriginal, identityCorrected );
880 Matrix4 stTransformed2stOriginal = matrix4_multiplied_by_matrix4( identity2stOriginal, stTransformed2identity );
882 Texdef_fromTransform( projection, (float)width, (float)height, stTransformed2stOriginal );
883 Texdef_normalise( projection, (float)width, (float)height );
887 void Q3_to_matrix( const texdef_t& texdef, float width, float height, const Vector3& normal, Matrix4& matrix ){
888 Normal_GetTransform( normal, matrix );
892 Texdef_toTransform( texdef, width, height, transform );
894 matrix4_multiply_by_matrix4( matrix, transform );
897 void BP_from_matrix( brushprimit_texdef_t& bp_texdef, const Vector3& normal, const Matrix4& transform ){
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 );
905 Matrix4 basis2texture = matrix4_multiplied_by_matrix4( basis, transform );
907 BPTexdef_fromTransform( bp_texdef, basis2texture );
910 void Q3_to_BP( const texdef_t& texdef, float width, float height, const Vector3& normal, brushprimit_texdef_t& bp_texdef ){
912 Q3_to_matrix( texdef, width, height, normal, matrix );
913 BP_from_matrix( bp_texdef, normal, matrix );