/* ===================== idRenderModelOverlay::CreateOverlay This projects on both front and back sides to avoid seams The material should be clamped, because entire triangles are added, some of which may extend well past the 0.0 to 1.0 texture range ===================== */ void idRenderModelOverlay::CreateOverlay( const idRenderModel* model, const idPlane localTextureAxis[2], const idMaterial* material ) { // count up the maximum possible vertices and indexes per surface int maxVerts = 0; int maxIndexes = 0; for( int surfNum = 0; surfNum < model->NumSurfaces(); surfNum++ ) { const modelSurface_t* surf = model->Surface( surfNum ); if( surf->geometry->numVerts > maxVerts ) { maxVerts = surf->geometry->numVerts; } if( surf->geometry->numIndexes > maxIndexes ) { maxIndexes = surf->geometry->numIndexes; } } maxIndexes += 3 * 16 / sizeof( triIndex_t ); // to allow the index size to be a multiple of 16 bytes // make temporary buffers for the building process idTempArray< byte > cullBits( maxVerts ); idTempArray< halfFloat_t > texCoordS( maxVerts ); idTempArray< halfFloat_t > texCoordT( maxVerts ); idTempArray< triIndex_t > vertexRemap( maxVerts ); idTempArray< overlayVertex_t > overlayVerts( maxVerts ); idTempArray< triIndex_t > overlayIndexes( maxIndexes ); // pull out the triangles we need from the base surfaces for( int surfNum = 0; surfNum < model->NumBaseSurfaces(); surfNum++ ) { const modelSurface_t* surf = model->Surface( surfNum ); if( surf->geometry == NULL || surf->shader == NULL ) { continue; } // some surfaces can explicitly disallow overlays if( !surf->shader->AllowOverlays() ) { continue; } const srfTriangles_t* tri = surf->geometry; // try to cull the whole surface along the first texture axis const float d0 = tri->bounds.PlaneDistance( localTextureAxis[0] ); if( d0 < 0.0f || d0 > 1.0f ) { continue; } // try to cull the whole surface along the second texture axis const float d1 = tri->bounds.PlaneDistance( localTextureAxis[1] ); if( d1 < 0.0f || d1 > 1.0f ) { continue; } if( tri->staticModelWithJoints != NULL && r_useGPUSkinning.GetBool() ) { R_OverlayPointCullSkinned( cullBits.Ptr(), texCoordS.Ptr(), texCoordT.Ptr(), localTextureAxis, tri->verts, tri->numVerts, tri->staticModelWithJoints->jointsInverted ); } else { R_OverlayPointCullStatic( cullBits.Ptr(), texCoordS.Ptr(), texCoordT.Ptr(), localTextureAxis, tri->verts, tri->numVerts ); } // start streaming the indexes idODSStreamedArray< triIndex_t, 256, SBT_QUAD, 3 > indexesODS( tri->indexes, tri->numIndexes ); memset( vertexRemap.Ptr(), -1, vertexRemap.Size() ); int numIndexes = 0; int numVerts = 0; int maxReferencedVertex = 0; // find triangles that need the overlay for( int i = 0; i < tri->numIndexes; ) { const int nextNumIndexes = indexesODS.FetchNextBatch() - 3; for( ; i <= nextNumIndexes; i += 3 ) { const int i0 = indexesODS[i + 0]; const int i1 = indexesODS[i + 1]; const int i2 = indexesODS[i + 2]; // skip triangles completely off one side if( cullBits[i0] & cullBits[i1] & cullBits[i2] ) { continue; } // we could do more precise triangle culling, like a light interaction does, but it's not worth it // keep this triangle for( int j = 0; j < 3; j++ ) { int index = tri->indexes[i + j]; if( vertexRemap[index] == ( triIndex_t ) - 1 ) { vertexRemap[index] = numVerts; overlayVerts[numVerts].vertexNum = index; overlayVerts[numVerts].st[0] = texCoordS[index]; overlayVerts[numVerts].st[1] = texCoordT[index]; numVerts++; maxReferencedVertex = Max( maxReferencedVertex, index ); } overlayIndexes[numIndexes] = vertexRemap[index]; numIndexes++; } } } if( numIndexes == 0 ) { continue; } // add degenerate triangles until the index size is a multiple of 16 bytes for( ; ( ( ( numIndexes * sizeof( triIndex_t ) ) & 15 ) != 0 ); numIndexes += 3 ) { overlayIndexes[numIndexes + 0] = 0; overlayIndexes[numIndexes + 1] = 0; overlayIndexes[numIndexes + 2] = 0; } // allocate a new overlay overlay_t& overlay = overlays[nextOverlay++ & ( MAX_OVERLAYS - 1 )]; FreeOverlay( overlay ); overlay.material = material; overlay.surfaceNum = surfNum; overlay.surfaceId = surf->id; overlay.numIndexes = numIndexes; overlay.indexes = ( triIndex_t* )Mem_Alloc( numIndexes * sizeof( overlay.indexes[0] ), TAG_MODEL ); memcpy( overlay.indexes, overlayIndexes.Ptr(), numIndexes * sizeof( overlay.indexes[0] ) ); overlay.numVerts = numVerts; overlay.verts = ( overlayVertex_t* )Mem_Alloc( numVerts * sizeof( overlay.verts[0] ), TAG_MODEL ); memcpy( overlay.verts, overlayVerts.Ptr(), numVerts * sizeof( overlay.verts[0] ) ); overlay.maxReferencedVertex = maxReferencedVertex; if( nextOverlay - firstOverlay > MAX_OVERLAYS ) { firstOverlay = nextOverlay - MAX_OVERLAYS; } } }
void MeshUtil::splitModel( Model* model, Mesh* mesh ) { // split by main axis OBBox box = model->boundBox(); Vector3 axis = box.rotation().getColumn(0); Vector3 point = box.translation(); float plane[4] = { axis.x, axis.y, axis.z, -(point.dot(axis)) }; // count number of polygons on each side (in/out sides) VertexAndIndexLock<Model> modelLock( model, Model::LOCK_READ ); int vertsOut = 0; int vertsIn = 0; int indicesOut = 0; int indicesIn = 0; Vector<bool> vertsUsedOut( Allocator<bool>(__FILE__) ); Vector<bool> vertsUsedIn( Allocator<bool>(__FILE__) ); Vector<bool> faceIn( Allocator<bool>(__FILE__) ); vertsUsedOut.setSize( model->vertices(), false ); vertsUsedIn.setSize( model->vertices(), false ); faceIn.setSize( model->indices()/3, false ); faceIn.clear(); for ( int i = 0 ; i < model->indices() ; i += 3 ) { // get face vertices and classify them (in/out) int face[3]; model->getIndices( i, face, 3 ); Vector3 verts[3]; int outcode = 0; for ( int k = 0 ; k < 3 ; ++k ) { model->getVertexPositions( face[k], verts+k, 1 ); if ( verts[k].x*plane[0] + verts[k].y*plane[1] + verts[k].z*plane[2] + plane[3] > 0.f ) outcode |= (1<<k); } if ( 0 == outcode || (7 != outcode && 0 != (i&1)) ) // odd intersecting faces { // in / intersects odd faceIn.add( true ); indicesIn += 3; for ( int k = 0 ; k < 3 ; ++k ) { if ( !vertsUsedIn[face[k]] ) { vertsUsedIn[face[k]] = true; ++vertsIn; } } } else // if ( 7 == outcode ) { // out / intersects even faceIn.add( false ); indicesOut += 3; for ( int k = 0 ; k < 3 ; ++k ) { if ( !vertsUsedOut[face[k]] ) { vertsUsedOut[face[k]] = true; ++vertsOut; } } } } if ( indicesIn < 3 || indicesOut < 3 ) { // nothing to split mesh->addPrimitive( model ); return; } Debug::println( "sgu: Splitting mesh {0} model ({1,#} polys) to two ({2,#} and {3,#} polys)", mesh->name(), model->indices()/3, indicesIn/3, indicesOut/3 ); // build primitives Vector<int> vertexRemap( Allocator<int>(__FILE__) ); int modelFaceIndex; // In P(Model) modelIn = new Model( vertsIn, indicesIn, model->vertexFormat(), model->usage() ); VertexAndIndexLock<Model> modelInLock( modelIn, Model::LOCK_READWRITE ); vertexRemap.clear(); vertexRemap.setSize( model->vertices(), -1 ); int vertIn = 0; for ( int i = 0 ; i < model->vertices() ; ++i ) { if ( vertsUsedIn[i] ) { modelIn->copyVertices( vertIn, model, i, 1 ); vertexRemap[i] = vertIn; ++vertIn; } } assert( vertIn == vertsIn ); modelFaceIndex = 0; int indexIn = 0; for ( int i = 0 ; i < model->indices() ; i += 3 ) { if ( faceIn[modelFaceIndex] ) { int face[3]; model->getIndices( i, face, 3 ); for ( int k = 0 ; k < 3 ; ++k ) face[k] = vertexRemap[ face[k] ]; modelIn->setIndices( indexIn, face, 3 ); indexIn += 3; } ++modelFaceIndex; } assert( indexIn == indicesIn ); modelIn->setShader( model->shader() ); mesh->addPrimitive( modelIn ); // Out P(Model) modelOut = new Model( vertsOut, indicesOut, model->vertexFormat(), model->usage() ); VertexAndIndexLock<Model> modelOutLock( modelOut, Model::LOCK_READWRITE ); vertexRemap.clear(); vertexRemap.setSize( model->vertices(), -1 ); int vertOut = 0; for ( int i = 0 ; i < model->vertices() ; ++i ) { if ( vertsUsedOut[i] ) { modelOut->copyVertices( vertOut, model, i, 1 ); vertexRemap[i] = vertOut; ++vertOut; } } assert( vertOut == vertsOut ); modelFaceIndex = 0; int indexOut = 0; for ( int i = 0 ; i < model->indices() ; i += 3 ) { if ( !faceIn[modelFaceIndex] ) { int face[3]; model->getIndices( i, face, 3 ); for ( int k = 0 ; k < 3 ; ++k ) face[k] = vertexRemap[ face[k] ]; modelOut->setIndices( indexOut, face, 3 ); indexOut += 3; } ++modelFaceIndex; } assert( indexOut == indicesOut ); modelOut->setShader( model->shader() ); mesh->addPrimitive( modelOut ); assert( model->indices() == modelIn->indices()+modelOut->indices() ); }