Aabb::Aabb(const glm::vec4& min, const glm::vec4& max) : corners{min, max}, asObb(), asSphere() { calculateObb(); calculateSphere(); }
Aabb& Aabb::operator +=(const Aabb& other) { for (int32_t element = 0; element < 3; element++) { if (other.corners[0][element] < corners[0][element]) { corners[0][element] = other.corners[0][element]; } if (other.corners[1][element] > corners[1][element]) { corners[1][element] = other.corners[1][element]; } } // calculateObb(); calculateSphere(); // return *this; }
Aabb::Aabb(const float* vertices, const uint32_t numberVertices, const uint32_t stride) : corners(), asObb(), asSphere() { glm::vec4 min; glm::vec4 max; if (vertices) { const uint8_t* p = (const uint8_t*)vertices; for (uint32_t i = 0; i < numberVertices; i++) { const float* v = (const float*)(&p[i * stride]); glm::vec4 currentVertex = glm::vec4(v[0], v[1], v[2], v[3]); if (i == 0) { min = currentVertex; max = currentVertex; } else { for (int32_t element = 0; element < 3; element++) { if (currentVertex[element] < min[element]) { min[element] = currentVertex[element]; } else if (currentVertex[element] > max[element]) { max[element] = currentVertex[element]; } } } } } else { min = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); max = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); } // corners[0] = min; corners[1] = max; // calculateObb(); calculateSphere(); }
Aabb::Aabb(const glm::vec3& translate, const glm::vec3& scale) : corners(), asObb(), asSphere() { glm::mat4 transfom = translateMat4(translate.x, translate.y, translate.z) * scaleMat4(scale.x, scale.y, scale.z); for (int32_t i = 0; i < 2; i++) { corners[i] = transfom * cornersUnit[i]; } calculateObb(); calculateSphere(); }
ContainSphere Scene::carregaObjecte(std::string obj) { objects.push_back(Object(obj)); return calculateSphere(); }
kdNode::kdNode(kdTree *tree, const u::vector<int> &tris, size_t recursionDepth) : m_front(nullptr) , m_back(nullptr) , m_sphereRadius(0.0f) { const size_t triangleCount = tris.size(); if (recursionDepth > tree->m_depth) tree->m_depth = recursionDepth; if (recursionDepth > kdTree::kMaxRecursionDepth) return; tree->m_nodeCount++; if (!calculateSphere(tree, tris)) { u::Log::err("[world] => level geometry is too large: collision detection and rendering may not work"); return; } u::vector<int> fx, fy, fz; // front u::vector<int> bx, by, bz; // back u::vector<int> sx, sy, sz; // split u::vector<int> *const frontList[3] = { &fx, &fy, &fz }; u::vector<int> *const backList[3] = { &bx, &by, &bz }; u::vector<int> *const splitList[3] = { &sx, &sy, &sz }; m::plane plane[3]; float ratio[3]; size_t best = recursionDepth % 3; // find a plane which gives a good balanced node for (size_t i = 0; i < 3; i++) { split(tree, tris, m::axis(i), *frontList[i], *backList[i], *splitList[i], plane[i]); const size_t fsize = (*frontList[i]).size(); const size_t bsize = (*backList[i]).size(); if (fsize > bsize) ratio[i] = (float)bsize / (float)fsize; else ratio[i] = (float)fsize / (float)bsize; } float bestRatio = 0.0f; for (size_t i = 0; i < 3; i++) { if (ratio[i] > bestRatio) { best = i; bestRatio = ratio[i]; } } m_splitPlane = plane[best]; // when there isn't many triangles left, create a leaf. In doing so we can // continue to create further subdivisions. if (frontList[best]->size() == 0 || backList[best]->size() == 0 || triangleCount <= kdTree::kMaxTrianglesPerLeaf) { // create subspace with `triangleCount` polygons m_triangles.insert(m_triangles.begin(), tris.begin(), tris.end()); tree->m_leafCount++; return; } // insert the split triangles on both sides of the plane. frontList[best]->insert(frontList[best]->end(), splitList[best]->begin(), splitList[best]->end()); backList[best]->insert(backList[best]->end(), splitList[best]->begin(), splitList[best]->end()); // recurse m_front = new kdNode(tree, *frontList[best], recursionDepth + 1); m_back = new kdNode(tree, *backList[best], recursionDepth + 1); }