⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gl_light.pas

📁 雷神之锤2(Quake2)Delphi源码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
	light := 0;
	dl := r_newrefdef.dlights;
	for lnum := 0 to r_newrefdef.num_dlights-1 do
	begin
	 VectorSubtract (currententity.origin ,dl.origin ,dist);
	add := dl.intensity - (VectorLength(dist)) ;
	add := add * (1 /256);
	if (add > 0) then
        VectorMA (color , add ,dl.color , color );
	
	Inc (dl);
	end;

       	VectorScale (color ,(gl_modulate.value) , color );
end;

(*/*
===============
R_AddDynamicLights
===============
*/*)
procedure R_AddDynamicLights (surf : msurface_p );
var
lnum ,sd,td :integer ;
fdist ,frad ,fminlight :single;
impact , local : vec3_t ;
s ,t, i, smax ,tmax :integer ;
tex : mtexinfo_p ;
dl : dlight_p ;
pfBL : Array of Single; // or pSingle
fsacc , ftacc : Single ;
begin
	 smax := (surf.extents [0] shr 4)+1 ;
	 tmax := (surf.extents [1] shr 4)+1 ;
	 tex := surf.texinfo ;
	 	 for lnum := 0 to r_newrefdef.num_dlights-1 do
		 begin
		 if (surf.dlightbits = 0) then continue ; // {Fab} check this translation ,please.
		 // if ( !(surf->dlightbits & (1<<lnum) ) )
			//continue;		// not lit by this light 
			
		       	dl := @r_newrefdef.dlights [lnum];
			frad := dl.intensity ;
			fdist := DotProduct (dl.origin , surf.plane.normal ) - surf.plane.dist ;
			frad := frad - fabs(fdist) ;
			// rad is now the highest intensity on the plane
			
			fminlight := DLIGHT_CUTOFF ; // FIXME: make configurable?
			
			if (frad < fminlight ) then continue ;
			fminlight := frad - fminlight ;
			
			for i := 0 to 2 do
			begin
			impact [i] := dl.origin[i] - surf.plane.normal[i] * fdist ;
			end;
			
		      	local[0] := DotProduct (impact, tex.vecs[0]) + tex.vecs[0][3] - surf.texturemins[0];
		      	local[1] := DotProduct (impact, tex.vecs[1]) + tex.vecs[1][3] - surf.texturemins[1];

			pfBL := @s_blocklights;
			ftacc :=0 ;
			for t:=0 to tmax-1 do
			begin
			td :=round( local[1] - ftacc) ;
			if (td < 0) then td := -td ;
			   fsacc := 0.0 ;
			   for s := 0 to smax-1 do
			   begin
			   sd := Q_ftol (local[0] - fsacc);
			   	  if (sd < 0) then sd := - sd;
				  if (sd > td) then fdist := sd + (td shr 1) 
				  else ;
				  fdist := td + (sd shr 1) ;
				  		if (fdist < fminlight ) then
						begin
						pfBL[0] := pfBL[0] + ( frad - fdist ) * dl.color[0];
						pfBL[1] := pfBL[1] + ( frad - fdist ) * dl.color[1];
						pfBL[2] := pfBL[2] + ( frad - fdist ) * dl.color[2];
						end;   
			   fsacc := fsacc + 16;
			   pfBL := pfBL +3 ;
			   end;
			   
			ftacc := ftacc +16;   
			end ;
		end;		
end;    		  			  	 

(*/*
** R_SetCacheState
*/*)
procedure R_SetCacheState( surf : msurface_p );
var
maps :integer ;
begin
	 for maps := 0 to MAXLIGHTMAPS -1 do
	 begin
         if surf.styles [maps] <> 255 then
         surf.cached_light[maps] := r_newrefdef.lightstyles[surf.styles[maps]].white;
		  
	end;	  
end;

