本次作业的任务是填写一个旋转矩阵和一个透视投影矩阵。给定三维下三个 点 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. 和xshell和crt说再见,认识了一款55k star多端跨平台终端神器,强大酷炫

    一.Tabby简介 Tabby(以前称为Terminus)是一款高度可配置的终端仿真器.SSH 和串行客户端.开源且跨平台,支持在Windows.macOS和Linux系统下使用. 源码下载 http ...

  2. 嵌入式数据库sqlite3【进阶篇】-子句和函数的使用,小白一文入门

    在<嵌入式数据库sqlite3命令操作基础篇-增删改查,小白一文入门>一文中讲解了如何实现sqlite3的基本操作增删改查,本文介绍一些其他复杂一点的操作.比如where.order by ...

  3. OpenHarmony编译构建系统详解,从零搭建windows下开发环境,巨方便!

    自从OpenHarmony更新了dev-tool,就可以在windows下构建鸿蒙(轻量型)系统了,这对于进行MCU开发的朋友们,学习鸿蒙OS会友好许多!我们可以更快的构建出系统,方便快速学习和验证. ...

  4. Java中处理SocketException: Connection reset”异常的方法

    Java中处理SocketException: Connection reset"异常的方法 在Java编程中,有时候我们会遇到java.net.SocketException: Conne ...

  5. 彻底解决ROS1安装问题,一键解决远离rosdep init 和rosdep update出错

    彻底解决ROS1安装问题 相信很多小伙伴在刚入门ROS的时候就遇到ROS安装这个拦路虎. 普遍出错的环节在rosdep init 和rosdep update,由于要访问国外的站点就导致国内访问很容易 ...

  6. .NET 开源报表神器 Seal-Report

    前言 Seal-Report 是一款.NET 开源报表工具,拥有 1.4K Star.它提供了一个完整的框架,使用 C# 编写,最新的版本采用的是 .NET 8.0 . 它能够高效地从各种数据库或 N ...

  7. mysql事务隔离级别及MVCC 原理

    一.事务的隔离级别 为了保证事务与事务之间的修改操作不会互相影响,innodb希望不同的事务是隔离的执行的,互不干扰. 两个并发的事务在执行过程中有 读读.读写(一个事务在读某条数据的同时另一个事务在 ...

  8. C#/.NET/.NET Core优质学习资料,干货收藏!

    前言 今天大姚给大家分享一些C#/.NET/.NET Core优质学习资料,希望可以帮助到有需要的小伙伴. 什么是 .NET? .NET 是一个免费的.跨平台的.开源开发人员平台,用于构建许多不同类型 ...

  9. Goby漏洞发布 | CVE-2024-38856 Apache OFbiz /ProgramExport 命令执行漏洞【已复现】

    漏洞名称:Apache OFbiz /ProgramExport 命令执行漏洞(CVE-2024-38856) English Name:Apache OFbiz /ProgramExport Com ...

  10. 理解IO多路复用

    I/O 多路复用是什么? I/O 多路复用是用户程序通过复用一个线程来服务多个 I/O 事件的机制,我们也可以将他说成是一个线程服务多个文件描述符 fd,而 I/O 多路复用是在操作系统层面实现提供的 ...