added objects
This commit is contained in:
parent
7be006ff05
commit
ed9c300d77
2 changed files with 109 additions and 0 deletions
86
primitive/objmodel.cpp
Normal file
86
primitive/objmodel.cpp
Normal file
|
@ -0,0 +1,86 @@
|
|||
//
|
||||
// Created by arvids on 21.11.22.
|
||||
//
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <sstream>
|
||||
#include "objmodel.h"
|
||||
|
||||
void ObjModel::loadObj(const std::string& 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();
|
||||
}
|
||||
}
|
||||
|
||||
bool ObjModel::intersect(Ray &ray) const {
|
||||
return std::any_of(this->faces.begin(),
|
||||
this->faces.end(),
|
||||
[&ray](auto& face){
|
||||
return face.intersect(ray);
|
||||
});
|
||||
}
|
||||
|
||||
float ObjModel::minimumBounds(int dimension) const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float ObjModel::maximumBounds(int dimension) const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
23
primitive/objmodel.h
Normal file
23
primitive/objmodel.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef CG1_TRACER_OBJMODEL_H
|
||||
#define CG1_TRACER_OBJMODEL_H
|
||||
|
||||
#include "primitive.h"
|
||||
#include "triangle.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class ObjModel : public Primitive {
|
||||
public:
|
||||
explicit ObjModel(const std::shared_ptr<Shader> &shader) : Primitive(shader) {}
|
||||
void loadObj(const std::string& fileName, Vector3d translation, Vector3d upVector);
|
||||
|
||||
bool intersect(Ray &ray) const override;
|
||||
|
||||
[[nodiscard]] float minimumBounds(int dimension) const override;
|
||||
[[nodiscard]] float maximumBounds(int dimension) const override;
|
||||
|
||||
protected:
|
||||
std::vector<Triangle> faces;
|
||||
};
|
||||
|
||||
#endif //CG1_TRACER_OBJMODEL_H
|
Loading…
Reference in a new issue