• 【GAMES101 作业3】渲染小奶牛+使用.obj模型+双线性插值+踩坑总结


    目录

    一、渲染小奶牛

    1. normal shader

    get_projection_matrix (来自作业1,填充在main.cpp中)

    rasterize_triangle(来自作业2,填充在rasterizer.cpp中)

    2. phong shader

    phong_fragment_shader(填充在main.cpp中)

    3. texture shader

    texture_fragment_shader(填充在main.cpp中)

    4. bump shader

    bump_fragment_shader(填充在main.cpp中)

    4. displacement shader

    displacement_fragment_shader(填充在main.cpp中)

    二、使用.obj模型

    三、双线性插值

    四、踩坑总结


    一、渲染小奶牛

    1. normal shader

    根据官方教程,我们需要使用 作业1、作业2 里面实现的代码来填充以下两个函数,并得到样例normal shader的渲染结果:

    • get_projection_matrix (来自作业1,填充在main.cpp中)

    1. // TODO: Use the same projection matrix from the previous assignments
    2. Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar)
    3. {
    4. Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
    5. Eigen::Matrix4f M_p = Eigen::Matrix4f::Identity();
    6. M_p << zNear,0,0,0,
    7. 0,zNear,0,0,
    8. 0,0,zNear+zFar,(-1.0*zNear*zFar),
    9. 0,0,1,0;
    10. //[l,r] [b,t] [f,n]
    11. float angle = eye_fov*MY_PI/180;
    12. float t = tan(angle/2)*-zNear;
    13. float b = -1.0*t;
    14. float r = t*aspect_ratio;
    15. float l = -1.0*r;
    16. Eigen::Matrix4f M_s = Eigen::Matrix4f::Identity();
    17. M_s << 2/(r-l),0,0,0,
    18. 0,2/(t-b),0,0,
    19. 0,0,2/(zNear-zFar),0,
    20. 0,0,0,1;
    21. Eigen::Matrix4f M_t = Eigen::Matrix4f::Identity();
    22. M_t << 1,0,0,(-1.0)*(r+l)/2,
    23. 0,1,0,(-1.0)*(t+b)/2,
    24. 0,0,1,(-1.0)*(zNear+zFar)/2,
    25. 0,0,0,1;
    26. projection = M_s*M_t*M_p*projection;
    27. return projection;
    28. }
    • rasterize_triangle(来自作业2,填充在rasterizer.cpp中)

    1. // my func
    2. float min2(float a, float b) {
    3. if (a < b) return a;
    4. else return b;
    5. }
    6. // my func
    7. float min3(float a, float b, float c) {
    8. return min2(min2(a,b), c);
    9. }
    10. // my func
    11. float max2(float a, float b) {
    12. if (a > b) return a;
    13. else return b;
    14. }
    15. // my func
    16. float max3(float a, float b, float c) {
    17. return max2(max2(a,b), c);
    18. }
    19. //Screen space rasterization
    20. void rst::rasterizer::rasterize_triangle(const Triangle& t, const std::array3>& view_pos)
    21. {
    22. auto v = t.toVector4();
    23. const Eigen::Vector4f _v[3] = {
    24. {v[0].x(), v[0].y(), v[0].z(), v[0].w()},
    25. {v[1].x(), v[1].y(), v[1].z(), v[1].w()},
    26. {v[2].x(), v[2].y(), v[2].z(), v[2].w()}
    27. };
    28. // TODO : Find out the bounding box of current triangle.
    29. int x_min = (int) (min3(v[0].x(), v[1].x(), v[2].x())/1.0) -1;
    30. int x_max = (int) (max3(v[0].x(), v[1].x(), v[2].x())/1.0) +1;
    31. int y_min = (int) (min3(v[0].y(), v[1].y(), v[2].y())/1.0) -1;
    32. int y_max = (int) (max3(v[0].y(), v[1].y(), v[2].y())/1.0) +1;
    33. // iterate through the pixel and find if the current pixel is inside the triangle.
    34. for (int x = x_min-1; x < x_max; ++x) {
    35. for (int y = y_min-1; y < y_max; ++y) {
    36. if (insideTriangle(x+0.5, y+0.5, _v)) {
    37. // If so, use the following code to get the interpolated z value.
    38. auto[alpha, beta, gamma] = computeBarycentric2D(x, y, t.v);
    39. // Z is interpolated view space depth for the current pixel
    40. float Z = 1.0 / (alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
    41. float zp = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
    42. zp *= Z; // zp is for z-buffer
    43. auto ind = get_index(x, y);
    44. if (ind < width * height && zp < depth_buf[ind]) {
    45. depth_buf[ind] = zp; // update z-buffer
    46. // TODO: Interpolate the attributes:
    47. auto interpolated_color = alpha*t.color[0] + beta*t.color[1] + gamma*t.color[2];
    48. auto interpolated_normal = alpha*t.normal[0] + beta*t.normal[1] + gamma*t.normal[2];
    49. auto interpolated_texcoords = alpha*t.tex_coords[0] + beta*t.tex_coords[1] + gamma*t.tex_coords[2];
    50. auto interpolated_shadingcoords = interpolate(alpha, beta, gamma, view_pos[0], view_pos[1], view_pos[2], 1);
    51. fragment_shader_payload payload(interpolated_color, interpolated_normal.normalized(), interpolated_texcoords, texture ? &*texture : nullptr);
    52. payload.view_pos = interpolated_shadingcoords;
    53. auto pixel_color = fragment_shader(payload);
    54. // TODO : set the current pixel (use the set_pixel function) to the color of the triangle (use getColor function) if it should be painted.
    55. Eigen::Vector2i point(x, y); // the point of the pixel
    56. set_pixel(point, pixel_color);
    57. }
    58. }
    59. }
    60. }
    61. }

    然后在项目文件夹中使用如下指令:

    1. mkdir build
    2. cd build
    3. cmake ..
    4. make
    5. ./Rasterizer normal.png normal

    即可看到normal shader渲染结果:

    normal shader 渲染结果

    2. phong shader

    • phong_fragment_shader(填充在main.cpp中)

    这里要注意一个点,ka kd ks这些系数与后面的变量相乘都是使用 cwiseProduct() 方法,其作用为矩阵点对点相乘,因为这些系数都是表示颜色的(R,G,B)三维向量,而不是常数。

    1. Eigen::Vector3f phong_fragment_shader(const fragment_shader_payload& payload)
    2. {
    3. Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
    4. Eigen::Vector3f kd = payload.color;
    5. Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
    6. auto l1 = light{{20, 20, 20}, {500, 500, 500}};
    7. auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
    8. std::vector lights = {l1, l2};
    9. Eigen::Vector3f amb_light_intensity{10, 10, 10};
    10. Eigen::Vector3f eye_pos{0, 0, 10};
    11. float p = 150;
    12. Eigen::Vector3f color = payload.color;
    13. Eigen::Vector3f point = payload.view_pos;
    14. Eigen::Vector3f normal = payload.normal;
    15. Eigen::Vector3f result_color = {0, 0, 0};
    16. for (auto& light : lights)
    17. {
    18. // TODO: For each light source in the code, calculate what the *ambient*, *diffuse*, and *specular* components are.
    19. Eigen::Vector3f light_dir = (light.position - point).normalized();
    20. Eigen::Vector3f view_dir = (eye_pos - point).normalized();
    21. Eigen::Vector3f half_vector = (light_dir + view_dir).normalized();
    22. // distance r^2
    23. float r2 = (light.position - point).dot(light.position - point);
    24. // ambient
    25. Eigen::Vector3f La = ka.cwiseProduct(amb_light_intensity); //cwiseProduct():矩阵点对点相乘
    26. // diffuse
    27. Eigen::Vector3f Ld = kd.cwiseProduct(light.intensity / r2);
    28. Ld *= std::max(0.0f, normal.normalized().dot(light_dir));
    29. // specular
    30. Eigen::Vector3f Ls = ks.cwiseProduct(light.intensity / r2);
    31. Ls *= std::pow(std::max(0.0f, normal.normalized().dot(half_vector)), p);
    32. // Then, accumulate that result on the *result_color* object.
    33. result_color += (La + Ld + Ls);
    34. }
    35. return result_color * 255.f;
    36. }

    然后在项目文件夹中使用如下指令:

    1. mkdir build
    2. cd build
    3. cmake ..
    4. make
    5. ./Rasterizer phong.png phong

    即可看到phong shader渲染结果:

     phong shader 渲染结果

    3. texture shader

    • texture_fragment_shader(填充在main.cpp中)

    1. Eigen::Vector3f texture_fragment_shader(const fragment_shader_payload& payload)
    2. {
    3. Eigen::Vector3f return_color = {0, 0, 0};
    4. if (payload.texture)
    5. {
    6. // TODO: Get the texture value at the texture coordinates of the current fragment
    7. return_color = payload.texture->getColor(payload.tex_coords.x(), payload.tex_coords.y());
    8. }
    9. Eigen::Vector3f texture_color;
    10. texture_color << return_color.x(), return_color.y(), return_color.z();
    11. Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
    12. Eigen::Vector3f kd = texture_color / 255.f; // change kd to texture color, compared to phong shader
    13. Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
    14. auto l1 = light{{20, 20, 20}, {500, 500, 500}};
    15. auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
    16. std::vector lights = {l1, l2};
    17. Eigen::Vector3f amb_light_intensity{10, 10, 10};
    18. Eigen::Vector3f eye_pos{0, 0, 10};
    19. float p = 150;
    20. Eigen::Vector3f color = texture_color;
    21. Eigen::Vector3f point = payload.view_pos;
    22. Eigen::Vector3f normal = payload.normal;
    23. Eigen::Vector3f result_color = {0, 0, 0};
    24. for (auto& light : lights)
    25. {
    26. // TODO: For each light source in the code, calculate what the *ambient*, *diffuse*, and *specular* components are.
    27. Eigen::Vector3f light_dir = (light.position - point).normalized();
    28. Eigen::Vector3f view_dir = (eye_pos - point).normalized();
    29. Eigen::Vector3f half_vector = (light_dir + view_dir).normalized();
    30. // distance r^2
    31. float r2 = (light.position - point).dot(light.position - point);
    32. // ambient
    33. Eigen::Vector3f La = ka.cwiseProduct(amb_light_intensity); //cwiseProduct():矩阵点对点相乘
    34. // diffuse
    35. Eigen::Vector3f Ld = kd.cwiseProduct(light.intensity / r2);
    36. Ld *= std::max(0.0f, normal.normalized().dot(light_dir));
    37. // specular
    38. Eigen::Vector3f Ls = ks.cwiseProduct(light.intensity / r2);
    39. Ls *= std::pow(std::max(0.0f, normal.normalized().dot(half_vector)), p);
    40. // Then, accumulate that result on the *result_color* object.
    41. result_color += (La + Ld + Ls);
    42. }
    43. return result_color * 255.f;
    44. }

    然后在项目文件夹中使用如下指令:

    1. mkdir build
    2. cd build
    3. cmake ..
    4. make
    5. ./Rasterizer texture.png texture

    即可看到phong shader渲染结果:

     texture shader 渲染结果

    4. bump shader

    • bump_fragment_shader(填充在main.cpp中)

    这里有一点坑,此处参考了大佬的文章:

    Games101-作业3 - 知乎

    大体来说,通过改变物体表面的法线朝向而不是改变物体的体积来实现细小的凹凸不平的效果,所以bump mapping译作凹凸贴图。

    需注意的是 h(u, v) 的实现方法:

    1. Eigen::Vector3f bump_fragment_shader(const fragment_shader_payload& payload)
    2. {
    3. Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
    4. Eigen::Vector3f kd = payload.color;
    5. Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
    6. auto l1 = light{{20, 20, 20}, {500, 500, 500}};
    7. auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
    8. std::vector lights = {l1, l2};
    9. Eigen::Vector3f amb_light_intensity{10, 10, 10};
    10. Eigen::Vector3f eye_pos{0, 0, 10};
    11. float p = 150;
    12. Eigen::Vector3f color = payload.color;
    13. Eigen::Vector3f point = payload.view_pos;
    14. Eigen::Vector3f normal = payload.normal;
    15. float kh = 0.2, kn = 0.1;
    16. // TODO: Implement bump mapping here
    17. // Let n = normal = (x, y, z)
    18. // Vector t = (x*y/sqrt(x*x+z*z),sqrt(x*x+z*z),z*y/sqrt(x*x+z*z))
    19. float x = normal.x(), y = normal.y(), z = normal.z();
    20. Eigen::Vector3f t(
    21. x*y/sqrt(x*x+z*z),
    22. sqrt(x*x+z*z),
    23. z*y/sqrt(x*x+z*z)
    24. );
    25. // Vector b = n cross product t
    26. Eigen::Vector3f b = normal.cross(t);
    27. // Matrix TBN = [t b n]
    28. Eigen::Matrix3f TBN;
    29. TBN << t.x(), b.x(), normal.x(),
    30. t.y(), b.y(), normal.y(),
    31. t.z(), b.z(), normal.z();
    32. // dU = kh * kn * (h(u+1/w,v)-h(u,v))
    33. // dV = kh * kn * (h(u,v+1/h)-h(u,v))
    34. float u = payload.tex_coords.x(), v = payload.tex_coords.y();
    35. float w = payload.texture->width, h = payload.texture->height;
    36. float dU = kh * kn * (payload.texture->getColor(u+1.0/w,v).norm() - payload.texture->getColor(u,v).norm());
    37. float dV = kh * kn * (payload.texture->getColor(u,v+1/h).norm() - payload.texture->getColor(u,v).norm());
    38. // Vector ln = (-dU, -dV, 1)
    39. Eigen::Vector3f ln(-dU, -dV, 1.0f);
    40. // Normal n = normalize(TBN * ln)
    41. normal = (TBN * ln).normalized();
    42. Eigen::Vector3f result_color = {0, 0, 0};
    43. result_color = normal;
    44. return result_color * 255.f;
    45. }

    然后在项目文件夹中使用如下指令:

    1. mkdir build
    2. cd build
    3. cmake ..
    4. make
    5. ./Rasterizer bump.png bump

    即可看到bump shader渲染结果:

     bump shader 渲染结果

    4. displacement shader

    • displacement_fragment_shader(填充在main.cpp中)

    1. Eigen::Vector3f displacement_fragment_shader(const fragment_shader_payload& payload)
    2. {
    3. Eigen::Vector3f ka = Eigen::Vector3f(0.005, 0.005, 0.005);
    4. Eigen::Vector3f kd = payload.color;
    5. Eigen::Vector3f ks = Eigen::Vector3f(0.7937, 0.7937, 0.7937);
    6. auto l1 = light{{20, 20, 20}, {500, 500, 500}};
    7. auto l2 = light{{-20, 20, 0}, {500, 500, 500}};
    8. std::vector lights = {l1, l2};
    9. Eigen::Vector3f amb_light_intensity{10, 10, 10};
    10. Eigen::Vector3f eye_pos{0, 0, 10};
    11. float p = 150;
    12. Eigen::Vector3f color = payload.color;
    13. Eigen::Vector3f point = payload.view_pos;
    14. Eigen::Vector3f normal = payload.normal;
    15. float kh = 0.2, kn = 0.1;
    16. // Let n = normal = (x, y, z)
    17. // Vector t = (x*y/sqrt(x*x+z*z),sqrt(x*x+z*z),z*y/sqrt(x*x+z*z))
    18. float x = normal.x(), y = normal.y(), z = normal.z();
    19. Eigen::Vector3f t(
    20. x*y/sqrt(x*x+z*z),
    21. sqrt(x*x+z*z),
    22. z*y/sqrt(x*x+z*z)
    23. );
    24. // Vector b = n cross product t
    25. Eigen::Vector3f b = normal.cross(t);
    26. // Matrix TBN = [t b n]
    27. Eigen::Matrix3f TBN;
    28. TBN << t.x(), b.x(), normal.x(),
    29. t.y(), b.y(), normal.y(),
    30. t.z(), b.z(), normal.z();
    31. // dU = kh * kn * (h(u+1/w,v)-h(u,v))
    32. // dV = kh * kn * (h(u,v+1/h)-h(u,v))
    33. float u = payload.tex_coords.x(), v = payload.tex_coords.y();
    34. float w = payload.texture->width, h = payload.texture->height;
    35. float dU = kh * kn * (payload.texture->getColor(u+1.0/w,v).norm() - payload.texture->getColor(u,v).norm());
    36. float dV = kh * kn * (payload.texture->getColor(u,v+1/h).norm() - payload.texture->getColor(u,v).norm());
    37. // Vector ln = (-dU, -dV, 1)
    38. Eigen::Vector3f ln(-dU, -dV, 1.0f);
    39. // Position p = p + kn * n * h(u,v)
    40. point = point + kn * normal * payload.texture->getColor(u,v).norm();
    41. // Normal n = normalize(TBN * ln)
    42. normal = (TBN * ln).normalized();
    43. Eigen::Vector3f result_color = {0, 0, 0};
    44. for (auto& light : lights)
    45. {
    46. // TODO: For each light source in the code, calculate what the *ambient*, *diffuse*, and *specular* components are.
    47. Eigen::Vector3f light_dir = (light.position - point).normalized();
    48. Eigen::Vector3f view_dir = (eye_pos - point).normalized();
    49. Eigen::Vector3f half_vector = (light_dir + view_dir).normalized();
    50. // distance r^2
    51. float r2 = (light.position - point).dot(light.position - point);
    52. // ambient
    53. Eigen::Vector3f La = ka.cwiseProduct(amb_light_intensity); //cwiseProduct():矩阵点对点相乘
    54. // diffuse
    55. Eigen::Vector3f Ld = kd.cwiseProduct(light.intensity / r2);
    56. Ld *= std::max(0.0f, normal.normalized().dot(light_dir));
    57. // specular
    58. Eigen::Vector3f Ls = ks.cwiseProduct(light.intensity / r2);
    59. Ls *= std::pow(std::max(0.0f, normal.normalized().dot(half_vector)), p);
    60. // Then, accumulate that result on the *result_color* object.
    61. result_color += (La + Ld + Ls);
    62. }
    63. return result_color * 255.f;
    64. }

    然后在项目文件夹中使用如下指令:

    1. mkdir build
    2. cd build
    3. cmake ..
    4. make
    5. ./Rasterizer displacement.png displacement

    即可看到displacement shader渲染结果:

     displacement shader 渲染结果

     到这里基础部分已经全部做完!

    二、使用.obj模型

    这里还是参考大佬的文章:

    Games101|作业3 + shading + 双线性插值 + 疑惑 - 知乎

    重点如下:

     其中提到了meshlab,在虚拟机ubuntu系统中直接搜索就能安装了:

    然后按照大佬前面说的步骤,将.obj文件载入并添加一些变量就行。

    最后修改main.cpp中对应的文件路径、贴图路径即可。

    下面是我做的效果图(bunny.obj):(texture那里换了一张卡比的贴图,欸嘿~)

    三、双线性插值

    这里参考了大佬:

    GAMES101-现代计算机图形学学习笔记(作业03)_CCCCCCros____的博客-CSDN博客_games101 作业3

    需要注意的是,不同于我们往常使用的XY坐标轴,opencv图像中的坐标轴是这样的:

    在这里插入图片描述

     所以需要先经过 v' = 1 - v 转换后才能取颜色值!

    先在Texture.hpp里面加一句:

    然后在Texture.cpp中实现如下函数:

    1. //
    2. // Created by LEI XU on 4/27/19.
    3. //
    4. #include "Texture.hpp"
    5. #include "global.hpp"
    6. #include
    7. #include
    8. // my func
    9. cv::Vec3b lerp(float x, cv::Vec3b v0, cv::Vec3b v1)
    10. {
    11. return v0 + x*(v1-v0);
    12. };
    13. // my func
    14. float adjust_by_range(float x, float x_min, float x_max)
    15. {
    16. if (x < x_min) return x_min;
    17. if (x > x_max) return x_max;
    18. return x;
    19. };
    20. Eigen::Vector3f Texture::getColor(float u, float v)
    21. {
    22. // 防止数组越界
    23. u = adjust_by_range(u, 0, 1.0f);
    24. v = adjust_by_range(v, 0, 1.0f);
    25. auto u_img = u * (width - 1);
    26. auto v_img = (1 - v) * (height-1);
    27. auto color = image_data.at(v_img, u_img);
    28. return Eigen::Vector3f(color[0], color[1], color[2]);
    29. }
    30. Eigen::Vector3f Texture::getColorBilinear(float u,float v)
    31. {
    32. // 防止数组越界
    33. u = adjust_by_range(u, 0, 1.0f);
    34. v = adjust_by_range(v, 0, 1.0f);
    35. // height和width都-1是指数组从0开始
    36. float u_img = u * (width - 1);
    37. float v_img = (1 - v) * (height - 1);
    38. float u_min = std::floor(u_img);
    39. float u_max = std::ceil(u_img);
    40. float v_min = std::floor(v_img);
    41. float v_max = std::ceil(v_img);
    42. // auto color = image_data.at(v_img, u_img);
    43. auto u00 = image_data.at(v_min, u_min);
    44. auto u10 = image_data.at(v_min, u_max);
    45. auto u01 = image_data.at(v_max, u_min);
    46. auto u11 = image_data.at(v_max, u_max);
    47. float s = (u_img - u_min) / (u_max - u_min);
    48. float t = (v_img - v_min) / (v_max - v_min);
    49. auto u0 = lerp(s, u00, u10);
    50. auto u1 = lerp(s, u01, u11);
    51. auto result = lerp(t, u0, u1);
    52. return Eigen::Vector3f(result[0], result[1], result[2]);
    53. };

    使用以下网站压缩小奶牛的图片,为了效果明显,我压缩到100*100:

    图片压缩 - 在线工具 - OKTools

    然后记得替换main.cpp中的文件路径:

    得到结果如下:

    使用双线性插值前(100*100) 

    使用双线性插值后(100*100) 

    可以看见,插值前小奶牛的纹路边缘较为锋利,像素感比较明显;而插值后的像素感(尤其是眼睛、鼻子处)明显减弱了很多,出现了预料中的与周围颜色平均的结果。

    四、踩坑总结

    记得检查数组越界!!!记得检查数组越界!!!记得检查数组越界!!!

    否则在模型超出opencv的窗口大小(width*height)的时候,容易报错(core dumped)!

    1)在rasterizer类中,depth_buf(存储深度值的数组)和frame_buf(存储像素颜色的数组)是容易产生越界的,检查方法为 :

    ① 在z value存入depth_buf之前先检查get_index函数的返回值ind是否越界,即使用如下语句:

             

    ② 在frame_buf的修改函数set_pixel中设置数组越界检查:

             

    2)在Texture类中增加如下代码,限制(u, v)越界(u, v的范围应为0~1):

  • 相关阅读:
    OpenCV 环境变量参考
    Java实现Excel数据导入数据库
    ubuntu默认关联程序
    Ubuntu 22.04 (WSL2) 安装 libssl1.1
    LinkedList和链表
    Linux--ssh基本操作与配置
    微服务简介
    面试题:Java序列化与反序列化
    UVa11887 Tetrahedrons and Spheres
    【PowerQuery】Excel和PowerBI的PowerQuery 数据刷新
  • 原文地址:https://blog.csdn.net/onion23/article/details/126456501