/* ==================== FacetsForPatch ==================== */ void FacetsForPatch( dsurface_t *dsurf, shaderInfo_t *si, surfaceTest_t *test ) { int i, j; drawVert_t *v1, *v2, *v3, *v4; int count; mesh_t srcMesh, *subdivided, *mesh; srcMesh.width = dsurf->patchWidth; srcMesh.height = dsurf->patchHeight; srcMesh.verts = &drawVerts[ dsurf->firstVert ]; //subdivided = SubdivideMesh( mesh, CURVE_FACET_ERROR, 9999 ); mesh = SubdivideMesh( srcMesh, 8, 999 ); PutMeshOnCurve( *mesh ); MakeMeshNormals( *mesh ); subdivided = RemoveLinearMeshColumnsRows( mesh ); FreeMesh(mesh); test->patch = qtrue; test->numFacets = ( subdivided->width - 1 ) * ( subdivided->height - 1 ) * 2; test->facets = malloc( sizeof( test->facets[0] ) * test->numFacets ); test->shader = si; count = 0; for ( i = 0 ; i < subdivided->width - 1 ; i++ ) { for ( j = 0 ; j < subdivided->height - 1 ; j++ ) { v1 = subdivided->verts + j * subdivided->width + i; v2 = v1 + 1; v3 = v1 + subdivided->width + 1; v4 = v1 + subdivided->width; if ( CM_GenerateFacetFor4Points( &test->facets[count], v1, v4, v3, v2 ) ) { count++; } else { if (CM_GenerateFacetFor3Points( &test->facets[count], v1, v4, v3 )) count++; if (CM_GenerateFacetFor3Points( &test->facets[count], v1, v3, v2 )) count++; } } } test->numFacets = count; FreeMesh(subdivided); }
/* ==================== FacetsForTriangleSurface ==================== */ void FacetsForTriangleSurface( dsurface_t *dsurf, shaderInfo_t *si, surfaceTest_t *test ) { int i; drawVert_t *v1, *v2, *v3, *v4; int count; int i1, i2, i3, i4, i5, i6; test->patch = qfalse; test->numFacets = dsurf->numIndexes / 3; test->facets = malloc( sizeof( test->facets[0] ) * test->numFacets ); test->shader = si; count = 0; for ( i = 0 ; i < test->numFacets ; i++ ) { i1 = drawIndexes[ dsurf->firstIndex + i*3 ]; i2 = drawIndexes[ dsurf->firstIndex + i*3 + 1 ]; i3 = drawIndexes[ dsurf->firstIndex + i*3 + 2 ]; v1 = &drawVerts[ dsurf->firstVert + i1 ]; v2 = &drawVerts[ dsurf->firstVert + i2 ]; v3 = &drawVerts[ dsurf->firstVert + i3 ]; // try and make a quad out of two triangles if ( i != test->numFacets - 1 ) { i4 = drawIndexes[ dsurf->firstIndex + i*3 + 3 ]; i5 = drawIndexes[ dsurf->firstIndex + i*3 + 4 ]; i6 = drawIndexes[ dsurf->firstIndex + i*3 + 5 ]; if ( i4 == i3 && i5 == i2 ) { v4 = &drawVerts[ dsurf->firstVert + i6 ]; if ( CM_GenerateFacetFor4Points( &test->facets[count], v1, v2, v4, v3 ) ) { count++; i++; // skip next tri continue; } } } if (CM_GenerateFacetFor3Points( &test->facets[count], v1, v2, v3 )) count++; } // we may have turned some pairs into quads test->numFacets = count; }
/* ================== CM_SurfaceCollideFromTriangleSoup ================== */ static void CM_SurfaceCollideFromTriangleSoup(cTriangleSoup_t* triSoup, cSurfaceCollide_t* sc) { int i; float* p1, *p2, *p3; // int i1, i2, i3; cFacet_t* facet; CM_ResetPlaneCounts(); // find the planes for each triangle of the grid for (i = 0; i < triSoup->numTriangles; i++) { p1 = triSoup->points[i][0]; p2 = triSoup->points[i][1]; p3 = triSoup->points[i][2]; triSoup->trianglePlanes[i] = CM_FindPlane(p1, p2, p3); // Log::Notive( "trianglePlane[%i] = %i", i, trianglePlanes[i]); } // create the borders for each triangle for (i = 0; i < triSoup->numTriangles; i++) { facet = &facets[numFacets]; Com_Memset(facet, 0, sizeof(*facet)); p1 = triSoup->points[i][0]; p2 = triSoup->points[i][1]; p3 = triSoup->points[i][2]; facet->surfacePlane = triSoup->trianglePlanes[i]; // CM_FindPlane(p1, p2, p3); // try and make a quad out of two triangles #if 0 i1 = triSoup->indexes[ i * 3 + 0 ]; i2 = triSoup->indexes[ i * 3 + 1 ]; i3 = triSoup->indexes[ i * 3 + 2 ]; if ( i != triSoup->numTriangles - 1 ) { i4 = triSoup->indexes[ i * 3 + 3 ]; i5 = triSoup->indexes[ i * 3 + 4 ]; i6 = triSoup->indexes[ i * 3 + 5 ]; if ( i4 == i3 && i5 == i2 ) { p4 = triSoup->points[ i ][ 5 ]; // vertex at i6 if ( CM_GenerateFacetFor4Points( facet, p1, p2, p4, p3 ) ) //test->facets[count], v1, v2, v4, v3)) { CM_SetBorderInward( facet, triSoup, i, 0 ); if ( CM_ValidateFacet( facet ) ) { CM_AddFacetBevels( facet ); numFacets++; i++; // skip next tri continue; } } } } #endif if (CM_GenerateFacetFor3Points(facet, p1, p2, p3)) { CM_SetBorderInward(facet, triSoup, i, 0); if (CM_ValidateFacet(facet)) { CM_AddFacetBevels(facet); numFacets++; } } } // copy the results out sc->numPlanes = numPlanes; sc->planes = (cPlane_t*) CM_Alloc(numPlanes * sizeof(*sc->planes)); Com_Memcpy(sc->planes, planes, numPlanes * sizeof(*sc->planes)); sc->numFacets = numFacets; sc->facets = (cFacet_t*) CM_Alloc(numFacets * sizeof(*sc->facets)); Com_Memcpy(sc->facets, facets, numFacets * sizeof(*sc->facets)); }
/* ================== CM_SurfaceCollideFromTriangleSoup ================== */ static void CM_SurfaceCollideFromTriangleSoup( cTriangleSoup_t *triSoup, cSurfaceCollide_t *sc ) { float *p1, *p2, *p3; int i, i1, i2, i3; cfacet_t *facet; numPlanes = 0; numFacets = 0; #ifdef USE_HASHING // initialize hash table Mem_Set( planeHashTable, 0, sizeof( planeHashTable )); #endif // find the planes for each triangle of the grid for( i = 0; i < triSoup->numTriangles; i++ ) { p1 = triSoup->points[i][0]; p2 = triSoup->points[i][1]; p3 = triSoup->points[i][2]; triSoup->trianglePlanes[i] = CM_FindPlane( p1, p2, p3 ); } // create the borders for each triangle for( i = 0; i < triSoup->numTriangles; i++ ) { facet = &facets[numFacets]; Mem_Set( facet, 0, sizeof( *facet )); i1 = triSoup->indexes[i*3+0]; i2 = triSoup->indexes[i*3+1]; i3 = triSoup->indexes[i*3+2]; p1 = triSoup->points[i][0]; p2 = triSoup->points[i][1]; p3 = triSoup->points[i][2]; facet->surfacePlane = triSoup->trianglePlanes[i]; // try and make a quad out of two triangles #if 0 if( i != triSoup->numTriangles - 1 ) { int i4, i5, i6; float *p4; i4 = triSoup->indexes[i*3+3]; i5 = triSoup->indexes[i*3+4]; i6 = triSoup->indexes[i*3+5]; if( i4 == i3 && i5 == i2 ) { p4 = triSoup->points[i][5]; // vertex at i6 if(CM_GenerateFacetFor4Points( facet, p1, p2, p4, p3 )) { CM_SetBorderInward( facet, triSoup, i, 0 ); if( CM_ValidateFacet( facet )) { CM_AddFacetBevels( facet ); numFacets++; i++; // skip next tri continue; } } } } #endif if( CM_GenerateFacetFor3Points( facet, p1, p2, p3 )) { CM_SetBorderInward( facet, triSoup, i, 0 ); if( CM_ValidateFacet( facet )) { CM_AddFacetBevels( facet ); numFacets++; } } } // copy the results out sc->numPlanes = numPlanes; sc->planes = Mem_Alloc( cms.mempool, numPlanes * sizeof( *sc->planes )); Mem_Copy( sc->planes, planes, numPlanes * sizeof( *sc->planes )); sc->numFacets = numFacets; sc->facets = Mem_Alloc( cms.mempool, numFacets * sizeof( *sc->facets )); Mem_Copy( sc->facets, facets, numFacets * sizeof( *sc->facets )); }