前言:图形学第二弹。这一章还讲了一点点光照算法,很是炫酷。立一个 Flag。四月份之前搞定基础图形学知识和《统计学习方法》
不填充的三角形
还记得我们上一次说的画直线的算法吗?所以最简单的不填充的算法就是
void trangle1(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage &image, TGAColor color) {
line(t0, t1, image, color);
line(t1, t2, image, color);
line(t2, t1, image, color);
}
变化
首先,对于需要填充的三角形,我们的外围不用画的这么好
其次,我们需要一行一行的填充像素,所以画填充三角形时,我们是一行一行的填充像素。
改进
为了方便小伙伴们理解,先画一部分的三角形
代码
void triangle1(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage &image, TGAColor color) {
// 先按 y 排序
if(t0.y > t1.y) { std::swap(t0, t1); }
if(t0.y > t2.y) { std::swap(t0, t2); }
if(t1.y > t2.y) { std::swap(t1, t2); }
// 递增 y 画点
int total_height = t2.y - t0.y;
for(int y = t0.y; y <= t1.y; y++) {
// 两条直线一起画
// 由于 segment_height 可能为 0 所以我们加 1
int segment_height = t1.y - t0.y + 1;
float alpha = (float)(y - t0.y) / segment_height;
float beta = (float)(y - t0.y) / total_height;
Vec2i A = t0 + (t1 - t0) * alpha;
Vec2i B = t0 + (t2 - t0) * beta;
image.set(A.x, y, white);
image.set(B.x, y, green);
}
}
int main(int argc, char **argv) {
TGAImage image(width, height, TGAImage::RGB);
std::cout<<"test"<<std::endl;
Vec2i t0[3] = {Vec2i(10, 70), Vec2i(50, 160), Vec2i(70, 80)};
Vec2i t1[3] = {Vec2i(180, 50), Vec2i(150, 1), Vec2i(70, 180)};
Vec2i t2[3] = {Vec2i(180, 150), Vec2i(120, 160), Vec2i(130, 180)};
triangle1(t0[0], t0[1], t0[2], image, red);
triangle1(t1[0], t1[1], t1[2], image, white);
triangle1(t2[0], t2[1], t2[2], image, green);
image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
image.write_tga_file("output.tga");
return 0;
}
分析
你们看!这里存在洞洞!!!
但是如果你们填充起来,就一点都不影响,因为我们是!一行!一行画的。就是没有洞洞,怕不怕!
最难理解的是
alpha 和 beta 的含义,以及新的点是如何计算出来的。
其实就是计算 y 的比例。然后计算整体向量的比例。
填像素
代码
void triangle2(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage &image, TGAColor color) {
// 先按 y 排序
if(t0.y > t1.y) { std::swap(t0, t1); }
if(t0.y > t2.y) { std::swap(t0, t2); }
if(t1.y > t2.y) { std::swap(t1, t2); }
// std::cout<< t0.y << t1.y << t2.y << std::endl;
// 递增 y 画点
int total_height = t2.y - t0.y;
for(int y = t0.y; y <= t1.y; y++) {
// 两条直线一起画
// 由于 segment_height 可能为 0 所以我们加 1
int segment_height = t1.y - t0.y + 1;
float alpha = (float)(y - t0.y) / segment_height;
float beta = (float)(y - t0.y) / total_height;
Vec2i A = t0 + (t1 - t0) * alpha;
Vec2i B = t0 + (t2 - t0) * beta;
if(A.x > B.x) { std::swap(A, B); }
// 连接两个点
for(int x = A.x; x <= B.x; x++) {
image.set(x, y, white);
}
}
}
结果
分析
你们看!洞洞不见了,怕不怕!
画另一部分三角形
void triangle(Vec2i t0, Vec2i t1, Vec2i t2, TGAImage &image, TGAColor color) {
// 先按 y 排序
if(t0.y > t1.y) { std::swap(t0, t1); }
if(t0.y > t2.y) { std::swap(t0, t2); }
if(t1.y > t2.y) { std::swap(t1, t2); }
// std::cout<< t0.y << t1.y << t2.y << std::endl;
// 递增 y 画点
int total_height = t2.y - t0.y;
for(int y = t0.y; y <= t1.y; y++) {
// 两条直线一起画
// 由于 segment_height 可能为 0 所以我们加 1
int segment_height = t1.y - t0.y + 1;
float alpha = (float)(y - t0.y) / segment_height;
float beta = (float)(y - t0.y) / total_height;
Vec2i A = t0 + (t1 - t0) * alpha;
Vec2i B = t0 + (t2 - t0) * beta;
if(A.x > B.x) { std::swap(A, B); }
// 连接两个点
for(int x = A.x; x <= B.x; x++) {
image.set(x, y, white);
}
}
// 画第二部分
for(int y = t1.y; y <= t2.y; y++) {
// 两条直线一起画
int segment_height = t2.y - t1.y + 1;
float alpha = (float)(y - t1.y) / segment_height;
float beta = (float)(y - t0.y) / total_height;
Vec2i A = t1 + (t2 - t1) * alpha;
Vec2i B = t0 + (t2 - t0) * beta;
if(A.x > B.x) { std::swap(A, B); }
// 连接两个点
for(int x = A.x; x <= B.x; x++) {
image.set(x, y, white);
}
}
}
结果
画填充三角形的另一个算法
先看一段伪代码
triangle(vec2 points[3]) {
vec2 bbox[2] = find_bounding_box(points);
for (each pixel in the bounding box) {
if (inside(points, pixel)) {
put_pixel(pixel);
}
}
}
意思是,先找到这个三角形属于的长方形,遍历这个长方形的每一个像素。看这个像素的位置在不在三角形里面,如果在。就画这个点,如果不在就不画这个点。
最后
我会在另一篇文章中,介绍我们学习到的第一个光照算法