当前位置: 首页 > article >正文

计算机图形学学习日志3

计算机图形学学习日志3

      • 1.Z-Buffer算法
      • 2.漫反射的能量计算
      • 作业2

1.Z-Buffer算法

由画家算法推广而来。
画家算法:优先选择深度更深的东西渲染,循序渐进。
不足:无法处理3个或者更多个两两互相覆盖的图形。
Z-Buffer在以画家算法思想(没有排序)渲染的时候,记录当前像素点的深度,如果后续该点的深度大于新渲染的点的深度,那么更新这个像素点的深度为较浅的这个。

2.漫反射的能量计算

在这里插入图片描述
kd表示漫反射系数,i表示单位长度能获取的能量,r表示距离光源距离,max(0, n*l)表示入射和发现夹角余弦值,与v无关。

在这里插入图片描述
由于能量守恒,单位面积光强相等,小的圆球表面积总和光强应该等于大圆球表面积总和光强。所以图示点位光强是I/r^2。

作业2

在这里插入图片描述
来源:九九345

auto v = t.toVector4();

// TODO : Find out the bounding box of current triangle.

int minX = std::min(std::min(v[0].x(), v[1].x()), v[2].x());
int minY = std::min(std::min(v[0].y(), v[1].y()), v[2].y());
int maxX = std::max(std::max(v[0].x(), v[1].x()), v[2].x());
int maxY = std::max(std::max(v[0].y(), v[1].y()), v[2].y());
//得到三角形的边框盒
// iterate through the pixel and find if the current pixel is inside the triangle
for (int i = minX; i <= maxX; i++)
{
    for (int j = maxY; j <= maxY; j++)
    {
        if (insideTriangle(i + 0.5, j +0.5, t.v))
        {
            auto[alpha, beta, gamma] = computeBarycentric2D(i, j, 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;
            //得到目前的深度z
            int cur_index = get_index(i, j);//获取当前坐标的索引
            if (z_interpolated < depth_buf[cur_index])//通过索引找到该坐标目前的深度 
            {
                depth_buf[cur_index] = z_interpolated;//如果比目前的深则更新
                Vector3f vertex;
                vertex << i, j, z_interpolated;
                set_pixel(vertex, t.getColor());//重新渲染该点
            }
        }
        
    }
static bool insideTriangle(int x, int y, const Vector3f* _v)
{   //向量叉乘
    // TODO : Implement this function to check if the point (x, y) is inside the triangle represented by _v[0], _v[1], _v[2]
    Eigen::Vector2f AB, BC, CA, AP, BP, CP, p;
    float a, b, c;
    p << x, y;
    AB = _v[1].head(2) - _v[0].head(2);
    AP = p - _v[0].head(2);
    BC = _v[2].head(2) - _v[1].head(2);
    BP = p - _v[1].head(2);
    CA = _v[0].head(2) - _v[2].head(2);
    CP = p - _v[2].head(2);
    a = AB[0] * AP[1] - AB[1] * AP[0];
    b = BC[0] * BP[1] - BC[1] * BP[0];
    c = CA[0] * CP[1] - CA[1] * CP[0];
    if (a > 0 && b > 0 && c > 0) return true;
    else if (a < 0 && b < 0 && c < 0) return true;
    return false;
}

http://www.kler.cn/a/589641.html

相关文章:

  • 数智读书笔记系列018 《多模态大模型:技术原理与实战》解锁多模态从原理到实战的深度探索
  • 3.14学习总结 排序算法
  • 高精度加法,高精度乘法,高精度除法,高精度减法,链表相加
  • 如何仅在conda中更新gcc版本
  • 什么是数学建模?数学建模是将实际问题转化为数学问题
  • Linux服务器跑python脚本定时任务
  • AIP-181 稳定级别
  • fastapi+angular实现个人博客
  • 什么是死锁?如何避免死锁?
  • 【spring boot 实现图片验证码 前后端】
  • ESP32学习 -从STM32工程架构进阶到ESP32架构
  • MySQL 性能优化:索引优化 + 读写分离 + Redis 缓存,TPS 提升 175% 实战解析
  • 《Classifier-Free Diffusion Guidance》的核心观点与方法
  • 三层架构与MVC架构的本质:从设计思想到实战选择
  • 贝叶斯网络的基本概念并构建一个贝叶斯网络(实例)
  • QT 磁盘文件 教程04-创建目录、删除目录、遍历目录
  • IntelliJ IDEA 中 Maven 的 `pom.xml` 变灰带横线?一文详解解决方法
  • 微服务即时通信系统---(八)用户管理子服务
  • 2025交易所开发突围:AI增强型撮合引擎与零知识证明跨链架构
  • 有趣的算法实践:整数反转与回文检测(Java实现)