private static ModelDocument Load(BinaryReader r) { var res = new ModelDocument(); var pos = r.BaseStream.Position; IModelReader reader = null; var magic = r.ReadUInt32(); if (magic == 0x54534449) { magic = r.ReadUInt32(); if (magic == 44 || magic == 48) reader = new HL2.MdlReader(); else if (magic == 10) reader = new HL1.MdlReader(); } else if (magic == 0x4F504449) reader = new Q1.MdlReader(); if (reader == null) throw new ApplicationException("Format is not supported"); r.BaseStream.Seek(pos, SeekOrigin.Begin); reader.ReadModel(r, res); return res; }
public void Convert(ModelDocument model, CIwResGroup group) { this.group = group; this.modelMesh = new CIwModel(); modelMesh.Name = model.Name; group.AddRes(new CIwTexture() { FilePath = "../textures/checkers.png" }); group.AddRes(modelMesh); var mesh = new CMesh(); mesh.Scale = 1.0f / scale; modelMesh.ModelBlocks.Add(mesh); writer = new ModelWriter(modelMesh); WriteSkeleton(model); WriteMaterials(model); //WriteMesh(model.Meshes[1]); foreach (var m in model.Meshes) { WriteMesh(m); //break; } WriteAnimations(model,modelMesh); }
private void WriteSkeleton(ModelDocument model) { foreach (var bone in model.Bones) FixBoneName(bone); foreach (var bone in model.Bones) WriteBone(bone); }
private void WriteMaterials(ModelDocument model) { var textures = new Dictionary<ModelTexture,bool>(); foreach (var mesh in model.Meshes) foreach (var face in mesh.Faces) if (!textures.ContainsKey(face.Texture)) { textures[face.Texture] = true; if (face.Texture is ModelEmbeddedTexture) group.AddRes(new CIwTexture() { FilePath = "../textures/" + face.Texture.Name + ".png", Bitmap = ((ModelEmbeddedTexture)face.Texture).Bitmap }); else group.AddRes(new CIwTexture() { FilePath = "../textures/" + face.Texture.Name+".png" }); group.AddRes(new CIwMaterial() { Texture0 = face.Texture.Name, Name = face.Texture.Name }); } }
private void WriteAnimations(ModelDocument model, CIwModel modelMesh) { if (model.Animations == null || model.Animations.Count == 0) return; modelMesh.Skin.Animations = new List<CIwAnim>(); foreach (var a in model.Animations) { var anim = new CIwAnim() { Name = a.Name, skeleton=modelMesh.Skin.skeleton }; if (a.Frames != null) foreach (var f in a.Frames) { var frame = new CIwAnimKeyFrame(); frame.time = f.Time; foreach (var b in f.Bones) { var bone = new CIwAnimKeyFrameBone(); bone.bone = b.Bone.Name; bone.pos = GetVec3(b.Position); bone.rot = GetQuat(b.Rotation); frame.bones.Add(bone); } anim.KeyFrames.Add(frame); } modelMesh.Skin.Animations.Add(anim); } }