Ejemplo n.º 1
0
/*
=================
idRenderModelDecal::CreateDecal
=================
*/
void idRenderModelDecal::CreateDecal( const idRenderModel *model, const decalProjectionParms_t &localParms ) {
	int maxVerts = 0;
	for ( int surfNum = 0; surfNum < model->NumSurfaces(); surfNum++ ) {
		const modelSurface_t *surf = model->Surface( surfNum );
		if ( surf->geometry != NULL && surf->shader != NULL ) {
			maxVerts = Max( maxVerts, surf->geometry->numVerts );
		}
	}

	idTempArray< byte > cullBits( ALIGN( maxVerts, 4 ) );

	// check all model surfaces
	for ( int surfNum = 0; surfNum < model->NumSurfaces(); surfNum++ ) {
		const modelSurface_t *surf = model->Surface( surfNum );

		// if no geometry or no shader
		if ( surf->geometry == NULL || surf->shader == NULL ) {
			continue;
		}

		// decals and overlays use the same rules
		if ( !localParms.force && !surf->shader->AllowOverlays() ) {
			continue;
		}

		srfTriangles_t *tri = surf->geometry;

		// if the triangle bounds do not overlap with the projection bounds
		if ( !localParms.projectionBounds.IntersectsBounds( tri->bounds ) ) {
			continue;
		}

		// decals don't work on animated models
		assert( tri->staticModelWithJoints == NULL );

		// catagorize all points by the planes
		R_DecalPointCullStatic( cullBits.Ptr(), localParms.boundingPlanes, tri->verts, tri->numVerts );

		// start streaming the indexes
		idODSStreamedArray< triIndex_t, 256, SBT_QUAD, 3 > indexesODS( tri->indexes, tri->numIndexes );

		// find triangles inside the projection volume
		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;
				}

				const idDrawVert * verts[3] = {
					&tri->verts[i0],
					&tri->verts[i1],
					&tri->verts[i2]
				};

				// skip back facing triangles
				const idPlane plane( verts[0]->xyz, verts[1]->xyz, verts[2]->xyz );
				if ( plane.Normal() * localParms.boundingPlanes[NUM_DECAL_BOUNDING_PLANES - 2].Normal() < -0.1f ) {
					continue;
				}

				// create a winding with texture coordinates for the triangle
				idFixedWinding fw;
				fw.SetNumPoints( 3 );
				if ( localParms.parallel ) {
					for ( int j = 0; j < 3; j++ ) {
						fw[j] = verts[j]->xyz;
						fw[j].s = localParms.textureAxis[0].Distance( verts[j]->xyz );
						fw[j].t = localParms.textureAxis[1].Distance( verts[j]->xyz );
					}
				} else {
					for ( int j = 0; j < 3; j++ ) {
						const idVec3 dir = verts[j]->xyz - localParms.projectionOrigin;
						float scale;
						localParms.boundingPlanes[NUM_DECAL_BOUNDING_PLANES - 1].RayIntersection( verts[j]->xyz, dir, scale );
						const idVec3 intersection = verts[j]->xyz + scale * dir;

						fw[j] = verts[j]->xyz;
						fw[j].s = localParms.textureAxis[0].Distance( intersection );
						fw[j].t = localParms.textureAxis[1].Distance( intersection );
					}
				}

				const int orBits = cullBits[i0] | cullBits[i1] | cullBits[i2];

				// clip the exact surface triangle to the projection volume
				for ( int j = 0; j < NUM_DECAL_BOUNDING_PLANES; j++ ) {
					if ( ( orBits & ( 1 << j ) ) != 0 ) {
						if ( !fw.ClipInPlace( -localParms.boundingPlanes[j] ) ) {
							break;
						}
					}
				}

				// if there is a part of the triangle between the bounding planes then clip
				// the triangle based on depth and add decals for the depth faded parts
				if ( fw.GetNumPoints() != 0 ) {
					idFixedWinding back;

					if ( fw.Split( &back, localParms.fadePlanes[0], 0.1f ) == SIDE_CROSS ) {
						CreateDecalFromWinding( back, localParms.material, localParms.fadePlanes, localParms.fadeDepth, localParms.startTime );
					}

					if ( fw.Split( &back, localParms.fadePlanes[1], 0.1f ) == SIDE_CROSS ) {
						CreateDecalFromWinding( back, localParms.material, localParms.fadePlanes, localParms.fadeDepth, localParms.startTime );
					}

					CreateDecalFromWinding( fw, localParms.material, localParms.fadePlanes, localParms.fadeDepth, localParms.startTime );
				}
			}
		}
	}
}
Ejemplo n.º 2
0
/*
=====================
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;
		}
	}
}