本次作业的任务是填写一个旋转矩阵和一个透视投影矩阵。给定三维下三个 点 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 轴旋转的变换矩阵, 而不用处理平移与缩放。该项只要求我们传入一个旋转角度然后返回一个旋转矩阵即可。(很简单)

  1. Eigen::Matrix4f get_model_matrix(float rotation_angle)
  2. {
  3. Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
  4.  
  5. // TODO: Implement this function
  6. // Create the model matrix for rotating the triangle around the Z axis.
  7. // Then return it.
  8.  
  9. // Rz matrix is (cosa, -sina, 0, 0)(sina, cosa, 0, 0)(0, 0, 1, 0)(0, 0, 0, 1)
  10. Eigen::Matrix4f Rz;
  11. Rz << cos(rotation_angle / 180.0 * MY_PI), -sin(rotation_angle / 180.0 * MY_PI), 0, 0,
  12. sin(rotation_angle / 180.0 * MY_PI), cos(rotation_angle / 180.0 * MY_PI), 0, 0,
  13. 0, 0, 1, 0,
  14. 0, 0, 0, 1;
  15.  
  16. return Rz * model;
  17. }

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

  1. Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
  2. float zNear, float zFar)
  3. {
  4. // Students will implement this function
  5.  
  6. Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
  7.  
  8. // TODO: Implement this function
  9. // Create the projection matrix for the given parameters.
  10. // Then return it.
  11.  
  12. // Get t、r、l、b by eye_fov and aspect_ration
  13. float t = abs(zNear) * tanf(eye_fov / 2); // tan需传入角度,tanf传入一个float弧度返回一个float,此处fov/2为弧度值
  14. float r = t * aspect_ratio;
  15. float l = -r;
  16. float b = -t;
  17.  
  18. // Create the perspective projection matrix Mpo
  19. Eigen::Matrix4f Mpo = Eigen::Matrix4f::Identity();;
  20. Mpo << zNear, 0, 0, 0,
  21. 0, zNear, 0, 0,
  22. 0, 0, zNear + zFar, -(zFar * zFar),
  23. 0, 0, 1, 0;
  24.  
  25. // Create the orthographic projection matrix Mor
  26. Eigen::Matrix4f MorTran = Eigen::Matrix4f::Identity(); //Mor平移矩阵
  27. MorTran << 1, 0, 0, -((l + r) / 2),
  28. 0, 1, 0, -((t + b) / 2),
  29. 0, 0, 1, -((zNear + zFar) / 2),
  30. 0, 0, 0, 1;
  31.  
  32. Eigen::Matrix4f MorScal = Eigen::Matrix4f::Identity(); //Mor大小变换矩阵
  33. MorScal << 2 / (r - l), 0, 0, 0, //Notice: t\r\l\b need be float
  34. 0, 2 / (t - b), 0, 0,
  35. 0, 0, 2 / (zNear - zFar), 0,
  36. 0, 0, 0, 1;
  37.  
  38. projection = MorScal * MorTran * Mpo * projection;
  39. return projection;
  40. }

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

  1. Eigen::Matrix4f get_rotation(Vector3f axis, float angle){
  2. //R1 = cosa * I
  3. Eigen::Matrix3f I = Eigen::Matrix3f::Identity();
  4. Eigen::Matrix3f R1 = cosf(angle) * I;
  5.  
  6. //R2 = (1 - cosa) * n*nT 即 (1 - cosa)* (a[0], a[1], a[2])T * (a[0], a[1], a[2])
  7. Eigen::Matrix3f R2;
  8. R2 << axis[0] * axis[0], axis[0] * axis[1], axis[0] * axis[2],
  9. axis[1] * axis[0], axis[1] * axis[1], axis[1] * axis[2],
  10. axis[2] * axis[0], axis[2] * axis[1], axis[2] * axis[2];
  11. R2 = (1 - cosf(angle)) * R2;
  12.  
  13. //R3 = sina * (0, -nz, ny)(nz, 0, -nx)(-ny, nx, 0)
  14. Eigen::Matrix3f R3;
  15. R3 << 0, -axis[2], axis[1],
  16. axis[2], 0, -axis[0],
  17. -axis[1], axis[0], 0;
  18. R3 = sinf(angle) * R3;
  19.  
  20. Eigen::Matrix3f R = R1 + R2 + R3;
  21. Eigen::Matrix4f Res;
  22. Res << R(0,0), R(0,1), R(0,2), 0,
  23. R(1,0), R(1,1), R(1,2), 0,
  24. R(2,0), R(2,1), R(2,2), 0,
  25. 0, 0, 0, 1;
  26. return Res;
  27. }

这里贴出绕x轴旋转效果

随机推荐

  1. ARMv8-A 地址翻译技术之MMU的前世今生

    MMU的重要性不言而喻,支撑操作系统之上的各种复杂应用.但在正式讲MMU之前,我们先说说MMU的发展史,因为ARMv8-A的MMU相当复杂,直接切入正题,会显得比较枯燥.废话不多说,咱们马上开始: 一 ...

  2. 基于donetcore/CAP实现分布式事务一致性

    官网:https://cap.dotnetcore.xyz 相关介绍 CAP 是一个EventBus,同时也是一个在微服务或者SOA系统中解决分布式事务问题的一个框架.它有助于创建可扩展,可靠并且易于 ...

  3. 基于gitee+hexo搭建个人博客

    gitee准备 注册好gitee git安装与配置 下载git默认安装,配置 在之前下载的目录下,右键,选择[Git Bash Here] 配置用户 git config --global user. ...

  4. 【音视频通话】使用asp.net core 8+vue3 实现高效音视频通话

    引言 在三年前,写智能小车的时候,当时小车上有一个摄像头需要采集,实现推拉流的操作,技术选型当时第一版用的是nginx的rtmp的推拉流,服务器的配置环境是centos,2H4G3M的一个配置,ngi ...

  5. 配置 GRUB2

    配置 GRUB2(GRand Unified Bootloader version 2)通常涉及编辑其配置文件和更新引导程序.以下是一个基础流程,适用于大多数 Linux 发行版: 备份现有配置 在进 ...

  6. .NET 8.0 文档管理系统网盘功能的实现

    前言 大家好,今天推荐一个文档管理系统Dorisoy.Pan. Dorisoy.Pan 是一个基于 .NET 8 和 WebAPI 构建的文档管理系统,它集成了 Autofac.MediatR.JWT ...

  7. java 知识

    1. 单文件java 例子和简单项目例子 http://kleinfelter.com/java-hello-world-with-visual-studio-code-and-eclipse jav ...

  8. EF Core – 大杂烩

    前言 记入一些零零碎碎的知识. Shadow Properties 参考:Docs – Shadow and Indexer Properties Shadow Property 指的是那些在数据库有 ...

  9. Google Maps Embed API & JavaScript API

    前言 很多年前写过一篇 Google Map 谷歌地图, 这篇算是翻新版本. Google Map Registration Google Maps Platform 是整个 Google Map 的 ...

  10. migration to end point routing

    花了几个小时,记入一下吧. 1. odata https://devblogs.microsoft.com/odata/enabling-endpoint-routing-in-odata/ 找着弄就 ...