reorganizing mesh data
This commit is contained in:
+3
-54
@@ -9,64 +9,13 @@ void Model::transform(glm::mat4 matrix)
|
||||
|
||||
boundingBox.transform(matrix);
|
||||
|
||||
for (int i = 0; i < indices.size(); i+=3)
|
||||
for (int i = 0; i < indices.size(); i++)
|
||||
{
|
||||
auto e0 = positions[indices[i + 1]] - positions[indices[i + 0]];
|
||||
auto e1 = positions[indices[i + 2]] - positions[indices[i + 0]];
|
||||
auto e0 = positions[indices[i].y] - positions[indices[i].x];
|
||||
auto e1 = positions[indices[i].z] - positions[indices[i].x];
|
||||
edges.push_back(e0);
|
||||
edges.push_back(e1);
|
||||
|
||||
faceNormals.push_back(glm::cross(e0, e1));
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<IntersectionInfo> Model::intersect(const Ray ray) const
|
||||
{
|
||||
std::optional<IntersectionInfo> intersection = {};
|
||||
float distance = 0;
|
||||
|
||||
for(size_t posIndex=0, edgeIndex=0, normalIndex=0; posIndex <indices.size(); posIndex+=3, edgeIndex+=2, normalIndex++)
|
||||
{
|
||||
const auto p0 = positions[indices[posIndex]];
|
||||
const auto p1 = positions[indices[posIndex+1]];
|
||||
const auto p2 = positions[indices[posIndex+2]];
|
||||
|
||||
const auto e0 = edges[edgeIndex];
|
||||
const auto e1 = edges[edgeIndex+1];
|
||||
|
||||
const auto n = faceNormals[normalIndex];
|
||||
|
||||
const auto s = ray.origin - p0;
|
||||
const auto s1 = glm::cross(ray.direction, e1);
|
||||
const auto s2 = glm::cross(s, e0);
|
||||
|
||||
const float fraction = 1.0f / glm::dot(s1, e0);
|
||||
const auto resultVector = glm::vec3(glm::dot(s2, e1), glm::dot(s1, s), glm::dot(s2, ray.direction)) * fraction;
|
||||
|
||||
const float b3 = 1.0f - resultVector.y - resultVector.z;
|
||||
|
||||
if(b3 < 0 || b3 > 1)
|
||||
continue;
|
||||
if(resultVector.y < 0 || resultVector.y > 1)
|
||||
continue;
|
||||
if(resultVector.z < 0 || resultVector.z > 1)
|
||||
continue;
|
||||
|
||||
if(resultVector.x < 1e-6)
|
||||
continue;
|
||||
|
||||
if(!intersection.has_value() || resultVector.x < distance)
|
||||
{
|
||||
intersection = IntersectionInfo {
|
||||
.position = ray.origin + ray.direction * resultVector.x,
|
||||
.normal = n,
|
||||
.albedo = glm::vec3(0.7f, 0.7f, 0.7f),
|
||||
.emissive = glm::vec3(0.0f, 0.0f, 0.0f)
|
||||
};
|
||||
distance = resultVector.x;
|
||||
}
|
||||
}
|
||||
|
||||
return intersection;
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -21,10 +21,9 @@ class Model
|
||||
public:
|
||||
AABB boundingBox;
|
||||
std::vector<glm::vec3> positions;
|
||||
std::vector<uint32_t> indices;
|
||||
std::vector<glm::uvec3> indices;
|
||||
std::vector<glm::vec3> edges;
|
||||
std::vector<glm::vec3> faceNormals;
|
||||
void transform(glm::mat4 matrix);
|
||||
std::optional<IntersectionInfo> intersect(Ray ray) const;
|
||||
};
|
||||
DECLARE_REF(Model)
|
||||
+21
-24
@@ -7,30 +7,27 @@
|
||||
|
||||
std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
|
||||
{
|
||||
Assimp::Importer importer;
|
||||
const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate);
|
||||
std::vector<PModel> result;
|
||||
for (int m = 0; m < scene->mNumMeshes; ++m)
|
||||
Assimp::Importer importer;
|
||||
const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate);
|
||||
std::vector<PModel> result;
|
||||
for (int m = 0; m < scene->mNumMeshes; ++m)
|
||||
{
|
||||
PModel model = std::make_unique<Model>();
|
||||
const aiMesh* mesh = scene->mMeshes[m];
|
||||
AABB aabb;
|
||||
for (int v = 0; v < mesh->mNumVertices; ++v)
|
||||
{
|
||||
PModel model = std::make_unique<Model>();
|
||||
const aiMesh* mesh = scene->mMeshes[m];
|
||||
AABB aabb;
|
||||
for (int v = 0; v < mesh->mNumVertices; ++v)
|
||||
{
|
||||
auto aiVert = mesh->mVertices[v];
|
||||
model->positions.push_back(glm::vec3(aiVert.x, aiVert.y, aiVert.z));
|
||||
aabb.adjust(model->positions.back());
|
||||
}
|
||||
for (int i = 0; i < mesh->mNumFaces; ++i)
|
||||
{
|
||||
auto face = mesh->mFaces[i];
|
||||
model->indices.push_back(face.mIndices[0]);
|
||||
model->indices.push_back(face.mIndices[1]);
|
||||
model->indices.push_back(face.mIndices[2]);
|
||||
|
||||
}
|
||||
model->boundingBox = aabb;
|
||||
result.push_back(std::move(model));
|
||||
auto aiVert = mesh->mVertices[v];
|
||||
model->positions.push_back(glm::vec3(aiVert.x, aiVert.y, aiVert.z));
|
||||
aabb.adjust(model->positions.back());
|
||||
}
|
||||
return result;
|
||||
for (int i = 0; i < mesh->mNumFaces; ++i)
|
||||
{
|
||||
auto face = mesh->mFaces[i];
|
||||
model->indices.push_back(glm::uvec3(face.mIndices[0], face.mIndices[1], face.mIndices[2]));
|
||||
}
|
||||
model->boundingBox = aabb;
|
||||
result.push_back(std::move(model));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user