medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit df530cce6214ed1f47af331f5e1b0d29ac21c088
parent 8623d653d8652230c2fec2bb9813e1be29c054ba
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Dec 30 21:33:30 +0200

Use column-major matrices

Diffstat:
linear_algebra.h | 254+++++++++++++++++++++++++++++++++++++++----------------------------------------
visitors/linear_algebra.h | 36++++++++++++++++++------------------
2 files changed, 144 insertions(+), 146 deletions(-)
diff --git a/linear_algebra.h b/linear_algebra.h @@ -49,17 +49,25 @@ struct v2s32 { // AUTOVISITOR struct m2 { - v2 row0, row1; + v2 col0, col1; m2() { } - explicit m2( float e0, float e1, float e2, float e3 ) { - row0 = v2( e0, e1 ); - row1 = v2( e2, e3 ); + explicit m2( + float e00, float e01, + float e10, float e11 + ) { + col0 = v2( e00, e10 ); + col1 = v2( e01, e11 ); + } + + explicit m2( const v2 & c0, const v2 & c1 ) { + col0 = c0; + col1 = c1; } - v2 col0() const { return v2( row0.x, row1.x ); } - v2 col1() const { return v2( row0.y, row1.y ); } + v2 row0() const { return v2( col0.x, col1.x ); } + v2 row1() const { return v2( col0.y, col1.y ); } }; // AUTOVISITOR @@ -108,29 +116,29 @@ struct v3u32 { // AUTOVISITOR struct m3 { - v3 row0, row1, row2; + v3 col0, col1, col2; m3() { } explicit m3( - float e0, float e1, float e2, - float e3, float e4, float e5, - float e6, float e7, float e8 + float e00, float e01, float e02, + float e10, float e11, float e12, + float e20, float e21, float e22 ) { - row0 = v3( e0, e1, e2 ); - row1 = v3( e3, e4, e5 ); - row2 = v3( e6, e7, e8 ); + col0 = v3( e00, e10, e20 ); + col1 = v3( e01, e11, e21 ); + col2 = v3( e02, e12, e22 ); } - explicit m3( v3 r0, v3 r1, v3 r2 ) { - row0 = r0; - row1 = r1; - row2 = r2; + explicit m3( const v3 & c0, const v3 & c1, const v3 & c2 ) { + col0 = c0; + col1 = c1; + col2 = c2; } - v3 col0() const { return v3( row0.x, row1.x, row2.x ); } - v3 col1() const { return v3( row0.y, row1.y, row2.y ); } - v3 col2() const { return v3( row0.z, row1.z, row2.z ); } + v3 row0() const { return v3( col0.x, col1.x, col2.x ); } + v3 row1() const { return v3( col0.y, col1.y, col2.y ); } + v3 row2() const { return v3( col0.z, col1.z, col2.z ); } }; // AUTOVISITOR @@ -179,52 +187,33 @@ struct v4 { // AUTOVISITOR struct m4 { - v4 row0, row1, row2, row3; + v4 col0, col1, col2, col3; m4() { } explicit m4( - float e0, float e1, float e2, float e3, - float e4, float e5, float e6, float e7, - float e8, float e9, float e10, float e11, - float e12, float e13, float e14, float e15 + float e00, float e01, float e02, float e03, + float e10, float e11, float e12, float e13, + float e20, float e21, float e22, float e23, + float e30, float e31, float e32, float e33 ) { - row0 = v4( e0, e1, e2, e3 ); - row1 = v4( e4, e5, e6, e7 ); - row2 = v4( e8, e9, e10, e11 ); - row3 = v4( e12, e13, e14, e15 ); - } - - explicit m4( v4 r0, v4 r1, v4 r2, v4 r3 ) { - row0 = r0; - row1 = r1; - row2 = r2; - row3 = r3; - } - - explicit m4( const m3 & m ) { - row0 = v4( m.row0, 0 ); - row1 = v4( m.row1, 0 ); - row2 = v4( m.row2, 0 ); - row3 = v4( 0, 0, 0, 1 ); + col0 = v4( e00, e10, e20, e30 ); + col1 = v4( e01, e11, e21, e31 ); + col2 = v4( e02, e12, e22, e32 ); + col3 = v4( e03, e13, e23, e33 ); } - v4 col0() const { return v4( row0.x, row1.x, row2.x, row3.x ); } - v4 col1() const { return v4( row0.y, row1.y, row2.y, row3.y ); } - v4 col2() const { return v4( row0.z, row1.z, row2.z, row3.z ); } - v4 col3() const { return v4( row0.w, row1.w, row2.w, row3.w ); } - - float & operator[]( size_t idx ) { - ASSERT( idx < 16 ); - v4 rows[] = { row0, row1, row2, row3 }; - return rows[ idx / 4 ][ idx % 4 ]; + explicit m4( const v4 & c0, const v4 & c1, const v4 & c2, const v4 & c3 ) { + col0 = c0; + col1 = c1; + col2 = c2; + col3 = c3; } - const float & operator[]( size_t idx ) const { - ASSERT( idx < 16 ); - const v4 rows[] = { row0, row1, row2, row3 }; - return rows[ idx / 4 ][ idx % 4 ]; - } + v4 row0() const { return v4( col0.x, col1.x, col2.x, col3.x ); } + v4 row1() const { return v4( col0.y, col1.y, col2.y, col3.y ); } + v4 row2() const { return v4( col0.z, col1.z, col2.z, col3.z ); } + v4 row3() const { return v4( col0.w, col1.w, col2.w, col3.w ); } }; // AUTOVISITOR @@ -235,6 +224,26 @@ struct quat { // TODO: v3x4, v4x4, simd matrices? /* + * misc. TODO: put this stuff somewhere else + */ + +forceinline float deg_to_rad( float x ) { + return x * float( M_PI ) / 180.0f; +} + +forceinline float rad_to_deg( float x ) { + return x * 180.0f / float( M_PI ); +} + +forceinline v3 deg_to_rad( const v3 & v ) { + return v3( deg_to_rad( v.x ), deg_to_rad( v.y ), deg_to_rad( v.z ) ); +} + +forceinline v3 rad_to_deg( const v3 & v ) { + return v3( rad_to_deg( v.x ), rad_to_deg( v.y ), rad_to_deg( v.z ) ); +} + +/* * v2 */ @@ -320,13 +329,13 @@ forceinline m2 m2_scale( float x, float y ) { forceinline m2 operator*( const m2 & lhs, const m2 & rhs ) { return m2( - dot( lhs.row0, rhs.col0() ), dot( lhs.row0, rhs.col1() ), - dot( lhs.row1, rhs.col0() ), dot( lhs.row1, rhs.col1() ) + dot( lhs.row0(), rhs.col0 ), dot( lhs.row0(), rhs.col1 ), + dot( lhs.row1(), rhs.col0 ), dot( lhs.row1(), rhs.col1 ) ); } forceinline v2 operator*( const m2 & m, v2 v ) { - return v2( dot( m.row0, v ), dot( m.row1, v ) ); + return v2( dot( m.row0(), v ), dot( m.row1(), v ) ); } /* @@ -468,30 +477,30 @@ forceinline m3 m3_rotz( float theta ) { forceinline m3 operator*( const m3 & lhs, const m3 & rhs ) { return m3( - dot( lhs.row0, rhs.col0() ), - dot( lhs.row0, rhs.col1() ), - dot( lhs.row0, rhs.col2() ), + dot( lhs.row0(), rhs.col0 ), + dot( lhs.row0(), rhs.col1 ), + dot( lhs.row0(), rhs.col2 ), - dot( lhs.row1, rhs.col0() ), - dot( lhs.row1, rhs.col1() ), - dot( lhs.row1, rhs.col2() ), + dot( lhs.row1(), rhs.col0 ), + dot( lhs.row1(), rhs.col1 ), + dot( lhs.row1(), rhs.col2 ), - dot( lhs.row2, rhs.col0() ), - dot( lhs.row2, rhs.col1() ), - dot( lhs.row2, rhs.col2() ) + dot( lhs.row2(), rhs.col0 ), + dot( lhs.row2(), rhs.col1 ), + dot( lhs.row2(), rhs.col2 ) ); } forceinline v3 operator*( const m3 & m, v3 v ) { return v3( - dot( m.row0, v ), - dot( m.row1, v ), - dot( m.row2, v ) + dot( m.row0(), v ), + dot( m.row1(), v ), + dot( m.row2(), v ) ); } forceinline m3 operator-( const m3 & m ) { - return m3( -m.row0, -m.row1, -m.row2 ); + return m3( -m.col0, -m.col1, -m.col2 ); } /* @@ -616,10 +625,10 @@ forceinline m4 m4_rotz( float theta ) { forceinline m4 m4_translation( float x, float y, float z ) { return m4( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - x, y, z, 1 + 1, 0, 0, x, + 0, 1, 0, y, + 0, 0, 1, z, + 0, 0, 0, 1 ); } @@ -632,87 +641,86 @@ forceinline m4 m4_ortho( float left, float right, float top, float bottom, float 2.0f / ( right - left ), 0.0f, 0.0f, - 0.0f, + -( right + left ) / ( right - left ), 0.0f, 2.0f / ( top - bottom ), 0.0f, - 0.0f, + -( top + bottom ) / ( top - bottom ), 0.0f, 0.0f, -2.0f / ( far_plane - near_plane ), - 0.0f, + -( far_plane + near_plane ) / ( far_plane - near_plane ), - ( right + left ) / ( left - right ), - ( top + bottom ) / ( bottom - top ), - ( far_plane + near_plane ) / ( near_plane - far_plane ), + 0.0f, + 0.0f, + 0.0f, 1.0f ); } forceinline m4 m4_perspective( float vertical_fov_degrees, float aspect_ratio, float near_plane, float far_plane ) { - float vertical_fov = vertical_fov_degrees * float( M_PI ) / 180.0f / 2.0f; - float horizontal_fov = atanf( tanf( vertical_fov ) * aspect_ratio ); + float tan_half_vertical_fov = tanf( deg_to_rad( vertical_fov_degrees ) / 2.0f ); return m4( - 1.0f / tanf( horizontal_fov ), + 1.0f / ( tan_half_vertical_fov * aspect_ratio ), 0.0f, 0.0f, 0.0f, 0.0f, - 1.0f / tanf( vertical_fov ), + 1.0f / tan_half_vertical_fov, 0.0f, 0.0f, 0.0f, 0.0f, - ( far_plane + near_plane ) / ( near_plane - far_plane ), - -1.0f, + -( far_plane + near_plane ) / ( far_plane - near_plane ), + -( 2.0f * far_plane * near_plane ) / ( far_plane - near_plane ), 0.0f, 0.0f, - ( 2.0f * far_plane * near_plane ) / ( near_plane - far_plane ), + -1.0f, 0.0f ); } -forceinline v4 operator*( const m4 & m, v4 v ) { - return v4( - dot( m.row0, v ), - dot( m.row1, v ), - dot( m.row2, v ), - dot( m.row3, v ) +forceinline m4 operator*( const m4 & lhs, const m4 & rhs ) { + return m4( + dot( lhs.row0(), rhs.col0 ), + dot( lhs.row0(), rhs.col1 ), + dot( lhs.row0(), rhs.col2 ), + dot( lhs.row0(), rhs.col3 ), + + dot( lhs.row1(), rhs.col0 ), + dot( lhs.row1(), rhs.col1 ), + dot( lhs.row1(), rhs.col2 ), + dot( lhs.row1(), rhs.col3 ), + + dot( lhs.row2(), rhs.col0 ), + dot( lhs.row2(), rhs.col1 ), + dot( lhs.row2(), rhs.col2 ), + dot( lhs.row2(), rhs.col3 ), + + dot( lhs.row3(), rhs.col0 ), + dot( lhs.row3(), rhs.col1 ), + dot( lhs.row3(), rhs.col2 ), + dot( lhs.row3(), rhs.col3 ) ); } -forceinline m4 operator*( const m4 & lhs, const m4 & rhs ) { - return m4( - dot( lhs.row0, rhs.col0() ), - dot( lhs.row0, rhs.col1() ), - dot( lhs.row0, rhs.col2() ), - dot( lhs.row0, rhs.col3() ), - - dot( lhs.row1, rhs.col0() ), - dot( lhs.row1, rhs.col1() ), - dot( lhs.row1, rhs.col2() ), - dot( lhs.row1, rhs.col3() ), - - dot( lhs.row2, rhs.col0() ), - dot( lhs.row2, rhs.col1() ), - dot( lhs.row2, rhs.col2() ), - dot( lhs.row2, rhs.col3() ), - - dot( lhs.row3, rhs.col0() ), - dot( lhs.row3, rhs.col1() ), - dot( lhs.row3, rhs.col2() ), - dot( lhs.row3, rhs.col3() ) +forceinline v4 operator*( const m4 & m, v4 v ) { + return v4( + dot( m.row0(), v ), + dot( m.row1(), v ), + dot( m.row2(), v ), + dot( m.row3(), v ) ); } forceinline m4 operator-( const m4 & m ) { - return m4( -m.row0, -m.row1, -m.row2, -m.row3 ); + return m4( -m.col0, -m.col1, -m.col2, -m.col3 ); } /* @@ -723,16 +731,6 @@ forceinline m4 operator-( const m4 & m ) { * misc. TODO: put this stuff somewhere else */ -template< typename T > -forceinline T radians( const T & x ) { - return x * float( M_PI ) / 180.0; -} - -template< typename T > -forceinline T degrees( const T & x ) { - return x * 180.0 / float( M_PI ); -} - forceinline u32 mean( u32 a, u32 b ) { return a / 2 + b / 2 + ( a & b & 1 ); } diff --git a/visitors/linear_algebra.h b/visitors/linear_algebra.h @@ -30,14 +30,14 @@ void visit( const v2s32 & obj, F f ) { template< typename F > void visit( m2 & obj, F f ) { - visit( obj.row0, f ); - visit( obj.row1, f ); + visit( obj.col0, f ); + visit( obj.col1, f ); } template< typename F > void visit( const m2 & obj, F f ) { - visit( obj.row0, f ); - visit( obj.row1, f ); + visit( obj.col0, f ); + visit( obj.col1, f ); } template< typename F > @@ -70,16 +70,16 @@ void visit( const v3u32 & obj, F f ) { template< typename F > void visit( m3 & obj, F f ) { - visit( obj.row0, f ); - visit( obj.row1, f ); - visit( obj.row2, f ); + visit( obj.col0, f ); + visit( obj.col1, f ); + visit( obj.col2, f ); } template< typename F > void visit( const m3 & obj, F f ) { - visit( obj.row0, f ); - visit( obj.row1, f ); - visit( obj.row2, f ); + visit( obj.col0, f ); + visit( obj.col1, f ); + visit( obj.col2, f ); } template< typename F > @@ -100,18 +100,18 @@ void visit( const v4 & obj, F f ) { template< typename F > void visit( m4 & obj, F f ) { - visit( obj.row0, f ); - visit( obj.row1, f ); - visit( obj.row2, f ); - visit( obj.row3, f ); + visit( obj.col0, f ); + visit( obj.col1, f ); + visit( obj.col2, f ); + visit( obj.col3, f ); } template< typename F > void visit( const m4 & obj, F f ) { - visit( obj.row0, f ); - visit( obj.row1, f ); - visit( obj.row2, f ); - visit( obj.row3, f ); + visit( obj.col0, f ); + visit( obj.col1, f ); + visit( obj.col2, f ); + visit( obj.col3, f ); } template< typename F >