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

📄 loadmap.c

📁 著名物理引擎Hawk的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	BSP.dareas = TagMalloc(lumpLength(LUMP_AREAS), TAG_LEVEL);
	BSP.numareas = copyLump (LUMP_AREAS, BSP.dareas, sizeof(darea_t));
	
	BSP.dareaportals = TagMalloc(lumpLength(LUMP_AREAPORTALS), TAG_LEVEL);
	BSP.numareaportals = copyLump (LUMP_AREAPORTALS, BSP.dareaportals, sizeof(dareaportal_t));

	BSP.dvisdata = TagMalloc(lumpLength(LUMP_VISIBILITY), TAG_LEVEL);
	BSP.visdatasize = copyLump (LUMP_VISIBILITY, BSP.dvisdata, 1);
	
	BSP.dlightdata = TagMalloc(lumpLength(LUMP_LIGHTING), TAG_LEVEL);
	BSP.lightdatasize = copyLump (LUMP_LIGHTING, BSP.dlightdata, 1);
	
	BSP.dentdata = TagMalloc(lumpLength(LUMP_ENTITIES), TAG_LEVEL);
	BSP.entdatasize = copyLump (LUMP_ENTITIES, BSP.dentdata, 1);

	BSP.dpop = TagMalloc(256, TAG_LEVEL);
	copyLump (LUMP_POP, BSP.dpop, 1);

	TempFree(header);		/* everything has been copied out */
		
/* swap everything */
	swapBSPFile ();
	return TRUE;
}

void loadPrepareTextures(void)
{
	int			i, j;
	TEXTURE		tex;

	/* load textures */
	/* convert and load textures */
	logf("Loading level textures\n");
	Level.textures = TagMalloc(BSP.numtexinfo * sizeof(Level.textures[0]), TAG_LEVEL);
	Level.ntextures = BSP.numtexinfo;
	for(i=0;i<BSP.numtexinfo;i++)
	{
		char	path[1024];
		texinfo_t *texture = &BSP.texinfo[i];
		
		/* check for loaded texture */
		for(j=0;j<i;j++)
		{
			if(!strcmp(BSP.texinfo[i].texture, BSP.texinfo[j].texture))
			{
				memcpy(&Level.textures[i], &Level.textures[j], sizeof(Level.textures[0]));
				Level.textures[i].type = texture->flags;
				break;
			}
		}
		if (j==i)
		{
			sprintf(path, "textures/%s.wal", BSP.texinfo[i].texture);
			tex.type = texture->flags;
			Level.textures[i].number = loadTexture(path, &tex);
			Level.textures[i].width = tex.width;
			Level.textures[i].height = tex.height;
			Level.textures[i].type = tex.type;
			Level.textures[i].ntextures = tex.ntextures;
			memcpy(&Level.textures[i].name, &texture->texture, 32);
		}
		
	}

	/* fix up texture animation data */
	for(i=0;i<BSP.numtexinfo;i++)
	{
		static TEXTURE *first = NULL;
		static int firstnumber;
		static ntextures = 0;
		texinfo_t *texture = &BSP.texinfo[i];

		if((texture->nexttexinfo>0) && (texture->nexttexinfo != firstnumber))
		{
			ntextures++;
			if(!first)
			{
				first = &Level.textures[i];
				firstnumber = i;
			}
		}
		else
		{
			if(first)
			{
				first->ntextures = ++ntextures;
				ntextures = 0;
				first = NULL;
			}
		}
	}
}

void loadSetLMCoords(SURFACE *surf, dface_t *face)
{
}

void loadPrepareSurfaces(void)
{
	int			i, j, totalpoints = 0;

	/* create surfaces from faces */
	/* allocate surfaces */
	logf("Creating surfaces and lightmaps\n");
	Level.nsurf = BSP.numfaces;
	Level.surf = TagMalloc(Level.nsurf * sizeof(Level.surf[0]), TAG_LEVEL);
	renTextPrint("Level.nsurf =  %d\n", Level.nsurf);
	/* allocate points */
	for(i=0;i<BSP.numfaces;i++)
		totalpoints += BSP.dfaces[i].numedges;
	Level.npoints = totalpoints;
	Level.points = TagMalloc(Level.npoints * sizeof(Level.points[0]), TAG_LEVEL);
	renTextPrint("Level.npoints =  %d\n", Level.npoints);
	renTextPrint("numvertexes = %d\n", BSP.numvertexes);
	/* allocate lightmap texture info */
	Level.nlighttex = Level.nsurf;
	Level.lighttex = malloc(Level.nlighttex * sizeof(Level.lighttex[0]));
	/* do the conversion */
	totalpoints = 0;
	for(i=0;i<BSP.numfaces;i++)
	{
		dface_t		*face = &BSP.dfaces[i];
		SURFACE		*surf = &Level.surf[i];
		dplane_t	*plane = &BSP.dplanes[face->planenum];
		int			first = face->firstedge;
		int			last = face->firstedge + face->numedges;
		int			usepoly;
		int			k;
		vec2_t		mins, maxs, start;
		
		usepoly = 0;
		surf->firstpoint = totalpoints;
		surf->numpoints = face->numedges;
		ClearBounds(surf->mins, surf->maxs);
		/* convert edges to points */
		for(j=first;j<last;j++)
		{
			int	sedge = BSP.dsurfedges[j];
			vec3_t		temp;
			
			if(sedge>0)
			{
				dedge_t		*edge = &BSP.dedges[sedge];
				dvertex_t	*vert = &BSP.dvertexes[0];
				
				VectorCopy(vert[edge->v[0]].point,
					Level.points[totalpoints].v);
				VectorCopy(vert[edge->v[0]].point,
					temp);
				AddPointToBounds(Level.points[totalpoints].v,
					surf->mins, surf->maxs);
				totalpoints++;
			}
			else
			{
				dedge_t		*edge = &BSP.dedges[-sedge];
				dvertex_t	*vert = &BSP.dvertexes[0];
				
				VectorCopy(vert[edge->v[1]].point,
					Level.points[totalpoints].v);
				VectorCopy(vert[edge->v[0]].point,
					temp);
				AddPointToBounds(Level.points[totalpoints].v,
					surf->mins, surf->maxs);
				usepoly++;
				totalpoints++;
			}
		}
		
		/* calculate the texture coords */
		surf->type = plane->type;
		memcpy(surf->name, BSP.texinfo[face->texinfo].texture, 32);
		surf->textype = BSP.texinfo[face->texinfo].flags;
		first = surf->firstpoint;
		last = first + surf->numpoints;
		for(j=first;j<last;j++)
		{
			texinfo_t	*texture = &BSP.texinfo[face->texinfo];
			TEXTURE		*tex = &Level.textures[face->texinfo];
			TPOINT		*point = &Level.points[j];
			float		s, t;
			
			s = DotProduct(point->v, texture->vecs[S]);
			s += texture->vecs[S][SHIFT];
			s /= tex->width;
			t = DotProduct (point->v, texture->vecs[T]);
			t += texture->vecs[T][SHIFT];
			t /= tex->height;
			point->t[S] = s;
			point->t[T] = t;
		}
		/* calculate the lightmap extents */

		mins[S] = mins[T] = 999999;
		maxs[S] = maxs[T] = -999999;
		first = surf->firstpoint;
		last = first + surf->numpoints;
		for(j=first;j<last;j++)
		{
			texinfo_t	*texture = &BSP.texinfo[face->texinfo];
			TPOINT		*point = &Level.points[j];
			vec3_t		p;
			float		val;
			
			VectorCopy (point->v, p);
			for (k=0;k<2;k++)
			{
				val = DotProduct (p, texture->vecs[k]) + texture->vecs[k][3];
				if (val < mins[k])
					mins[k] = val;
				if (val > maxs[k])
					maxs[k] = val;
			}
		}
		mins[S] = floor(mins[S]/16);
		maxs[S] = ceil(maxs[S]/16);
		start[S] = mins[S] * 16;
		Level.lighttex[i].width = (int)(maxs[S] - mins[S]) + 1;
		mins[T] = floor(mins[T]/16);
		maxs[T] = ceil(maxs[T]/16);
		start[T] = mins[T] * 16;
		Level.lighttex[i].height = (int)(maxs[T] - mins[T]) + 1;

		/* calculate the lightmap texture coords */
		first = surf->firstpoint;
		last = first + surf->numpoints;
		for(j=first;j<last;j++)
		{
			texinfo_t	*texture = &BSP.texinfo[face->texinfo];
			TPOINT		*point = &Level.points[j];
			vec3_t		p;
			float		s, t;
			float		width = Level.lighttex[i].width - 1;
			float		height = Level.lighttex[i].height - 1;
			
			VectorCopy (point->v, p);
			s = DotProduct(p, texture->vecs[S]) + texture->vecs[S][3];
			s -= start[S];
/*			s += (texture->vecs[S][SHIFT] - start[S]); */
			s /= width;
			s /= 16;
/*			s = s * (width - 0.5) / width + (1.0 - s) * (0.5 / width); */
			s = s * (width-1) / width + (0.5 / width);
			t = DotProduct(p, texture->vecs[T]) + texture->vecs[S][3];
			t -= start[T];
/*			t += (texture->vecs[T][SHIFT] - start[T]); */
			t /= height;
			t /= 16;
/*			t = t * (height - 0.5) / height + (1.0 - s) * (0.5 / height); */
			t = t * (height-1) / height + (0.5 / height);
			if(s < 0.0f)
				point->lm[S] = s;
			else
				point->lm[S] = s;
			if(t < 0.0f)
				point->lm[T] = t;
			else
				point->lm[T] = t;
		}
		
		/* other misc stuff */
		Level.lighttex[i].number = 0;
		surf->dist = plane->dist;
		VectorCopy(plane->normal, surf->normal);
		surf->texture = Level.textures[BSP.dfaces[i].texinfo].number;
		surf->ntextures = Level.textures[BSP.dfaces[i].texinfo].ntextures;
		surf->light = 0;
		if(surf->numpoints == 3)
		{
			surf->mode = G_TRIANGLES;
		}
		else
		{
			if(usepoly)
			{
				surf->mode = G_POLYGON;
			}
			else
			{
				surf->mode = G_TRIANGLE_FAN;
			}
		}
		surf->light = 0;
	}
}

int loadMap(char *filename)
{
	
	renTextPrint("Loading BSP file %s\n", filename);
	if(!loadBSPFile(filename))
		return FALSE;
	
	/* decompress vis lists */
	loadPrepareVis();
	
	loadPrepareTextures();

	loadPrepareSurfaces();
	
	Level.surflist = TagMalloc(Level.nsurf * 2 * sizeof(&Level.surflist[0]), TAG_LEVEL);
	
	return TRUE;
}

⌨️ 快捷键说明

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