(*/*
===============
R_BuildLightMap

Combine and scale multiple lightmaps into the floating format in blocklights
===============
*/*)
procedure R_BuildLightMap (surf :msurface_p ;dest :Array of Byte{pByte} ; stride : integer );
var
smax ,tmax : integer ;
r ,g ,b ,a ,max :integer;
ia,ib,ic,id :integer ; // {FAB} var added by me used in various FOR-Loop cycle
i ,j ,size :integer ;
lightmap :Array of Byte ;
scale :array [0..3] of Single ;
nummaps :integer ;
bl : Array of Single ; // or pSingle 
style : lightstyle_p;
monolightmap : integer ;
maps :integer ;
t :Single;
label Store ;
begin
	       { if (surf.texinfo.flags = SURF_SKY) or
	 	(surf.texinfo.flags = SURF_TRANS33) or
		(surf.texinfo.flags = SURF_TRANS66) or
		(surf.texinfo.flags = SURF_WARP) then }  {Fab} //check this traslation
        if (surf.TexInfo.Flags) and (SURF_SKY or SURF_TRANS33 or SURF_TRANS66 or SURF_WARP ) <> 0 then {Fab} //check this
        
       	ri.Sys_Error (ERR_DROP ,'R_BuildLightMap called for non-lit surface');
	
	smax := (surf.extents[0]shr 4)+1;
	tmax := (surf.extents[1]shr 4)+1;
	size := smax * tmax;
		 if (size > (sizeof (s_blocklights)shr 4)) then 
	       	 ri.Sys_Error (ERR_DROP , 'Bad s_blocklights size' );
	
	// set to full bright if no light data
	if not assigned (surf.samples ) then 
                begin
		   for i := 0 to (size*3)-1 do
		   //begin
		   s_blocklights[i] := 255;
		   //end ;
		   for maps := 0 to MAXLIGHTMAPS -1 do
		   	   begin
			   if surf.styles [maps] <> 255 then
			   style := @r_newrefdef.lightstyles[surf.styles[maps]];
			   end;
                   goto store ;
		end;
		// count the # of maps
		for nummaps := 0 to MAXLIGHTMAPS -1 do
		begin
			 if surf.styles [nummaps] <> 255 then
			 lightmap := @surf.samples ;
			 
			 // add all the lightmaps
			 if (nummaps = 1 ) then
			 begin
			 	  for maps := 0 to MAXLIGHTMAPS -1 do
				  begin
				  	   if surf.styles [maps] <> 255 then
					   bl := @s_blocklights ;
					   for i := 0 to 2 do
					   begin
					   scale[i] :=gl_modulate.value * r_newrefdef.lightstyles[surf.styles[maps]].rgb[i];
					   if (scale[0] = 1) and
					   	  (scale[1] = 1) and
					   	  (scale[2] = 1) then
					   	  begin
						  for ia := 0 to size-1 do
						  begin
						  bl[0] := lightmap[ia*3+0];
						  bl[1] := lightmap[ia*3+1];
						  bl[2] := lightmap[ia*3+2];
						  bl := bl+3;
						  end;
						  end
						else
						  begin
						  for ib := 0 to size-1 do
						  begin
						  bl[0] := lightmap[ib*3+0] * scale[0];
						  bl[1] := lightmap[ib*3+1] * scale[1];
						  bl[2] := lightmap[ib*3+2] * scale[2];
						  bl := bl+3;
						  end;
						  end;
						  lightmap := lightmap + size*3 ; // skip to next lightmap
					   end;
				      end;
				  end
				 else
				memset (@s_blocklights, 0, sizeof( s_blocklights[0] ) * size * 3 );
				for maps := 0 to MAXLIGHTMAPS -1 do
				begin
					 if surf.styles [maps] <> 255 then
					 bl := @s_blocklights ;
					 
					 for i := 0 to 2 do
					 begin
				       	 scale[i] := gl_modulate.value * r_newrefdef.lightstyles[surf.styles[maps]].rgb[i];
					 
                                                if (scale[0] = 1) and
                                                   (scale[1] = 1) and
                                                   (scale[2] = 1) then
                                                   begin
                                                        for ic := 0 to size-1 do
								      begin
								 	  bl[0] := lightmap[ic*3+0];
						  			  bl[1] := lightmap[ic*3+1];
						  			  bl[2] := lightmap[ic*3+2];
						  			  bl := bl+3;
								      end;
                                                   end
                                                   else
                                                   begin
                                                        for id:= 0 to size-1 do
						  		 begin
						  		 bl[0] := lightmap[id*3+0] * scale[0];
						  		 bl[1] := lightmap[id*3+1] * scale[1];
						  		 bl[2] := lightmap[id*3+2] * scale[2];
						  	       	 bl := bl+3;
						  		 end;
                                                   end;
                                         lightmap := lightmap + size*3 ; // skip to next lightmap
                                end;
                         end;
			// add all the dynamic lights
			if (surf.dlightframe = r_framecount) then R_AddDynamicLights (surf);

				
				  
                end;


