Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "material.h"
- #include "vector.h"
- #include "object.h"
- #include "light.h"
- #include <vector>
- #include <unordered_map>
- #include <string>
- #include <filesystem>
- #include <fstream>
- #include <sstream>
- class Scene {
- public:
- std::vector<Object> objects_;
- std::vector<SphereObject> sphere_objects_;
- std::vector<Light> lights_;
- std::unordered_map<std::string, Material> materials_;
- const std::vector<Object>& GetObjects() const {
- return objects_;
- }
- const std::vector<SphereObject>& GetSphereObjects() const {
- return sphere_objects_;
- }
- const std::vector<Light>& GetLights() const {
- return lights_;
- }
- const std::unordered_map<std::string, Material>& GetMaterials() const {
- return materials_;
- }
- };
- struct Face {
- int vertexIndex;
- int normalIndex;
- };
- std::unordered_map<std::string, Material> ReadMaterials(const std::filesystem::path& path) {
- std::ifstream file(path.filename());
- std::unordered_map<std::string, Material> materials_;
- Material currentMaterial;
- std::string line;
- while (std::getline(file, line)) {
- std::istringstream iss(line);
- std::string prefix;
- iss >> prefix;
- if (prefix == "newmtl") {
- if (!currentMaterial.name.empty()) {
- materials_[currentMaterial.name] = currentMaterial;
- }
- iss >> currentMaterial.name;
- } else if (prefix == "Ka") {
- iss >> currentMaterial.ambient_color[0] >> currentMaterial.ambient_color[1] >>
- currentMaterial.ambient_color[2];
- } else if (prefix == "Kd") {
- iss >> currentMaterial.diffuse_color[0] >> currentMaterial.diffuse_color[1] >>
- currentMaterial.diffuse_color[2];
- } else if (prefix == "Ks") {
- iss >> currentMaterial.specular_color[0] >> currentMaterial.specular_color[1] >>
- currentMaterial.specular_color[2];
- } else if (prefix == "Ns") {
- iss >> currentMaterial.specular_exponent;
- } else if (prefix == "Ke") {
- iss >> currentMaterial.intensity[0] >> currentMaterial.intensity[1] >>
- currentMaterial.intensity[2];
- } else if (prefix == "Ni") {
- iss >> currentMaterial.refraction_index;
- } else if (prefix == "al") {
- iss >> currentMaterial.albedo[0] >> currentMaterial.albedo[1] >>
- currentMaterial.albedo[2];
- }
- }
- if (!currentMaterial.name.empty()) {
- materials_[currentMaterial.name] = currentMaterial;
- }
- file.close();
- return materials_;
- }
- Scene ReadScene(const std::filesystem::path& path) {
- std::unordered_map<std::string, Material> materials;
- std::unordered_map<std::string, Material> res;
- std::string filename = path.filename();
- std::vector<Vector> vertices;
- std::vector<Vector> normals;
- std::vector<Face> faces;
- std::vector<Light> lights;
- std::vector<Sphere> spheres;
- Scene scene;
- std::ifstream file(filename);
- std::string line;
- std::string cur_material;
- while (std::getline(file, line)) {
- std::istringstream stream(line);
- std::string type;
- stream >> type;
- if (type == "v") {
- Vector vertex;
- stream >> vertex[0] >> vertex[1] >> vertex[2];
- vertices.push_back(vertex);
- } else if (type == "vn") {
- Vector normal;
- stream >> normal[0] >> normal[1] >> normal[2];
- normals.push_back(normal);
- } else if (type == "f") {
- Face face;
- std::string vertexStr;
- while (stream >> vertexStr) {
- std::istringstream vStream(vertexStr);
- std::string index;
- std::getline(vStream, index, '/');
- face.vertexIndex = std::stoi(index) - 1;
- if (std::getline(vStream, index, '/')) {
- if (!index.empty()) {
- face.normalIndex = std::stoi(index) - 1;
- }
- }
- for (size_t i = 2; i < faces.size(); ++i) {
- Triangle triangle(vertices[faces[0].vertexIndex],
- vertices[faces[1].vertexIndex],
- vertices[faces[i].vertexIndex]);
- Object object{&materials[cur_material],
- {&normals[faces[0].normalIndex], &normals[faces[1].normalIndex],
- &normals[faces[i].normalIndex]},
- triangle};
- scene.objects_.emplace_back(object);
- }
- }
- faces.push_back(face);
- } else if (type == "P") {
- Light light;
- stream >> light.position[0] >> light.position[1] >> light.position[2] >>
- light.intensity[0] >> light.intensity[1] >> light.intensity[2];
- light.intensity /= 255.;
- lights.emplace_back(light);
- } else if (type == "S") {
- Vector center;
- double radius;
- stream >> center[0] >> center[1] >> center[2] >> radius;
- Sphere sphere(center, radius);
- SphereObject sphere_object{&materials[cur_material], sphere};
- scene.sphere_objects_.emplace_back(sphere_object);
- } else if (type == "mtllib") {
- materials = ReadMaterials(path.filename());
- } else if (type == "usemtl") {
- stream >> cur_material;
- }
- }
- scene.materials_ = materials;
- file.close();
- return scene;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement