-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
104 lines (90 loc) · 4.22 KB
/
Copy pathMain.cpp
File metadata and controls
104 lines (90 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
** EPITECH PROJECT, 2023
** B-OOP-400-LIL-4-1-bsraytracer-victor.braun
** File description:
** main
*/
#include <fstream>
#include <iostream>
#include "usetools/Vector3D.hpp"
#include "Scene/Camera.hpp"
#include "Scene/Scene.hpp"
#include "Primitive/Shape.hpp"
#include "Primitive/Cylinder.hpp"
#include "Primitive/Cone.hpp"
#include "Light/DirectionalLight.hpp"
#include "Light/PointLight.hpp"
#include "Light/ConeLight.hpp"
#include "Color/Color.hpp"
#include "Color/PhongColor.hpp"
#include "Material.hpp"
#include "RenderParametre.hpp"
#include <SFML/Graphics.hpp>
#include "Parser/ConfigParser.hpp"
void saveToPPM(const sf::Image &image, const std::string &filename) {
std::ofstream outFile(filename, std::ios::binary);
if (!outFile) {
std::cerr << "Error: Unable to open output file." << std::endl;
return;
}
unsigned int width = image.getSize().x;
unsigned int height = image.getSize().y;
outFile << "P6\n" << width << " " << height << "\n255\n";
for (unsigned int y = 0; y < height; y++) {
for (unsigned int x = 0; x < width; x++) {
sf::Color color = image.getPixel(x, y);
outFile.write(reinterpret_cast<const char *>(&color.r), 1);
outFile.write(reinterpret_cast<const char *>(&color.g), 1);
outFile.write(reinterpret_cast<const char *>(&color.b), 1);
}
}
outFile.close();
}
int main(int ac, char **av)
{
if (ac != 2)
{
std::cerr << "Error: Invalid number of arguments." << std::endl;
return 84;
}
ConfigParser configParser;
if (!configParser.parseFile(av[1]))
{
std::cerr << "Error: Unable to open config file." << std::endl;
return 84;
}
ConfigParser::Resolution resolution = configParser.getResolution();
int samples = configParser.getSamples();
int bounceReflections = configParser.getBouncesReflection();
ConfigParser::Vector origin = configParser.getOrigin();
ConfigParser::Vector target = configParser.getTarget();
ConfigParser::Vector upguide = configParser.getUpguide();
double aspectRatio = configParser.getAspectRatio();
double fov = configParser.getFov();
Scene scene(RenderParametre(resolution.width, resolution.height, bounceReflections, samples),
Camera(Point3D(origin.x, origin.y, origin.z), Point3D(target.x, target.y, target.z), Vector3D(upguide.x, upguide.y, upguide.z), fov, aspectRatio));
std::vector<ConfigParser::Material> materials = configParser.getMaterials();
for (const auto &materials : materials)
scene.add_material(Material(Color(materials.color.r, materials.color.g, materials.color.b), materials.roughness, materials.reflectance));
std::vector<ConfigParser::Sphere> spheres = configParser.getSpheres();
for (const auto &sphere : spheres)
scene.add_primitive(std::make_shared<Sphere>(Point3D(sphere.center.x, sphere.center.y, sphere.center.z), sphere.radius, sphere.index_material));
std::vector<ConfigParser::Plane> planes = configParser.getPlanes();
for (const auto &plane : planes)
scene.add_primitive(std::make_shared<Plane>(Point3D(plane.origin.x, plane.origin.y, plane.origin.z), Vector3D(plane.normal.x, plane.normal.y, plane.normal.z), plane.index_material));
std::vector<ConfigParser::PointLight> pointLights = configParser.getPointLights();
for (const auto &pointLight : pointLights)
scene.add_light(std::make_shared<PointLight>(Point3D(pointLight.position.x, pointLight.position.y, pointLight.position.z), Color(pointLight.color.r, pointLight.color.g, pointLight.color.b), pointLight.intensity));
std::vector<ConfigParser::DirectionalLight> directionalLights = configParser.getDirectionalLights();
for (const auto &directionalLight : directionalLights)
scene.add_light(std::make_shared<DirectionalLight>(Vector3D(directionalLight.direction.x, directionalLight.direction.y, directionalLight.direction.z), Color(directionalLight.color.r, directionalLight.color.g, directionalLight.color.b), directionalLight.intensity));
sf::Image image;
if (scene.mode == 0) {
image = scene.render_must();
}
else {
image = scene.render_could();
}
image.saveToFile("image/test.png");
saveToPPM(image, "image/test.ppm");
}