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,故出现黑线
随机推荐
- 如何实现基于Cortex-A9 的UART裸机驱动
前言 Uart在一个嵌入式系统中是一个非常重要的模块,他承担了CPU与用户交互的桥梁.用户输入信息给程序.CPU要打印一些信息给终端都要依赖UART. 本文将以Exynos4412的UART控制器为基 ...
- nginx配置请求头防止点击劫持
在返回index.html配置中加入add_header X-Frame-Options DENY; location / {undefined root /data/nginx/html/dist/ ...
- ggml 简介
ggml 是一个用 C 和 C++ 编写.专注于 Transformer 架构模型推理的机器学习库.该项目完全开源,处于活跃的开发阶段,开发社区也在不断壮大.ggml 和 PyTorch.Tensor ...
- 1p-frac:已开源,仅用单张分形图片即可媲美ImageNet的预训练效果 | ECCV 2024
分形几何是一个数学分支,主要应用于作图方面.一般来说,分形经过无数次递归迭代后的结果.比如取一条线段,抹去中间的三分之一,会得到长度是原三分之一长的两条线段,中间隔着相同长度的间隙.然后重复这个动作, ...
- 【YashanDB数据库】YAS-02032 column type is incompatible with referenced column type
[标题]错误码处理 [问题分类]外键约束创建报错 [关键字]YAS-02032 [问题描述]设置外键约束报错,数据元数据不正确. [问题原因分析]外键字段类型不支持,比如varchar2(64) ,指 ...
- 推荐一款开源、高效、灵活的Redis桌面管理工具:Tiny RDM!支持调试与分析功能!
1.引言 在大数据和云计算快速发展的今天,Redis作为一款高性能的内存键值存储系统,在数据缓存.实时计算.消息队列等领域发挥着重要作用.然而,随着Redis集群规模的扩大和复杂度的增加,如何高效地管 ...
- Vue3 动态子页面和菜单栏同步
动态子页面 <router-view></router-view>显示子页面的内容 main.vue <template> <a-layout id=&quo ...
- “RazorTagHelper”任务意外失败。解决方案
严重性 代码 说明 项目 文件 行 禁止显示状态错误 MSB4018 "RazorTagHelper"任务意外失败.System.I ...
- 线段树 ----洛谷p3372
问题描述: 已知一个数列,对数列进行两种操作:1,对数列某个区间中的所有数加d:2,查询数列某区间的区间和 输入: 第一行输入两个整数n和m,分别代表数列中元素个数和对数列的操作次数,第二行输入n个用 ...
- [OI] 莫比乌斯函数与莫比乌斯反演
9. 莫比乌斯函数与莫比乌斯反演 9.1 莫比乌斯函数 9.1.1 定义 设 \(\mu\) 为莫比乌斯函数,则有: \[\mu(x)=\begin{cases}1\qquad (n=1)\\ 0\q ...