Пример #1
0
void ShapeInfoTests::testBoxShape() {
    ShapeInfo info;
    glm::vec3 halfExtents(1.23f, 4.56f, 7.89f);
    info.setBox(halfExtents);
    DoubleHashKey key = info.getHash();

    btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info);
    if (!shape) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: NULL Box shape" << std::endl;
    }

    ShapeInfo otherInfo = info;
    DoubleHashKey otherKey = otherInfo.getHash();
    if (key.getHash() != otherKey.getHash()) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected Box shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl;
    }

    if (key.getHash2() != otherKey.getHash2()) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected Box shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl;
    }

    delete shape;
}
Пример #2
0
void ShapeManagerTests::addManyShapes() {
    ShapeManager shapeManager;

    int numSizes = 100;
    float startSize = 1.0f;
    float endSize = 99.0f;
    float deltaSize = (endSize - startSize) / (float)numSizes;
    ShapeInfo info;
    for (int i = 0; i < numSizes; ++i) {
        float s = startSize + (float)i * deltaSize;
        glm::vec3 scale(s, 1.23f + s, s - 0.573f);
        info.setBox(0.5f * scale);
        btCollisionShape* shape = shapeManager.getShape(info);
        if (!shape) {
            std::cout << __FILE__ << ":" << __LINE__
                << " ERROR: i = " << i << " null box shape for scale = " << scale << std::endl;
        }
        float radius = 0.5f * s;
        info.setSphere(radius);
        shape = shapeManager.getShape(info);
        if (!shape) {
            std::cout << __FILE__ << ":" << __LINE__
                << " ERROR: i = " << i << " null sphere shape for radius = " << radius << std::endl;
        }
    }
    int numShapes = shapeManager.getNumShapes();
    if (numShapes != 2 * numSizes) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected numShapes = " << numSizes << " but found numShapes = " << numShapes << std::endl;
    }
}
Пример #3
0
void CollisionPick::computeShapeInfoDimensionsOnly(const CollisionRegion& pick, ShapeInfo& shapeInfo, QSharedPointer<GeometryResource> resource) {
    ShapeType type = shapeInfo.getType();
    glm::vec3 dimensions = pick.transform.getScale();
    QString modelURL = (resource ? resource->getURL().toString() : "");
    if (type == SHAPE_TYPE_COMPOUND) {
        shapeInfo.setParams(type, dimensions, modelURL);
    } else if (type >= SHAPE_TYPE_SIMPLE_HULL && type <= SHAPE_TYPE_STATIC_MESH) {
        shapeInfo.setParams(type, 0.5f * dimensions, modelURL);
    } else {
        shapeInfo.setParams(type, 0.5f * dimensions, modelURL);
    }
}
Пример #4
0
void ShapeManagerTests::addSphereShape() {
    ShapeInfo info;
    float radius = 1.23f;
    info.setSphere(radius);

    ShapeManager shapeManager;
    btCollisionShape* shape = shapeManager.getShape(info);

    ShapeInfo otherInfo;
    ShapeInfoUtil::collectInfoFromShape(shape, otherInfo);

    btCollisionShape* otherShape = shapeManager.getShape(otherInfo);
    if (shape != otherShape) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: Sphere ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
    }
}
Пример #5
0
void ShapeManagerTests::addBoxShape() {
    ShapeInfo info;
    glm::vec3 halfExtents(1.23f, 4.56f, 7.89f);
    info.setBox(halfExtents);

    ShapeManager shapeManager;
    btCollisionShape* shape = shapeManager.getShape(info);

    ShapeInfo otherInfo;
    ShapeInfoUtil::collectInfoFromShape(shape, otherInfo);

    btCollisionShape* otherShape = shapeManager.getShape(otherInfo);
    if (shape != otherShape) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: Box ShapeInfo --> shape --> ShapeInfo --> shape did not work" << std::endl;
    }
}
Пример #6
0
void ShapeInfoTests::testSphereShape() {
    ShapeInfo info;
    float radius = 1.23f;
    info.setSphere(radius);
    DoubleHashKey key = info.getHash();

    btCollisionShape* shape = ShapeInfoUtil::createShapeFromInfo(info);

    ShapeInfo otherInfo = info;
    DoubleHashKey otherKey = otherInfo.getHash();
    if (key.getHash() != otherKey.getHash()) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected Sphere shape hash = " << key.getHash() << " but found hash = " << otherKey.getHash() << std::endl;
    }
    if (key.getHash2() != otherKey.getHash2()) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected Sphere shape hash2 = " << key.getHash2() << " but found hash2 = " << otherKey.getHash2() << std::endl;
    }

    delete shape;
}
Пример #7
0
void shapeInfoCalculator(const ShapeEntityItem * const shapeEntity, ShapeInfo &shapeInfo) {

    if (shapeEntity == nullptr) {

        //--EARLY EXIT--
        return;
    }

    ShapeInfo::PointCollection pointCollection;
    ShapeInfo::PointList points;
    pointCollection.push_back(points);

    GeometryCache::computeSimpleHullPointListForShape((int)shapeEntity->getShape(), shapeEntity->getScaledDimensions(), pointCollection.back());
    shapeInfo.setPointCollection(pointCollection);
}
Пример #8
0
void SphereEntityItem::computeShapeInfo(ShapeInfo& info) const {
    glm::vec3 halfExtents = 0.5f * getDimensionsInMeters();
    // TODO: support ellipsoid shapes
    info.setSphere(halfExtents.x);
}
Пример #9
0
void ShapeInfoTests::testHashFunctions() {
    int maxTests = 10000000;
    ShapeInfo info;
    btHashMap<btHashInt, uint32_t> hashes;

    uint32_t bits[32];
    uint32_t masks[32];
    for (int i = 0; i < 32; ++i) {
        bits[i] = 0;
        masks[i] = 1U << i;
    }

    float deltaLength = 0.002f;
    float endLength = 100.0f;
    int numSteps = (int)(endLength / deltaLength);

    int testCount = 0;
    int numCollisions = 0;
    btClock timer;
    for (int x = 1; x < numSteps && testCount < maxTests; ++x) {
        float radiusX = (float)x * deltaLength;
        // test sphere
        info.setSphere(radiusX);
        ++testCount;
        DoubleHashKey key = info.getHash();
        uint32_t* hashPtr = hashes.find(key.getHash());
        if (hashPtr && *hashPtr == key.getHash2()) {
            std::cout << testCount << "  hash collision radiusX = " << radiusX 
                << "  h1 = 0x" << std::hex << key.getHash()
                << "  h2 = 0x" << std::hex << key.getHash2()
                << std::endl;
            ++numCollisions;
            assert(false);
        } else {
            hashes.insert(key.getHash(), key.getHash2());
        }
        for (int k = 0; k < 32; ++k) {
            if (masks[k] & key.getHash2()) {
                ++bits[k];
            }
        }

        for (int y = 1; y < numSteps && testCount < maxTests; ++y) {
            float radiusY = (float)y * deltaLength;
            /* TODO: reimplement Cylinder and Capsule shapes
            // test cylinder and capsule
            int types[] = { CYLINDER_SHAPE_PROXYTYPE, CAPSULE_SHAPE_PROXYTYPE };
            for (int i = 0; i < 2; ++i) {
                switch(types[i]) {
                    case CYLINDER_SHAPE_PROXYTYPE: {
                        info.setCylinder(radiusX, radiusY);
                        break;
                    }
                    case CAPSULE_SHAPE_PROXYTYPE: {
                        info.setCapsuleY(radiusX, radiusY);
                        break;
                    }
                }

                ++testCount;
                key = info.getHash();
                hashPtr = hashes.find(key.getHash());
                if (hashPtr && *hashPtr == key.getHash2()) {
                    std::cout << testCount << "  hash collision radiusX = " << radiusX << "  radiusY = " << radiusY 
                        << "  h1 = 0x" << std::hex << key.getHash()
                        << "  h2 = 0x" << std::hex << key.getHash2()
                        << std::endl;
                    ++numCollisions;
                    assert(false);
                } else {
                    hashes.insert(key.getHash(), key.getHash2());
                }
                for (int k = 0; k < 32; ++k) {
                    if (masks[k] & key.getHash2()) {
                        ++bits[k];
                    }
                }
            }
            */

            for (int z = 1; z < numSteps && testCount < maxTests; ++z) {
                float radiusZ = (float)z * deltaLength;
                // test box
                info.setBox(glm::vec3(radiusX, radiusY, radiusZ));
                ++testCount;
                DoubleHashKey key = info.getHash();
                hashPtr = hashes.find(key.getHash());
                if (hashPtr && *hashPtr == key.getHash2()) {
                    std::cout << testCount << "  hash collision radiusX = " << radiusX 
                        << "  radiusY = " << radiusY << "  radiusZ = " << radiusZ 
                        << "  h1 = 0x" << std::hex << key.getHash()
                        << "  h2 = 0x" << std::hex << key.getHash2()
                        << std::endl;
                    ++numCollisions;
                    assert(false);
                } else {
                    hashes.insert(key.getHash(), key.getHash2());
                }
                for (int k = 0; k < 32; ++k) {
                    if (masks[k] & key.getHash2()) {
                        ++bits[k];
                    }
                }
            }
        }
    }
    uint64_t msec = timer.getTimeMilliseconds();
    std::cout << msec << " msec with " << numCollisions << " collisions out of " << testCount << " hashes" << std::endl;

    // print out distribution of bits
    for (int i = 0; i < 32; ++i) {
        std::cout << "bit 0x" << std::hex << masks[i] << std::dec << " = " << bits[i] << std::endl;
    }
}
Пример #10
0
void BoxEntityItem::computeShapeInfo(ShapeInfo& info) const {
    glm::vec3 halfExtents = 0.5f * getDimensionsInMeters();
    info.setBox(halfExtents);
}
Пример #11
0
void CollisionPick::computeShapeInfo(const CollisionRegion& pick, ShapeInfo& shapeInfo, QSharedPointer<GeometryResource> resource) {
    // This code was copied and modified from RenderableModelEntityItem::computeShapeInfo
    // TODO: Move to some shared code area (in entities-renderer? model-networking?)
    // after we verify this is working and do a diff comparison with RenderableModelEntityItem::computeShapeInfo
    // to consolidate the code.
    // We may also want to make computeShapeInfo always abstract away from the gpu model mesh, like it does here.
    const uint32_t TRIANGLE_STRIDE = 3;
    const uint32_t QUAD_STRIDE = 4;

    ShapeType type = shapeInfo.getType();
    glm::vec3 dimensions = pick.transform.getScale();
    if (type == SHAPE_TYPE_COMPOUND) {
        // should never fall in here when collision model not fully loaded
        // TODO: assert that all geometries exist and are loaded
        //assert(_model && _model->isLoaded() && _compoundShapeResource && _compoundShapeResource->isLoaded());
        const HFMModel& collisionModel = resource->getHFMModel();

        ShapeInfo::PointCollection& pointCollection = shapeInfo.getPointCollection();
        pointCollection.clear();
        uint32_t i = 0;

        // the way OBJ files get read, each section under a "g" line is its own meshPart.  We only expect
        // to find one actual "mesh" (with one or more meshParts in it), but we loop over the meshes, just in case.
        foreach (const HFMMesh& mesh, collisionModel.meshes) {
            // each meshPart is a convex hull
            foreach (const HFMMeshPart &meshPart, mesh.parts) {
                pointCollection.push_back(QVector<glm::vec3>());
                ShapeInfo::PointList& pointsInPart = pointCollection[i];

                // run through all the triangles and (uniquely) add each point to the hull
                uint32_t numIndices = (uint32_t)meshPart.triangleIndices.size();
                // TODO: assert rather than workaround after we start sanitizing HFMMesh higher up
                //assert(numIndices % TRIANGLE_STRIDE == 0);
                numIndices -= numIndices % TRIANGLE_STRIDE; // WORKAROUND lack of sanity checking in FBXSerializer

                for (uint32_t j = 0; j < numIndices; j += TRIANGLE_STRIDE) {
                    glm::vec3 p0 = mesh.vertices[meshPart.triangleIndices[j]];
                    glm::vec3 p1 = mesh.vertices[meshPart.triangleIndices[j + 1]];
                    glm::vec3 p2 = mesh.vertices[meshPart.triangleIndices[j + 2]];
                    if (!pointsInPart.contains(p0)) {
                        pointsInPart << p0;
                    }
                    if (!pointsInPart.contains(p1)) {
                        pointsInPart << p1;
                    }
                    if (!pointsInPart.contains(p2)) {
                        pointsInPart << p2;
                    }
                }

                // run through all the quads and (uniquely) add each point to the hull
                numIndices = (uint32_t)meshPart.quadIndices.size();
                // TODO: assert rather than workaround after we start sanitizing HFMMesh higher up
                //assert(numIndices % QUAD_STRIDE == 0);
                numIndices -= numIndices % QUAD_STRIDE; // WORKAROUND lack of sanity checking in FBXSerializer

                for (uint32_t j = 0; j < numIndices; j += QUAD_STRIDE) {
                    glm::vec3 p0 = mesh.vertices[meshPart.quadIndices[j]];
                    glm::vec3 p1 = mesh.vertices[meshPart.quadIndices[j + 1]];
                    glm::vec3 p2 = mesh.vertices[meshPart.quadIndices[j + 2]];
                    glm::vec3 p3 = mesh.vertices[meshPart.quadIndices[j + 3]];
                    if (!pointsInPart.contains(p0)) {
                        pointsInPart << p0;
                    }
                    if (!pointsInPart.contains(p1)) {
                        pointsInPart << p1;
                    }
                    if (!pointsInPart.contains(p2)) {
                        pointsInPart << p2;
                    }
                    if (!pointsInPart.contains(p3)) {
                        pointsInPart << p3;
                    }
                }

                if (pointsInPart.size() == 0) {
                    qCDebug(scriptengine) << "Warning -- meshPart has no faces";
                    pointCollection.pop_back();
                    continue;
                }
                ++i;
            }
        }

        // We expect that the collision model will have the same units and will be displaced
        // from its origin in the same way the visual model is.  The visual model has
        // been centered and probably scaled.  We take the scaling and offset which were applied
        // to the visual model and apply them to the collision model (without regard for the
        // collision model's extents).

        glm::vec3 scaleToFit = dimensions / resource->getHFMModel().getUnscaledMeshExtents().size();
        // multiply each point by scale
        for (int32_t i = 0; i < pointCollection.size(); i++) {
            for (int32_t j = 0; j < pointCollection[i].size(); j++) {
                // back compensate for registration so we can apply that offset to the shapeInfo later
                pointCollection[i][j] = scaleToFit * pointCollection[i][j];
            }
        }
        shapeInfo.setParams(type, dimensions, resource->getURL().toString());
    } else if (type >= SHAPE_TYPE_SIMPLE_HULL && type <= SHAPE_TYPE_STATIC_MESH) {
Пример #12
0
void ShapeManagerTests::testShapeAccounting() {
    ShapeManager shapeManager;
    ShapeInfo info;
    info.setBox(glm::vec3(1.0f, 1.0f, 1.0f));
    
    // NOTE: ShapeManager returns -1 as refcount when the shape is unknown, 
    // which is distinct from "known but with zero references"
    int numReferences = shapeManager.getNumReferences(info);
    if (numReferences != -1) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected ignorant ShapeManager after initialization" << std::endl;
    }

    // create one shape and verify we get a valid pointer
    btCollisionShape* shape = shapeManager.getShape(info);
    if (!shape) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected shape creation for default parameters" << std::endl;
    }

    // verify number of shapes
    if (shapeManager.getNumShapes() != 1) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected one shape" << std::endl;
    }

    // reference the shape again and verify that we get the same pointer
    btCollisionShape* otherShape = shapeManager.getShape(info);
    if (otherShape != shape) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected shape* " << (void*)(shape) 
            << " but found shape* " << (void*)(otherShape) << std::endl;
    }

    // verify number of references
    numReferences = shapeManager.getNumReferences(info);
    int expectedNumReferences = 2;
    if (numReferences != expectedNumReferences) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected " << expectedNumReferences 
            << " references but found " << numReferences << std::endl;
    }

    // release all references
    bool released = shapeManager.releaseShape(info);
    numReferences--;
    while (numReferences > 0) {
        released = shapeManager.releaseShape(info) && released;
        numReferences--;
    }
    if (!released) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected shape released" << std::endl;
    }

    // verify shape still exists (not yet garbage collected)
    if (shapeManager.getNumShapes() != 1) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected one shape after release but before garbage collection" << std::endl;
    }

    // verify shape's refcount is zero
    numReferences = shapeManager.getNumReferences(info);
    if (numReferences != 0) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected refcount = 0 for shape but found refcount = " << numReferences << std::endl;
    }

    // reference the shape again and verify refcount is updated
    otherShape = shapeManager.getShape(info);
    numReferences = shapeManager.getNumReferences(info);
    if (numReferences != 1) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected refcount = 1 for shape but found refcount = " << numReferences << std::endl;
    }

    // verify that shape is not collected as garbage
    shapeManager.collectGarbage();
    if (shapeManager.getNumShapes() != 1) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected one shape after release" << std::endl;
    }
    numReferences = shapeManager.getNumReferences(info);
    if (numReferences != 1) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected refcount = 1 for shape but found refcount = " << numReferences << std::endl;
    }

    // release reference and verify that it is collected as garbage
    released = shapeManager.releaseShape(info);
    shapeManager.collectGarbage();
    if (shapeManager.getNumShapes() != 0) {
        std::cout << __FILE__ << ":" << __LINE__ << " ERROR: expected zero shapes after release" << std::endl;
    }
    numReferences = shapeManager.getNumReferences(info);
    if (numReferences != -1) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected ignorant ShapeManager after garbage collection" << std::endl;
    }

    // add the shape again and verify that it gets added again
    otherShape = shapeManager.getShape(info);
    numReferences = shapeManager.getNumReferences(info);
    if (numReferences != 1) {
        std::cout << __FILE__ << ":" << __LINE__
            << " ERROR: expected refcount = 1 for shape but found refcount = " << numReferences << std::endl;
    }
}
Пример #13
0
void RenderableModelEntityItem::computeShapeInfo(ShapeInfo& info) {
    ShapeType type = getShapeType();
    if (type != SHAPE_TYPE_COMPOUND) {
        ModelEntityItem::computeShapeInfo(info);
        info.setParams(type, 0.5f * getDimensions());
    } else {
        const QSharedPointer<NetworkGeometry> collisionNetworkGeometry = _model->getCollisionGeometry();

        // should never fall in here when collision model not fully loaded
        // hence we assert collisionNetworkGeometry is not NULL
        assert(collisionNetworkGeometry);

        const FBXGeometry& collisionGeometry = collisionNetworkGeometry->getFBXGeometry();
        const QSharedPointer<NetworkGeometry> renderNetworkGeometry = _model->getGeometry();
        const FBXGeometry& renderGeometry = renderNetworkGeometry->getFBXGeometry();

        _points.clear();
        unsigned int i = 0;

        // the way OBJ files get read, each section under a "g" line is its own meshPart.  We only expect
        // to find one actual "mesh" (with one or more meshParts in it), but we loop over the meshes, just in case.
        foreach (const FBXMesh& mesh, collisionGeometry.meshes) {
            // each meshPart is a convex hull
            foreach (const FBXMeshPart &meshPart, mesh.parts) {
                QVector<glm::vec3> pointsInPart;

                // run through all the triangles and (uniquely) add each point to the hull
                unsigned int triangleCount = meshPart.triangleIndices.size() / 3;
                for (unsigned int j = 0; j < triangleCount; j++) {
                    unsigned int p0Index = meshPart.triangleIndices[j*3];
                    unsigned int p1Index = meshPart.triangleIndices[j*3+1];
                    unsigned int p2Index = meshPart.triangleIndices[j*3+2];
                    glm::vec3 p0 = mesh.vertices[p0Index];
                    glm::vec3 p1 = mesh.vertices[p1Index];
                    glm::vec3 p2 = mesh.vertices[p2Index];
                    if (!pointsInPart.contains(p0)) {
                        pointsInPart << p0;
                    }
                    if (!pointsInPart.contains(p1)) {
                        pointsInPart << p1;
                    }
                    if (!pointsInPart.contains(p2)) {
                        pointsInPart << p2;
                    }
                }

                // run through all the quads and (uniquely) add each point to the hull
                unsigned int quadCount = meshPart.quadIndices.size() / 4;
                assert((unsigned int)meshPart.quadIndices.size() == quadCount*4);
                for (unsigned int j = 0; j < quadCount; j++) {
                    unsigned int p0Index = meshPart.quadIndices[j*4];
                    unsigned int p1Index = meshPart.quadIndices[j*4+1];
                    unsigned int p2Index = meshPart.quadIndices[j*4+2];
                    unsigned int p3Index = meshPart.quadIndices[j*4+3];
                    glm::vec3 p0 = mesh.vertices[p0Index];
                    glm::vec3 p1 = mesh.vertices[p1Index];
                    glm::vec3 p2 = mesh.vertices[p2Index];
                    glm::vec3 p3 = mesh.vertices[p3Index];
                    if (!pointsInPart.contains(p0)) {
                        pointsInPart << p0;
                    }
                    if (!pointsInPart.contains(p1)) {
                        pointsInPart << p1;
                    }
                    if (!pointsInPart.contains(p2)) {
                        pointsInPart << p2;
                    }
                    if (!pointsInPart.contains(p3)) {
                        pointsInPart << p3;
                    }
                }

                if (pointsInPart.size() == 0) {
                    qCDebug(entitiesrenderer) << "Warning -- meshPart has no faces";
                    continue;
                }

                // add next convex hull
                QVector<glm::vec3> newMeshPoints;
                _points << newMeshPoints;
                // add points to the new convex hull
                _points[i++] << pointsInPart;
            }
        }

        // We expect that the collision model will have the same units and will be displaced
        // from its origin in the same way the visual model is.  The visual model has
        // been centered and probably scaled.  We take the scaling and offset which were applied
        // to the visual model and apply them to the collision model (without regard for the
        // collision model's extents).

        glm::vec3 scale = getDimensions() / renderGeometry.getUnscaledMeshExtents().size();
        // multiply each point by scale before handing the point-set off to the physics engine.
        // also determine the extents of the collision model.
        AABox box;
        for (int i = 0; i < _points.size(); i++) {
            for (int j = 0; j < _points[i].size(); j++) {
                // compensate for registraion
                _points[i][j] += _model->getOffset();
                // scale so the collision points match the model points
                _points[i][j] *= scale;
                box += _points[i][j];
            }
        }

        glm::vec3 collisionModelDimensions = box.getDimensions();
        info.setParams(type, collisionModelDimensions, _compoundShapeURL);
        info.setConvexHulls(_points);
    }
}