首页 > 其他分享 >games101_Homework1

games101_Homework1

时间:2024-02-22 23:55:28浏览次数:19  
标签:Eigen get float 矩阵 Matrix4f Homework1 games101 matrix

作业描述:

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

 

标签:Eigen,get,float,矩阵,Matrix4f,Homework1,games101,matrix
From: https://www.cnblogs.com/wosun/p/18028447

相关文章

  • games101_Homework0
    作业描述:给定一个点P=(2,1),将该点绕原点先逆时针旋转45◦,再平移(1,2),计算出变换后点的坐标(要求用齐次坐标进行计算)。作业解答:#include<cmath>#include<eigen3/Eigen/Core>#include<eigen3/Eigen/Dense>#include<iostream>intmain(){//definepoin......
  • games101-2 透视深度插值矫正与抗锯齿分析
    透视深度插值矫正与抗锯齿分析深度插值的差错原因透视深度插值公式推导games101中的错误msaa与ssaa简要定义games101中ssaa的实现games101中msaa的实现深度插值的差错原因当投影的图形与投影的平面不平行时,这时进行透视投影,从上图中可以看出,投影平面上的线段时均匀......
  • games101一些问题及思考
    games101一些问题及思考1.透视投影为什么z值变大从透视投影矩阵可以看出z会变大,但从直观上怎么想呢。想象一段向无穷远处延伸的铁轨,假设有100m,但照片中前一半明显不足50m,后一段明显多于50m,可以体会到近平面和远平面之间的点都会向远平面压缩,使得出现近大远小的情况。2.各个......
  • [GAMES101] 我的结课打卡
    ......
  • games101-homework-notes
    Games101作业笔记Created:2023-06-19T12:00+08:00Published:2023-08-17T16:23+08:00Categories:ComputerGraphics目录pa0hw1ProjectionMatrixTriangleRasterizerprocesshw2insidetrianglerasterize_trianglemyhw2bugs使用\(y+0.5\)setindex导致三角形分裂屏幕两......
  • games101-lecture-notes
    Games101课程笔记Created:2023-06-07T20:54+08:00Published:2023-08-16T21:05+08:00Categories:ComputerGraphics目录Lecture01:OverviewofComputerGraphicsmotivation:学了有什么用?课程内容课程资源Lecture02:ReviewofLinearAlgebra点乘叉乘叉乘的作用Lecture03......
  • GAMES101笔记(04)
    本篇对应的是第七课上节课讲完了光栅化的内容,这节课讲的有深度测试,光照和着色深度测试我在学校看shader入门精要的时候有些印象,但也仅此而已了,我觉得还是要先补一下图形学的知识再去啃入门精要会好一些 深度缓存在计算机成像时,对于一个我们要输出的画面,如何确保画面上的东......
  • GAMES101笔记(03)
    前几个月忙着拯救地球所以有比较长时间的空档这次笔记对应的是games101内容的第六课,至于为什么跳过第五课因为第五课我感觉也没啥需要记笔记的,基本就是光栅化的一些基本概念以及最基本的一些实现理念,视频最后讲到了关于锯齿和走样的一些东西,第六课开头即紧接着这部分进行讲解采......
  • games101 HomeWork6
    Games101HomeWork6导航导航作业要求IntersectP(constRay&ray,constVector3f&invDir,conststd::array<int,3>&dirIsNeg)intheBounds3.hpp:这个函数的作用是判断包围盒BoundingBox与光线是否相交,你需要按照课程介绍的算法实现求交过程。getIntersection(BVHBuil......
  • games101 HomeWork5
    Games101HomeWork5导航导航任务Renderer.cpp中的Render():这里你需要为每个像素生成一条对应的光线,然后调用函数castRay()来得到颜色,最后将颜色存储在帧缓冲区的相应像素中。Triangle.hpp中的rayTriangleIntersect():v0,v1,v2是三角形的三个顶点,orig是光线的起点......