以下代码是一个典型的渲染程序的主函数,它通过调用其他自定义的类和函数来实现渲染场景的目的。主要流程如下:

  1. 引入需要的头文件,包括自定义的'Scene'、'Sphere'、'Triangle'、'Light' 和 'Renderer' 类。

  2. 在主函数中创建一个 'Scene' 对象,设置图像的宽度和高度。

  3. 创建两个 'Sphere' 对象,设置它们的位置、半径和材质属性,然后将它们添加到 'Scene' 中。

  4. 创建一个 'MeshTriangle' 对象,设置它的顶点坐标、索引、纹理坐标和材质属性,并将其添加到 'Scene' 中。

  5. 创建两个 'Light' 对象,设置它们的位置和强度,并将它们添加到 'Scene' 中。

  6. 创建一个 'Renderer' 对象,调用它的 'Render' 函数来渲染 'Scene' 中的内容。

  7. 程序结束并返回 0。

整个程序的目的是渲染一个包含两个球体和一个平面的场景,并在场景中添加两个光源。其中球体和平面的材质属性不同,分别为漫反射和反射折射,平面的纹理坐标也不同。最终渲染出的图像将显示出这些对象之间的光照和阴影效果。

#include "Scene.hpp"
#include "Sphere.hpp"
#include "Triangle.hpp"
#include "Light.hpp"
#include "Renderer.hpp"

// In the main function of the program, we create the scene (create objects and lights)
// as well as set the options for the render (image width and height, maximum recursion
// depth, field-of-view, etc.). We then call the render function().
int main()
{
    Scene scene(1280, 960);

    auto sph1 = std::make_unique<Sphere>(Vector3f(-1, 0, -12), 2);
    sph1->materialType = DIFFUSE_AND_GLOSSY;
    sph1->diffuseColor = Vector3f(0.6, 0.7, 0.8);

    auto sph2 = std::make_unique<Sphere>(Vector3f(0.5, -0.5, -8), 1.5);
    sph2->ior = 1.5;
    sph2->materialType = REFLECTION_AND_REFRACTION;

    scene.Add(std::move(sph1));
    scene.Add(std::move(sph2));

    Vector3f verts[4] = {{-5,-3,-6}, {5,-3,-6}, {5,-3,-16}, {-5,-3,-16}};
    uint32_t vertIndex[6] = {0, 1, 3, 1, 2, 3};
    Vector2f st[4] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
    auto mesh = std::make_unique<MeshTriangle>(verts, vertIndex, 2, st);
    mesh->materialType = DIFFUSE_AND_GLOSSY;

    scene.Add(std::move(mesh));
    scene.Add(std::make_unique<Light>(Vector3f(-20, 70, 20), 0.5));
    scene.Add(std::make_unique<Light>(Vector3f(30, 50, -12), 0.5));    

    Renderer r;
    r.Render(scene);

    return 0;
}
C++ 渲染程序主函数代码示例:场景创建与渲染流程

原文地址: https://www.cveoy.top/t/topic/ofsr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录