private Texture LoadTexture(string filename, out string realname) { filename = filename.Substring(0, filename.Length - 4); Console.Write("Loading texture {0}", filename); realname = ""; foreach (string ext in possibleExtensions) { if (Q3FileSystem.WriteResourceToStream(filename + ext, msTexture)) { Texture texture; try { texture = TextureLoader.FromStream(d3dDevice, msTexture); } catch { break; } Console.WriteLine(ext + "\tsuccess"); realname = filename + ext; return(texture); } } Console.WriteLine("...\tfailed"); return(null); }
public Q3Map(string filename) { Q3BspTextureUnsafe [] textures_u; Q3BspPlaneUnsafe [] planes_u; Q3BspNodeUnsafe [] nodes_u; Q3BspLeafUnsafe [] leafs_u; Q3BspVertexUnsafe [] vertices_u; Q3BspFaceUnsafe [] faces_u; Q3BspLightMapUnsafe [] lightMaps_u; MemoryStream ms = new MemoryStream(); Q3FileSystem.WriteResourceToStream(filename, ms); bspHeader = ( Q3BspHeader )ReadStruct(ms, typeof(Q3BspHeader)); string m = bspHeader.Magic; if (bspHeader.Magic == "IBSP") { } else if (bspHeader.Magic == "FAKK") { } ReadEntities(ms); textures_u = ( Q3BspTextureUnsafe [] )ReadLump(ms, typeof(Q3BspTextureUnsafe), LumpType.Textures); planes_u = ( Q3BspPlaneUnsafe [] )ReadLump(ms, typeof(Q3BspPlaneUnsafe), LumpType.Planes); nodes_u = ( Q3BspNodeUnsafe [] )ReadLump(ms, typeof(Q3BspNodeUnsafe), LumpType.Nodes); leafs_u = ( Q3BspLeafUnsafe [] )ReadLump(ms, typeof(Q3BspLeafUnsafe), LumpType.Leafs); leafFaces = ( int [] )ReadLump(ms, typeof(int), LumpType.LeafFaces); leafBrushes = ( int [] )ReadLump(ms, typeof(int), LumpType.LeafBrushes); brushes = ( Q3BspBrush [] )ReadLump(ms, typeof(Q3BspBrush), LumpType.Brushes); brushSides = ( Q3BspBrushSide [] )ReadLump(ms, typeof(Q3BspBrushSide), LumpType.BrushSides); vertices_u = ( Q3BspVertexUnsafe [] )ReadLump(ms, typeof(Q3BspVertexUnsafe), LumpType.Vertices); meshVertices = ( int [] )ReadLump(ms, typeof(int), LumpType.MeshVertices); faces_u = ( Q3BspFaceUnsafe [] )ReadLump(ms, typeof(Q3BspFaceUnsafe), LumpType.Faces); lightMaps_u = ( Q3BspLightMapUnsafe [] )ReadLump(ms, typeof(Q3BspLightMapUnsafe), LumpType.LightMaps); ReadVisData(ms); textures = new Q3BspTexture [textures_u.Length]; planes = new Q3BspPlane [planes_u.Length]; nodes = new Q3BspNode [nodes_u.Length]; leafs = new Q3BspLeaf [leafs_u.Length]; vertices = new Q3BspVertex [vertices_u.Length]; faces = new Q3BspFace [faces_u.Length]; lightmaps = new Q3BspLightMap [lightMaps_u.Length]; int i = 0; for (i = 0; i < textures.Length; i++) { textures [i].FromUnsafe(textures_u [i]); } for (i = 0; i < planes.Length; i++) { planes [i].FromUnsafe(planes_u [i]); } for (i = 0; i < nodes.Length; i++) { nodes [i].FromUnsafe(nodes_u [i]); } for (i = 0; i < leafs.Length; i++) { leafs [i].FromUnsafe(leafs_u [i]); } for (i = 0; i < vertices.Length; i++) { vertices [i].FromUnsafe(vertices_u [i]); } for (i = 0; i < faces.Length; i++) { faces [i].FromUnsafe(faces_u [i], vertices); } for (i = 0; i < lightmaps.Length; i++) { lightmaps [i].FromUnsafe(lightMaps_u [i]); } visibleFaces = new int [faces.Length]; int lastSlash = filename.LastIndexOf('/') + 1; mapName = filename.Substring(lastSlash, filename.Length - lastSlash - 4); }
public Md3Model(string filename, Device d3dDevice, Q3ModelViewerForm parent) { this.d3dDevice = d3dDevice; this.parent = parent; int lastSlash = filename.LastIndexOf('/'); name = filename.Substring(lastSlash + 1); name = name.Substring(0, name.Length - 4); MemoryStream ms = new MemoryStream(); Q3FileSystem.WriteResourceToStream(filename, ms); header.FromUnsafe(( Md3HeaderUnsafe )ReadStruct(ms, typeof(Md3HeaderUnsafe))); // Frames ms.Position = header.framesStart; frames = new Md3Frame [header.numFrames]; for (int i = 0; i < header.numFrames; i++) { frames [i].FromUnsafe(( Md3FrameUnsafe )ReadStruct(ms, typeof(Md3FrameUnsafe))); } // Tags ms.Position = header.tagsStart; tags = new Md3Tag [header.numTags]; for (int i = 0; i < header.numTags; i++) { tags [i].FromUnsafe(( Md3TagUnsafe )ReadStruct(ms, typeof(Md3TagUnsafe))); } // Meshes int meshStart = header.meshesStart; ms.Position = meshStart; meshes = new Md3Mesh [header.numMeshes]; for (int i = 0; i < header.numMeshes; i++) { Md3Mesh md3Mesh = new Md3Mesh(); md3Mesh.FromUnsafe(( Md3MeshUnsafe )ReadStruct(ms, typeof(Md3MeshUnsafe))); // Mesh Textures ms.Position = meshStart + md3Mesh.texturesStart; md3Mesh.textures = new Md3Texture [md3Mesh.numTextures]; for (int j = 0; j < md3Mesh.numTextures; j++) { md3Mesh.textures [j].FromUnsafe(( Md3TextureUnsafe )ReadStruct(ms, typeof(Md3TextureUnsafe))); } // Mesh Faces ms.Position = meshStart + md3Mesh.facesStart; md3Mesh.faces = new Md3Face [md3Mesh.numFaces]; for (int j = 0; j < md3Mesh.numFaces; j++) { md3Mesh.faces [j].FromUnsafe(( Md3FaceUnsafe )ReadStruct(ms, typeof(Md3FaceUnsafe))); } // Mesh TexCoords ms.Position = meshStart + md3Mesh.texCoordsStart; md3Mesh.texCoords = new Md3TexCoord [md3Mesh.numVertices]; for (int j = 0; j < md3Mesh.numVertices; j++) { md3Mesh.texCoords [j] = ( Md3TexCoord )ReadStruct(ms, typeof(Md3TexCoord)); } // Vertices ms.Position = meshStart + md3Mesh.verticesStart; md3Mesh.vertices = new Md3PositionNormal [md3Mesh.numVertices]; for (int j = 0; j < md3Mesh.numVertices; j++) { md3Mesh.vertices [j].FromUnsafe(( Md3PositionNormalUnsafe )ReadStruct(ms, typeof(Md3PositionNormalUnsafe))); } meshes [i] = md3Mesh; ms.Position = meshStart + md3Mesh.size; meshStart = ( int )ms.Position; } // Load to dx mesh objects dxMeshes = new Mesh [meshes.Length]; radiuses = new float [meshes.Length]; centers = new Vector3 [meshes.Length]; for (int i = 0; i < meshes.Length; i++) { Md3Mesh md3Mesh = meshes [i]; Mesh mesh = new Mesh(md3Mesh.numFaces, md3Mesh.numVertices, MeshFlags.WriteOnly, CustomVertex.PositionNormalTextured.Format, d3dDevice); GraphicsStream vbStream = mesh.VertexBuffer.Lock(0, 0, LockFlags.Discard); for (int j = 0; j < md3Mesh.numVertices; j++) { Md3TexCoord texCoord = md3Mesh.texCoords [j]; vbStream.Write(new CustomVertex.PositionNormalTextured(md3Mesh.vertices [j].pos, Vector3.Empty, texCoord.u, texCoord.v)); } vbStream.Position = 0; radiuses [i] = Geometry.ComputeBoundingSphere(vbStream, md3Mesh.numVertices, CustomVertex.PositionNormalTextured.Format, out centers [i]); mesh.VertexBuffer.Unlock(); GraphicsStream ibStream = mesh.IndexBuffer.Lock(0, 0, LockFlags.Discard); for (int j = 0; j < md3Mesh.numFaces; j++) { Md3Face face = md3Mesh.faces [j]; ibStream.Write(( short )face.indices [0]); ibStream.Write(( short )face.indices [1]); ibStream.Write(( short )face.indices [2]); } mesh.IndexBuffer.Unlock(); dxMeshes [i] = mesh; // Load textures for (int j = 0; j < md3Mesh.numTextures; j++) { string realname = ""; Texture t = null; try { t = LoadTexture(md3Mesh.textures [j].name, out realname); } catch {} textures.Add(t != null ? t : parent.textureNotFound); realnames.Add(realname); } } if (header.numMeshes > 0) { totalCenter = centers [0]; totalRadius = radiuses [0]; for (int i = 1; i < centers.Length; i++) { totalCenter = (totalCenter + centers [i]) * 0.5f; totalRadius = ((totalCenter - centers [i]).Length() + totalRadius + radiuses [i]) / 2; } } }
private Texture LoadTexture(Q3BspTexture texInfo) { Console.Write("Loading texture {0}", texInfo.name); string saveDir = Path.Combine(@"D:\Temp\q3bw\", map.MapName); var encoders = DR.Imaging.ImageCodecInfo.GetImageEncoders(); var cmpRegex = new System.Text.RegularExpressions.Regex( "(" + string.Join("|", encoders.Select(e => string.Join("|", e.FilenameExtension.Split(new [] { "*.", ";" }, StringSplitOptions.RemoveEmptyEntries))).ToArray() ) + ")$", System.Text.RegularExpressions.RegexOptions.IgnoreCase); foreach (string ext in possibleExtensions) { if (Q3FileSystem.WriteResourceToStream(texInfo.name + ext, msTexture)) { Texture texture; try { texture = TextureLoader.FromStream(d3dDevice, msTexture); } catch { break; } Console.WriteLine(ext + "\tsuccess"); if (cmpRegex.IsMatch(ext)) { string texPkgPath = texInfo.name + ext; string texDir = Path.Combine(saveDir, Path.GetDirectoryName(texPkgPath)); /*try { * Directory.CreateDirectory ( texDir ); * DR.Bitmap bmp = new DR.Bitmap ( msTexture ); * * for ( int x = 0 ; x < bmp.Width ; x++ ) { * for ( int y = 0 ; y < bmp.Height ; y++ ) { * DR.Color c = bmp.GetPixel ( x, y ); * int l = ( c.R + c.G + c.B ) / 3; * * bmp.SetPixel ( x, y, DR.Color.FromArgb ( l, l, l ) ); * } * } * * string texPath = Path.Combine ( texDir, Path.GetFileNameWithoutExtension ( texInfo.name ) + ext ); * * DR.Imaging.ImageCodecInfo encoder = null; * * foreach ( var enc in encoders ) { * string extWoDot = ext.Substring ( 1 ).ToLower (); * * if ( enc.FilenameExtension.ToLower ().Contains ( extWoDot ) ) { * encoder = enc; * break; * } * } * //new DR.Imaging.EncoderParameters () * if ( encoder != null ) * bmp.Save ( texPath, encoder, null ); * } catch {}*/ } return(texture); } } Console.WriteLine("...\tfailed"); return(null); }