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

📄 cbsplevel.cpp

📁 一个DXD制作的读取QUAKE的BSP结构的Demo可以参考
💻 CPP
字号:
/*
   Class Name:

      CBspLevel.

   Created by:

      Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).

   Description:

      This class represents a Quake3 3D map that can be rendered in OpenGL
      or in Direct3D.
*/


#include<stdio.h>
#include<string.h>
#include"CBspLevel.h"


bool CBspLevel::LoadLevel(char *filename)
{
   stQ3Header Header;
   stQ3FileLump Lumps[iMaxLumpsOffset];

   // Open file.
   FILE *fp = fopen(filename, "rb");
   if(!fp) return false;

   // Read the header data and make sure this is a bsp level.
   fread(&Header, 1, sizeof(stQ3Header), fp);
   if(strcmp(Header.bspID, "IBSP.") != 0) return false;

   // Read all the lumps in the file.
	fread(&Lumps, iMaxLumpsOffset, sizeof(stQ3FileLump), fp);

   // Allocate data for the vertex points, triangles, textures, and lightmaps.
   m_totalVertices = Lumps[iVerticesOffset].length / sizeof(stQ3Vertex);
	m_vertices = new stQ3Vertex[m_totalVertices];

   m_totalFaces = Lumps[iFacesOffset].length / sizeof(stQ3Face);
	m_faces = new stQ3Face[m_totalFaces];

   m_totalTextures = Lumps[iTextureOffset].length / sizeof(stQ3Texture);
   m_textures = new stQ3Texture[m_totalTextures];

   m_totalLightMaps = Lumps[iLightMapsOffset].length / sizeof(stQ3LightMap);
   m_lightMaps = new stQ3LightMap[m_totalLightMaps];

   // Load the vertex data.
	fseek(fp, Lumps[iVerticesOffset].offset, SEEK_SET);
	fread(m_vertices, m_totalVertices, sizeof(stQ3Vertex), fp);

   // Load the triangle data.
   fseek(fp, Lumps[iFacesOffset].offset, SEEK_SET);
	fread(m_faces, m_totalFaces, sizeof(stQ3Face), fp);

   // Load the texture data.
	fseek(fp, Lumps[iTextureOffset].offset, SEEK_SET);
	fread(m_textures, m_totalTextures, sizeof(stQ3Texture), fp);

   // Load the lightmap data.
	fseek(fp, Lumps[iLightMapsOffset].offset, SEEK_SET);
	fread(m_lightMaps, m_totalLightMaps, sizeof(stQ3LightMap), fp);

   fclose(fp);
   return true;
}


void CBspLevel::SwapAxis()
{
   // Must swap the axis for OpenGL.
   for(int i = 0; i < m_totalVertices; i++)
	   {
		   float temp = m_vertices[i].vertex.y;
		   m_vertices[i].vertex.y = m_vertices[i].vertex.z;
		   m_vertices[i].vertex.z = -temp;

		   m_vertices[i].texCoord.tv *= -1;
	   }
}


void CBspLevel::Shutdown()
{
   if(m_lightMaps)
      {
         delete[] m_lightMaps;
         m_lightMaps = NULL;
      }

   if(m_textures)
      {
         delete[] m_textures;
         m_textures = NULL;
      }

   if(m_vertices)
      {
         delete[] m_vertices;
         m_vertices = NULL;
      }

   if(m_faces)
      {
         delete[] m_faces;
         m_faces = NULL;
      }

   m_totalLightMaps = 0;
   m_totalTextures = 0;
   m_totalVertices = 0;
   m_totalFaces = 0;
}


// Copyright December 2004
// All Rights Reserved!
// Allen Sherrod
// ProgrammingAce@UltimateGameProgramming.com
// www.UltimateGameProgramming.com

⌨️ 快捷键说明

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