- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 975字
- 2021-06-30 14:45:56
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.
- Create a new file, mat4.h. This file is needed to declare the mat4 struct.
- 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];
- 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;
};
- 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;
};
- 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;
};
- 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
- 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) {}
- 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]) { }
- 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.
- 程序員數學:用Python學透線性代數和微積分
- PHP+MySQL網站開發項目式教程
- Hands-On Swift 5 Microservices Development
- 利用Python進行數據分析(原書第3版)
- Android Wear Projects
- Solr Cookbook(Third Edition)
- Django 3.0入門與實踐
- Mastering AWS Security
- Advanced UFT 12 for Test Engineers Cookbook
- Flink技術內幕:架構設計與實現原理
- Android Studio開發實戰:從零基礎到App上線 (移動開發叢書)
- Python Programming for Arduino
- H5+移動營銷設計寶典
- 從零開始構建深度前饋神經網絡:Python+TensorFlow 2.x
- C語言程序設計實驗指導與習題精解