// put into texture format
Store :           // Label

 stride :=stride - (smax shl 2);
 bl := @s_blocklights;

 monolightmap := StrToInt(gl_monolightmap.string_[0]);
 if ( monolightmap = 0 ) then
 begin
        for i:=0 to tmax-1 do
        begin
                for j:=0 to smax-1 do
                begin
                r := Q_ftol( bl[0] );
                g := Q_ftol( bl[1] );
                b := Q_ftol( bl[2] );

                // catch negative lights
                if (r < 0)then r := 0;
                if (g < 0)then g := 0;
                if (b < 0)then b := 0;
                (*/*
                //** determine the brightest of the three color components
                //*/*)
                if (r > g)then max := r
                else
                max := g;
                if (b > max)then max := b;
                (*/*
                ** alpha is ONLY used for the mono lightmap case.  For this reason
                ** we set it to the brightest of the color components so that
                ** things don't get too dim.
                */*)

                a := max;

                (*/*
                ** rescale all the color components if the intensity of the greatest
                ** channel exceeds 1.0
                */*)
                if (max > 255)then
                        begin
                        t := 255 div max;

                        r := r*Round(t);
                        g := g*Round(t);
                        b := b*Round(t);
                        a := a*Round(t);
                        end;
                      dest[0] := r;
                      dest[1] := g;
                      dest[2] := b;
                      dest[3] := a;

                      bl :=bl + 3;
                      dest :=dest + 4;

                end;
        end;
       dest :=dest + stride;
    end

    else
       for i:=0 to tmax-1 do
        begin
                for j:=0 to smax-1 do
                begin
                r := Q_ftol( bl[0] );
                g := Q_ftol( bl[1] );
                b := Q_ftol( bl[2] );

                // catch negative lights
                if (r < 0)then r := 0;
                if (g < 0)then g := 0;
                if (b < 0)then b := 0;
                (*/*
                //** determine the brightest of the three color components
                //*/*)
                if (r > g)then max := r
                else
                max := g;
                if (b > max)then max := b;
                (*/*
                ** alpha is ONLY used for the mono lightmap case.  For this reason
                ** we set it to the brightest of the color components so that
                ** things don't get too dim.
                */*)

                a := max;

                (*/*
                ** rescale all the color components if the intensity of the greatest
                ** channel exceeds 1.0
                */*)
                if (max > 255)then
                        begin
                        t := 255 div max;

                        r := r*Round(t);
                        g := g*Round(t);
                        b := b*Round(t);
                        a := a*Round(t);
                        end;
                (*/*
                ** So if we are doing alpha lightmaps we need to set the R, G, and B
                ** components to 0 and we need to set alpha to 1-alpha.
                */*)
                case monolightmap of
                // TODO need to know the appropriate numbers to substitute the letters
                //
                // by {FAB}
                //'L' : begin end;
                {'I'} : begin r:=a ; g:=0; b:=0 ;end;
                {'C'} :begin
                     // try faking colored lighting
                     a := 255 - ((r+g+b) mod 3);
                     r :=r * (a mod 255);
                     g :=g * (a mod 255);
                     b :=b * (a mod 255);
                     end;
                 {'A'} : begin
                     r := 0; g :=0 ; b := 0;
                     a := 255 - a;
                     end ;
                  end;
                 dest[0] := r;
                 dest[1] := g;
                 dest[2] := b;
                 dest[3] := a;

                 bl :=bl + 3;
                 dest :=dest + 4;
             end;
          dest := dest + stride ;
        end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -