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

📄 gl_rsurf.pas

📁 雷神之锤2(Quake2)Delphi源码
💻 PAS
📖 第 1 页 / 共 4 页
字号:
  begin
//Y    memcpy (fatvis, vis, (r_worldmodel.numleafs+7)/8);
    vis := Mod_ClusterPVS (r_viewcluster2, r_worldmodel);
    c := (r_worldmodel.numleafs+31) {/} DIV 32;
    for i:=0 to c-1 do
//Y{Y}      ((int * )fatvis)[i] |= ((int * )vis)[i];
    vis := @fatvis;
  end;

//Y{Y}  for (i=0,leaf=r_worldmodel->leafs ; i<r_worldmodel->numleafs ; i++, leaf++)
  begin
      cluster := leaf.cluster;
(*Y      if (cluster = -1) then
        Continue;
      if ( vis[cluster SHR 3] AND (1 SHL (cluster AND 7)) ) then*)
      begin
          node := mnode_p(leaf);
(*          do
          {
              if (node.visframe = r_visframecount) then
                Break;
              node.visframe := r_visframecount;
              node := node.parent;
          } while (node);*)
          repeat
            if (node.visframe = r_visframecount) then
              Break;
            node.visframe := r_visframecount;
            node := node.parent;
          until node=Nil;
      end;//if
  end;//for???

(*#if 0
	for (i=0 ; i<r_worldmodel->vis->numclusters ; i++)
	{
		if (vis[i>>3] & (1<<(i&7)))
		{
			node = (mnode_t * )&r_worldmodel->leafs[i];	// FIXME: cluster
			do
			{
				if (node->visframe == r_visframecount)
					break;
				node->visframe = r_visframecount;
				node = node->parent;
			} while (node);
		}
	}
#endif*)
end;//procedure



{*
=============================================================================

  LIGHTMAP ALLOCATION

=============================================================================
*}

procedure {static} LM_InitBlock;
begin
//Y   memset (gl_lms.allocated, 0, sizeof( gl_lms.allocated ));
end;//procedure

procedure {static} LM_UploadBlock (dynamic_ : qboolean);
var
  texture, i,
  height     : integer;
begin
  height := 0;

  if dynamic_
  then texture := 0
  else texture := gl_lms.current_lightmap_texture;

  GL_Bind (gl_state.lightmap_textures + texture);
  qglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  qglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  if dynamic_
  then begin
    for i:=0 to BLOCK_WIDTH-1 do
      if (gl_lms.allocated[i] > height) then
        height := gl_lms.allocated[i];

    qglTexSubImage2D (GL_TEXTURE_2D,
                      0,
                      0, 0,
                      BLOCK_WIDTH, height,
                      GL_LIGHTMAP_FORMAT,
                      GL_UNSIGNED_BYTE,
                      @gl_lms.lightmap_buffer);
  end
  else begin
    qglTexImage2D (GL_TEXTURE_2D,
                   0,
                   gl_lms.internal_format,
                   BLOCK_WIDTH, BLOCK_HEIGHT,
                   0,
                   GL_LIGHTMAP_FORMAT,
                   GL_UNSIGNED_BYTE,
                   @gl_lms.lightmap_buffer);
//    if ( ++gl_lms.current_lightmap_texture == MAX_LIGHTMAPS ) then
    Inc(gl_lms.current_lightmap_texture);
    if (gl_lms.current_lightmap_texture = MAX_LIGHTMAPS) then
      ri.Sys_Error (ERR_DROP, 'LM_UploadBlock() - MAX_LIGHTMAPS exceeded\n', []);
  end;
end;//procedure

// returns a texture number and the position inside it
function {static} LM_AllocBlock (w, h : integer; {int *x, int *y}var x, y : integer) : qboolean;
var
  i, j,
  best, best2 : integer;
begin
  best := BLOCK_HEIGHT;

  for i:=0 to BLOCK_WIDTH-w-1 do
  begin
    best2 := 0;

    for j:=0 to w-1 do
    begin
      if (gl_lms.allocated[i+j] >= best) then
        Break;
      if (gl_lms.allocated[i+j] > best2) then
        best2 := gl_lms.allocated[i+j];
    end;
    if (j = w) then
    begin
    	 // this is a valid spot
      {*}x := i;
//      {*}y := best = best2;
      best := best2;
      y := best;
    end;
  end;//for

  if (best + h > BLOCK_HEIGHT) then
  begin
    Result := false;
    Exit;
  end;

  for i:=0 to w-1 do
    gl_lms.allocated[{*}x + i] := best + h;

  Result := true;
end;//function

{*
================
GL_BuildPolygonFromSurface
================
*}
procedure GL_BuildPolygonFromSurface (fa : msurface_p); //for gl_model
var
  i, lindex,
  lnumverts,
  vertpage   : integer;
  pedges,
  r_pedge    : medge_p;

//	float		*vec;}
  vec : vec3_t;

  s, t       : float;
  poly       : glpoly_p;
  total      : vec3_t;
begin
// reconstruct the polygon
  pedges := @currentmodel.edges;
  lnumverts := fa.numedges;
  vertpage := 0;

  VectorClear (total);
  //
  // draw texture
  //
//Y  poly := Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
  poly.next := fa.polys;
  poly.flags := fa.flags;
  fa.polys := poly;
  poly.numverts := lnumverts;

  for i:=0 to lnumverts-1 do
  begin
    lindex := currentmodel.surfedges[fa.firstedge + i];

    if (lindex > 0)
    then begin
//Y      r_pedge := &pedges[lindex];
      vec := currentmodel.vertexes[r_pedge.v[0]].position;
    end
    else begin
//Y      r_pedge := &pedges[-lindex];
      vec := currentmodel.vertexes[r_pedge.v[1]].position;
    end;
(*Y    s := DotProduct (vec, fa.texinfo.vecs[0]) + fa.texinfo.vecs[0][3];
    s := s /fa.texinfo.image.width;

    t := DotProduct (vec, fa.texinfo.vecs[1]) + fa.texinfo.vecs[1][3];
    t := t /fa.texinfo.image.height;

    VectorAdd (total, vec, total);
    VectorCopy (vec, poly.verts[i]);*)
    poly.verts[i][3] := s;
    poly.verts[i][4] := t;

    //
    // lightmap texture coordinates
    //
//Y    s := DotProduct (vec, fa.texinfo.vecs[0]) + fa.texinfo.vecs[0][3];
    s := s -fa.texturemins[0];
    s := s +fa.light_s*16;
    s := s +8;
    s := s /BLOCK_WIDTH*16; //fa->texinfo->texture->width;

//Y    t := DotProduct (vec, fa.texinfo.vecs[1]) + fa.texinfo.vecs[1][3];
    t := t -fa.texturemins[1];
    t := t +fa.light_t*16;
    t := t +8;
    t := t /BLOCK_HEIGHT*16; //fa->texinfo->texture->height;

    poly.verts[i][5] := s;
    poly.verts[i][6] := t;
  end;//for

  poly.numverts := lnumverts;
end;//procedure

{*
========================
GL_CreateSurfaceLightmap
========================
*}
procedure GL_CreateSurfaceLightmap (surf : msurface_p); //for gl_model
var
  smax, tmax : integer;
  base : PByte;
begin
//  if (surf.flags & (SURF_DRAWSKY|SURF_DRAWTURB)) then
  if (surf.flags AND (SURF_DRAWSKY OR SURF_DRAWTURB) <> 0) then
    Exit;

  smax := (surf.extents[0] SHR 4)+1;
  tmax := (surf.extents[1] SHR 4)+1;

  if (NOT LM_AllocBlock (smax, tmax, surf.light_s, surf.light_t) ) then
  begin
    LM_UploadBlock (false);
    LM_InitBlock();
    if (NOT LM_AllocBlock (smax, tmax, surf.light_s, surf.light_t) ) then
      ri.Sys_Error (ERR_FATAL, 'Consecutive calls to LM_AllocBlock(%d,%d) failed\n', [smax, tmax]);
  end;

  surf.lightmaptexturenum := gl_lms.current_lightmap_texture;

  base := @gl_lms.lightmap_buffer;
  Inc(base, (surf.light_t * BLOCK_WIDTH + surf.light_s) * LIGHTMAP_BYTES);

(*Y  R_SetCacheState (surf);
  R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);*)
end;//procedure

{*
==================
GL_BeginBuildingLightmaps
==================
*}
var
  lightstyles : array[0..MAX_LIGHTSTYLES-1] of lightstyle_t;
procedure GL_BeginBuildingLightmaps (m : model_p); //for gl_model
//	static lightstyle_t	lightstyles[MAX_LIGHTSTYLES];
var
  i : integer;
  dummy : array [0..128*128-1] of integer; //array [0..128*128*4-1] of byte
begin
//Y  memset (gl_lms.allocated, 0, sizeof(gl_lms.allocated));

  r_framecount := 1;		// no dlightcache

  GL_EnableMultitexture (true);
//Y  GL_SelectTexture (GL_TEXTURE1_SGIS);

  {*
  ** setup the base lightstyles so the lightmaps won't have to be regenerated
  ** the first time they're seen
  *}
  for i:=0 to MAX_LIGHTSTYLES-1 do
  begin
    lightstyles[i].rgb[0] := 1;
    lightstyles[i].rgb[1] := 1;
    lightstyles[i].rgb[2] := 1;
    lightstyles[i].white  := 3;
  end;
//Y  r_newrefdef.lightstyles := lightstyles;

  if (gl_state.lightmap_textures=0) then
  begin
    gl_state.lightmap_textures := TEXNUM_LIGHTMAPS;
//idsoft		gl_state.lightmap_textures	= gl_state.texture_extension_number;
//idsoft		gl_state.texture_extension_number = gl_state.lightmap_textures + MAX_LIGHTMAPS;
  end;

  gl_lms.current_lightmap_texture := 1;

  {*
  ** if mono lightmaps are enabled and we want to use alpha
  ** blending (a,1-a) then we're likely running on a 3DLabs
  ** Permedia2.  In a perfect world we'd use a GL_ALPHA lightmap
  ** in order to conserve space and maximize bandwidth, however
  ** this isn't a perfect world.
  **
  ** So we have to use alpha lightmaps, but stored in GL_RGBA format,
  ** which means we only get 1/16th the color resolution we should when
  ** using alpha lightmaps.  If we find another board that supports
  ** only alpha lightmaps but that can at least support the GL_ALPHA
  ** format then we should change this code to use real alpha maps.
  *}
//  if ( toupper( gl_monolightmap.string[0] ) == 'A' )
  if UpCase (gl_monolightmap.string_[0]) = 'A'
  then begin
    gl_lms.internal_format := gl_tex_alpha_format;
  end
  {*
  ** try to do hacked colored lighting with a blended texture
  *}
  else
    if UpCase (gl_monolightmap.string_[0]) = 'C'
    then gl_lms.internal_format := gl_tex_alpha_format
    else
      if UpCase (gl_monolightmap.string_[0]) = 'I'
      then gl_lms.internal_format := GL_INTENSITY8
      else
        if UpCase (gl_monolightmap.string_[0]) = 'L'
        then gl_lms.internal_format := GL_LUMINANCE8
        else gl_lms.internal_format := gl_tex_solid_format;

  {*
  ** initialize the dynamic lightmap texture
  *}
  GL_Bind (gl_state.lightmap_textures + 0);
  qglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  qglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  qglTexImage2D (GL_TEXTURE_2D,
                 0,
                 gl_lms.internal_format,
                 BLOCK_WIDTH, BLOCK_HEIGHT,
                 0,
                 GL_LIGHTMAP_FORMAT,
                 GL_UNSIGNED_BYTE,
                 @dummy);
end;//procedure

{*
=======================
GL_EndBuildingLightmaps
=======================
*}
procedure GL_EndBuildingLightmaps; //for gl_model
begin
  LM_UploadBlock (false);
  GL_EnableMultitexture (false);
end;//procedure

// End of file
end.



My current problems:
--------------------
1) all local variable


sample cycle kind of:
template C-code:
  for ( _part1_ ; _part2_ ; _part3_ ) {...}

template PAS-code:
  _part1_;
  while (_part2_) do
  begin
    ...
    _part3_;
  end;

2) sample cycle kind of:
   C-code:
     float *v;
     v = p->verts[0];
     for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE) {...}

   PAS-code 1:
     v : Pfloat;
     v := p.verts[0];
     for i:=0 to p.numverts-1 do
     begin
       ...

       Inc(v, VERTEXSIZE); //This is EQUAL, but larger-code: v := v + VERTEXSIZE * sizeof(Pfloat);
     end;

   PAS-code 2: this is TRUE code (!!! & ???)
     v : Pfloat;
     for i:=0 to p.numverts-1 do
     begin
       v := p.verts[i];  //add info: gl_model.h (typedef struct gl_poly_s)

       ...
     end;

3.1) sample cycle kind of:
   C-code:
     for ( ; p != 0; p = p->chain ) {...}
   PAS-code:
     while p<>Nil do
     begin
       ...
       p := p.chain;
     end;
3.2) sample cycle kind of:
   C-code:
     for ( ; s ; s=s->texturechain) {...}
   PAS-code:
     while s<>Nil do
     begin
       ...
       s := s.texturechain;
     end;

4) sample cycle kind of:
   C-code:
     for ( surf = gl_lms.lightmap_surfaces[i]; surf != 0; surf = surf->lightmapchain ) {...}
   PAS-code:
     surf := gl_lms.lightmap_surfaces[i];
     while surf<>Nil do
     begin
       ...
       surf = surf.lightmapchain;
     end;

5) sample cycle kind of:
   C-code:
     for ( maps = 0; maps < MAXLIGHTMAPS && fa->styles[maps] != 255; maps++ ) {...}

   PAS-code 1:
     for maps:=0 to MAXLIGHTMAPS-1 do
     begin
       if (fa.styles[maps] <> 255) then
         Break;
       ...
     end;

   PAS-code 2:
     maps := 0;
     while (maps < MAXLIGHTMAPS) and (fa.styles[maps] <> 255) do
     begin
       ...
       Inc(maps);
     end;


6) Optimized code:
     A * (1.0/128.0) --> A * invert128;

7) OK!

⌨️ 快捷键说明

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