games101_Homework2
完成函数static bool insideTriangle(): 测试点是否在三角形内。
一段优雅的easy代码,没什么好说的。(但是需要修改这里传入的xy的类型为float,默认为int是想让我通过修改返回值的方式来实现MSAA:例如返回一个int值,这里传入单个像素坐标后在insideTriangle函数中实现超采样,然后返回k,再在设置颜色时* k/4)

static bool insideTriangle(float x, float y, const Vector3f* _v)
{
// TODO : Implement this function to check if the point p(x, y) is inside the triangle represented by _v[0], _v[1], _v[2] (a, b, c)
// 如果p(x,y)与三点a、b、c构成的向量x乘同向(z坐标都为正)则在其内部
// 构建向量
Vector3f p = Vector3f(x, y, 1);
Vector3f ap = p - _v[0];
Vector3f bp = p - _v[1];
Vector3f cp = p - _v[2];
Vector3f ab = _v[1] - _v[0];
Vector3f bc = _v[2] - _v[1];
Vector3f ca = _v[0] - _v[2];
// 运算x乘结果的z值
float z1 = ab.cross(ap).z(); //ab x ap
float z2 = bc.cross(bp).z(); //bc x bp
float z3 = ca.cross(cp).z(); //ca x cp
// in Triangle
return (z1 > 0 && z2 > 0 && z3 > 0) || (z1 < 0 && z2 < 0 && z3 < 0);
}
rasterize_triangle(): 执行三角形栅格化算法
在采样点在三角形内部时更新z_buffer和frame_buffer(这里使用了MSAA,故颜色值需乘以一个float的k)

//Screen space rasterization
void rst::rasterizer::rasterize_triangle(const Triangle& t) {
auto v = t.toVector4();
// 获取三角形数据并分别传入 insideTriangle()
Vector3f triangle[3];
for(int i = 0; i < 3; i++){
triangle[i] = Vector3f(v[i].x(), v[i].y(), v[i].z());
} // TODO : Find out the bounding box of current triangle.
float min_x = std::min(v[0].x(), std::min(v[1].x(), v[2].x()));
float max_x = std::max(v[0].x(), std::max(v[1].x(), v[2].x()));
float min_y = std::min(v[0].y(), std::min(v[1].y(), v[2].y()));
float max_y = std::max(v[0].y(), std::max(v[1].y(), v[2].y())); // iterate through the pixel and find if the current pixel is inside the triangle
// frame_buffer 和 z_buffer早已定义好
for(int x = min_x; x <= max_x; x++){
for(int y = min_y; y <= max_y; y++){
// MSAA version
float k = 0;
if(insideTriangle(0.25 + x, 0.25 + y, triangle)) k += 0.25;
if(insideTriangle(0.75 + x, 0.25 + y, triangle)) k += 0.25;
if(insideTriangle(0.25 + x, 0.75 + y, triangle)) k += 0.25;
if(insideTriangle(0.75 + x, 0.75 + y, triangle)) k += 0.25;
if(k != 0){
// Simple Sampling
//if(insideTriangle(x + 0.5, y + 0.5, triangle)){
// If so, use the following code to get the interpolated z value.
auto[alpha, beta, gamma] = computeBarycentric2D(x, y, t.v);
float w_reciprocal = 1.0/(alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
float z_interpolated = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
z_interpolated *= w_reciprocal; if(z_interpolated < depth_buf[get_index(x, y)]){
// TODO : set the current pixel (use the set_pixel function) to the color of the triangle (use getColor function) if it should be painted.
depth_buf[get_index(x, y)] = z_interpolated;
//set_pixel(Vector3f(x, y, 1), t.getColor());
set_pixel(Vector3f(x, y, 1),k * t.getColor());
}
}
}
}
}
值得注意的是,这里zNear和zFar使用的是正值,故在main.cpp中需要修改float t = -1 * abs(zNear) * tanf(eye_fov / 2);将其y轴颠倒一下才能做出正确图形(第八节课讲这是左手右手系的原因)

注:MSAA将会导致黑线是正常的,因为我们在计算边缘像素时他的z是高于下方三角形的,但实际可能覆盖面积过小导致0.25*color,故出现黑线
随机推荐
- Camera | 12.瑞芯微摄像头自动焦距马达驱动移植
本为你主要讲解如何让摄像头ov13850支持自动对焦功能. 摄像头的对角主要通过VCM马达驱动芯片DW9714来实现的. 一.环境 soc : rk3568 board: EVB1-DDR4-V10 ...
- JavaScript设计模式样例二十 —— 中介者模式
中介者模式(Mediator Pattern) 定义:用来降低多个对象和类之间的通信复杂性.目的:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独 ...
- Maven 设置 JDK 版本
Maven 设置 JDK 版本是通过 Apache Maven Compiler Plugin 插件实现的.它用于编译项目的源代码. 方法一 有时候你可能需要将某个项目编译到与当前使用的 JDK 版本 ...
- Linux 安装 TeX Live
安装新版本 cd /tmp # 下载安装压缩包 wget https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz # 解 ...
- 使用.NET源生成器(SG)生成项目的版本号信息
之前写过一篇 源生成器生成自动注入的代码 主要是通过SyntaxProvider查找标注特性实现 其实除了SyntaxProvider之外还有几个很重要的Provider,比如:MetadataRef ...
- Go runtime 调度器精讲(一):Go 程序初始化
原创文章,欢迎转载,转载请注明出处,谢谢. 0. 前言 本系列将介绍 Go runtime 调度器.要学好 Go 语言,runtime 运行时是绕不过去的,它相当于一层"操作系统" ...
- 小tips:docker 配置国内镜像地址
在配置文件daemon.json中添加国内镜像,让其下载加速. vi /etc/docker/daemon.json 如下国内镜像: { "registry-mirrors": [ ...
- 运输小猫娘之再续 5k 传奇
写的比较意识流 前情提要 上回书说到,5k 因为拯救大家被炸断了 \(1000000007\) 米的牛至中的十五千米,尽管大家的欢呼声如此热烈,就像大家的热量正在像烈火一样散发出来,但是 5k 却无心 ...
- 《Neo4j 图数据库扩展指南:APOC和ALGO》
https://detail.tmall.com/item.htm?spm=a2e2i.11532906.0.d2960ced2.f27a6abbrEMtHp&id=622478213458 ...
- Flutter将视频或图文分享到抖音
如何在 Flutter 中分享视频到抖音 话不多说,先上效果: 原理 发布内容至抖音 H5 场景_移动/网站应用_抖音开放平台 (open-douyin.com) 本教程没有接入抖音原生 SDK 以及 ...