本次作业的任务是填写一个旋转矩阵和一个透视投影矩阵。给定三维下三个 点 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. mybatis打印sql 转

    我们在使用mybatis开发过程中,经常需要打印sql以及输入输出,下面说一下mybatis结合log4j打印sql的. 1.添加mybatis配置 mybatis的日志打印方式比较多,SLF4J | ...

  2. lbs 地理位置

    lbs 地理位置 https://caorong.github.io/2018/05/04/lbs/ https://www.cnblogs.com/lbser/p/3310455.html http ...

  3. JAVA——instanceof运算符(问题待处理)

    2024/07/12 1.问题 2.问题拆解 3.解决 1.问题 今天学了一个运算符--instanceof,概念很抽象,感觉暂时理解不了,什么实例对象.类.子类,看得迷迷糊糊的,先记录下来,有空做一 ...

  4. docker安装cdh6.3.2集群(联网版)

    一.前言 本次采用的在线安装方式,cdh为6.3.2版本,系统为centos7.4, docker节点可以为任意多个,下文将以3个docker容器为示例进行展示.此方法也可用在docker swarm ...

  5. history的replace("/admin")与("admin")的区别

    假设当前路由为:localhost:3000/index/a 有"/"的情况是直接从根目录替换 改完之后的路由为:localhost:3000/admin 没有"/&qu ...

  6. Ubuntu 更换 macOS Big Sur 主题

    我们很多人使用 Mac 的原因之一是 macOS 是最像 Linux 的操作系统(bushi),而 macOS 精美的图形界面又让我们欲罢不能.那么能不能将 macOS 的图形界面搬到 Linux 上 ...

  7. Kubernetes-9:Service介绍及演示

    Service Kubernetes 的Service定义了这样一种抽象:一个 Pod 的逻辑分组,一种可以访问他们的策略 -- 微服务,这一组Pod能够被Service访问到,通常是通过tabel ...

  8. [big data] main entry for Spark, Zeppelin, Delta Lake ...

    1. 环境搭建 big data env setup 2. Spark 学习 spark 怎么读写 elasticsearch spark 怎么 连接 读写 ElasticSearch Spark 上 ...

  9. tarjan—算法的神(一)

    本篇包含 tarjan 求强连通分量.边双连通分量.割点 部分, tarjan 求点双连通分量.桥(割边)在下一篇. 伟大的 Robert Tarjan 创造了众多被人们所熟知的算法及数据结构,最著名 ...

  10. 欢迎加入d3shop,一个DDD实战项目

    背景 整个<老肖的领域驱动设计之路>系列关于认知的核心部分已经基本闭环,但纸上得来终觉浅,还是需要通过实际操作来体会和验证我们的观点,接下来,我将通过一个实战项目来带着大家一起体验从需求到 ...