现代计算机图形学入门笔记,GAMES101-现代计算机图形学学习笔记(作业01)

参考:https://blog.youkuaiyun.com/qq_36242312/article/details/105742949

Eigen::Matrix4f get_model_matrix(float rotation_angle)

{

Eigen::Matrix4f model = Eigen::Matrix4f::Identity();

Eigen::Matrix4f rotate(4,4); // z-axis rotation

float radian = rotation_angle / 180.0 * MY_PI;

rotate << cos(radian), -sin(radian), 0, 0,

sin(radian), cos(radian), 0, 0,

0, 0, 1, 0,

0, 0, 0, 1;

model = rotate * model;

return model;

}

Eigen::Matrix4f get_model_matrix_axis(float rotation_angle, Eigen::Vector3f axis_start, Eigen::Vector3f axis_end)

{

// Eigen::Vector3f axis_start 为起点

// Eigen::Vector3f axis_end 为终点

Eigen::Matrix4f model = Eigen::Matrix4f::Identity();

// normalize axis

Eigen::Vector3f axis;

axis[0] = axis_end[0] - axis_start[0];

axis[1] = axis_end[1] - axis_start[1];

axis[2] = axis_end[2] - axis_start[2];

float norm = sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);

axis[0] /= norm;

axis[1] /= norm;

axis[2] /= norm;

// compute radian

float radian = rotation_angle / 180.0 * MY_PI;

// compute component 计算轴角旋转矩阵分量

Eigen::Matrix3f n(3, 3);

n << 0, -axis[2], axis[1],

axis[2], 0, -axis[0],

-axis[1], axis[0], 0;

Eigen::Matrix3f component1 = Eigen::Matrix3f::Identity() * cos(radian);

Eigen::Matrix3f component2 = axis * axis.transpose() * (1 - cos(radian));

Eigen::Matrix3f component3 = n * sin(radian);

Eigen::Matrix3f rotate_m = component1 + component2 + component3;

// Eigen 自带构造轴角旋转矩阵

// 下列注释用于验证我们构造的轴角旋转矩阵是否和Eigen的构造的轴角旋转矩阵一致

//Eigen::AngleAxisf rotation_vector(radian, Vector3f(axis[0], axis[1], axis[2]));

//Eigen::Matrix3f rotation_matrix;

//rotation_m = rotation_vector.toRotationMatrix();

Eigen::Matrix4f rotate_martix = Eigen::Matrix4f::Identity();

rotate_martix.block(0, 0, 3, 3) = rotate_m; // 前三个维度为旋转矩阵

model = rotate_martix * model;

return model;

}

Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,

float zNear, float zFar)

{

Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();

Eigen::Matrix4f M_persp2ortho(4, 4);

Eigen::Matrix4f M_ortho_scale(4, 4);

Eigen::Matrix4f M_ortho_trans(4, 4);

//已更正

float angle = eye_fov * MY_PI / 180.0;

float height = zNear * tan(angle) * 2;

float width = height * aspect_ratio;

auto t = -zNear * tan(angle / 2); // 上截面

auto r = t * aspect_ratio; //右截面

auto l = -r; // 左截面

auto b = -t; // 下截面

// 透视矩阵"挤压"

M_persp2ortho << zNear, 0, 0, 0,

0, zNear, 0, 0,

0, 0, zNear + zFar, -zNear * zFar,

0, 0, 1, 0;

// 正交矩阵-缩放

M_ortho_scale << 2 / (r - l), 0, 0, 0,

0, 2 / (t - b), 0, 0,

0, 0, 2 / (zNear - zFar), 0,

0, 0, 0, 1;

// 正交矩阵-平移

M_ortho_trans << 1, 0, 0, -(r + l) / 2,

0, 1, 0, -(t + b) / 2,

0, 0, 1, -(zNear + zFar) / 2,

0, 0, 0, 1;

Eigen::Matrix4f M_ortho = M_ortho_scale * M_ortho_trans;

projection = M_ortho * M_persp2ortho * projection;

return projection;

}

结果

以下依次是绕z轴旋转0/45/90度的结果:

7d48df4c05e980041768f56f1a71cbe6.png

07d9b77035c2dd9e8f1ecf304b031e4b.png

64132c7cd52747178821cbdae27ed85b.png

绕任意轴旋转的结果可以通过调用get_model_matrix_axis函数实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值