コード例 #1
0
        public static GeometryModel ImportOBJModel(string FileName)
        {
            GeometryModel Model = LoadModel(FileName);


            return(Model);
        }
コード例 #2
0
        public virtual object Clone()
        {
            GeometryModel NewModel = new GeometryModel(this.Name);

            List <Vector3> NewVertexList = new List <Vector3>();

            for (int i = 0; i < VertexList.Count; i++)
            {
                NewVertexList.Add(new Vector3(VertexList[i].X, VertexList[i].Y, VertexList[i].Z));
            }
            List <Vector2> NewTexcoordList = new List <Vector2>();

            for (int i = 0; i < TexcoordList.Count; i++)
            {
                NewTexcoordList.Add(new Vector2(TexcoordList[i].X, TexcoordList[i].Y));
            }
            List <Vector3> NewNormalList = new List <Vector3>();

            for (int i = 0; i < NormalList.Count; i++)
            {
                NewNormalList.Add(new Vector3(NormalList[i].X, NormalList[i].Y, NormalList[i].Z));
            }
            Dictionary <string, string> NewTextureReferences = new Dictionary <string, string>();

            foreach (KeyValuePair <string, string> entry in Textures)
            {
                NewTextureReferences.Add(entry.Key, entry.Value);
            }

            NewModel.Name         = (string)Name.Clone();
            NewModel.VertexList   = NewVertexList;
            NewModel.TexcoordList = NewTexcoordList;
            NewModel.NormalList   = NewNormalList;

            NewModel.Textures    = NewTextureReferences;
            NewModel.Shader      = Shader.Clone();
            NewModel.DepthShader = DepthShader.Clone();
            return(NewModel);
        }
コード例 #3
0
        //TODO
        //Add Vertex Normal Smoothing

        public static GeometryModel LoadModel(String ModelName)
        {
            List <Vector3> VertexBuffer   = new List <Vector3>();
            List <Vector2> TexCoordBuffer = new List <Vector2>();
            List <Vector3> NormalBuffer   = new List <Vector3>();

            try
            {
                StreamReader file = new StreamReader(ModelName);

                string[] Lines = File.ReadAllLines(ModelName);

                List <Vector3> VertexLookupBuffer   = new List <Vector3>();
                List <Vector2> TexCoordLookupBuffer = new List <Vector2>();
                List <Vector3> NormalLookupBuffer   = new List <Vector3>();
                List <Vector3> SmoothNormalLookUp   = new List <Vector3>();

                for (int i = 0; i < Lines.Length; i++)
                {
                    if (Lines[i].StartsWith("v "))
                    {
                        String[] bits = Lines[i].Replace("v ", "").Split();
                        VertexLookupBuffer.Add(new Vector3(float.Parse(bits[0]), float.Parse(bits[1]), float.Parse(bits[2])));
                    }
                    else if (Lines[i].StartsWith("vt "))
                    {
                        String[] bits = Lines[i].Replace("vt ", "").Split();
                        TexCoordLookupBuffer.Add(new Vector2(float.Parse(bits[0]), 1.0f - float.Parse(bits[1])));
                    }
                    else if (Lines[i].StartsWith("vn "))
                    {
                        String[] bits = Lines[i].Replace("vn ", "").Split();
                        NormalLookupBuffer.Add(new Vector3(float.Parse(bits[0]), float.Parse(bits[1]), float.Parse(bits[2])));
                    }
                }

                /*
                 * for (int i = 0; i < Lines.Length; i++)
                 * {
                 *  if (Lines[i].StartsWith("v "))
                 *  {
                 *      String[] bits = Lines[i].Replace("v ", "").Split();
                 *      VertexLookupBuffer.Add(new Vector3(float.Parse(bits[0]), float.Parse(bits[1]), float.Parse(bits[2])));
                 *  }
                 *  else if (Lines[i].StartsWith("vt "))
                 *  {
                 *      TexCoordStartIndex = i;
                 *      break;
                 *  }
                 * }
                 *
                 * for (int i = TexCoordStartIndex; i < Lines.Length; i++)
                 * {
                 *  if (Lines[i].StartsWith("vt "))
                 *  {
                 *      String[] bits = Lines[i].Replace("vt ", "").Split();
                 *      TexCoordLookupBuffer.Add(new Vector2(float.Parse(bits[0]), 1.0f - float.Parse(bits[1])));
                 *  }
                 *  else if (Lines[i].StartsWith("vn "))
                 *  {
                 *      NormalStartIndex = i;
                 *      break;
                 *  }
                 * }
                 *
                 * for (int i = NormalStartIndex; i < Lines.Length; i++)
                 * {
                 *  if (Lines[i].StartsWith("vn "))
                 *  {
                 *      String[] bits = Lines[i].Replace("vn ", "").Split();
                 *      NormalLookupBuffer.Add(new Vector3(float.Parse(bits[0]), float.Parse(bits[1]), float.Parse(bits[2])));
                 *  }
                 *  else if (Lines[i].StartsWith("f "))
                 *  {
                 *      FaceStartIndex = i;
                 *      break;
                 *  }
                 * }
                 *
                 * //generate smooth normals
                 *
                 * for (int i = 0; i < VertexLookupBuffer.Count; i++)
                 * {
                 *  int NormalCount = 0;
                 *  Vector3 NormalSum = new Vector3();
                 *
                 *  for (int j = FaceStartIndex; j < Lines.Length; j++)
                 *  {
                 *      if (Lines[j].StartsWith("f "))
                 *      {
                 *          String[] bits = Lines[j].Replace("f ", "").Replace(" ", "/").Split('/');
                 *
                 *          int Index0 = int.Parse(bits[2]) - 1;
                 *          int Index1 = int.Parse(bits[5]) - 1;
                 *          int Index2 = int.Parse(bits[8]) - 1;
                 *
                 *          if (Index0 == i || Index1 == i || Index2 == i)
                 *          {
                 *              //Shares Vertex with lookup vertex
                 *              NormalSum += NormalLookupBuffer.ElementAt(int.Parse(bits[2]) - 1);
                 *              NormalCount++;
                 *          }
                 *      }
                 *  }
                 *  NormalSum /= NormalCount;
                 *  SmoothNormalLookUp.Add( NormalSum);
                 * }
                 */


                for (int i = 0; i < Lines.Length; i++)
                {
                    if (Lines[i].StartsWith("f "))
                    {
                        String[] bits = Lines[i].Replace("f ", "").Replace(" ", "/").Split('/');

                        for (int j = 0; j < bits.Length; j += 3)
                        {
                            VertexBuffer.Add(VertexLookupBuffer.ElementAt(int.Parse(bits[j]) - 1));
                            TexCoordBuffer.Add(TexCoordLookupBuffer.ElementAt(int.Parse(bits[j + 1]) - 1));
                            NormalBuffer.Add(NormalLookupBuffer.ElementAt(int.Parse(bits[j + 2]) - 1));
                        }
                    }
                }
            }
            catch (FileNotFoundException e)
            {
                Debug.WriteLine(e.Message);
                Debug.WriteLine("Model " + ModelName + " Not found");
            }

            //check if smooth normals

            //grab 3 normal values for polygon bias

            //search all


            GeometryModel Model = new GeometryModel();

            Model.VertexList   = VertexBuffer;
            Model.TexcoordList = TexCoordBuffer;
            Model.NormalList   = NormalBuffer;

            List <Vector3> Tangents = GraphicsUtil.GenerateTangents(VertexBuffer, TexCoordBuffer, NormalBuffer);

            return(Model);
        }