static void LoadNodesAndLeafs(BinaryReader br, Header header) { // Read nodes br.BaseStream.Seek(header.lumps[5].fileofs, SeekOrigin.Begin); int nNodes = header.lumps[5].filelen / 32; if (header.lumps[5].filelen % 32 > 0) { Common.Instance.Error("LoadNodesAndLeafs: Weird node lumpsize"); } world.nodes = new dnode_t[nNodes]; for (int i = 0; i < nNodes; i++) { dnode_t node = new dnode_t(); node.planenum = br.ReadInt32(); node.children = new int[] { br.ReadInt32(), br.ReadInt32() }; node.mins = SourceParser.SwapZY(new Vector3(br.ReadInt16(), br.ReadInt16(), br.ReadInt16())); // 3 For frustrum culling node.maxs = SourceParser.SwapZY(new Vector3(br.ReadInt16(), br.ReadInt16(), br.ReadInt16())); // 3 node.firstface = br.ReadUInt16(); // index into face array node.numfaces = br.ReadUInt16(); ; // counting both sides node.area = br.ReadInt16(); ; // If all leaves below this node are in the same area, then // this is the area index. If not, this is -1. node.paddding = br.ReadInt16(); ; // pad to 32 bytes length node.plane = world.planes[node.planenum]; world.nodes[i] = node; } // Determine size int leafSize = 56; if (header.version == 20 || header.version == 17) { if (header.lumps[10].filelen % 32 == 0) leafSize = 32; else System.Console.WriteLine("Problem reading leafs.."); } world.numLeafs = header.lumps[10].filelen / leafSize; world.leafs = new dleaf_t[world.numLeafs]; br.BaseStream.Seek(header.lumps[10].fileofs, SeekOrigin.Begin); for (int i = 0; i < world.numLeafs; i++) { // dleaf_t leaf = new dleaf_t(); leaf.contents = br.ReadInt32(); leaf.cluster = br.ReadInt16(); if (leaf.cluster > world.numClusters) world.numClusters = leaf.cluster + 1; ushort packed = br.ReadUInt16(); leaf.area = (short)((ushort)(packed << 7) >> 7); leaf.flags = (short)(packed >> 9); if (packed > 0) { int test = 2; } leaf.mins = SourceParser.SwapZY(new Vector3(br.ReadInt16(), br.ReadInt16(), br.ReadInt16())); // 3 For frustrum culling leaf.maxs = SourceParser.SwapZY(new Vector3(br.ReadInt16(), br.ReadInt16(), br.ReadInt16())); // 3 leaf.firstleafface = br.ReadUInt16(); leaf.numleaffaces = br.ReadUInt16(); leaf.firstleafbrush = br.ReadUInt16(); leaf.numleafbrushes = br.ReadUInt16(); leaf.leafWaterDataID = br.ReadInt16(); leaf.ambientLighting = new CompressedLightCube(); if (leafSize > 32) { leaf.ambientLighting.Color = new Vector3[6]; for (int j = 0; j < 6; j++) { RGBExp color = new RGBExp(); color.r = br.ReadByte(); color.g = br.ReadByte(); color.b = br.ReadByte(); color.exp = br.ReadSByte(); float r = SourceParser.TexLightToLinear((int)color.r, color.exp); float g = SourceParser.TexLightToLinear((int)color.g, color.exp); float b = SourceParser.TexLightToLinear((int)color.b, color.exp); leaf.ambientLighting.Color[j] = new Vector3(r, g, b); } } else { if (world.LightGrid != null && world.LightGrid.Length > i) leaf.ambientLighting = world.LightGrid[i]; } leaf.padding = br.ReadInt16(); leaf.staticProps = new List<SourceProp>(); world.leafs[i] = leaf; } }
static void LoadLightGrids(BinaryReader br, Header header) { // HDR Ambient light may be in lump for itself if (header.lumps[55].filelen > 0) { int nHDRAmbient = header.lumps[55].filelen / 24; CompressedLightCube[] HDRCubes = new CompressedLightCube[nHDRAmbient]; br.BaseStream.Seek(header.lumps[55].fileofs, SeekOrigin.Begin); for (int i = 0; i < nHDRAmbient; i++) { CompressedLightCube cube = new CompressedLightCube(); cube.Color = new Vector3[6]; for (int j = 0; j < 6; j++) { RGBExp color = new RGBExp(); color.r = br.ReadByte(); color.g = br.ReadByte(); color.b = br.ReadByte(); color.exp = br.ReadSByte(); float r = TexLightToLinear((int)color.r, color.exp) * 255; float g = TexLightToLinear((int)color.g, color.exp) * 255; float b = TexLightToLinear((int)color.b, color.exp) * 255; float exp = 128 + (int)color.exp; cube.Color[j] = new Vector3(r, g, b); } HDRCubes[i] = cube; } world.LightGrid = HDRCubes; } // LDR Ambient light may be in lump for itself else if (header.lumps[56].filelen > 0) { int nLDRAmbient = header.lumps[56].filelen / 24; CompressedLightCube[] LDRCubes = new CompressedLightCube[nLDRAmbient]; br.BaseStream.Seek(header.lumps[56].fileofs, SeekOrigin.Begin); for (int i = 0; i < nLDRAmbient; i++) { CompressedLightCube cube = new CompressedLightCube(); cube.Color = new Vector3[6]; for (int j = 0; j < 6; j++) { RGBExp color = new RGBExp(); color.r = br.ReadByte(); color.g = br.ReadByte(); color.b = br.ReadByte(); color.exp = br.ReadSByte(); float r = TexLightToLinear((int)color.r, color.exp) * 255; float g = TexLightToLinear((int)color.g, color.exp) * 255; float b = TexLightToLinear((int)color.b, color.exp) * 255; float exp = 128 + (int)color.exp; cube.Color[j] = new Vector3(r, g, b); } LDRCubes[i] = cube; } world.LightGrid = LDRCubes; } }
static void LoadLightmaps(BinaryReader br, Header header) { // Read Lightmaps br.BaseStream.Seek(header.lumps[8].fileofs, SeekOrigin.Begin); int nLightmaps = header.lumps[8].filelen / 4; Color3[] Lightmap = new Color3[nLightmaps]; RGBExp[] LightmapColors = new RGBExp[nLightmaps]; for (int i = 0; i < nLightmaps; i++) { LightmapColors[i] = new RGBExp(); LightmapColors[i].r = br.ReadByte(); LightmapColors[i].g = br.ReadByte(); LightmapColors[i].b = br.ReadByte(); LightmapColors[i].exp = br.ReadSByte() ; float r = TexLightToLinear((int)LightmapColors[i].r, LightmapColors[i].exp); float g = TexLightToLinear((int)LightmapColors[i].g, LightmapColors[i].exp); float b = TexLightToLinear((int)LightmapColors[i].b, LightmapColors[i].exp); float exp = 128 + (int)LightmapColors[i].exp; Lightmap[i] = new Color3(r, g, b); } world.LightData = Lightmap; }
void ReadLeafs(Header header, BinaryReader br) { // Determine size int leafSize = 56; if (header.version == 20 || header.version == 17) { if (header.lumps[10].filelen % 32 == 0) leafSize = 32; else System.Console.WriteLine("Problem reading leafs.."); } numLeafs = header.lumps[10].filelen / leafSize; leafs = new dleaf_t[numLeafs]; for (int i = 0; i < numLeafs; i++) { br.BaseStream.Seek(header.lumps[10].fileofs + (i * leafSize), SeekOrigin.Begin); dleaf_t leaf = new dleaf_t(); leaf.contents = br.ReadInt32(); leaf.cluster = br.ReadInt16(); if (leaf.cluster > numClusters) numClusters = leaf.cluster + 1; leaf.area = 0;//br.ReadInt16(); leaf.flags = br.ReadInt16(); leaf.mins = SourceParser.SwapZY(new Vector3(br.ReadInt16(), br.ReadInt16(), br.ReadInt16())); // 3 For frustrum culling leaf.maxs = SourceParser.SwapZY(new Vector3(br.ReadInt16(), br.ReadInt16(), br.ReadInt16())); // 3 leaf.firstleafface = br.ReadUInt16(); leaf.numleaffaces = br.ReadUInt16(); leaf.firstleafbrush = br.ReadUInt16(); leaf.numleafbrushes = br.ReadUInt16(); leaf.leafWaterDataID = br.ReadInt16(); leaf.ambientLighting = new CompressedLightCube(); if (leafSize > 32) { leaf.ambientLighting.Color = new Vector3[6]; for (int j = 0; j < 6; j++) { RGBExp color = new RGBExp(); color.r = br.ReadByte(); color.g = br.ReadByte(); color.b = br.ReadByte(); color.exp = br.ReadSByte(); float r = SourceParser.TexLightToLinear((int)color.r, color.exp ) * 255; float g = SourceParser.TexLightToLinear((int)color.g, color.exp ) * 255; float b = SourceParser.TexLightToLinear((int)color.b, color.exp ) * 255; leaf.ambientLighting.Color[j] = new Vector3(r, g, b); } } //else if (map.HDRCubes != null && i < map.HDRCubes.Length) //{ // leaf.ambientLighting = map.HDRCubes[i]; //} //else if (map.LDRCubes != null && i < map.LDRCubes.Length) //{ // leaf.ambientLighting = map.LDRCubes[i]; //} //else //{ // System.Console.WriteLine("No ambient cube for this leaf :("); //} leaf.padding = 0;//br.ReadInt16(); leaf.staticProps = new List<SourceProp>(); leafs[i] = leaf; } numAreas = 0; }