10.Helper Recipes
[TOC]
介绍
内容
Preparing a translation matrix
Preparing a rotation matrix
Preparing a scaling matrix
Preparing a perspective projection matrix
Preparing an orthographic projection matrix
Loading texture data from a file
Loading a 3D model from an OBJ file
介绍
matrix
translation matrix
关于vulkan 坐标系x (lef/rightt), y (down/up), and z (near/far).右手坐标系

关于矩阵
1  | std::array<float, 16> translation_matrix = {  | 
All elements initialize with a 0.0f value
0th, 5th, 10th, and 15th elements (main diagonal) with a 1.0f value
12th element with a value stored in the x variable
13th element with a value stored in the y variable
14th element with a value stored in the z variable
也就是列主序

rotation matrix
0th element with a x  x  (1.0f - c) + c
1st element with a y  x  (1.0f - c) - z  s
2nd element with a z  x  (1.0f - c) + y  s
4th element with a x  y  (1.0f - c) + z  s
5th element with a y  y  (1.0f - c) + c
6th element with a z  y  (1.0f - c) - x  s
8th element with a x  z  (1.0f - c) - y  s
9th element with a y  z  (1.0f - c) + x  s
10th element with a z  z  (1.0f - c) + c
The rest of the elements initialize with a 0.0f value
Except for the 15th element, which should contain a 1.0f value
Each column of such matrix defines the directions of x, y, and z axes respectively after the rotation is performed.


一个例子
1  | if( normalize ) {  | 
Preparing a scaling matrix
- All elements initialize with a 0.0f value
 - 0th element with a value stored in the x variable
 - 5th element with a value stored in the y variable
 - 10th element with a value stored in the z variable
 - 15th element with a 1.0f value
 

..
1  | std::array<float, 16> scaling_matrix = {  | 
Preparing a perspective projection matrix
1  | float aspect_ratio;//width/height  | 
- 0th element with a f / aspect_ratio
 - 5th element with a -f
 - 10th element with a far_plane / (near_plane - far_plane)
 - 11th element with a -1.0f value
 - 14th element with a (near_plane * far_plane) / (near_plane -
 
far_plane)- The rest of the elements initialize with a 0.0f value
 
Preparing an orthographic projection matrix
1  | Matrix4x4 orthographic_projection_matrix = {  | 
Loading texture data
stb
1  | int width = 0;  | 
1  | std::vector<unsigned char> image_data;  | 
3D model obj file
https://github.com/syoyo/tinyobjloader.
1  | 
  | 
1  | tinyobj::attrib_t attribs;  |