官术网_书友最值得收藏!

Creating a matrix

In this section, you will create a new 4 x 4 matrix. This matrix will be stored as a 16-element array of floats. A union will be used to allow access to the data in the matrix in an easier-to-use fashion:

Important note

The identity matrix is a special matrix that multiplies anything by the identity matrix results in the original matrix. The identity matrix does no mapping. An identity matrix contains 0 in all elements except the main diagonal, which is made up entirely of 1.

  1. Create a new file, mat4.h. This file is needed to declare the mat4 struct.
  2. Add the following structure declaration to mat4.h, which starts a union by declaring a flat array of 16 elements as the first member of the union:

    struct mat4 {

        union {

            float v[16];

  3. The next member of the union is a structure of vec4 variables. Each of the vec4 variables represents one column of the matrix; they are named after the basis vector stored in those columns:

            struct {

                vec4 right;

                vec4 up;

                vec4 forward;

                vec4 position;

            };

  4. It might be useful to access members by element based on the basis vector. The following struct contains named pairs; the first letter represents the basis vector and the second letter represents the component of that vector:

            struct {

            //         row 1    row 2    row 3    row 4

            /*col 1*/float xx;float xy;float xz;float xw;

            /*col 2*/float yx;float yy;float yz;float yw;

            /*col 3*/float zx;float zy;float zz;float zw;

            /*col 4*/float tx;float ty;float tz;float tw;

            };

  5. The next struct will allow you to access the matrix using column-row notation:

            struct {

               float c0r0;float c0r1;float c0r2;float c0r3;

               float c1r0;float c1r1;float c1r2;float c1r3;

               float c2r0;float c2r1;float c2r2;float c2r3;

               float c3r0;float c3r1;float c3r2;float c3r3;

            };

  6. The final struct will allow you to access the matrix using row-column notation:

            struct {

               float r0c0;float r1c0;float r2c0;float r3c0;

               float r0c1;float r1c1;float r2c1;float r3c1;

               float r0c2;float r1c2;float r2c2;float r3c2;

               float r0c3;float r1c3;float r2c3;float r3c3;

            };

        }; // End union

  7. Add an inline constructor that will create the identity matrix:

        inline mat4() :

           xx(1), xy(0), xz(0), xw(0),

           yx(0), yy(1), yz(0), yw(0),

           zx(0), zy(0), zz(1), zw(0),

           tx(0), ty(0), tz(0), tw(1) {}

  8. Add an inline constructor that will create a matrix from a float array:

        inline mat4(float *fv) :

           xx( fv[0]), xy( fv[1]), xz( fv[2]), xw( fv[3]),

           yx( fv[4]), yy( fv[5]), yz( fv[6]), yw( fv[7]),

           zx( fv[8]), zy( fv[9]), zz(fv[10]), zw(fv[11]),

           tx(fv[12]), ty(fv[13]), tz(fv[14]), tw(fv[15]) { }

  9. Add an inline constructor that will let you create a matrix by specifying each element inside the matrix:

        inline mat4(

            float _00, float _01, float _02, float _03,

            float _10, float _11, float _12, float _13,

            float _20, float _21, float _22, float _23,

            float _30, float _31, float _32, float _33) :

            xx(_00), xy(_01), xz(_02), xw(_03),

            yx(_10), yy(_11), yz(_12), yw(_13),

            zx(_20), zy(_21), zz(_22), zw(_23),

            tx(_30), ty(_31), tz(_32), tw(_33) { }

    }; // end mat4 struct

The matrix struct you just declared is the final mat4 struct; the anonymous union provides five different ways of accessing matrix data. Matrix data can be accessed as a flat array, as four columns each stored as vec4, or as one of three mnemonics. The three mnemonics name elements using their basis vectors, their row and then column, or their column and then row.

Next, you will start working on functions that operate on the mat4 structure. You will implement common matrix operations, such as adding, scaling, and multiplying matrices, and see how to use matrices to transform vectors and points.

主站蜘蛛池模板: 延寿县| 泸溪县| 井陉县| 新乐市| 绥宁县| 黔南| 钦州市| 涞水县| 淮阳县| 康乐县| 萝北县| 丰城市| 祁阳县| 华坪县| 西盟| 梅河口市| 虹口区| 田阳县| 铁岭市| 奉新县| 奉新县| 鹤岗市| 东辽县| 翁牛特旗| 江山市| 翁牛特旗| 甘肃省| 彰化县| 岑溪市| 建德市| 搜索| 永川市| 忻城县| 青海省| 台北市| 偃师市| 历史| 曲松县| 会同县| 宁安市| 那坡县|