Games101 作业3 interpolate + phong + texture + bump

这篇博客详细记录了Games101作业中关于光栅化过程的三角形插值,以及Phong光照模型的实现。内容包括环境光、漫反射和镜面反射的计算,同时涉及纹理、凹凸贴图和位移贴图的应用,展示了不同效果的渲染牛牛图像。
摘要由CSDN通过智能技术生成

       学霸笔记
       又是学霸笔记
       还是学霸笔记

       rasterize_triangle 在作业2的基础上对法向量、颜色、纹理颜色与底纹颜色(Shading Colors) 进行插值。翻阅代码发现自带插值函数 interpolate,其余部分依照注释编写即可。

void rst::rasterizer::rasterize_triangle(const Triangle& t, const std::array<Eigen::Vector3f, 3>& view_pos) 
{
   
    // TODO: From your HW3, get the triangle rasterization code.
    // TODO: Inside your rasterization loop:
    //    * v[i].w() is the vertex view space depth value z.
    //    * Z is interpolated view space depth for the current pixel
    //    * zp is depth between zNear and zFar, used for z-buffer

    // float Z = 1.0 / (alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
    // float zp = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
    // zp *= Z;

    // TODO: Interpolate the attributes:

    auto v = t.toVector4();
    float xmin = std::min(t.v[0].x(), std::min(t.v[1].x(), t.v[2].x()));
    float xmax = std::max(t.v[0].x(), std::max(t.v[1].x(), t.v[2].x()));
    float ymin = std::min(t.v[0].y(), std::min(t.v[1].y(), t.v[2].y()));
    float ymax = std::max(t.v[0].y(), std::max(t.v[1].y(), t.v[2].y()));
    for(int x = (int)std::ceil(xmin); x <= xmax; x++){
   
        for(int y = (int)std::ceil(ymin); y <= ymax; y++){
   
            if(!insideTriangle(x, y, t.v)) continue;
            auto[alpha, beta, gamma] = computeBarycentric2D(x + 0.5, y + 0.5, 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;
            int id = get_index(x, y);
            if(z_interpolated < depth_buf[id]){
   
                auto interpolated_color = interpolate(alpha, beta, gamma, t.color[0], t.color[1], t.color[2], 1);
                auto interpolated_normal = interpolate(alpha, beta, gamma, t.normal[0], t.normal[1], t.normal[2], 1);
                auto interpolated_texcoords = interpolate(alpha, beta, gamma, t.tex_coords[0], t.tex_coords[1], t.tex_coords[2], 1);
                auto interpolated_shadingcoords = interpolate(alpha, beta, gamma, view_pos[0], view_pos[1], view_pos[2], 1);

                fragment_shader_payload payload( interpolated_color, interpolated_normal.normalized(), interpolated_texcoords, texture ? &*texture : nullptr);
                payload.view_pos = interpolated_shadingcoords;
                // Use:Instead of passing the triangle's color directly to the frame buffer, pass the color to the shaders first to get the final color;
                auto pixel_color = fragment_shader(payload);
                depth_buf[id] <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值