"Fossies" - the Fresh Open Source Software Archive

Member "audacious-plugins-3.10.1/src/xsf/desmume/matrix.cc" (26 Dec 2018, 8759 Bytes) of package /linux/misc/audacious-plugins-3.10.1.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "matrix.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2     Copyright (C) 2006-2007 shash
    3 
    4     This file is part of DeSmuME
    5 
    6     DeSmuME is free software; you can redistribute it and/or modify
    7     it under the terms of the GNU General Public License as published by
    8     the Free Software Foundation; either version 2 of the License, or
    9     (at your option) any later version.
   10 
   11     DeSmuME is distributed in the hope that it will be useful,
   12     but WITHOUT ANY WARRANTY; without even the implied warranty of
   13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14     GNU General Public License for more details.
   15 
   16     You should have received a copy of the GNU General Public License
   17     along with DeSmuME; if not, write to the Free Software
   18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   19 */
   20 
   21 #include <stdio.h>
   22 #include <string.h>
   23 #include <stdlib.h>
   24 #include "matrix.h"
   25 
   26 void MatrixInit  (float *matrix)
   27 {
   28     memset (matrix, 0, sizeof(float)*16);
   29 
   30     matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.f;
   31 }
   32 
   33 void MatrixMultVec4x4 (float *matrix, float *vecPtr)
   34 {
   35     float x = vecPtr[0];
   36     float y = vecPtr[1];
   37     float z = vecPtr[2];
   38 
   39     vecPtr[0] = x * matrix[0] + y * matrix[4] + z * matrix[ 8] + matrix[12];
   40     vecPtr[1] = x * matrix[1] + y * matrix[5] + z * matrix[ 9] + matrix[13];
   41     vecPtr[2] = x * matrix[2] + y * matrix[6] + z * matrix[10] + matrix[14];
   42 }
   43 
   44 void MatrixMultVec3x3 (float *matrix, float *vecPtr)
   45 {
   46     float x = vecPtr[0];
   47     float y = vecPtr[1];
   48     float z = vecPtr[2];
   49 
   50     vecPtr[0] = x * matrix[0] + y * matrix[4] + z * matrix[ 8];
   51     vecPtr[1] = x * matrix[1] + y * matrix[5] + z * matrix[ 9];
   52     vecPtr[2] = x * matrix[2] + y * matrix[6] + z * matrix[10];
   53 }
   54 
   55 void MatrixIdentity (float *matrix)
   56 {
   57     memset (matrix, 0, sizeof(float)*16);
   58 
   59     matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.f;
   60 }
   61 
   62 void MatrixMultiply (float *matrix, float *rightMatrix)
   63 {
   64     float tmpMatrix[16];
   65 
   66     tmpMatrix[0]  = (matrix[0]*rightMatrix[0])+(matrix[4]*rightMatrix[1])+(matrix[8]*rightMatrix[2])+(matrix[12]*rightMatrix[3]);
   67     tmpMatrix[1]  = (matrix[1]*rightMatrix[0])+(matrix[5]*rightMatrix[1])+(matrix[9]*rightMatrix[2])+(matrix[13]*rightMatrix[3]);
   68     tmpMatrix[2]  = (matrix[2]*rightMatrix[0])+(matrix[6]*rightMatrix[1])+(matrix[10]*rightMatrix[2])+(matrix[14]*rightMatrix[3]);
   69     tmpMatrix[3]  = (matrix[3]*rightMatrix[0])+(matrix[7]*rightMatrix[1])+(matrix[11]*rightMatrix[2])+(matrix[15]*rightMatrix[3]);
   70 
   71     tmpMatrix[4]  = (matrix[0]*rightMatrix[4])+(matrix[4]*rightMatrix[5])+(matrix[8]*rightMatrix[6])+(matrix[12]*rightMatrix[7]);
   72     tmpMatrix[5]  = (matrix[1]*rightMatrix[4])+(matrix[5]*rightMatrix[5])+(matrix[9]*rightMatrix[6])+(matrix[13]*rightMatrix[7]);
   73     tmpMatrix[6]  = (matrix[2]*rightMatrix[4])+(matrix[6]*rightMatrix[5])+(matrix[10]*rightMatrix[6])+(matrix[14]*rightMatrix[7]);
   74     tmpMatrix[7]  = (matrix[3]*rightMatrix[4])+(matrix[7]*rightMatrix[5])+(matrix[11]*rightMatrix[6])+(matrix[15]*rightMatrix[7]);
   75 
   76     tmpMatrix[8]  = (matrix[0]*rightMatrix[8])+(matrix[4]*rightMatrix[9])+(matrix[8]*rightMatrix[10])+(matrix[12]*rightMatrix[11]);
   77     tmpMatrix[9]  = (matrix[1]*rightMatrix[8])+(matrix[5]*rightMatrix[9])+(matrix[9]*rightMatrix[10])+(matrix[13]*rightMatrix[11]);
   78     tmpMatrix[10] = (matrix[2]*rightMatrix[8])+(matrix[6]*rightMatrix[9])+(matrix[10]*rightMatrix[10])+(matrix[14]*rightMatrix[11]);
   79     tmpMatrix[11] = (matrix[3]*rightMatrix[8])+(matrix[7]*rightMatrix[9])+(matrix[11]*rightMatrix[10])+(matrix[15]*rightMatrix[11]);
   80 
   81     tmpMatrix[12] = (matrix[0]*rightMatrix[12])+(matrix[4]*rightMatrix[13])+(matrix[8]*rightMatrix[14])+(matrix[12]*rightMatrix[15]);
   82     tmpMatrix[13] = (matrix[1]*rightMatrix[12])+(matrix[5]*rightMatrix[13])+(matrix[9]*rightMatrix[14])+(matrix[13]*rightMatrix[15]);
   83     tmpMatrix[14] = (matrix[2]*rightMatrix[12])+(matrix[6]*rightMatrix[13])+(matrix[10]*rightMatrix[14])+(matrix[14]*rightMatrix[15]);
   84     tmpMatrix[15] = (matrix[3]*rightMatrix[12])+(matrix[7]*rightMatrix[13])+(matrix[11]*rightMatrix[14])+(matrix[15]*rightMatrix[15]);
   85 
   86     memcpy (matrix, tmpMatrix, sizeof(float)*16);
   87 }
   88 /*
   89 void MatrixMulti (float* right)
   90 {
   91     float tmpMatrix[16];
   92 
   93     tmpMatrix[0]  = (matrix[0]*right[0])+(matrix[4]*right[1])+(matrix[8]*right[2])+(matrix[12]*right[3]);
   94     tmpMatrix[1]  = (matrix[1]*right[0])+(matrix[5]*right[1])+(matrix[9]*right[2])+(matrix[13]*right[3]);
   95     tmpMatrix[2]  = (matrix[2]*right[0])+(matrix[6]*right[1])+(matrix[10]*right[2])+(matrix[14]*right[3]);
   96     tmpMatrix[3]  = (matrix[3]*right[0])+(matrix[7]*right[1])+(matrix[11]*right[2])+(matrix[15]*right[3]);
   97 
   98     tmpMatrix[4]  = (matrix[0]*right[4])+(matrix[4]*right[5])+(matrix[8]*right[6])+(matrix[12]*right[7]);
   99     tmpMatrix[5]  = (matrix[1]*right[4])+(matrix[5]*right[5])+(matrix[9]*right[6])+(matrix[13]*right[7]);
  100     tmpMatrix[6]  = (matrix[2]*right[4])+(matrix[6]*right[5])+(matrix[10]*right[6])+(matrix[14]*right[7]);
  101     tmpMatrix[7]  = (matrix[3]*right[4])+(matrix[7]*right[5])+(matrix[11]*right[6])+(matrix[15]*right[7]);
  102 
  103     tmpMatrix[8]  = (matrix[0]*right[8])+(matrix[4]*right[9])+(matrix[8]*right[10])+(matrix[12]*right[11]);
  104     tmpMatrix[9]  = (matrix[1]*right[8])+(matrix[5]*right[9])+(matrix[9]*right[10])+(matrix[13]*right[11]);
  105     tmpMatrix[10] = (matrix[2]*right[8])+(matrix[6]*right[9])+(matrix[10]*right[10])+(matrix[14]*right[11]);
  106     tmpMatrix[11] = (matrix[3]*right[8])+(matrix[7]*right[9])+(matrix[11]*right[10])+(matrix[15]*right[11]);
  107 
  108     tmpMatrix[12] = (matrix[0]*right[12])+(matrix[4]*right[13])+(matrix[8]*right[14])+(matrix[12]*right[15]);
  109     tmpMatrix[13] = (matrix[1]*right[12])+(matrix[5]*right[13])+(matrix[9]*right[14])+(matrix[13]*right[15]);
  110     tmpMatrix[14] = (matrix[2]*right[12])+(matrix[6]*right[13])+(matrix[10]*right[14])+(matrix[14]*right[15]);
  111     tmpMatrix[15] = (matrix[3]*right[12])+(matrix[7]*right[13])+(matrix[11]*right[14])+(matrix[15]*right[15]);
  112 
  113     memcpy (matrix, tmpMatrix, sizeof(float)*16);
  114 }
  115 
  116 
  117 float*  Matrix::Get     (void)
  118 {
  119     return matrix;
  120 }
  121 
  122 float MatrixGet (float *matrix, int index)
  123 {
  124     return matrix[index];
  125 }
  126 */
  127 
  128 float MatrixGetMultipliedIndex (int index, float *matrix, float *rightMatrix)
  129 {
  130     int iMod = index%4, iDiv = (index>>2)<<2;
  131 
  132     return  (matrix[iMod  ]*rightMatrix[iDiv  ])+(matrix[iMod+ 4]*rightMatrix[iDiv+1])+
  133             (matrix[iMod+8]*rightMatrix[iDiv+2])+(matrix[iMod+12]*rightMatrix[iDiv+3]);
  134 }
  135 
  136 void MatrixSet (float *matrix, int x, int y, float value)
  137 {
  138     matrix [x+(y<<2)] = value;
  139 }
  140 /*
  141 void    Matrix::Set     (int pos, float value)
  142 {
  143     matrix [pos] = value;
  144 }
  145 */
  146 void MatrixCopy (float *matrixDST, float *matrixSRC)
  147 {
  148     memcpy (matrixDST, matrixSRC, sizeof(float)*16);
  149 }
  150 
  151 void MatrixTranslate    (float *matrix, float *ptr)
  152 {
  153     matrix[12] += (matrix[0]*ptr[0])+(matrix[4]*ptr[1])+(matrix[ 8]*ptr[2]);
  154     matrix[13] += (matrix[1]*ptr[0])+(matrix[5]*ptr[1])+(matrix[ 9]*ptr[2]);
  155     matrix[14] += (matrix[2]*ptr[0])+(matrix[6]*ptr[1])+(matrix[10]*ptr[2]);
  156     matrix[15] += (matrix[3]*ptr[0])+(matrix[7]*ptr[1])+(matrix[11]*ptr[2]);
  157 }
  158 
  159 void MatrixScale (float *matrix, float *ptr)
  160 {
  161     matrix[0]  *= ptr[0];
  162     matrix[1]  *= ptr[0];
  163     matrix[2]  *= ptr[0];
  164     matrix[3]  *= ptr[0];
  165 
  166     matrix[4]  *= ptr[1];
  167     matrix[5]  *= ptr[1];
  168     matrix[6]  *= ptr[1];
  169     matrix[7]  *= ptr[1];
  170 
  171     matrix[8] *= ptr[2];
  172     matrix[9] *= ptr[2];
  173     matrix[10] *= ptr[2];
  174     matrix[11] *= ptr[2];
  175 }
  176 /*
  177 void    Matrix::Set (float a11, float a21, float a31, float a41,
  178                      float a12, float a22, float a32, float a42,
  179                      float a13, float a23, float a33, float a43,
  180                      float a14, float a24, float a34, float a44)
  181 {
  182 }
  183 */
  184 
  185 
  186 //-----------------------------------------
  187 
  188 void MatrixStackInit (MatrixStack *stack)
  189 {
  190     stack->matrix   = nullptr;
  191     stack->position = 0;
  192     stack->size     = 0;
  193 }
  194 
  195 void MatrixStackSetMaxSize (MatrixStack *stack, int size)
  196 {
  197     int i = 0;
  198 
  199     stack->size = size;
  200 
  201     if (stack->matrix == nullptr)
  202     {
  203         stack->matrix = (float*) malloc (stack->size*16*sizeof(float));
  204     }
  205     else
  206     {
  207         free (stack->matrix);
  208         stack->matrix = (float*) malloc (stack->size*16*sizeof(float));
  209     }
  210 
  211     for (i = 0; i < stack->size; i++)
  212     {
  213         MatrixInit (&stack->matrix[i*16]);
  214     }
  215 
  216     stack->size--;
  217 }
  218 
  219 
  220 void MatrixStackSetStackPosition (MatrixStack *stack, int pos)
  221 {
  222     stack->position += pos;
  223 
  224     if (stack->position < 0)
  225         stack->position = 0;
  226     else if (stack->position > stack->size)
  227         stack->position = stack->size;
  228 }
  229 
  230 void MatrixStackPushMatrix (MatrixStack *stack, float *ptr)
  231 {
  232     MatrixCopy (&stack->matrix[stack->position*16], ptr);
  233 
  234     MatrixStackSetStackPosition (stack, 1);
  235 }
  236 
  237 float * MatrixStackPopMatrix (MatrixStack *stack, int size)
  238 {
  239     MatrixStackSetStackPosition(stack, -size);
  240 
  241     return &stack->matrix[stack->position*16];
  242 }
  243 
  244 float * MatrixStackGetPos (MatrixStack *stack, int pos)
  245 {
  246     return &stack->matrix[pos*16];
  247 }
  248 
  249 float * MatrixStackGet (MatrixStack *stack)
  250 {
  251     return &stack->matrix[stack->position*16];
  252 }
  253 
  254 void MatrixStackLoadMatrix (MatrixStack *stack, int pos, float *ptr)
  255 {
  256     MatrixCopy (&stack->matrix[pos*16], ptr);
  257 }