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,故出现黑线
随机推荐
- Linux内核信号SIGIO使用实例讲解
一.信号 1. 基本概念 信号是在软件层次上对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是异步的,一个进程不必通过任何操作来等待信号的到达,事实上, ...
- mysql bit(1) 字段insert 报错 Data too long
insert数据时,给bit(1)字段赋值 '1' 报错 将字符串'1',修改为数字1即可
- NumPy从入门到放弃
看前建议: 本文以jupyter notebook为编辑器进行示例,建议有一定python基础后再进行学习. python的安装:https://www.cnblogs.com/scfssq/p/17 ...
- Flask 从开发到部署
整理一下怎么开发flask程序应部署到生产环境中 1. 第一个flask 程序 myapp.py from flask import Flask app = Flask(__name__) @app. ...
- Mongodb入门3
company数据库下面heros集合里的数据: { "_id" : ObjectId("6100c897d0c9f4158c2b0c9b"), "n ...
- JavaScript Library – Lit
前言 我写过一篇关于 Lit 的文章,Material Design, Angular Material, MDC, MWC, Lit 的关系. 如今 material-web MWC 已经发布 1. ...
- Redis 入门 - 收官
<Redis 入门>系列文章总算完成了,希望这个系列文章可以想入门或刚入门的同学提供帮助,希望能让你形成学习Redis系统性概念. 当时为什么要写这个系列文章,是因为我自己就是迷迷糊糊一路 ...
- GPUStack正式发布: 为大模型而生的开源GPU集群管理器
经过 Seal 研发团队几个月来持续的开发和测试,我们满怀期待及热情地发布新的产品 GPUStack,GPUStack 是一个用于运行 LLM(大型语言模型)的开源 GPU 集群管理器.尽管如今大语言 ...
- CentOS 7 下通过 Cython 编写 python 扩展
1. 安装 python 和 python-devel(没有后者,install 的时候会报错 "Scanners.c:21:20: fatal error: Python.h: No su ...
- 6款支持C#语言的AI辅助编程工具,开发效率提升利器!
前言 在这个AI迅速发展的阶段,涌现出了一大批好用的AI辅助编程工具.AI辅助编程工具能够提高开发效率.改善代码质量.降低bug率,是现代软件开发过程中的重要助手.今天大姚给大家分享6款AI辅助编程工 ...