本次作业的任务是填写一个旋转矩阵和一个透视投影矩阵。给定三维下三个 点 v0(2.0, 0.0, −2.0), v1(0.0, 2.0, −2.0), v2(−2.0, 0.0, −2.0), 你需要将这三个点的坐 标变换为屏幕坐标,并在屏幕上绘制出对应的线框三角形 (在代码框架中,我们 已经提供了 draw_triangle 函数,所以你只需要去构建变换矩阵即可)。简而言 之,我们需要进行模型、视图、投影、视口等变换来将三角形显示在屏幕上。在 提供的代码框架中,我们留下了模型变换和投影变换的部分给你去完成。

作业解答:

  作业1:get_model_matrix(float rotation_angle): 逐个元素地构建模型变换矩 阵并返回该矩阵。在此函数中,你只需要实现三维中绕 z 轴旋转的变换矩阵, 而不用处理平移与缩放。该项只要求我们传入一个旋转角度然后返回一个旋转矩阵即可。(很简单)

Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity(); // TODO: Implement this function
// Create the model matrix for rotating the triangle around the Z axis.
// Then return it. // Rz matrix is (cosa, -sina, 0, 0)(sina, cosa, 0, 0)(0, 0, 1, 0)(0, 0, 0, 1)
Eigen::Matrix4f Rz;
Rz << cos(rotation_angle / 180.0 * MY_PI), -sin(rotation_angle / 180.0 * MY_PI), 0, 0,
sin(rotation_angle / 180.0 * MY_PI), cos(rotation_angle / 180.0 * MY_PI), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1; return Rz * model;
}

  作业2:get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar): 使用给定的参数逐个元素地构建透视投影矩阵并返回 该矩阵。该项要求通过已知条件写出投影矩阵,这个需要用到L4、L5中提到的知识,即MVP变换。

Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function Eigen::Matrix4f projection = Eigen::Matrix4f::Identity(); // TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it. // Get t、r、l、b by eye_fov and aspect_ration
float t = abs(zNear) * tanf(eye_fov / 2); // tan需传入角度,tanf传入一个float弧度返回一个float,此处fov/2为弧度值
float r = t * aspect_ratio;
float l = -r;
float b = -t; // Create the perspective projection matrix Mpo
Eigen::Matrix4f Mpo = Eigen::Matrix4f::Identity();;
Mpo << zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear + zFar, -(zFar * zFar),
0, 0, 1, 0; // Create the orthographic projection matrix Mor
Eigen::Matrix4f MorTran = Eigen::Matrix4f::Identity(); //Mor平移矩阵
MorTran << 1, 0, 0, -((l + r) / 2),
0, 1, 0, -((t + b) / 2),
0, 0, 1, -((zNear + zFar) / 2),
0, 0, 0, 1; Eigen::Matrix4f MorScal = Eigen::Matrix4f::Identity(); //Mor大小变换矩阵
MorScal << 2 / (r - l), 0, 0, 0, //Notice: t\r\l\b need be float
0, 2 / (t - b), 0, 0,
0, 0, 2 / (zNear - zFar), 0,
0, 0, 0, 1; projection = MorScal * MorTran * Mpo * projection;
return projection;
}

  附加作业:在 main.cpp 中构造一个函数,该函数的作用是得到绕任意 过原点的轴的旋转变换矩阵。 Eigen::Matrix4f get_rotation(Vector3f axis, float angle) 。直接使用罗德里格斯公式返回一个旋转矩阵。

Eigen::Matrix4f get_rotation(Vector3f axis, float angle){
//R1 = cosa * I
Eigen::Matrix3f I = Eigen::Matrix3f::Identity();
Eigen::Matrix3f R1 = cosf(angle) * I; //R2 = (1 - cosa) * n*nT 即 (1 - cosa)* (a[0], a[1], a[2])T * (a[0], a[1], a[2])
Eigen::Matrix3f R2;
R2 << axis[0] * axis[0], axis[0] * axis[1], axis[0] * axis[2],
axis[1] * axis[0], axis[1] * axis[1], axis[1] * axis[2],
axis[2] * axis[0], axis[2] * axis[1], axis[2] * axis[2];
R2 = (1 - cosf(angle)) * R2; //R3 = sina * (0, -nz, ny)(nz, 0, -nx)(-ny, nx, 0)
Eigen::Matrix3f R3;
R3 << 0, -axis[2], axis[1],
axis[2], 0, -axis[0],
-axis[1], axis[0], 0;
R3 = sinf(angle) * R3; Eigen::Matrix3f R = R1 + R2 + R3;
Eigen::Matrix4f Res;
Res << R(0,0), R(0,1), R(0,2), 0,
R(1,0), R(1,1), R(1,2), 0,
R(2,0), R(2,1), R(2,2), 0,
0, 0, 0, 1;
return Res;
}

这里贴出绕x轴旋转效果

随机推荐

  1. jxls导入excel

    我们在开发中经常用jxls实现导出功能,殊不知jxls也有导入功能,下面来介绍下如何使用jxls导入excel. 首先在maven的pom中添加相关依赖,如下: <dependency> ...

  2. CRC算法原理、推导及实现

    CRC, Cyclic Redundancy Check, 循环冗余校验 1. 基本原理 CRC的本质是除法,把待检验的数据当作一个很大(很长)的被除数,两边选定一个除数(有的文献叫poly),最后得 ...

  3. 如何让你的C语言程序打印的log多一点色彩?(超级实用)

    接着上一篇文章<由字节对齐引发的一场"血案" > 在平常的调试中,printf字体格式与颜色都是默认一致的. 如果可以根据log信息的重要程度,配以不同的颜色与格式,可 ...

  4. Ubuntu 切换显示管理器

    比较流行的显示管理器有: gdm3 - GNOME Display Manager lightdm - Light Display Manager sddm - Simple Desktop Disp ...

  5. Windows 服务管理

    创建服务 New-Service -Name NAME -BinaryPathName COMMAND -StartupType Automatic -Description DESCRIPTION ...

  6. docker高级篇:实战-自己开发的微服务怎么在docker上面运行?

    通过前面的一系列学习,我们已经知道怎么制作dockerfile了.那么,本篇文章,咱们就把自己写的spring boot的demo项目,部署在docker上面. 案例目标: 我们自己开发的微服务怎么在 ...

  7. PDF解析,还能做得更好

    随着大模型文档智能应用逐渐步入正轨,文档解析类产品成为其中重要的一环.文档解析工具能够"唤醒"沉睡在PDF文件中的知识,将其转化为机器能够识别.读取的信息,将可用数据从txt.cs ...

  8. JavaScript – Fetch

    前言 上一篇 JavaScript – XMLHttpRequest 有提到 XMLHttpRequest 正在被 Fetch 取代,这篇就继续介绍 Fetch 吧. 参考 阮一峰 – Fetch A ...

  9. EF Core – Custom Migrations (高级篇)

    前言 会写这篇是因为最近开始大量使用 SQL Server Trigger 来维护冗余 (也不清楚这路对不对). EF Core migrations 没有支持 Trigger Github Issu ...

  10. SQL Server 备份方案

    参考 SQL Server三种常见备份 SQL Server备份策略 以前写的笔记 目的 在发生意外 (人为删除, 磁盘坏掉) 之后, 让数据可还原到指定时间点上. Backup 的种类 备份分 3 ...