Added bounds checking
This commit is contained in:
parent
01c4b95c56
commit
3b2bffb3da
2 changed files with 52 additions and 9 deletions
|
@ -6,8 +6,9 @@
|
|||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "objmodel.h"
|
||||
#include "shader/flatshader.h"
|
||||
|
||||
void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d translation) {
|
||||
void ObjModel::loadObj(const std::string &fileName, Vector3d scale, Vector3d translation) {
|
||||
|
||||
std::vector<Vector3d> vertices;
|
||||
std::vector<Vector3d> vertex_normals;
|
||||
|
@ -27,7 +28,7 @@ void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d tra
|
|||
vertex_normals.emplace_back(x, y, z);
|
||||
} else if (key == "f") {
|
||||
int x, y, z,
|
||||
xn, yn, zn = 0;
|
||||
xn, yn, zn = 0;
|
||||
{
|
||||
std::string token;
|
||||
std::getline(file, token, '/');
|
||||
|
@ -53,7 +54,8 @@ void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d tra
|
|||
file.ignore(1);
|
||||
file >> zn;
|
||||
|
||||
this->faces.emplace_back(vertices[x - 1] * scale + translation, vertices[y - 1] * scale + translation, vertices[z - 1] * scale + translation,
|
||||
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 == "#") {
|
||||
|
@ -63,23 +65,60 @@ void ObjModel::loadObj(const std::string& fileName, Vector3d scale, Vector3d tra
|
|||
}
|
||||
}
|
||||
file.close();
|
||||
|
||||
Vector3d minBounds;
|
||||
Vector3d maxBounds;
|
||||
|
||||
auto minCalc = [this](int dimension) {
|
||||
return std::min_element(this->faces.begin(),
|
||||
this->faces.end(),
|
||||
[&dimension](auto &face1, auto &face2) {
|
||||
return face1.minimumBounds(dimension) < face2.minimumBounds(dimension);
|
||||
})->minimumBounds(dimension);
|
||||
};
|
||||
minBounds.x = minCalc(0);
|
||||
minBounds.y = minCalc(1);
|
||||
minBounds.z = 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);
|
||||
};
|
||||
maxBounds.x = maxCalc(0);
|
||||
maxBounds.y = maxCalc(1);
|
||||
maxBounds.z = maxCalc(2);
|
||||
|
||||
boundingBox.setCenter(minBounds + (maxBounds - minBounds) / 2);
|
||||
boundingBox.setSize(maxBounds - minBounds);
|
||||
}
|
||||
}
|
||||
|
||||
bool ObjModel::intersect(Ray &ray) const {
|
||||
auto testRay = Ray(ray.origin, ray.direction);
|
||||
if (!boundingBox.intersect(testRay)) {
|
||||
return false;
|
||||
}
|
||||
return std::count_if(this->faces.begin(),
|
||||
this->faces.end(),
|
||||
[&ray](auto& face){
|
||||
return face.intersect(ray);
|
||||
}) > 0;
|
||||
[&ray](auto &face) {
|
||||
return face.intersect(ray);
|
||||
}) > 0;
|
||||
}
|
||||
|
||||
float ObjModel::minimumBounds(int dimension) const {
|
||||
return 0;
|
||||
return this->boundingBox.minimumBounds(dimension);
|
||||
}
|
||||
|
||||
float ObjModel::maximumBounds(int dimension) const {
|
||||
return 0;
|
||||
return this->boundingBox.maximumBounds(dimension);
|
||||
}
|
||||
|
||||
ObjModel::ObjModel(const std::shared_ptr<Shader> &shader) :
|
||||
Primitive(shader),
|
||||
boundingBox(Box(std::make_shared<FlatShader>(Color(0, 0, 0)))) {
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -3,12 +3,13 @@
|
|||
|
||||
#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) : Primitive(shader) {}
|
||||
explicit ObjModel(const std::shared_ptr<Shader> &shader);
|
||||
void loadObj(const std::string& fileName, Vector3d translation, Vector3d upVector);
|
||||
|
||||
bool intersect(Ray &ray) const override;
|
||||
|
@ -18,6 +19,9 @@ public:
|
|||
|
||||
protected:
|
||||
std::vector<Triangle> faces;
|
||||
Box boundingBox;
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif //CG1_TRACER_OBJMODEL_H
|
||||
|
|
Loading…
Reference in a new issue