Moved code to correct files
This commit is contained in:
parent
3b2bffb3da
commit
0a905edf0c
3 changed files with 84 additions and 94 deletions
|
@ -1,70 +1,10 @@
|
|||
//
|
||||
// Created by arvids on 21.11.22.
|
||||
//
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "objmodel.h"
|
||||
#include "shader/flatshader.h"
|
||||
#include "../scene/scene.h"
|
||||
|
||||
void ObjModel::loadObj(const std::string &fileName, Vector3d scale, Vector3d translation) {
|
||||
void ObjModel::loadObj(const char* fileName, Vector3d scale, Vector3d translation) {
|
||||
|
||||
std::vector<Vector3d> vertices;
|
||||
std::vector<Vector3d> vertex_normals;
|
||||
std::ifstream file;
|
||||
file.open(fileName.c_str());
|
||||
if (file.is_open()) {
|
||||
while (!file.eof()) {
|
||||
std::string key;
|
||||
file >> key;
|
||||
if (key == "v") {
|
||||
float x, y, z;
|
||||
file >> x >> y >> z;
|
||||
vertices.emplace_back(x, y, z);
|
||||
} else if (key == "vn") {
|
||||
float x, y, z;
|
||||
file >> x >> y >> z;
|
||||
vertex_normals.emplace_back(x, y, z);
|
||||
} else if (key == "f") {
|
||||
int x, y, z,
|
||||
xn, yn, zn = 0;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
std::stringstream tokens(token);
|
||||
tokens >> x;
|
||||
}
|
||||
file.ignore(1);
|
||||
file >> xn;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
std::stringstream tokens(token);
|
||||
tokens >> y;
|
||||
}
|
||||
file.ignore(1);
|
||||
file >> yn;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
std::stringstream tokens(token);
|
||||
tokens >> z;
|
||||
}
|
||||
file.ignore(1);
|
||||
file >> zn;
|
||||
|
||||
this->faces.emplace_back(vertices[x - 1] * scale + translation, vertices[y - 1] * scale + translation,
|
||||
vertices[z - 1] * scale + translation,
|
||||
vertex_normals[xn - 1], vertex_normals[yn - 1], vertex_normals[zn - 1],
|
||||
this->shader());
|
||||
} else if (key == "#") {
|
||||
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
} else {
|
||||
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
this->faces = (Scene::loadObj(fileName, scale, translation, this->shader()));
|
||||
|
||||
Vector3d minBounds;
|
||||
Vector3d maxBounds;
|
||||
|
@ -73,27 +13,23 @@ void ObjModel::loadObj(const std::string &fileName, Vector3d scale, Vector3d tra
|
|||
return std::min_element(this->faces.begin(),
|
||||
this->faces.end(),
|
||||
[&dimension](auto &face1, auto &face2) {
|
||||
return face1.minimumBounds(dimension) < face2.minimumBounds(dimension);
|
||||
})->minimumBounds(dimension);
|
||||
return face1->minimumBounds(dimension) < face2->minimumBounds(dimension);
|
||||
})->get()->minimumBounds(dimension);
|
||||
};
|
||||
minBounds.x = minCalc(0);
|
||||
minBounds.y = minCalc(1);
|
||||
minBounds.z = minCalc(2);
|
||||
minBounds = {minCalc(0), minCalc(1), minCalc(2)};
|
||||
|
||||
auto maxCalc = [this](int dimension) {
|
||||
return std::max_element(this->faces.begin(),
|
||||
this->faces.end(),
|
||||
[&dimension](auto &face1, auto &face2) {
|
||||
return face1.maximumBounds(dimension) < face2.maximumBounds(dimension);
|
||||
})->maximumBounds(dimension);
|
||||
return face1->maximumBounds(dimension) < face2->maximumBounds(dimension);
|
||||
})->get()->maximumBounds(dimension);
|
||||
};
|
||||
maxBounds.x = maxCalc(0);
|
||||
maxBounds.y = maxCalc(1);
|
||||
maxBounds.z = maxCalc(2);
|
||||
maxBounds = {maxCalc(0), maxCalc(1), maxCalc(2)};
|
||||
|
||||
boundingBox.setCenter(minBounds + (maxBounds - minBounds) / 2);
|
||||
boundingBox.setSize(maxBounds - minBounds);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ObjModel::intersect(Ray &ray) const {
|
||||
|
@ -104,7 +40,7 @@ bool ObjModel::intersect(Ray &ray) const {
|
|||
return std::count_if(this->faces.begin(),
|
||||
this->faces.end(),
|
||||
[&ray](auto &face) {
|
||||
return face.intersect(ray);
|
||||
return face->intersect(ray);
|
||||
}) > 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@
|
|||
#define CG1_TRACER_OBJMODEL_H
|
||||
|
||||
#include "primitive.h"
|
||||
#include "triangle.h"
|
||||
#include "box.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class ObjModel : public Primitive {
|
||||
public:
|
||||
explicit ObjModel(const std::shared_ptr<Shader> &shader);
|
||||
void loadObj(const std::string& fileName, Vector3d translation, Vector3d upVector);
|
||||
void loadObj(const char* fileName, Vector3d scale, Vector3d translation);
|
||||
|
||||
bool intersect(Ray &ray) const override;
|
||||
|
||||
|
@ -18,7 +16,7 @@ public:
|
|||
[[nodiscard]] float maximumBounds(int dimension) const override;
|
||||
|
||||
protected:
|
||||
std::vector<Triangle> faces;
|
||||
std::vector<std::shared_ptr<Primitive>> faces;
|
||||
Box boundingBox;
|
||||
|
||||
|
||||
|
|
|
@ -42,8 +42,64 @@ std::vector<std::shared_ptr<Primitive>> Scene::loadObj(char const *fileName, Vec
|
|||
std::vector<std::shared_ptr<Primitive>> faces;
|
||||
std::vector<std::array<int, 3>> indices;
|
||||
|
||||
// IMPLEMENT ME
|
||||
std::vector<Vector3d> vertices;
|
||||
std::vector<Vector3d> vertex_normals;
|
||||
std::ifstream file;
|
||||
file.open(fileName);
|
||||
if (file.is_open()) {
|
||||
while (!file.eof()) {
|
||||
std::string key;
|
||||
file >> key;
|
||||
if (key == "v") {
|
||||
float x, y, z;
|
||||
file >> x >> y >> z;
|
||||
vertices.emplace_back(x, y, z);
|
||||
} else if (key == "vn") {
|
||||
float x, y, z;
|
||||
file >> x >> y >> z;
|
||||
vertex_normals.emplace_back(x, y, z);
|
||||
} else if (key == "f") {
|
||||
int x, y, z,
|
||||
xn, yn, zn = 0;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
std::stringstream tokens(token);
|
||||
tokens >> x;
|
||||
}
|
||||
file.ignore(1);
|
||||
file >> xn;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
std::stringstream tokens(token);
|
||||
tokens >> y;
|
||||
}
|
||||
file.ignore(1);
|
||||
file >> yn;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
std::stringstream tokens(token);
|
||||
tokens >> z;
|
||||
}
|
||||
file.ignore(1);
|
||||
file >> zn;
|
||||
|
||||
faces.push_back(std::make_shared<Triangle>(vertices[x - 1] * scale + translation,
|
||||
vertices[y - 1] * scale + translation,
|
||||
vertices[z - 1] * scale + translation,
|
||||
vertex_normals[xn - 1], vertex_normals[yn - 1],
|
||||
vertex_normals[zn - 1],
|
||||
shader));
|
||||
} else if (key == "#") {
|
||||
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
} else {
|
||||
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
return faces;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue