Example #1
0
bool VerifyGraphOp::verifyIndexMap(GeometryPtr &geo, bool &repair)
{
    repair = false;

    if(geo == NullFC)
        return true;

    if(geo->getIndices() == NullFC)
        return true;

    if(!geo->getMFIndexMapping()->empty())
        return true;

    if(geo->getPositions() == NullFC)
        return true;

    UInt32 positions_size = geo->getPositions()->getSize();

    UInt32 normals_size = 0;
    if(geo->getNormals() != NullFC)
        normals_size = geo->getNormals()->getSize();

    UInt32 colors_size = 0;
    if(geo->getColors() != NullFC)
        colors_size = geo->getColors()->getSize();

    UInt32 secondary_colors_size = 0;
    if(geo->getSecondaryColors() != NullFC)
        secondary_colors_size = geo->getSecondaryColors()->getSize();

    UInt32 texccords_size = 0;
    if(geo->getTexCoords() != NullFC)
        texccords_size = geo->getTexCoords()->getSize();

    UInt32 texccords1_size = 0;
    if(geo->getTexCoords1() != NullFC)
        texccords1_size = geo->getTexCoords1()->getSize();

    UInt32 texccords2_size = 0;
    if(geo->getTexCoords2() != NullFC)
        texccords2_size = geo->getTexCoords2()->getSize();

    UInt32 texccords3_size = 0;
    if(geo->getTexCoords3() != NullFC)
        texccords3_size = geo->getTexCoords3()->getSize();

    /*
    printf("sizes: %u %u %u %u %u %u %u %u\n", positions_size, normals_size,
    colors_size, secondary_colors_size,
    texccords_size, texccords1_size,
    texccords2_size, texccords3_size);
    */
    if((positions_size == normals_size || normals_size == 0) &&
       (positions_size == colors_size || colors_size == 0) &&
       (positions_size == secondary_colors_size || secondary_colors_size == 0) &&
       (positions_size == texccords_size || texccords_size == 0) &&
       (positions_size == texccords1_size || texccords1_size == 0) &&
       (positions_size == texccords2_size || texccords2_size == 0) &&
       (positions_size == texccords3_size || texccords3_size == 0)
      )
    {
        UInt16 indexmap = 0;
        if(positions_size > 0)
            indexmap |= Geometry::MapPosition;
        if(normals_size > 0)
            indexmap |= Geometry::MapNormal;
        if(colors_size > 0)
            indexmap |= Geometry::MapColor;
        if(secondary_colors_size > 0)
            indexmap |= Geometry::MapSecondaryColor;
        if(texccords_size > 0)
            indexmap |= Geometry::MapTexCoords;
        if(texccords1_size > 0)
            indexmap |= Geometry::MapTexCoords1;
        if(texccords2_size > 0)
            indexmap |= Geometry::MapTexCoords2;
        if(texccords3_size > 0)
            indexmap |= Geometry::MapTexCoords3;

        beginEditCP(geo, Geometry::IndexMappingFieldMask);
        geo->editMFIndexMapping()->push_back(indexmap);
        endEditCP(geo, Geometry::IndexMappingFieldMask);
        repair = true;
        return false;
    }
    else
    {
        return false;
    }
}
Example #2
0
static void calcVertexNormals(GeometryPtr geo)
{
    GeoNormals3fPtr norms = GeoNormals3fPtr::dcast(geo->getNormals());
    GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions());

    MFPnt3f *p = pos->getFieldPtr();
    MFVec3f *n = norms->getFieldPtr();

    beginEditCP(norms);

    Vec3f a, b, c;
    int l = 0;
        for(int i=0; i<_size; ++i)
        {
            for(int j=0; j<_size; ++j)
            {
                int m = i*_size+j;

                if (i!=_size-1 && j!=_size-1)
                {
                    a = (*p)[l+m+1] - (*p)[l+m];
                    b = (*p)[l+m+_size] - (*p)[l+m];
                }
                else
                {
                    a = (*p)[l+m-1] - (*p)[l+m];
                    
                    int index = l+m-_size;
                    if(index < 0)
                        index += norms->getSize();
                    
                    b = (*p)[index] - (*p)[l+m];
                }

                c = a.cross(b);
                c.normalize();
        
                if (i==0 && j==_size-1)
                {
                    a = (*p)[l+m-1] - (*p)[l+m];
                    b = (*p)[l+m+_size] - (*p)[l+m];
        
                    c = a.cross(b);
                    c.normalize();
                    c.negate();
                }

                if (i==_size-1 && j==0)
                {
                    a = (*p)[l+m-_size] - (*p)[l+m];
                    b = (*p)[l+m+1] - (*p)[l+m];
        
                    c = a.cross(b);
                    c.normalize();
                }
                (*n)[l+m] = c;
            }
        }
        l += _size*_size;
    endEditCP(norms);
}
Example #3
0
/** Verify geometry method. */
Action::ResultE VerifyGraphOp::verifyGeometry(NodePtr &node)
{
    GeometryPtr geo = GeometryPtr::dcast(node->getCore());

    if(geo == NullFC)
        return Action::Continue;

    if(geo->getPositions() == NullFC)
        return Action::Continue;

    UInt32 start_errors = _numErrors;

    Int32 positions_size = geo->getPositions()->getSize();

    Int32 normals_size = 0;
    if(geo->getNormals() != NullFC)
        normals_size = geo->getNormals()->getSize();

    Int32 colors_size = 0;
    if(geo->getColors() != NullFC)
        colors_size = geo->getColors()->getSize();

    Int32 secondary_colors_size = 0;
    if(geo->getSecondaryColors() != NullFC)
        secondary_colors_size = geo->getSecondaryColors()->getSize();

    Int32 texccords_size = 0;
    if(geo->getTexCoords() != NullFC)
        texccords_size = geo->getTexCoords()->getSize();

    Int32 texccords1_size = 0;
    if(geo->getTexCoords1() != NullFC)
        texccords1_size = geo->getTexCoords1()->getSize();

    Int32 texccords2_size = 0;
    if(geo->getTexCoords2() != NullFC)
        texccords2_size = geo->getTexCoords2()->getSize();

    Int32 texccords3_size = 0;
    if(geo->getTexCoords3() != NullFC)
        texccords3_size = geo->getTexCoords3()->getSize();

    UInt32 pos_errors = 0;
    UInt32 norm_errors = 0;
    UInt32 col_errors = 0;
    UInt32 col2_errors = 0;
    UInt32 tex0_errors = 0;
    UInt32 tex1_errors = 0;
    UInt32 tex2_errors = 0;
    UInt32 tex3_errors = 0;

    PrimitiveIterator it;
    for(it = geo->beginPrimitives(); it != geo->endPrimitives(); ++it)
    {
        for(UInt32 v=0; v < it.getLength(); ++v)
        {
            if(it.getPositionIndex(v) >= positions_size)
                ++pos_errors;
            if(it.getNormalIndex(v) >= normals_size)
                ++norm_errors;
            if(it.getColorIndex(v) >= colors_size)
                ++col_errors;
            if(it.getSecondaryColorIndex(v) >= secondary_colors_size)
                ++col2_errors;
            if(it.getTexCoordsIndex(v) >= texccords_size)
                ++tex0_errors;
            if(it.getTexCoordsIndex1(v) >= texccords1_size)
                ++tex1_errors;
            if(it.getTexCoordsIndex2(v) >= texccords2_size)
                ++tex2_errors;
            if(it.getTexCoordsIndex3(v) >= texccords3_size)
                ++tex3_errors;
        }
    }

    if(norm_errors > 0)
    {
        norm_errors = 0;
        if(_verbose) SINFO << "removed corrupted normals!\n";
        beginEditCP(geo);
        geo->setNormals(NullFC);
        endEditCP(geo);
    }

    if(col_errors > 0)
    {
        col_errors = 0;
        if(_verbose) SINFO << "removed corrupted colors!\n";
        beginEditCP(geo);
        geo->setColors(NullFC);
        endEditCP(geo);
    }

    if(tex0_errors > 0)
    {
        tex0_errors = 0;
        if(_verbose) SINFO << "removed corrupted tex coords0!\n";
        beginEditCP(geo);
        geo->setTexCoords(NullFC);
        endEditCP(geo);
    }

    _numErrors += (pos_errors + norm_errors + col_errors +
                   col2_errors + tex0_errors + tex1_errors +
                   tex2_errors + tex3_errors);

    // found some errors.
    if(_numErrors > start_errors)
    {
        _corruptedGeos.push_back(geo); 
    }

    // ok we found no errors now check for missing index map.
    bool need_repair(false);
    if(!verifyIndexMap(geo, need_repair))
    {
        if(need_repair)
        { 
            SINFO << "verifyGeometry : added missing index map!" << endLog; 
        }
        else
        { 
            SINFO << "verifyGeometry : couldn't add missing index map!\n" 
                  << endLog; 
        }
    }

    return Action::Continue;
}
Example #4
0
/***************************************************************************\
*                              Field Set	                               *
\***************************************************************************/
void PhysicsTriMeshGeom::setGeometryNode(NodePtr& node)
{
    PhysicsTriMeshGeomPtr tmpPtr(*this);

    GeometryPtr geo = GeometryPtr::dcast(node->getCore());
    if(geo!=NullFC)
    {
        calcVertexNormals(geo, deg2rad( 30));
        separateProperties(geo);
        createSingleIndex(geo);

        GeoPositions3f::StoredFieldType* positions =
            GeoPositions3fPtr::dcast( geo->getPositions())->getFieldPtr();
        GeoIndicesUI32::StoredFieldType* indices =
            GeoIndicesUI32Ptr::dcast( geo->getIndices())->getFieldPtr();
        GeoNormals3f::StoredFieldType* normals =
            GeoNormals3fPtr::dcast( geo->getNormals())->getFieldPtr();

        GeoPTypesPtr geoTypes = geo->getTypes();
        bool triangles = false;
        //has to be some triangle soup!
        for( Int32 i=0; i < geoTypes->size(); ++i) {
            switch( geoTypes->getValue(i)) {
            case GL_TRIANGLES:
                triangles=true;
                break;
            case GL_TRIANGLE_STRIP:
                triangles=true;
                break;
            case GL_TRIANGLE_FAN:
                triangles=true;
                break;
            }
        }

        UInt32 vertexCount =
            GeoPositions3fPtr::dcast(geo->getPositions())->getSize();
        UInt32 vertexStride = 3*sizeof(Real32);
        UInt32 indexCount =
            GeoIndicesUI32Ptr::dcast(geo->getIndices())->getSize();
        UInt32 indexStride = 3*sizeof(UInt32);

        //pass the pointers to ODE
        if(tmpPtr->data)
            dGeomTriMeshDataDestroy(tmpPtr->data);
        tmpPtr->data = dGeomTriMeshDataCreate();
        if(triangles)
        {
            dGeomTriMeshDataBuildSingle(tmpPtr->data, (Real32*)&positions->front(),
                                        vertexStride, vertexCount, (Int32*)&indices->front(), indexCount,
                                        indexStride/* just can't use this, (Real32*)&normals->front()*/);
            tmpPtr->setData(tmpPtr->data);

            /* use this method if you build with single precision
            dGeomTriMeshDataBuildSingle1(tmpPtr->data, (Real32*)&positions->front(),
                vertexStride, vertexCount, (Int32*)&indices->front(), indexCount,
                indexStride, (Real32*)&normals->front());
            tmpPtr->setData(tmpPtr->data);
            */

        }
        else
        {
            FWARNING(("No triangle mesh given to ODE! Convert to triangles first!\n"));
            tmpPtr->setData(tmpPtr->data);
        }
    }
    tmpPtr->geoNode=node;
    PhysicsTriMeshGeomBase::setGeometryNode(node);
}