Ticket #750: ro_matrix_r1.patch

File ro_matrix_r1.patch, 10.2 KB (added by Rodolphe Ortalo, 13 years ago)

Patch with respect to trunk and revision 9048

  • source/graphics/Camera.cpp

     
    189189
    190190void CCamera::GetScreenCoordinates(const CVector3D& world, float& x, float& y) const
    191191{
    192     CMatrix3D transform;
    193     m_Orientation.GetInverse(transform);
    194     transform.Concatenate(m_ProjMat);
     192    CMatrix3D transform = m_ProjMat * m_Orientation.GetInverse();
    195193
    196194    CVector4D screenspace = transform.Transform(CVector4D(world.X, world.Y, world.Z, 1.0f));
    197195
  • source/maths/Matrix3D.h

     
    3232// CMatrix3D: a 4x4 matrix class for common operations in 3D
    3333class CMatrix3D
    3434{
     35// TODO: Could be made private easily: a single usage in main.cpp for sound
    3536public:
    3637    // the matrix data itself - accessible as either longhand names
    3738    // or via a flat or 2d array
  • source/maths/Matrix3D.cpp

     
    2626#include "Quaternion.h"
    2727#include "Vector4D.h"
    2828
    29 CMatrix3D::CMatrix3D ()
    30 {
    31 }
     29CMatrix3D::CMatrix3D () { }
    3230
    3331CMatrix3D::CMatrix3D(float a11,float a12,float a13,float a14,float a21,float a22,float a23,float a24,
    3432            float a31,float a32,float a33,float a34,float a41,float a42,float a43,float a44)
    35 {
    36     _11=a11;
    37     _12=a12;
    38     _13=a13;
    39     _14=a14;
     33    :   _11(a11), _12(a12), _13(a13), _14(a14),
     34        _21(a21), _22(a22), _23(a23), _24(a24),
     35        _31(a31), _32(a32), _33(a33), _34(a34),
     36        _41(a41), _42(a42), _43(a43), _44(a44)
     37{ }
    4038
    41     _21=a21;
    42     _22=a22;
    43     _23=a23;
    44     _24=a24;
    45 
    46     _31=a31;
    47     _32=a32;
    48     _33=a33;
    49     _34=a34;
    50 
    51     _41=a41;
    52     _42=a42;
    53     _43=a43;
    54     _44=a44;
    55 }
    56 
    57 
    5839CMatrix3D::CMatrix3D(float data[])
    5940{
    6041    for(int i=0; i<16; i++)
     
    157138
    158139void CMatrix3D::SetXRotation (float angle)
    159140{
    160     float Cos = cosf (angle);
    161     float Sin = sinf (angle);
     141    const float Cos = cosf (angle);
     142    const float Sin = sinf (angle);
    162143   
    163144    _11=1.0f; _12=0.0f; _13=0.0f; _14=0.0f;
    164145    _21=0.0f; _22=Cos;  _23=-Sin; _24=0.0f;
     
    168149
    169150void CMatrix3D::SetYRotation (float angle)
    170151{
    171     float Cos = cosf (angle);
    172     float Sin = sinf (angle);
     152    const float Cos = cosf (angle);
     153    const float Sin = sinf (angle);
    173154
    174155    _11=Cos;  _12=0.0f; _13=Sin;  _14=0.0f;
    175156    _21=0.0f; _22=1.0f; _23=0.0f; _24=0.0f;
     
    179160
    180161void CMatrix3D::SetZRotation (float angle)
    181162{
    182     float Cos = cosf (angle);
    183     float Sin = sinf (angle);
     163    const float Cos = cosf (angle);
     164    const float Sin = sinf (angle);
    184165
    185166    _11=Cos;  _12=-Sin; _13=0.0f; _14=0.0f;
    186167    _21=Sin;  _22=Cos;  _23=0.0f; _24=0.0f;
     
    193174
    194175void CMatrix3D::RotateX (float angle)
    195176{
    196     CMatrix3D Temp;
    197     Temp.SetXRotation (angle);
    198     Concatenate(Temp); 
     177    const float Cos = cosf (angle);
     178    const float Sin = sinf (angle);
     179    const float tmp_21 = _21;
     180    const float tmp_22 = _22;
     181    const float tmp_23 = _23;
     182    const float tmp_24 = _24;
     183
     184    _21 =   Cos*_21 - Sin*_31;
     185    _22 =   Cos*_22 - Sin*_32;
     186    _23 =   Cos*_23 - Sin*_33;
     187    _24 =   Cos*_24 - Sin*_34;
     188
     189    _31 =   Sin*tmp_21 + Cos*_31;
     190    _32 =   Sin*tmp_22 + Cos*_32;
     191    _33 =   Sin*tmp_23 + Cos*_33;
     192    _34 =   Sin*tmp_24 + Cos*_34;
    199193}
    200194
    201195void CMatrix3D::RotateY (float angle)
    202196{
    203     CMatrix3D Temp;
    204     Temp.SetYRotation (angle);
    205     Concatenate(Temp);
     197    const float Cos = cosf (angle);
     198    const float Sin = sinf (angle);
     199    const float tmp_11 = _11;
     200    const float tmp_12 = _12;
     201    const float tmp_13 = _13;
     202    const float tmp_14 = _14;
     203
     204    _11 =   Cos*_11 + Sin*_31;
     205    _12 =   Cos*_12 + Sin*_32;
     206    _13 =   Cos*_13 + Sin*_33;
     207    _14 =   Cos*_14 + Sin*_34;
     208
     209    _31 =   -Sin*tmp_11 + Cos*_31;
     210    _32 =   -Sin*tmp_12 + Cos*_32;
     211    _33 =   -Sin*tmp_13 + Cos*_33;
     212    _34 =   -Sin*tmp_14 + Cos*_34;
    206213}
    207214
    208215void CMatrix3D::RotateZ (float angle)
    209216{
    210     CMatrix3D Temp;
    211     Temp.SetZRotation(angle);
    212     Concatenate(Temp);
     217    const float Cos = cosf (angle);
     218    const float Sin = sinf (angle);
     219    const float tmp_11 = _11;
     220    const float tmp_12 = _12;
     221    const float tmp_13 = _13;
     222    const float tmp_14 = _14;
     223
     224    _11 =   Cos*_11 - Sin*_21;
     225    _12 =   Cos*_12 - Sin*_22;
     226    _13 =   Cos*_13 - Sin*_23;
     227    _14 =   Cos*_14 - Sin*_24;
     228
     229    _21 =   Sin*tmp_11 + Cos*_21;
     230    _22 =   Sin*tmp_12 + Cos*_22;
     231    _23 =   Sin*tmp_13 + Cos*_23;
     232    _24 =   Sin*tmp_14 + Cos*_24;
    213233}
    214234
    215235//Sets the translation of the matrix
     
    248268
    249269CVector3D CMatrix3D::GetTranslation() const
    250270{
    251     CVector3D Temp;
    252 
    253     Temp.X = _14;
    254     Temp.Y = _24;
    255     Temp.Z = _34;
    256 
    257     return Temp;
     271    return CVector3D(_14,_24,_34);
    258272}
    259273
    260274//Clears and sets the scaling of the matrix
     
    269283//Scales the matrix
    270284void CMatrix3D::Scale (float x_scale, float y_scale, float z_scale)
    271285{
    272     CMatrix3D Temp;
    273     Temp.SetScaling(x_scale,y_scale,z_scale);
    274     Concatenate(Temp);
     286    _11 *=  x_scale;
     287    _12 *=  x_scale;
     288    _13 *=  x_scale;
     289    _14 *=  x_scale;
     290
     291    _21 *=  y_scale;
     292    _22 *=  y_scale;
     293    _23 *=  y_scale;
     294    _24 *=  y_scale;
     295
     296    _31 *=  z_scale;
     297    _32 *=  z_scale;
     298    _33 *=  z_scale;
     299    _34 *=  z_scale;
    275300}
    276301
    277302//Returns the transpose of the matrix. For orthonormal
    278303//matrices, this is the same is the inverse matrix
    279304CMatrix3D CMatrix3D::GetTranspose() const
    280305{
    281     CMatrix3D result;
    282 
    283     result._11 = _11;
    284     result._21 = _12;
    285     result._31 = _13;
    286     result._41 = _14;
    287 
    288     result._12 = _21;
    289     result._22 = _22;
    290     result._32 = _23;
    291     result._42 = _24;
    292 
    293     result._13 = _31;
    294     result._23 = _32;
    295     result._33 = _33;
    296     result._43 = _34;
    297 
    298     result._14 = _41;
    299     result._24 = _42;
    300     result._34 = _43;
    301     result._44 = _44;
    302 
    303     return result;
     306    return CMatrix3D(
     307        _11, _21, _31, _41,
     308        _12, _22, _32, _42,
     309        _13, _23, _33, _43,
     310        _14, _24, _34, _44 );
    304311}
    305312
    306313
    307314//Get a vector which points to the left of the matrix
    308315CVector3D CMatrix3D::GetLeft () const
    309316{
    310     CVector3D Temp;
    311 
    312     Temp.X = -_11;
    313     Temp.Y = -_21;
    314     Temp.Z = -_31;
    315 
    316     return Temp;
     317    return CVector3D(-_11,-_21,-_31);
    317318}
    318319
    319320//Get a vector which points up from the matrix
    320321CVector3D CMatrix3D::GetUp () const
    321322{
    322     CVector3D Temp;
    323 
    324     Temp.X = _12;
    325     Temp.Y = _22;
    326     Temp.Z = _32;
    327 
    328     return Temp;
     323    return CVector3D(_12,_22,_32);
    329324}
    330325
    331326//Get a vector which points to front of the matrix
    332327CVector3D CMatrix3D::GetIn () const
    333328{
    334     CVector3D Temp;
    335 
    336     Temp.X = _13;
    337     Temp.Y = _23;
    338     Temp.Z = _33;
    339 
    340     return Temp;
     329    return CVector3D(_13,_23,_33);
    341330}
    342331
    343332//Transform a vector by this matrix
     
    404393    tmp[11] = src[9] * src[12];
    405394   
    406395    // calculate first 8 elements (cofactors)
    407     dst._data[0] = tmp[0]*src[5] + tmp[3]*src[6] + tmp[4]*src[7];
    408     dst._data[0] -= tmp[1]*src[5] + tmp[2]*src[6] + tmp[5]*src[7];
    409     dst._data[1] = tmp[1]*src[4] + tmp[6]*src[6] + tmp[9]*src[7];
    410     dst._data[1] -= tmp[0]*src[4] + tmp[7]*src[6] + tmp[8]*src[7];
    411     dst._data[2] = tmp[2]*src[4] + tmp[7]*src[5] + tmp[10]*src[7];
    412     dst._data[2] -= tmp[3]*src[4] + tmp[6]*src[5] + tmp[11]*src[7];
    413     dst._data[3] = tmp[5]*src[4] + tmp[8]*src[5] + tmp[11]*src[6];
    414     dst._data[3] -= tmp[4]*src[4] + tmp[9]*src[5] + tmp[10]*src[6];
    415     dst._data[4] = tmp[1]*src[1] + tmp[2]*src[2] + tmp[5]*src[3];
    416     dst._data[4] -= tmp[0]*src[1] + tmp[3]*src[2] + tmp[4]*src[3];
    417     dst._data[5] = tmp[0]*src[0] + tmp[7]*src[2] + tmp[8]*src[3];
    418     dst._data[5] -= tmp[1]*src[0] + tmp[6]*src[2] + tmp[9]*src[3];
    419     dst._data[6] = tmp[3]*src[0] + tmp[6]*src[1] + tmp[11]*src[3];
    420     dst._data[6] -= tmp[2]*src[0] + tmp[7]*src[1] + tmp[10]*src[3];
    421     dst._data[7] = tmp[4]*src[0] + tmp[9]*src[1] + tmp[10]*src[2];
    422     dst._data[7] -= tmp[5]*src[0] + tmp[8]*src[1] + tmp[11]*src[2];
     396    dst._data[0] = (tmp[0]-tmp[1])*src[5] + (tmp[3]-tmp[2])*src[6] + (tmp[4]-tmp[5])*src[7];
     397    dst._data[1] = (tmp[1]-tmp[0])*src[4] + (tmp[6]-tmp[7])*src[6] + (tmp[9]-tmp[8])*src[7];
     398    dst._data[2] = (tmp[2]-tmp[3])*src[4] + (tmp[7]-tmp[6])*src[5] + (tmp[10]-tmp[11])*src[7];
     399    dst._data[3] = (tmp[5]-tmp[4])*src[4] + (tmp[8]-tmp[9])*src[5] + (tmp[11]-tmp[10])*src[6];
     400    dst._data[4] = (tmp[1]-tmp[0])*src[1] + (tmp[2]-tmp[3])*src[2] + (tmp[5]-tmp[4])*src[3];
     401    dst._data[5] = (tmp[0]-tmp[1])*src[0] + (tmp[7]-tmp[6])*src[2] + (tmp[8]-tmp[9])*src[3];
     402    dst._data[6] = (tmp[3]-tmp[2])*src[0] + (tmp[6]-tmp[7])*src[1] + (tmp[11]-tmp[10])*src[3];
     403    dst._data[7] = (tmp[4]-tmp[5])*src[0] + (tmp[9]-tmp[8])*src[1] + (tmp[10]-tmp[11])*src[2];
    423404   
    424405    // calculate pairs for second 8 elements (cofactors)
    425406    tmp[0] = src[2]*src[7];
     
    436417    tmp[11] = src[1]*src[4];
    437418
    438419    // calculate second 8 elements (cofactors)
    439     dst._data[8] = tmp[0]*src[13] + tmp[3]*src[14] + tmp[4]*src[15];
    440     dst._data[8] -= tmp[1]*src[13] + tmp[2]*src[14] + tmp[5]*src[15];
    441     dst._data[9] = tmp[1]*src[12] + tmp[6]*src[14] + tmp[9]*src[15];
    442     dst._data[9] -= tmp[0]*src[12] + tmp[7]*src[14] + tmp[8]*src[15];
    443     dst._data[10] = tmp[2]*src[12] + tmp[7]*src[13] + tmp[10]*src[15];
    444     dst._data[10]-= tmp[3]*src[12] + tmp[6]*src[13] + tmp[11]*src[15];
    445     dst._data[11] = tmp[5]*src[12] + tmp[8]*src[13] + tmp[11]*src[14];
    446     dst._data[11]-= tmp[4]*src[12] + tmp[9]*src[13] + tmp[10]*src[14];
    447     dst._data[12] = tmp[2]*src[10] + tmp[5]*src[11] + tmp[1]*src[9];
    448     dst._data[12]-= tmp[4]*src[11] + tmp[0]*src[9] + tmp[3]*src[10];
    449     dst._data[13] = tmp[8]*src[11] + tmp[0]*src[8] + tmp[7]*src[10];
    450     dst._data[13]-= tmp[6]*src[10] + tmp[9]*src[11] + tmp[1]*src[8];
    451     dst._data[14] = tmp[6]*src[9] + tmp[11]*src[11] + tmp[3]*src[8];
    452     dst._data[14]-= tmp[10]*src[11] + tmp[2]*src[8] + tmp[7]*src[9];
    453     dst._data[15] = tmp[10]*src[10] + tmp[4]*src[8] + tmp[9]*src[9];
    454     dst._data[15]-= tmp[8]*src[9] + tmp[11]*src[10] + tmp[5]*src[8];
     420    dst._data[8] = (tmp[0]-tmp[1])*src[13] + (tmp[3]-tmp[2])*src[14] + (tmp[4]-tmp[5])*src[15];
     421    dst._data[9] = (tmp[1]-tmp[0])*src[12] + (tmp[6]-tmp[7])*src[14] + (tmp[9]-tmp[8])*src[15];
     422    dst._data[10] = (tmp[2]-tmp[3])*src[12] + (tmp[7]-tmp[6])*src[13] + (tmp[10]-tmp[11])*src[15];
     423    dst._data[11] = (tmp[5]-tmp[4])*src[12] + (tmp[8]-tmp[9])*src[13] + (tmp[11]-tmp[10])*src[14];
     424    dst._data[12] = (tmp[2]-tmp[3])*src[10] + (tmp[5]-tmp[4])*src[11] + (tmp[1]-tmp[0])*src[9];
     425    dst._data[13] = (tmp[7]-tmp[6])*src[10] + (tmp[8]-tmp[9])*src[11] + (tmp[0]-tmp[1])*src[8];
     426    dst._data[14] = (tmp[6]-tmp[7])*src[9] + (tmp[11]-tmp[10])*src[11] + (tmp[3]-tmp[2])*src[8];
     427    dst._data[15] = (tmp[10]-tmp[11])*src[10] + (tmp[4]-tmp[5])*src[8] + (tmp[9]-tmp[8])*src[9];
    455428
    456429    // calculate matrix inverse
    457430    det=src[0]*dst._data[0]+src[1]*dst._data[1]+src[2]*dst._data[2]+src[3]*dst._data[3];