Some cleanup and comments
This commit is contained in:
parent
27dcc00e68
commit
6801208905
@ -28,7 +28,8 @@ class CollisionSystem : public EntitySystem {
|
|||||||
|
|
||||||
std::map<Vector, Point *> triangle_map;
|
std::map<Vector, Point *> triangle_map;
|
||||||
|
|
||||||
// TODO: Iterate over vertices, add triangles by also iterating over indices
|
// We iterate over the index array and add the resulting triangles to a hash map of
|
||||||
|
// vertices. That way, each vertex holds a list of all triangles it is involved in.
|
||||||
for (int i = 0; i < mesh->vertex_count; i += 3) {
|
for (int i = 0; i < mesh->vertex_count; i += 3) {
|
||||||
// Build vertices from this triangle
|
// Build vertices from this triangle
|
||||||
float v0p0 = vertices[indices[i + 0] * 14 + 0];
|
float v0p0 = vertices[indices[i + 0] * 14 + 0];
|
||||||
@ -90,9 +91,7 @@ class CollisionSystem : public EntitySystem {
|
|||||||
// TODO
|
// TODO
|
||||||
});
|
});
|
||||||
|
|
||||||
std::cout << "Start building kdtree with " << points.size() << " points" << std::endl;
|
|
||||||
kdtree = new KDTree(points);
|
kdtree = new KDTree(points);
|
||||||
std::cout << "Done" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tick(World *pWorld, float deltaTime) override {
|
void tick(World *pWorld, float deltaTime) override {
|
||||||
|
@ -22,6 +22,7 @@ struct Vector {
|
|||||||
return Vector(c[0] - other.c[0], c[1] - other.c[1], c[2] - other.c[2]);
|
return Vector(c[0] - other.c[0], c[1] - other.c[1], c[2] - other.c[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Arbitrary but functional definition of '<', primarily for use in an std::map.
|
||||||
bool operator<(const Vector &other) const {
|
bool operator<(const Vector &other) const {
|
||||||
if ((c[2] < other.c[2])) { return true; }
|
if ((c[2] < other.c[2])) { return true; }
|
||||||
if ((c[2] == other.c[2]) && (c[1] < other.c[1])) { return true; }
|
if ((c[2] == other.c[2]) && (c[1] < other.c[1])) { return true; }
|
||||||
@ -30,6 +31,7 @@ struct Vector {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Component-wise multiplication with a scalar
|
||||||
Vector operator*(float scalar) const {
|
Vector operator*(float scalar) const {
|
||||||
return Vector(c[0] * scalar, c[1] * scalar, c[2] * scalar);
|
return Vector(c[0] * scalar, c[1] * scalar, c[2] * scalar);
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include "geometry.h"
|
#include "geometry.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -9,7 +11,7 @@
|
|||||||
class KDTree {
|
class KDTree {
|
||||||
public:
|
public:
|
||||||
KDTree(std::vector<Point *> points) {
|
KDTree(std::vector<Point *> points) {
|
||||||
std::cout << "Starting to build tree with " << points.size() << " points took ";
|
std::cout << "Building tree with " << points.size() << " points took ";
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
root = build(points, 0);
|
root = build(points, 0);
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
@ -109,13 +111,17 @@ class KDTree {
|
|||||||
|
|
||||||
void intersect_ray_recurse(const Triangle *&nearest_triangle, Vector &result, Ray ray,
|
void intersect_ray_recurse(const Triangle *&nearest_triangle, Vector &result, Ray ray,
|
||||||
Node *node, int depth, float &nearest) {
|
Node *node, int depth, float &nearest) {
|
||||||
// Exit condition: There was no collision
|
// Exit condition: This node was iterated towards, but does not exist
|
||||||
if (node == nullptr) { return; }
|
if (node == nullptr) { return; }
|
||||||
|
|
||||||
// Check for a collision here
|
// Check for a collision here
|
||||||
|
// Iterate over all Triangles which this Point is involved in
|
||||||
for (const Triangle *triangle : node->point->triangles) {
|
for (const Triangle *triangle : node->point->triangles) {
|
||||||
Vector current_result(0, 0, 0);
|
Vector current_result(0, 0, 0);
|
||||||
float current_distance;
|
float current_distance;
|
||||||
|
|
||||||
|
// If we have a collision, and it is closer to the ray origin than the closest previous
|
||||||
|
// collision, remember it
|
||||||
if (ray.intersects_triangle(triangle, current_result, current_distance)) {
|
if (ray.intersects_triangle(triangle, current_result, current_distance)) {
|
||||||
if (current_distance < nearest) {
|
if (current_distance < nearest) {
|
||||||
nearest = current_distance;
|
nearest = current_distance;
|
||||||
@ -125,14 +131,16 @@ class KDTree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is the left or right child node closer to this point?
|
// Is the ray origin within the left or right child node bounding box?
|
||||||
Node *near =
|
Node *near =
|
||||||
ray.origin[node->axis] > node->point->pos[node->axis] ? node->right : node->left;
|
ray.origin[node->axis] > node->point->pos[node->axis] ? node->right : node->left;
|
||||||
Node *far = near == node->right ? node->left : node->right;
|
Node *far = near == node->right ? node->left : node->right;
|
||||||
|
|
||||||
if (ray.direction[node->axis] == 0.0) {
|
if (ray.direction[node->axis] == 0.0) {
|
||||||
|
// The ray is parallel to the splitting axis, so we only need to check within this box.
|
||||||
intersect_ray_recurse(nearest_triangle, result, ray, near, depth + 1, nearest);
|
intersect_ray_recurse(nearest_triangle, result, ray, near, depth + 1, nearest);
|
||||||
} else {
|
} else {
|
||||||
|
// Calculate the distance from the ray origin to the splitting axis
|
||||||
float t =
|
float t =
|
||||||
(node->point->pos[node->axis] - ray.origin[node->axis]) / ray.direction[node->axis];
|
(node->point->pos[node->axis] - ray.origin[node->axis]) / ray.direction[node->axis];
|
||||||
|
|
||||||
|
9
main.cpp
9
main.cpp
@ -204,11 +204,11 @@ int main(int argc, char **argv) {
|
|||||||
Entity *sun = world->create();
|
Entity *sun = world->create();
|
||||||
sun->assign<DirectionalLight>(glm::normalize(glm::vec3(1.0, 1.0, 1.0)));
|
sun->assign<DirectionalLight>(glm::normalize(glm::vec3(1.0, 1.0, 1.0)));
|
||||||
|
|
||||||
Entity *kdtree_vis = world->create();
|
|
||||||
|
|
||||||
// We're done loading geometry -> build the collision structure
|
// We're done loading geometry -> build the collision structure
|
||||||
collision_system->build();
|
collision_system->build();
|
||||||
|
|
||||||
|
// Save the visualized kdtree as another entity
|
||||||
|
Entity *kdtree_vis = world->create();
|
||||||
kdtree_vis->assign<Lines>(collision_system->kdtree->get_lines());
|
kdtree_vis->assign<Lines>(collision_system->kdtree->get_lines());
|
||||||
|
|
||||||
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs");
|
||||||
@ -229,15 +229,16 @@ int main(int argc, char **argv) {
|
|||||||
world->tick(delta);
|
world->tick(delta);
|
||||||
renderSystem->render(world, defaultShader, shadowShader, debugShader, lineShader);
|
renderSystem->render(world, defaultShader, shadowShader, debugShader, lineShader);
|
||||||
|
|
||||||
|
// Animations
|
||||||
/* ring->get<Transform>()->rotate(delta * 100.0, glm::vec3(0.0, 1.0, 0.0));
|
/* ring->get<Transform>()->rotate(delta * 100.0, glm::vec3(0.0, 1.0, 0.0));
|
||||||
sun->get<DirectionalLight>()->direction = glm::normalize(glm::vec3(
|
sun->get<DirectionalLight>()->direction = glm::normalize(glm::vec3(
|
||||||
glm::rotate(glm::mat4(1), (float)elapsed_time * 0.5f, glm::vec3(0.0, 1.0, 0.0))
|
glm::rotate(glm::mat4(1), (float)elapsed_time * 0.5f, glm::vec3(0.0, 1.0, 0.0))
|
||||||
* glm::vec4(1.0, 1.0, 1.0, 0.0))); */
|
* glm::vec4(1.0, 1.0, 1.0, 0.0))); */
|
||||||
|
|
||||||
/* Swap front and back buffers */
|
// Swap front and back buffers
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
/* Poll for and process events */
|
// Poll for and process events
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user