Source/ThunderBeast/QuakeToonProto1/ref_gl/gl_rsurf.c
void R_BlendLightmaps (void)
{
	int			i;
	msurface_t	*surf, *newdrawsurf = 0;

	// don't bother if we're set to fullbright
	if (r_fullbright->value)
		return;
	if (!r_worldmodel->lightdata)
		return;

	// don't bother writing Z
	qglDepthMask( 0 );

	/*
	** set the appropriate blending mode unless we're only looking at the
	** lightmaps.
	*/
	if (!gl_lightmap->value)
	{
		qglEnable (GL_BLEND);

		if ( gl_saturatelighting->value )
		{
			qglBlendFunc( GL_ONE, GL_ONE );
		}
		else
		{
			if ( gl_monolightmap->string[0] != '0' )
			{
				switch ( toupper( gl_monolightmap->string[0] ) )
				{
				case 'I':
					qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
					break;
				case 'L':
					qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
					break;
				case 'A':
				default:
					qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
					break;
				}
			}
			else
			{
				qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
			}
		}
	}

	if ( currentmodel == r_worldmodel )
		c_visible_lightmaps = 0;

	/*
	** render static lightmaps first
	*/
	for ( i = 1; i < MAX_LIGHTMAPS; i++ )
	{
		if ( gl_lms.lightmap_surfaces[i] )
		{
			if (currentmodel == r_worldmodel)
				c_visible_lightmaps++;
			GL_Bind( gl_state.lightmap_textures + i);

			for ( surf = gl_lms.lightmap_surfaces[i]; surf != 0; surf = surf->lightmapchain )
			{
				if ( surf->polys )
					DrawGLPolyChain( surf->polys, 0, 0 );
			}
		}
	}

	/*
	** render dynamic lightmaps
	*/
	if ( gl_dynamic->value )
	{
		LM_InitBlock();

		GL_Bind( gl_state.lightmap_textures+0 );

		if (currentmodel == r_worldmodel)
			c_visible_lightmaps++;

		newdrawsurf = gl_lms.lightmap_surfaces[0];

		for ( surf = gl_lms.lightmap_surfaces[0]; surf != 0; surf = surf->lightmapchain )
		{
			int		smax, tmax;
			byte	*base;

			smax = (surf->extents[0]>>4)+1;
			tmax = (surf->extents[1]>>4)+1;

			if ( LM_AllocBlock( smax, tmax, &surf->dlight_s, &surf->dlight_t ) )
			{
				base = gl_lms.lightmap_buffer;
				base += ( surf->dlight_t * BLOCK_WIDTH + surf->dlight_s ) * LIGHTMAP_BYTES;

				R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
			}
			else
			{
				msurface_t *drawsurf;

				// upload what we have so far
				LM_UploadBlock( true );

				// draw all surfaces that use this lightmap
				for ( drawsurf = newdrawsurf; drawsurf != surf; drawsurf = drawsurf->lightmapchain )
				{
					if ( drawsurf->polys )
						DrawGLPolyChain( drawsurf->polys, 
							              ( drawsurf->light_s - drawsurf->dlight_s ) * ( 1.0 / 128.0 ), 
										( drawsurf->light_t - drawsurf->dlight_t ) * ( 1.0 / 128.0 ) );
				}

				newdrawsurf = drawsurf;

				// clear the block
				LM_InitBlock();

				// try uploading the block now
				if ( !LM_AllocBlock( smax, tmax, &surf->dlight_s, &surf->dlight_t ) )
				{
					ri.Sys_Error( ERR_FATAL, "Consecutive calls to LM_AllocBlock(%d,%d) failed (dynamic)\n", smax, tmax );
				}

				base = gl_lms.lightmap_buffer;
				base += ( surf->dlight_t * BLOCK_WIDTH + surf->dlight_s ) * LIGHTMAP_BYTES;

				R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
			}
		}

		/*
		** draw remainder of dynamic lightmaps that haven't been uploaded yet
		*/
		if ( newdrawsurf )
			LM_UploadBlock( true );

		for ( surf = newdrawsurf; surf != 0; surf = surf->lightmapchain )
		{
			if ( surf->polys )
				DrawGLPolyChain( surf->polys, ( surf->light_s - surf->dlight_s ) * ( 1.0 / 128.0 ), ( surf->light_t - surf->dlight_t ) * ( 1.0 / 128.0 ) );
		}
	}

	/*
	** restore state
	*/
	qglDisable (GL_BLEND);
	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	qglDepthMask( 1 );
}
/*
** R_BlendLightMaps
**
** This routine takes all the given light mapped surfaces in the world and
** blends them into the framebuffer.
*/
R_BlendLightMaps
This routine takes all the given light mapped surfaces in the world and
blends them into the framebuffer.
R_BlendLightMaps This routine takes all the given light mapped surfaces in the world and blends them into the framebuffer .
void R_BlendLightmaps (void)
{
	int			i;
	msurface_t	*surf, *newdrawsurf = 0;
	if (r_fullbright->value)
		return;
	if (!r_worldmodel->lightdata)
		return;
	qglDepthMask( 0 );
	if (!gl_lightmap->value)
	{
		qglEnable (GL_BLEND);
		if ( gl_saturatelighting->value )
		{
			qglBlendFunc( GL_ONE, GL_ONE );
		}
		else
		{
			if ( gl_monolightmap->string[0] != '0' )
			{
				switch ( toupper( gl_monolightmap->string[0] ) )
				{
				case 'I':
					qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
					break;
				case 'L':
					qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
					break;
				case 'A':
				default:
					qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
					break;
				}
			}
			else
			{
				qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
			}
		}
	}
	if ( currentmodel == r_worldmodel )
		c_visible_lightmaps = 0;
	for ( i = 1; i < MAX_LIGHTMAPS; i++ )
	{
		if ( gl_lms.lightmap_surfaces[i] )
		{
			if (currentmodel == r_worldmodel)
				c_visible_lightmaps++;
			GL_Bind( gl_state.lightmap_textures + i);
			for ( surf = gl_lms.lightmap_surfaces[i]; surf != 0; surf = surf->lightmapchain )
			{
				if ( surf->polys )
					DrawGLPolyChain( surf->polys, 0, 0 );
			}
		}
	}
	if ( gl_dynamic->value )
	{
		LM_InitBlock();
		GL_Bind( gl_state.lightmap_textures+0 );
		if (currentmodel == r_worldmodel)
			c_visible_lightmaps++;
		newdrawsurf = gl_lms.lightmap_surfaces[0];
		for ( surf = gl_lms.lightmap_surfaces[0]; surf != 0; surf = surf->lightmapchain )
		{
			int		smax, tmax;
			byte	*base;
			smax = (surf->extents[0]>>4)+1;
			tmax = (surf->extents[1]>>4)+1;
			if ( LM_AllocBlock( smax, tmax, &surf->dlight_s, &surf->dlight_t ) )
			{
				base = gl_lms.lightmap_buffer;
				base += ( surf->dlight_t * BLOCK_WIDTH + surf->dlight_s ) * LIGHTMAP_BYTES;
				R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
			}
			else
			{
				msurface_t *drawsurf;
				LM_UploadBlock( true );
				for ( drawsurf = newdrawsurf; drawsurf != surf; drawsurf = drawsurf->lightmapchain )
				{
					if ( drawsurf->polys )
						DrawGLPolyChain( drawsurf->polys, 
							              ( drawsurf->light_s - drawsurf->dlight_s ) * ( 1.0 / 128.0 ), 
										( drawsurf->light_t - drawsurf->dlight_t ) * ( 1.0 / 128.0 ) );
				}
				newdrawsurf = drawsurf;
				LM_InitBlock();
				if ( !LM_AllocBlock( smax, tmax, &surf->dlight_s, &surf->dlight_t ) )
				{
					ri.Sys_Error( ERR_FATAL, "Consecutive calls to LM_AllocBlock(%d,%d) failed (dynamic)\n", smax, tmax );
				}
				base = gl_lms.lightmap_buffer;
				base += ( surf->dlight_t * BLOCK_WIDTH + surf->dlight_s ) * LIGHTMAP_BYTES;
				R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
			}
		}
		if ( newdrawsurf )
			LM_UploadBlock( true );
		for ( surf = newdrawsurf; surf != 0; surf = surf->lightmapchain )
		{
			if ( surf->polys )
				DrawGLPolyChain( surf->polys, ( surf->light_s - surf->dlight_s ) * ( 1.0 / 128.0 ), ( surf->light_t - surf->dlight_t ) * ( 1.0 / 128.0 ) );
		}
	}
	qglDisable (GL_BLEND);
	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	qglDepthMask( 1 );
}
void R_BlendLightmaps ( void ) { int i ; msurface_t * surf , * newdrawsurf = 0 ; if ( r_fullbright -> value ) return ; if ( ! r_worldmodel -> lightdata ) return ; qglDepthMask ( 0 ) ; if ( ! gl_lightmap -> value ) { qglEnable ( GL_BLEND ) ; if ( gl_saturatelighting -> value ) { qglBlendFunc ( GL_ONE , GL_ONE ) ; } else { if ( gl_monolightmap -> string [ 0 ] != ' ' ) { switch ( toupper ( gl_monolightmap -> string [ 0 ] ) ) { case ' ' : qglBlendFunc ( GL_ZERO , GL_SRC_COLOR ) ; break ; case ' ' : qglBlendFunc ( GL_ZERO , GL_SRC_COLOR ) ; break ; case ' ' : default : qglBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ) ; break ; } } else { qglBlendFunc ( GL_ZERO , GL_SRC_COLOR ) ; } } } if ( currentmodel == r_worldmodel ) c_visible_lightmaps = 0 ; for ( i = 1 ; i < MAX_LIGHTMAPS ; i ++ ) { if ( gl_lms . lightmap_surfaces [ i ] ) { if ( currentmodel == r_worldmodel ) c_visible_lightmaps ++ ; GL_Bind ( gl_state . lightmap_textures + i ) ; for ( surf = gl_lms . lightmap_surfaces [ i ] ; surf != 0 ; surf = surf -> lightmapchain ) { if ( surf -> polys ) DrawGLPolyChain ( surf -> polys , 0 , 0 ) ; } } } if ( gl_dynamic -> value ) { LM_InitBlock ( ) ; GL_Bind ( gl_state . lightmap_textures + 0 ) ; if ( currentmodel == r_worldmodel ) c_visible_lightmaps ++ ; newdrawsurf = gl_lms . lightmap_surfaces [ 0 ] ; for ( surf = gl_lms . lightmap_surfaces [ 0 ] ; surf != 0 ; surf = surf -> lightmapchain ) { int smax , tmax ; byte * base ; smax = ( surf -> extents [ 0 ] >> 4 ) + 1 ; tmax = ( surf -> extents [ 1 ] >> 4 ) + 1 ; if ( LM_AllocBlock ( smax , tmax , & surf -> dlight_s , & surf -> dlight_t ) ) { base = gl_lms . lightmap_buffer ; base += ( surf -> dlight_t * BLOCK_WIDTH + surf -> dlight_s ) * LIGHTMAP_BYTES ; R_BuildLightMap ( surf , base , BLOCK_WIDTH * LIGHTMAP_BYTES ) ; } else { msurface_t * drawsurf ; LM_UploadBlock ( true ) ; for ( drawsurf = newdrawsurf ; drawsurf != surf ; drawsurf = drawsurf -> lightmapchain ) { if ( drawsurf -> polys ) DrawGLPolyChain ( drawsurf -> polys , ( drawsurf -> light_s - drawsurf -> dlight_s ) * ( 1.0 / 128.0 ) , ( drawsurf -> light_t - drawsurf -> dlight_t ) * ( 1.0 / 128.0 ) ) ; } newdrawsurf = drawsurf ; LM_InitBlock ( ) ; if ( ! LM_AllocBlock ( smax , tmax , & surf -> dlight_s , & surf -> dlight_t ) ) { ri . Sys_Error ( ERR_FATAL , " \n " , smax , tmax ) ; } base = gl_lms . lightmap_buffer ; base += ( surf -> dlight_t * BLOCK_WIDTH + surf -> dlight_s ) * LIGHTMAP_BYTES ; R_BuildLightMap ( surf , base , BLOCK_WIDTH * LIGHTMAP_BYTES ) ; } } if ( newdrawsurf ) LM_UploadBlock ( true ) ; for ( surf = newdrawsurf ; surf != 0 ; surf = surf -> lightmapchain ) { if ( surf -> polys ) DrawGLPolyChain ( surf -> polys , ( surf -> light_s - surf -> dlight_s ) * ( 1.0 / 128.0 ) , ( surf -> light_t - surf -> dlight_t ) * ( 1.0 / 128.0 ) ) ; } } qglDisable ( GL_BLEND ) ; qglBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ) ; qglDepthMask ( 1 ) ; }
R_BlendLightMaps
This routine takes all the given light mapped surfaces in the world and
blends them into the framebuffer.
R_BlendLightMaps This routine takes all the given light mapped surfaces in the world and blends them into the framebuffer .
// don't bother if we're set to fullbright // don't bother writing Z /*
	** set the appropriate blending mode unless we're only looking at the
	** lightmaps.
	*/ /*
	** render static lightmaps first
	*/ /*
	** render dynamic lightmaps
	*/ // upload what we have so far // draw all surfaces that use this lightmap // clear the block // try uploading the block now /*
		** draw remainder of dynamic lightmaps that haven't been uploaded yet
		*/ /*
	** restore state
	*/