public md2LOL(string fileName) { AnimationSpeed = 0.001f; animlist = new List <anim_t>(); anim_t a1 = new anim_t(); a1.first_frame = 0; a1.last_frame = 30; a1.fps = 30; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 31; a1.last_frame = 60; a1.fps = 30; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 61; a1.last_frame = 90; a1.fps = 30; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 91; a1.last_frame = 120; a1.fps = 30; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 121; a1.last_frame = 150; a1.fps = 30; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 151; a1.last_frame = 178; a1.fps = 30; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 181; a1.last_frame = 210; a1.fps = 30; animlist.Add(a1); LOLMD2AnimationNames = new List <string>(); LOLMD2AnimationNames.Add("Stand"); LOLMD2AnimationNames.Add("Attack1"); LOLMD2AnimationNames.Add("Attack2"); LOLMD2AnimationNames.Add("Run"); LOLMD2AnimationNames.Add("Death"); LOLMD2AnimationNames.Add("Spell1"); LOLMD2AnimationNames.Add("Spell2"); TranslationMatrix = new mat4(1); rotationMatrix = new mat4(1); scaleMatrix = new mat4(1); LoadModel(fileName); }
public md2(string fileName) { animlist = new List <anim_t>(); anim_t a1 = new anim_t(); a1.first_frame = 0; a1.last_frame = 39; a1.fps = 9; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 40; a1.last_frame = 45; a1.fps = 10; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 46; a1.last_frame = 53; a1.fps = 10; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 54; a1.last_frame = 57; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 58; a1.last_frame = 61; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 62; a1.last_frame = 65; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 66; a1.last_frame = 71; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 72; a1.last_frame = 83; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 84; a1.last_frame = 94; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 95; a1.last_frame = 111; a1.fps = 10; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 112; a1.last_frame = 122; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 123; a1.last_frame = 134; a1.fps = 6; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 135; a1.last_frame = 153; a1.fps = 10; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 154; a1.last_frame = 159; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 160; a1.last_frame = 168; a1.fps = 10; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 169; a1.last_frame = 172; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 173; a1.last_frame = 177; a1.fps = 5; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 178; a1.last_frame = 183; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 184; a1.last_frame = 189; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 190; a1.last_frame = 197; a1.fps = 7; animlist.Add(a1); a1 = new anim_t(); a1.first_frame = 198; a1.last_frame = 198; a1.fps = 5; animlist.Add(a1); MD2AnimationNames = new List <string>(); MD2AnimationNames.Add("Stand"); MD2AnimationNames.Add("Run"); MD2AnimationNames.Add("Attach"); MD2AnimationNames.Add("Pain_A"); MD2AnimationNames.Add("Pain_B"); MD2AnimationNames.Add("Pain_C"); MD2AnimationNames.Add("Jump"); MD2AnimationNames.Add("Flip"); MD2AnimationNames.Add("Salute"); MD2AnimationNames.Add("Fallback"); MD2AnimationNames.Add("Wave"); MD2AnimationNames.Add("Pointing"); MD2AnimationNames.Add("Crouch_stand"); MD2AnimationNames.Add("Crouch_walk"); MD2AnimationNames.Add("Crouch_attack"); MD2AnimationNames.Add("Crouch_pain"); MD2AnimationNames.Add("Crouch_death"); MD2AnimationNames.Add("Death_fallback"); MD2AnimationNames.Add("Death_fallforward"); MD2AnimationNames.Add("Death_fallbackslow"); MD2AnimationNames.Add("BOOM"); TranslationMatrix = new mat4(1); rotationMatrix = new mat4(1); scaleMatrix = new mat4(1); LoadModel(fileName); }
void LoadModel(string path, bool flip = true) { modelPath = path; modelName = Path.GetFileName(path); FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(stream); header = new md2_t(); header.ident = br.ReadInt32(); header.version = br.ReadInt32(); header.skinheight = br.ReadInt32(); header.skinwidth = br.ReadInt32(); header.framesize = br.ReadInt32(); header.num_skins = br.ReadInt32(); header.num_xyz = br.ReadInt32(); header.num_st = br.ReadInt32(); header.num_tris = br.ReadInt32(); header.num_glcmds = br.ReadInt32(); header.num_frames = br.ReadInt32(); header.ofs_skins = br.ReadInt32(); header.ofs_st = br.ReadInt32(); header.ofs_tris = br.ReadInt32(); header.ofs_frames = br.ReadInt32(); header.ofs_glcmds = br.ReadInt32(); header.ofs_end = br.ReadInt32(); anim_t a1 = new anim_t(); a1.first_frame = 0; a1.last_frame = header.num_frames; a1.fps = 9; animlist.Add(a1); MD2AnimationNames.Add("AllAnimations"); br.BaseStream.Seek(header.ofs_skins, SeekOrigin.Begin); skins = new List <string>(); for (int i = 0; i < header.num_skins; i++) { string s = new string(br.ReadChars(64)); //skins.Add(s); string t = ""; int j = 0; for (j = s.Length - 1; j >= 0; j--) { if (s[j] == '/') { break; } } j++; for (int o = j; o < s.Length; o++) { t += s[o]; if (s[o] == 'x' && s[o - 1] == 'c' && s[o - 2] == 'p') { break; } } skins.Add(t); } UVData = new List <vec2>(); br.BaseStream.Seek(header.ofs_st, SeekOrigin.Begin); for (int i = 0; i < header.num_st; i++) { vec2 uv = new vec2(); uv.x = ((float)br.ReadInt16()) / header.skinwidth; uv.y = ((float)br.ReadInt16()) / header.skinheight; UVData.Add(uv); } int x = header.num_xyz - header.num_st; if (x > 0) { for (int i = 0; i < x; i++) { UVData.Add(new vec2(0)); } } indices = new List <int>(); br.BaseStream.Seek(header.ofs_tris, SeekOrigin.Begin); for (int i = 0; i < header.num_tris; i++) { indices.Add(br.ReadUInt16()); indices.Add(br.ReadUInt16()); indices.Add(br.ReadUInt16()); br.ReadUInt16(); br.ReadUInt16(); br.ReadUInt16(); //loop on indices each 3 consecutive vertices compute normal } br.BaseStream.Seek(header.ofs_frames, SeekOrigin.Begin); vVertices = new List <List <vec3> >(); vNormalsindex = new List <List <int> >(); vNormals = new List <List <vec3> >(); for (int i = 0; i < header.num_frames; i++) { br.BaseStream.Seek((header.ofs_frames + (i * header.framesize)), SeekOrigin.Begin); vVertices.Add(new List <vec3>(header.num_xyz)); vNormalsindex.Add(new List <int>(header.num_xyz)); vNormals.Add(new List <vec3>(header.num_xyz)); vec3 scale = new vec3(); byte[] buffer; buffer = br.ReadBytes(4); scale.x = BitConverter.ToSingle(buffer, 0); buffer = br.ReadBytes(4); scale.y = BitConverter.ToSingle(buffer, 0); buffer = br.ReadBytes(4); scale.z = BitConverter.ToSingle(buffer, 0); vec3 trans = new vec3(); buffer = br.ReadBytes(4); trans.x = BitConverter.ToSingle(buffer, 0); buffer = br.ReadBytes(4); trans.y = BitConverter.ToSingle(buffer, 0); buffer = br.ReadBytes(4); trans.z = BitConverter.ToSingle(buffer, 0); string name = new string(br.ReadChars(16)); List <vertex_t> vts = new List <vertex_t>(); for (int j = 0; j < header.num_xyz; j++) { vertex_t vt = new vertex_t(); vt.v = br.ReadBytes(3); vt.lightnormalindex = br.ReadByte(); vts.Add(vt); } frame_t f = new frame_t(); f.scale = scale.to_array(); f.translate = trans.to_array(); f.name = name.ToCharArray(); f.verts = vts; for (int j = 0; j < header.num_xyz; j++) { vVertices[i].Add(new vec3(0)); vNormals[i].Add(new vec3(0)); vVertices[i][j] = new vec3() { x = f.translate[0] + ((float)f.verts[j].v[0]) * f.scale[0], y = f.translate[1] + ((float)f.verts[j].v[1]) * f.scale[1], z = f.translate[2] + ((float)f.verts[j].v[2]) * f.scale[2] }; vNormalsindex[i].Add(f.verts[j].lightnormalindex); } for (int j = 0; j < indices.Count - 3; j += 3) { vec3 n1 = vVertices[i][indices[j + 1]] - vVertices[i][indices[j]]; vec3 n2 = vVertices[i][indices[j + 2]] - vVertices[i][indices[j]]; vec3 normal = glm.cross(n1, n2); vNormals[i][indices[j]] = normal; vNormals[i][indices[j + 1]] = normal; vNormals[i][indices[j + 2]] = normal; } } Texture tex = null; string folder = Path.GetDirectoryName(path); if (skins.Count != 0) { tex = new Texture(folder + "\\" + skins[0] + ".png", 20, true); //if (tex.width == 0) //{ // int handle = FreeImage.FreeImage_Load(FIF.FIF_PCX, folder + "\\" + skins[0], 0); // FreeImage.FreeImage_Save(FIF.FIF_PNG, handle, folder + "\\" + skins[0] + ".png", 0); // FreeImage.FreeImage_Unload(handle); //} } if (tex == null || tex.height == 0) { if (skins.Count > 1) { tex = new Texture(skins[1], 20, true); texpath = skins[1]; } else { FileInfo fi = new FileInfo(path); string name = fi.Name; int index = fi.FullName.IndexOf(name, StringComparison.CurrentCulture); string filePath = ""; for (int i = 0; i < index; i++) { filePath += fi.FullName[i]; } string[] extensions = { "jpg", "jpeg", "png", "bmp", "tga" }; for (int i = 0; i < 4; i++) { string stry = filePath + name.Split('.')[0] + "." + extensions[i]; tex = new Texture(stry, 20, true); if (tex.width > 0) { texpath = filePath + name.Split('.')[0] + "." + extensions[i]; hasTex = 1; break; } } } } else { hasTex = 1; texpath = folder + "\\" + skins[0] + ".png"; } br.BaseStream.Seek(header.ofs_glcmds, SeekOrigin.Begin); List <int> cmds = new List <int>(); renderModes = new List <int>(); numRenderVertices = new List <int>(); anotherUV = new List <vec2>(); AvVertices = new List <List <vec3> >(); AvNormals = new List <List <vec3> >(); CvNormals = new List <vec3>(); CvVertices = new List <vec3>(); for (int j = 0; j < header.num_frames; j++) { AvVertices.Add(new List <vec3>()); AvNormals.Add(new List <vec3>()); } for (int i = 0; i < header.num_glcmds; i++) { cmds.Add(br.ReadInt32()); } int k = 0; while (true) { int action; if (cmds.Count > 0) { action = cmds[k]; if (action == 0) { break; } } else { action = 1; } int renderMode = action < 0 ? Gl.GL_TRIANGLE_FAN : Gl.GL_TRIANGLE_STRIP; int numVertices = action < 0 ? -action : action; k++; renderModes.Add(renderMode); numRenderVertices.Add(numVertices); for (int i = 0; i < numVertices; i++) { unsafe { int sd = cmds[k++]; int td = cmds[k++]; float s = *((float *)(&sd)); float t = *((float *)(&td)); if (flip) { t = 1 - t; } int vi = cmds[k++]; anotherUV.Add(new vec2(s, t)); for (int j = 0; j < header.num_frames; j++) { AvVertices[j].Add(vVertices[j][vi]); AvNormals[j].Add(vNormals[j][vi]); CvVertices.Add(new vec3(0, 0, 0)); CvNormals.Add(new vec3(0, 0, 0)); } } } } mVertices = vVertices[0]; m = new Model(); m.indices = indices; m.texture = tex; m.vertices = vVertices[0]; m.uvCoordinates = UVData; m.normals = vNormals[0]; vs = new float[AvVertices[0].Count * 3]; ns = new float[AvNormals[0].Count * 3]; if (AvVertices.Count > 0) { vertexBufferID = GPU.GenerateBuffer(AvVertices[0]); } if (AvNormals.Count > 0) { normalBufferID = GPU.GenerateBuffer(AvNormals[0]); } if (anotherUV.Count > 0) { uvBufferID = GPU.GenerateBuffer(anotherUV); } }