/// <summary> /// Mod_LoadVisibility /// </summary> static void LoadVisibility(ref lump_t l) { if (l.filelen == 0) { _LoadModel.visdata = null; return; } _LoadModel.visdata = new byte[l.filelen]; Buffer.BlockCopy(_ModBase, l.fileofs, _LoadModel.visdata, 0, l.filelen); }
/// <summary> /// Mod_LoadClipnodes /// </summary> static void LoadClipNodes(ref lump_t l) { if ((l.filelen % dclipnode_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dclipnode_t.SizeInBytes; dclipnode_t[] dest = new dclipnode_t[count]; _LoadModel.clipnodes = dest; _LoadModel.numclipnodes = count; hull_t hull = _LoadModel.hulls[1]; hull.clipnodes = dest; hull.firstclipnode = 0; hull.lastclipnode = count - 1; hull.planes = _LoadModel.planes; hull.clip_mins.X = -16; hull.clip_mins.Y = -16; hull.clip_mins.Z = -24; hull.clip_maxs.X = 16; hull.clip_maxs.Y = 16; hull.clip_maxs.Z = 32; hull = _LoadModel.hulls[2]; hull.clipnodes = dest; hull.firstclipnode = 0; hull.lastclipnode = count - 1; hull.planes = _LoadModel.planes; hull.clip_mins.X = -32; hull.clip_mins.Y = -32; hull.clip_mins.Z = -24; hull.clip_maxs.X = 32; hull.clip_maxs.Y = 32; hull.clip_maxs.Z = 64; for (int i = 0, offset = l.fileofs; i < count; i++, offset += dclipnode_t.SizeInBytes) { dclipnode_t src = Sys.BytesToStructure<dclipnode_t>(_ModBase, offset); dest[i].planenum = Common.LittleLong(src.planenum); // Uze: changed from LittleShort dest[i].children = new short[2]; dest[i].children[0] = Common.LittleShort(src.children[0]); dest[i].children[1] = Common.LittleShort(src.children[1]); } }
/// <summary> /// Mod_LoadTextures /// </summary> static void LoadTextures(ref lump_t l) { if (l.filelen == 0) { _LoadModel.textures = null; return; } dmiptexlump_t m = Sys.BytesToStructure<dmiptexlump_t>(_ModBase, l.fileofs);// (dmiptexlump_t *)(mod_base + l.fileofs); m.nummiptex = Common.LittleLong(m.nummiptex); int[] dataofs = new int[m.nummiptex]; Buffer.BlockCopy(_ModBase, l.fileofs + dmiptexlump_t.SizeInBytes, dataofs, 0, dataofs.Length * sizeof(int)); _LoadModel.numtextures = m.nummiptex; _LoadModel.textures = new texture_t[m.nummiptex]; // Hunk_AllocName (m->nummiptex * sizeof(*loadmodel->textures) , loadname); for (int i = 0; i < m.nummiptex; i++) { dataofs[i] = Common.LittleLong(dataofs[i]); if (dataofs[i] == -1) continue; int mtOffset = l.fileofs + dataofs[i]; miptex_t mt = Sys.BytesToStructure<miptex_t>(_ModBase, mtOffset); //mt = (miptex_t *)((byte *)m + m.dataofs[i]); mt.width = (uint)Common.LittleLong((int)mt.width); mt.height = (uint)Common.LittleLong((int)mt.height); for (int j = 0; j < BspFile.MIPLEVELS; j++) mt.offsets[j] = (uint)Common.LittleLong((int)mt.offsets[j]); if ((mt.width & 15) != 0 || (mt.height & 15) != 0) Sys.Error("Texture {0} is not 16 aligned", mt.name); int pixels = (int)(mt.width * mt.height / 64 * 85); texture_t tx = new texture_t();// Hunk_AllocName(sizeof(texture_t) + pixels, loadname); _LoadModel.textures[i] = tx; tx.name = Common.GetString(mt.name);// memcpy (tx->name, mt->name, sizeof(tx.name)); tx.width = mt.width; tx.height = mt.height; for (int j = 0; j < BspFile.MIPLEVELS; j++) tx.offsets[j] = (int)mt.offsets[j] - miptex_t.SizeInBytes; // the pixels immediately follow the structures tx.pixels = new byte[pixels]; Buffer.BlockCopy(_ModBase, mtOffset + miptex_t.SizeInBytes, tx.pixels, 0, pixels); if (tx.name != null && tx.name.StartsWith("sky"))// !Q_strncmp(mt->name,"sky",3)) Render.InitSky(tx); else { tx.gl_texturenum = Drawer.LoadTexture(tx.name, (int)tx.width, (int)tx.height, new ByteArraySegment(tx.pixels), true, false); } } // // sequence the animations // texture_t[] anims = new texture_t[10]; texture_t[] altanims = new texture_t[10]; for (int i = 0; i < m.nummiptex; i++) { texture_t tx = _LoadModel.textures[i]; if (tx == null || !tx.name.StartsWith("+"))// [0] != '+') continue; if (tx.anim_next != null) continue; // allready sequenced // find the number of frames in the animation Array.Clear(anims, 0, anims.Length); Array.Clear(altanims, 0, altanims.Length); int max = tx.name[1]; int altmax = 0; if (max >= 'a' && max <= 'z') max -= 'a' - 'A'; if (max >= '0' && max <= '9') { max -= '0'; altmax = 0; anims[max] = tx; max++; } else if (max >= 'A' && max <= 'J') { altmax = max - 'A'; max = 0; altanims[altmax] = tx; altmax++; } else Sys.Error("Bad animating texture {0}", tx.name); for (int j = i + 1; j < m.nummiptex; j++) { texture_t tx2 = _LoadModel.textures[j]; if (tx2 == null || !tx2.name.StartsWith("+"))// tx2->name[0] != '+') continue; if (String.Compare(tx2.name, 2, tx.name, 2, Math.Min(tx.name.Length, tx2.name.Length)) != 0)// strcmp (tx2->name+2, tx->name+2)) continue; int num = tx2.name[1]; if (num >= 'a' && num <= 'z') num -= 'a' - 'A'; if (num >= '0' && num <= '9') { num -= '0'; anims[num] = tx2; if (num + 1 > max) max = num + 1; } else if (num >= 'A' && num <= 'J') { num = num - 'A'; altanims[num] = tx2; if (num + 1 > altmax) altmax = num + 1; } else Sys.Error("Bad animating texture {0}", tx2.name); } // link them all together for (int j = 0; j < max; j++) { texture_t tx2 = anims[j]; if (tx2 == null) Sys.Error("Missing frame {0} of {1}", j, tx.name); tx2.anim_total = max * ANIM_CYCLE; tx2.anim_min = j * ANIM_CYCLE; tx2.anim_max = (j + 1) * ANIM_CYCLE; tx2.anim_next = anims[(j + 1) % max]; if (altmax != 0) tx2.alternate_anims = altanims[0]; } for (int j = 0; j < altmax; j++) { texture_t tx2 = altanims[j]; if (tx2 == null) Sys.Error("Missing frame {0} of {1}", j, tx2.name); tx2.anim_total = altmax * ANIM_CYCLE; tx2.anim_min = j * ANIM_CYCLE; tx2.anim_max = (j + 1) * ANIM_CYCLE; tx2.anim_next = altanims[(j + 1) % altmax]; if (max != 0) tx2.alternate_anims = anims[0]; } } }
/// <summary> /// Mod_LoadVertexes /// </summary> static void LoadVertexes(ref lump_t l) { if ((l.filelen % dvertex_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dvertex_t.SizeInBytes; mvertex_t[] verts = new mvertex_t[count]; _LoadModel.vertexes = verts; _LoadModel.numvertexes = count; for (int i = 0, offset = l.fileofs; i < count; i++, offset += dvertex_t.SizeInBytes) { dvertex_t src = Sys.BytesToStructure<dvertex_t>(_ModBase, offset); verts[i].position = Common.LittleVector3(src.point); } }
/// <summary> /// Mod_LoadSurfedges /// </summary> static void LoadSurfEdges(ref lump_t l) { if ((l.filelen % sizeof(int)) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / sizeof(int); int[] edges = new int[count]; _LoadModel.surfedges = edges; _LoadModel.numsurfedges = count; for (int i = 0, offset = l.fileofs; i < count; i++, offset += 4) { int src = BitConverter.ToInt32(_ModBase, offset); edges[i] = src; // Common.LittleLong(in[i]); } }
/// <summary> /// Mod_LoadTexinfo /// </summary> static void LoadTexInfo(ref lump_t l) { //in = (void *)(mod_base + l->fileofs); if ((l.filelen % texinfo_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / texinfo_t.SizeInBytes; mtexinfo_t[] infos = new mtexinfo_t[count]; // out = Hunk_AllocName ( count*sizeof(*out), loadname); for (int i = 0; i < infos.Length; i++) infos[i] = new mtexinfo_t(); _LoadModel.texinfo = infos; _LoadModel.numtexinfo = count; for (int i = 0; i < count; i++)//, in++, out++) { texinfo_t src = Sys.BytesToStructure<texinfo_t>(_ModBase, l.fileofs + i * texinfo_t.SizeInBytes); for (int j = 0; j < 2; j++) infos[i].vecs[j] = Common.LittleVector4(src.vecs, j * 4); float len1 = infos[i].vecs[0].Length; float len2 = infos[i].vecs[1].Length; len1 = (len1 + len2) / 2; if (len1 < 0.32) infos[i].mipadjust = 4; else if (len1 < 0.49) infos[i].mipadjust = 3; else if (len1 < 0.99) infos[i].mipadjust = 2; else infos[i].mipadjust = 1; int miptex = Common.LittleLong(src.miptex); infos[i].flags = Common.LittleLong(src.flags); if (_LoadModel.textures == null) { infos[i].texture = Render.NoTextureMip; // checkerboard texture infos[i].flags = 0; } else { if (miptex >= _LoadModel.numtextures) Sys.Error("miptex >= loadmodel->numtextures"); infos[i].texture = _LoadModel.textures[miptex]; if (infos[i].texture == null) { infos[i].texture = Render.NoTextureMip; // texture not found infos[i].flags = 0; } } } }
/// <summary> /// Mod_LoadPlanes /// </summary> static void LoadPlanes(ref lump_t l) { if ((l.filelen % dplane_t.SizeInBytes) != 0) Sys.Error ("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dplane_t.SizeInBytes; // Uze: Possible error! Why in original is out = Hunk_AllocName ( count*2*sizeof(*out), loadname)??? mplane_t[] planes = new mplane_t[count]; for (int i = 0; i < planes.Length; i++) planes[i] = new mplane_t(); _LoadModel.planes = planes; _LoadModel.numplanes = count; for (int i=0 ; i<count ; i++) { dplane_t src = Sys.BytesToStructure<dplane_t>(_ModBase, l.fileofs + i * dplane_t.SizeInBytes); int bits = 0; planes[i].normal = Common.LittleVector3(src.normal); if (planes[i].normal.X < 0) bits |= 1; if (planes[i].normal.Y < 0) bits |= 1 << 1; if (planes[i].normal.Z < 0) bits |= 1 << 2; planes[i].dist = Common.LittleFloat(src.dist); planes[i].type = (byte)Common.LittleLong(src.type); planes[i].signbits = (byte)bits; } }
/// <summary> /// Mod_LoadSubmodels /// </summary> static void LoadSubModels(ref lump_t l) { if ((l.filelen % dmodel_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dmodel_t.SizeInBytes; dmodel_t[] dest = new dmodel_t[count]; _LoadModel.submodels = dest; _LoadModel.numsubmodels = count; for (int i = 0, offset = l.fileofs; i < count; i++, offset += dmodel_t.SizeInBytes) { dmodel_t src = Sys.BytesToStructure<dmodel_t>(_ModBase, offset); dest[i].mins = new float[3]; dest[i].maxs = new float[3]; dest[i].origin = new float[3]; for (int j = 0; j < 3; j++) { // spread the mins / maxs by a pixel dest[i].mins[j] = Common.LittleFloat(src.mins[j]) - 1; dest[i].maxs[j] = Common.LittleFloat(src.maxs[j]) + 1; dest[i].origin[j] = Common.LittleFloat(src.origin[j]); } dest[i].headnode = new int[BspFile.MAX_MAP_HULLS]; for (int j = 0; j < BspFile.MAX_MAP_HULLS; j++) dest[i].headnode[j] = Common.LittleLong(src.headnode[j]); dest[i].visleafs = Common.LittleLong(src.visleafs); dest[i].firstface = Common.LittleLong(src.firstface); dest[i].numfaces = Common.LittleLong(src.numfaces); } }
/// <summary> /// Mod_LoadMarksurfaces /// </summary> static void LoadMarkSurfaces(ref lump_t l) { if ((l.filelen % sizeof(short)) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / sizeof(short); msurface_t[] dest = new msurface_t[count]; _LoadModel.marksurfaces = dest; _LoadModel.nummarksurfaces = count; for (int i = 0; i < count; i++) { int j = BitConverter.ToInt16(_ModBase, l.fileofs + i * sizeof(short)); if (j >= _LoadModel.numsurfaces) Sys.Error("Mod_ParseMarksurfaces: bad surface number"); dest[i] = _LoadModel.surfaces[j]; } }
/// <summary> /// Mod_LoadNodes /// </summary> static void LoadNodes(ref lump_t l) { if ((l.filelen % dnode_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dnode_t.SizeInBytes; mnode_t[] dest = new mnode_t[count]; for (int i = 0; i < dest.Length; i++) dest[i] = new mnode_t(); _LoadModel.nodes = dest; _LoadModel.numnodes = count; for (int i = 0, offset = l.fileofs; i < count; i++, offset += dnode_t.SizeInBytes) { dnode_t src = Sys.BytesToStructure<dnode_t>(_ModBase, offset); dest[i].mins.X = Common.LittleShort(src.mins[0]); dest[i].mins.Y = Common.LittleShort(src.mins[1]); dest[i].mins.Z = Common.LittleShort(src.mins[2]); dest[i].maxs.X = Common.LittleShort(src.maxs[0]); dest[i].maxs.Y = Common.LittleShort(src.maxs[1]); dest[i].maxs.Z = Common.LittleShort(src.maxs[2]); int p = Common.LittleLong(src.planenum); dest[i].plane = _LoadModel.planes[p]; dest[i].firstsurface = (ushort)Common.LittleShort((short)src.firstface); dest[i].numsurfaces = (ushort)Common.LittleShort((short)src.numfaces); for (int j = 0; j < 2; j++) { p = Common.LittleShort(src.children[j]); if (p >= 0) dest[i].children[j] = _LoadModel.nodes[p]; else dest[i].children[j] = _LoadModel.leafs[-1 - p]; } } SetParent(_LoadModel.nodes[0], null); // sets nodes and leafs }
/// <summary> /// Mod_LoadLighting /// </summary> static void LoadLighting(ref lump_t l) { if (l.filelen == 0) { _LoadModel.lightdata = null; return; } _LoadModel.lightdata = new byte[l.filelen]; // Hunk_AllocName(l->filelen, loadname); Buffer.BlockCopy(_ModBase, l.fileofs, _LoadModel.lightdata, 0, l.filelen); }
/// <summary> /// Mod_LoadLeafs /// </summary> static void LoadLeafs(ref lump_t l) { if ((l.filelen % dleaf_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dleaf_t.SizeInBytes; mleaf_t[] dest = new mleaf_t[count]; for (int i = 0; i < dest.Length; i++) dest[i] = new mleaf_t(); _LoadModel.leafs = dest; _LoadModel.numleafs = count; for (int i = 0, offset = l.fileofs; i < count; i++, offset += dleaf_t.SizeInBytes) { dleaf_t src = Sys.BytesToStructure<dleaf_t>(_ModBase, offset); dest[i].mins.X = Common.LittleShort(src.mins[0]); dest[i].mins.Y = Common.LittleShort(src.mins[1]); dest[i].mins.Z = Common.LittleShort(src.mins[2]); dest[i].maxs.X = Common.LittleShort(src.maxs[0]); dest[i].maxs.Y = Common.LittleShort(src.maxs[1]); dest[i].maxs.Z = Common.LittleShort(src.maxs[2]); int p = Common.LittleLong(src.contents); dest[i].contents = p; dest[i].marksurfaces = _LoadModel.marksurfaces; dest[i].firstmarksurface = Common.LittleShort((short)src.firstmarksurface); dest[i].nummarksurfaces = Common.LittleShort((short)src.nummarksurfaces); p = Common.LittleLong(src.visofs); if (p == -1) dest[i].compressed_vis = null; else { dest[i].compressed_vis = _LoadModel.visdata; // loadmodel->visdata + p; dest[i].visofs = p; } dest[i].efrags = null; for (int j = 0; j < 4; j++) dest[i].ambient_sound_level[j] = src.ambient_level[j]; // gl underwater warp // Uze: removed underwater warp as too ugly //if (dest[i].contents != Contents.CONTENTS_EMPTY) //{ // for (int j = 0; j < dest[i].nummarksurfaces; j++) // dest[i].marksurfaces[dest[i].firstmarksurface + j].flags |= Surf.SURF_UNDERWATER; //} } }
/// <summary> /// Mod_LoadFaces /// </summary> static void LoadFaces(ref lump_t l) { if ((l.filelen % dface_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dface_t.SizeInBytes; msurface_t[] dest = new msurface_t[count]; for (int i = 0; i < dest.Length; i++) dest[i] = new msurface_t(); _LoadModel.surfaces = dest; _LoadModel.numsurfaces = count; int offset = l.fileofs; for (int surfnum = 0; surfnum < count; surfnum++, offset += dface_t.SizeInBytes) { dface_t src = Sys.BytesToStructure<dface_t>(_ModBase, offset); dest[surfnum].firstedge = Common.LittleLong(src.firstedge); dest[surfnum].numedges = Common.LittleShort(src.numedges); dest[surfnum].flags = 0; int planenum = Common.LittleShort(src.planenum); int side = Common.LittleShort(src.side); if (side != 0) dest[surfnum].flags |= Surf.SURF_PLANEBACK; dest[surfnum].plane = _LoadModel.planes[planenum]; dest[surfnum].texinfo = _LoadModel.texinfo[Common.LittleShort(src.texinfo)]; CalcSurfaceExtents(dest[surfnum]); // lighting info for (int i = 0; i < BspFile.MAXLIGHTMAPS; i++) dest[surfnum].styles[i] = src.styles[i]; int i2 = Common.LittleLong(src.lightofs); if (i2 == -1) dest[surfnum].sample_base = null; else { dest[surfnum].sample_base = _LoadModel.lightdata; dest[surfnum].sampleofs = i2; } // set the drawing flags flag if (dest[surfnum].texinfo.texture.name != null) { if (dest[surfnum].texinfo.texture.name.StartsWith("sky")) // sky { dest[surfnum].flags |= (Surf.SURF_DRAWSKY | Surf.SURF_DRAWTILED); Render.SubdivideSurface(dest[surfnum]); // cut up polygon for warps continue; } if (dest[surfnum].texinfo.texture.name.StartsWith("*")) // turbulent { dest[surfnum].flags |= (Surf.SURF_DRAWTURB | Surf.SURF_DRAWTILED); for (int i = 0; i < 2; i++) { dest[surfnum].extents[i] = 16384; dest[surfnum].texturemins[i] = -8192; } Render.SubdivideSurface(dest[surfnum]); // cut up polygon for warps continue; } } } }
/// <summary> /// Mod_LoadEntities /// </summary> static void LoadEntities(ref lump_t l) { if (l.filelen == 0) { _LoadModel.entities = null; return; } _LoadModel.entities = Encoding.ASCII.GetString(_ModBase, l.fileofs, l.filelen); }
/// <summary> /// Mod_LoadEdges /// </summary> static void LoadEdges(ref lump_t l) { if ((l.filelen % dedge_t.SizeInBytes) != 0) Sys.Error("MOD_LoadBmodel: funny lump size in {0}", _LoadModel.name); int count = l.filelen / dedge_t.SizeInBytes; // Uze: Why count + 1 ????? medge_t[] edges = new medge_t[count]; // out = Hunk_AllocName ( (count + 1) * sizeof(*out), loadname); _LoadModel.edges = edges; _LoadModel.numedges = count; for (int i = 0, offset = l.fileofs; i < count; i++, offset += dedge_t.SizeInBytes) { dedge_t src = Sys.BytesToStructure<dedge_t>(_ModBase, offset); edges[i].v = new ushort[] { (ushort)Common.LittleShort((short)src.v[0]), (ushort)Common.LittleShort((short)src.v[1]) }; } }