public static bool ParseLine(ref Buffer t, string ln, string str) { int typef = 0, typed = 0, i, ii; Cleanstring(ref ln); ln = ln.ToLower(); str = str.ToLower(); string[] splitln = ln.Split(new Char[] { ' ' }); string[] splitstr = str.Split(new Char[] { ' ' }); for (i = 0; i < str.Length - 1; i++) { if (str[i] == '%' && str[i + 1] == 'd') { typed++; } if (str[i] == '%' && str[i + 1] == 'f') { typef++; } } t.ibuffer = new int[typed]; t.fbuffer = new float[typef]; typef = 0; typed = 0; for (i = 0, ii = 0; i < splitstr.Length; i++, ii++) { if (string.Equals(splitstr[i], "%d")) //integer { t.ibuffer[typed] = (int)MathExt.GetFloat(splitln[ii]); typed++; } else if (string.Equals(splitstr[i], "%f")) //double { t.fbuffer[typef] = MathExt.GetFloat(splitln[ii]); typef++; } else if (string.Equals(splitstr[i], "%s")) //string { t.sbuffer = splitln[ii]; while (true) { int o; if (int.TryParse(splitln[ii + 1], out o)) { break; } else { t.sbuffer += " " + splitln[ii + 1]; ii++; } } } else if (!string.Equals(splitstr[i], splitln[i])) { return(false); } } return(true); }
public static Vector2 ParseUV(XmlElement XMLNode) { return(new Vector2( MathExt.GetFloat(XMLNode.GetAttribute("u")), MathExt.GetFloat(XMLNode.GetAttribute("v")) )); }
public static Vector3 ParseColor(XmlElement XMLNode) { return(new Vector3( MathExt.GetFloat(XMLNode.GetAttribute("r")), MathExt.GetFloat(XMLNode.GetAttribute("g")), MathExt.GetFloat(XMLNode.GetAttribute("b")))); }
public static Vector3 ParseFace(XmlElement XMLNode) { return(new Vector3( MathExt.GetFloat(XMLNode.GetAttribute("v1")), MathExt.GetFloat(XMLNode.GetAttribute("v2")), MathExt.GetFloat(XMLNode.GetAttribute("v3")))); }
public static Vector3 ParseVector3(XmlElement XMLNode) { return(new Vector3( MathExt.GetFloat(XMLNode.GetAttribute("x")), MathExt.GetFloat(XMLNode.GetAttribute("y")), MathExt.GetFloat(XMLNode.GetAttribute("z")) )); }
public static Vector3 ParseRotation(XmlElement XMLNode) { return(new Vector3( MathExt.GetFloat(XMLNode.GetAttribute("qx")), MathExt.GetFloat(XMLNode.GetAttribute("qy")), MathExt.GetFloat(XMLNode.GetAttribute("qz")) )); }
public static Quaternion ParseOrientation(XmlElement XMLNode) { Quaternion orientation = new Quaternion(); orientation.X = MathExt.GetFloat(XMLNode.GetAttribute("qx")); orientation.Y = MathExt.GetFloat(XMLNode.GetAttribute("qy")); orientation.Z = MathExt.GetFloat(XMLNode.GetAttribute("qz")); orientation.W = MathExt.GetFloat(XMLNode.GetAttribute("qw")); return(orientation); }
public static float GetAttribFloat(XmlElement XMLNode, String attrib, float defaultValue) { if (!string.IsNullOrEmpty(XMLNode.GetAttribute(attrib))) { return(MathExt.GetFloat(XMLNode.GetAttribute(attrib))); } else { return(defaultValue); } }
void LoadMesh(string fileName) { string data = null; try { using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName)) { // tiedosto muistiin data = file.ReadToEnd(); } } catch (Exception e) { Log.Error(e.ToString()); } // string[] lines = data.Split('\n'); int line = 0; while (true) { // new mesh if (lines[line].StartsWith("o")) { ObjMesh mesh = new ObjMesh(); mesh.Name = lines[line].Split(' ')[1]; while (true) { if (lines[line + 1].StartsWith("v ") == false) { break; } string[] vert = lines[line + 1].Split(' '); // [-1,1] -> [screenwidth, screenheight] float x = ((MathExt.GetFloat(vert[1]) + 1) * 0.5f) * Settings.Width; float y = MathExt.GetFloat(vert[2]); // not needed float z = Settings.Height - (((MathExt.GetFloat(vert[3]) + 1) * 0.5f) * Settings.Height); mesh.Vertices.Add(new Vector3(x, y, z)); line++; } // seuraava mesh if (lines[line + 1].StartsWith("o ")) { Meshes.Add(mesh); continue; } while (lines[line + 1].StartsWith("usemtl ") == false) { line++; } string[] mat = lines[line + 1].Split(' '); if (mat[1].StartsWith("_")) { mesh.UseMat = mat[1].Substring(1); } Meshes.Add(mesh); // luo texture jos meshillä on materiaali if (mesh.UseMat != "") { mesh.Tex = Texture2D.Load(Settings.TextureDir + mesh.UseMat); // lev ja kor float w = mesh.Vertices[0].X - mesh.Vertices[1].X; float h = mesh.Vertices[0].Z - mesh.Vertices[2].Z; // tuo on se koko millä se pitää piirtää esim 20x20 // mutta jos oikea kuva on 100x100, se pitää skaalata viidesosaks eli 0.2 // eli 20/100 = 0.2 eli w/img.w mesh.ScaleX = w / (float)mesh.Tex.Width; mesh.ScaleY = h / (float)mesh.Tex.Height; } if (mesh.Name.Contains("START")) { SX = mesh.Vertices[0].X; SY = mesh.Vertices[0].Z; } } line++; if (line == lines.Length) { break; } } }
/// <summary> /// lataa md5-animaatio. /// </summary> public override void LoadMD5Animation(string animName, string fileName) { if (fileName == null || fileName == "") { return; } Animation anim = new Animation(); anim.animName = animName; Buffer t = new Buffer(); MD5JointInfo[] jointInfos = null; MD5BaseFrameJoint[] baseFrame = null; float[] animFrameData = null; int numAnimatedComponents = 0; int frame_index; int i; using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName)) { string line; while ((line = file.ReadLine()) != null) { if (line == "") { continue; } // Read number of joints if (ParseLine(ref t, line, "numFrames %d")) { /* Allocate memory for skeleton frames and bounding boxes */ anim.numFrames = t.ibuffer[0]; if (anim.numFrames > 0) { anim.bboxes = new MD5BoundingBox[anim.numFrames]; } } if (ParseLine(ref t, line, "numJoints %d")) { /* Allocate memory for joints of each frame */ anim.numJoints = t.ibuffer[0]; if (anim.numJoints > 0) { /* Allocate temporary memory for building skeleton frames */ jointInfos = new MD5JointInfo[anim.numJoints]; baseFrame = new MD5BaseFrameJoint[anim.numJoints]; } anim.skelFrames = new MD5Joint[anim.numFrames, anim.numJoints]; } if (ParseLine(ref t, line, "frameRate %d")) { anim.frameRate = t.ibuffer[0]; } if (ParseLine(ref t, line, "numAnimatedComponents %d")) { numAnimatedComponents = t.ibuffer[0]; if (numAnimatedComponents > 0) { /* Allocate memory for animation frame data */ animFrameData = new float[numAnimatedComponents]; } } if (line.Equals("hierarchy {")) { for (i = 0; i < anim.numJoints; ++i) { /* Read whole line */ line = file.ReadLine(); Cleanstring(ref line); /* Read joint info */ ParseLine(ref t, line, "%s %d %d %d"); jointInfos[i].name = t.sbuffer; jointInfos[i].parent = t.ibuffer[0]; jointInfos[i].flags = t.ibuffer[1]; jointInfos[i].startIndex = t.ibuffer[2]; } } if (line.Equals("bounds {")) { for (i = 0; i < anim.numFrames; ++i) { /* Read whole line */ line = file.ReadLine(); Cleanstring(ref line); /* Read bounding box */ ParseLine(ref t, line, "( %f %f %f ) ( %f %f %f )"); anim.bboxes[i].min.X = t.fbuffer[0]; anim.bboxes[i].min.Y = t.fbuffer[1]; anim.bboxes[i].min.Z = t.fbuffer[2]; anim.bboxes[i].max.X = t.fbuffer[3]; anim.bboxes[i].max.Y = t.fbuffer[4]; anim.bboxes[i].max.Z = t.fbuffer[5]; } } if (line.Equals("baseframe {")) { for (i = 0; i < anim.numJoints; ++i) { /* Read whole line */ line = file.ReadLine(); Cleanstring(ref line); /* Read base frame joint */ ParseLine(ref t, line, "( %f %f %f ) ( %f %f %f )"); if (t.fbuffer.Length == 6) { baseFrame[i].pos.X = t.fbuffer[0]; baseFrame[i].pos.Y = t.fbuffer[1]; baseFrame[i].pos.Z = t.fbuffer[2]; baseFrame[i].orient.X = t.fbuffer[3]; baseFrame[i].orient.Y = t.fbuffer[4]; baseFrame[i].orient.Z = t.fbuffer[5]; /* Compute the w component */ QuaternionExt.ComputeW(ref baseFrame[i].orient); } } } if (ParseLine(ref t, line, "frame %d")) { frame_index = t.ibuffer[0]; /* Read frame data */ for (i = 0; i < numAnimatedComponents;) { line = file.ReadLine(); if (line[0] == '}') { break; } Cleanstring(ref line); string[] splt = line.Split(' '); for (int ww = 0; ww < splt.Length; ww++) { animFrameData[i++] = MathExt.GetFloat(splt[ww]); } } /* Build frame skeleton from the collected data */ BuildFrameSkeleton(ref jointInfos, ref baseFrame, ref animFrameData, frame_index, anim.numJoints, ref anim); } } anim.curFrame = 0; anim.nextFrame = 1; anim.lastTime = 0; anim.maxTime = 1.0f / anim.frameRate; /* Allocate memory for animated skeleton */ skeleton = new MD5Joint[anim.numJoints]; animated = true; Vector3 min = new Vector3(9999, 9999, 9999); Vector3 max = new Vector3(-9999, -9999, -9999); // laske bboxit for (int q = 0; q < anim.numFrames; q++) { if (anim.bboxes[q].min.X < min.X) { min.X = anim.bboxes[q].min.X; } if (anim.bboxes[q].min.Y < min.Y) { min.Y = anim.bboxes[q].min.Y; } if (anim.bboxes[q].min.Z < min.Z) { min.Z = anim.bboxes[q].min.Z; } if (anim.bboxes[q].max.X > max.X) { max.X = anim.bboxes[q].max.X; } if (anim.bboxes[q].max.Y > max.Y) { max.Y = anim.bboxes[q].max.Y; } if (anim.bboxes[q].max.Z > max.Z) { max.Z = anim.bboxes[q].max.Z; } } Boundings = new BoundingSphere(); Boundings.CreateBoundingVolume(this, min, max); Update(0); animations.Add(anim); Log.WriteLine("Animation: " + fileName, false); SetAnimation(animName); } }
void LoadMaterial(string fileName) { using (System.IO.StreamReader file = new System.IO.StreamReader(Settings.ModelDir + fileName)) { // tiedosto muistiin string data = file.ReadToEnd(); // pilko se string[] lines = data.Split('\n'); Material mat = new Material(); int curTexture = -1; for (int q = 0; q < lines.Length; q++) { string line = lines[q]; line = line.Trim('\r', '\t', ' '); if (line.StartsWith("//")) { continue; } string[] ln = line.Split(' '); // pilko datat if (ln[0] == "material") { curTexture = -1; mat = Material.CreateMaterial(ln[1]); Log.WriteLine("Material: " + mat.materialName, false); continue; } if (ln[0] == "shader") { mat.ShaderName = ln[1]; // ota shaderin nimi continue; } // lataa texture if (ln[0] == "texture") { curTexture++; if (ln[1] == "none") { continue; } mat.Textures[curTexture].Tex = Texture.Load(ln[1]); continue; } if (ln[0] == "tex_coord_set") { mat.Textures[curTexture].TexCoordSet = uint.Parse(ln[1]); continue; } if (ln[0] == "env_map") { if (ln[1] == "spherical") { mat.Textures[curTexture].EnvMap = TextureInfo.EnvMaps.Spherical; } else if (ln[1] == "cubic_reflection") { mat.Textures[curTexture].EnvMap = TextureInfo.EnvMaps.CubicReflection; } else { mat.Textures[curTexture].EnvMap = TextureInfo.EnvMaps.None; } continue; } // Ambient color if (ln[0] == "ambient") { mat.AmbientColor = new Vector4(MathExt.GetFloat(ln[1]), MathExt.GetFloat(ln[2]), MathExt.GetFloat(ln[3]), 1); continue; } // Diffuse color if (ln[0] == "diffuse") { mat.DiffuseColor = new Vector4(MathExt.GetFloat(ln[1]), MathExt.GetFloat(ln[2]), MathExt.GetFloat(ln[3]), 1); continue; } // Specular color if (ln[0] == "specular") { mat.SpecularColor = new Vector4(MathExt.GetFloat(ln[1]), MathExt.GetFloat(ln[2]), MathExt.GetFloat(ln[3]), MathExt.GetFloat(ln[4])); continue; } } } }