|
3 | 3 |
|
4 | 4 | #include <raytracer/raytracer.hpp>
|
5 | 5 |
|
6 |
| -static Vec3f GetCenter(const Scene& scene) { |
7 |
| - Vec3f c{0, 0, 0}; |
8 |
| - for (auto&& v : scene.GetGeometricVertices()) { |
9 |
| - c = c + Vec3f{v.x, v.y, v.z}; |
| 6 | +class BBox { |
| 7 | +public: |
| 8 | + BBox(const std::vector<GeometricVertex> &vertices) { |
| 9 | + const auto &v = vertices.front(); |
| 10 | + |
| 11 | + Vec3f min = Vec3f{v.x, v.y, v.z}; |
| 12 | + Vec3f max = Vec3f{v.x, v.y, v.z}; |
| 13 | + |
| 14 | + for (auto&& v : vertices) { |
| 15 | + min.x = std::min(min.x, v.x); |
| 16 | + min.y = std::min(min.y, v.y); |
| 17 | + min.z = std::min(min.z, v.z); |
| 18 | + |
| 19 | + max.x = std::max(max.x, v.x); |
| 20 | + max.y = std::max(max.y, v.y); |
| 21 | + max.z = std::max(max.z, v.z); |
| 22 | + } |
| 23 | + |
| 24 | + _coords[0] = Vec3f{min.x, min.y, min.z}; |
| 25 | + _coords[1] = Vec3f{max.x, min.y, min.z}; |
| 26 | + _coords[2] = Vec3f{min.x, max.y, min.z}; |
| 27 | + _coords[3] = Vec3f{max.x, max.y, min.z}; |
| 28 | + _coords[4] = Vec3f{min.x, min.y, max.z}; |
| 29 | + _coords[5] = Vec3f{max.x, min.y, max.z}; |
| 30 | + _coords[6] = Vec3f{min.x, max.y, max.z}; |
| 31 | + _coords[7] = Vec3f{max.x, max.y, max.z}; |
10 | 32 | }
|
11 |
| - return c / scene.GetGeometricVertices().size(); |
12 |
| -} |
| 33 | + |
| 34 | + Vec3f GetCenter() const { |
| 35 | + return { (_coords[0].x + _coords[7].x) / 2, |
| 36 | + (_coords[0].y + _coords[7].y) / 2, |
| 37 | + (_coords[0].z + _coords[7].z) / 2 }; |
| 38 | + } |
| 39 | + |
| 40 | + double GetDiag() const { |
| 41 | + return (_coords[0] - _coords[6]).norm(); |
| 42 | + } |
| 43 | + |
| 44 | +private: |
| 45 | + std::array<Vec3f, 8> _coords; |
| 46 | +}; |
13 | 47 |
|
14 | 48 | int main(int argc, const char** argv) {
|
15 | 49 | CameraOptions camera_opts(500, 500);
|
16 | 50 | RenderOptions render_opts{3};
|
17 | 51 |
|
| 52 | + if (argc < 2) { |
| 53 | + throw std::logic_error("Provide path to texture file (*.obj)"); |
| 54 | + } |
| 55 | + |
| 56 | + double zoom = 1.0; |
| 57 | + if (argc == 3) { |
| 58 | + zoom = std::stod(argv[2]); |
| 59 | + } |
| 60 | + |
18 | 61 | const std::string obj_filename = argv[1];
|
19 |
| - const auto scene = Parse(obj_filename); |
20 |
| - const auto center = GetCenter(scene); |
| 62 | + auto scene = Parse(obj_filename); |
| 63 | + BBox bbox(scene.GetGeometricVertices()); |
| 64 | + auto c = bbox.GetCenter(); |
| 65 | + auto d = bbox.GetDiag(); |
| 66 | + Vec3f look = {c.x + d * zoom, |
| 67 | + c.y + d * zoom, |
| 68 | + c.z + d * zoom}; |
| 69 | + |
| 70 | + std::cout << "[INFO] Zoom is equal to " << zoom << std::endl; |
| 71 | + std::cout << "[INFO] Object center is: " << c << std::endl; |
| 72 | + std::cout << "[INFO] Diag: " << d << std::endl; |
21 | 73 |
|
22 |
| - std::cout << "Object center is: " << center << std::endl; |
| 74 | + if (scene.GetLights().empty()) { |
| 75 | + std::cout << "[WARNING] Scene doesn't contain any light object." << std::endl; |
| 76 | + // NB: Put some light by default. |
| 77 | + scene.AddLight(Light{look, {1, 1, 1}}); |
| 78 | + } |
| 79 | + |
| 80 | + std::cout << "[INFO] Number of objects on scene: " << scene.GetObjects().size() << std::endl; |
23 | 81 |
|
24 |
| - camera_opts.look_from = std::array<double, 3>{std::stod(argv[2]), |
25 |
| - std::stod(argv[3]), |
26 |
| - std::stod(argv[4])}; |
27 |
| - camera_opts.look_to = std::array<double, 3>{center.x, center.y, center.z}; |
| 82 | + camera_opts.look_from = std::array<double, 3>{look.x, look.y, look.z}; |
| 83 | + camera_opts.look_to = std::array<double, 3>{c.x, c.y, c.z}; |
28 | 84 |
|
29 | 85 | using namespace std::chrono;
|
30 | 86 | auto start = high_resolution_clock::now();
|
31 |
| - auto image = Render(obj_filename, camera_opts, render_opts); |
| 87 | + auto image = Render(scene, camera_opts, render_opts); |
32 | 88 | auto end = high_resolution_clock::now();
|
33 | 89 | auto elapsed = duration_cast<milliseconds>(end-start).count();
|
34 |
| - image.Write("output.png"); |
35 | 90 |
|
36 |
| - std::cout << "Elapsed time: " << elapsed << " ms" << std::endl; |
| 91 | + std::cout << "[INFO] Rendering time: " << elapsed << " ms" << std::endl; |
| 92 | + |
| 93 | + image.Write("output.png"); |
| 94 | + std::cout << "[INFO] Dump result to output.png" << std::endl; |
37 | 95 |
|
38 | 96 | return 0;
|
39 | 97 | }
|
0 commit comments