Пример #1
0
        public List<Pass.PassData> PaserObj(string objName)
        {
            List<Pass.PassData> datalist = new List<Pass.PassData>();
            Dictionary<string, Material> matDic = new Dictionary<string,Material>();

            foreach (string line in System.IO.File.ReadAllLines(objName))
            {
                if (line.StartsWith("mtllib"))
                {
                    string matLibName = line.Substring(7);
                    matDic = parseMat("media/" + matLibName);
                }
                else if (line.StartsWith("vt"))
                {
                    string[] tempAry = line.Split(new char[1] { ' ' });
                    tempAry = Array.FindAll<string>(tempAry, x =>
                        {
                            float temp;
                            return float.TryParse(x, out temp);
                        });
                    SlimDX.Vector2 uv = new SlimDX.Vector2();
                    uv.X = System.Convert.ToSingle(tempAry[0]);
                    uv.Y = 1.0f - System.Convert.ToSingle(tempAry[1]);

                    uvList.Add(uv);
                }
                else if (line.StartsWith("vn"))
                {
                    string[] tempAry = line.Split(new char[1] { ' ' });
                    tempAry = Array.FindAll<string>(tempAry, x =>
                        {
                            float temp;
                            return float.TryParse(x, out temp);
                        });
                    SlimDX.Vector4 normal = new SlimDX.Vector4();
                    normal.X = System.Convert.ToSingle(tempAry[0]);
                    normal.Y = System.Convert.ToSingle(tempAry[1]);
                    normal.Z = System.Convert.ToSingle(tempAry[2])-1.0f;

                    normalList.Add(normal);
                }
                else if (line.StartsWith("v"))
                {
                    string[] tempAry = line.Split(new char[1] { ' ' });
                    tempAry = Array.FindAll<string>(tempAry, x =>
                        {
                            float temp;
                            return float.TryParse(x, out temp);
                        });
                    SlimDX.Vector4 pos = new SlimDX.Vector4();
                    pos.X = System.Convert.ToSingle(tempAry[0]);
                    pos.Y = System.Convert.ToSingle(tempAry[1]);
                    pos.Z = System.Convert.ToSingle(tempAry[2])*(-1.0f);
                    pos.W = 1.0f;

                    posList.Add(pos);
                }
            }

            List<Vertex> vertexList = new List<Vertex>();
            List<int[]> triangleIndexList = new List<int[]>();
            Material m = null;
            Pass.PassData data = null;

            Dictionary<string, int> cache = new Dictionary<string, int>();

            int dataCount = 0;
            foreach (string line in System.IO.File.ReadAllLines(objName))
            {
                if (line.StartsWith("usemtl"))
                {
                    if (data != null)
                    {
                        data.triangleIndexs = triangleIndexList;
                        data.vertexs = vertexList.ToArray();
                        data.materail = m;
                        datalist.Add(data);
                        dataCount++;
                        System.Console.WriteLine(dataCount);
                    }
                    data = new Pass.PassData();
                    string[] tempAry = line.Split(new char[1] { ' ' });
                    string matName = tempAry[1];
                    m = matDic[matName];
                    vertexList = new List<Vertex>();
                    triangleIndexList = new List<int[]>();

                    cache.Clear();
                }
                else if (line.StartsWith("f"))
                {
                    string[] tempAry = line.Split(new char[1] { ' ' });

                    int[] indexs = new int[3];

                    for (int i=3; i>=1; i--)
                    {
                        string s = tempAry[i];
                        if (string.IsNullOrWhiteSpace(s) || string.IsNullOrEmpty(s))
                            continue;

                        string[] IndexAry = s.Split(new char[1] { '/' });
                        if (IndexAry.Length == 1)
                        {
                            int posIndex = Convert.ToInt32(IndexAry[0]) - 1;
                            string cacheString = "pos" + posIndex.ToString();
                            if (cache.ContainsKey(cacheString))
                            {
                                int index = cache[cacheString];
                                indexs[i] = index;
                            }
                            else
                            {
                                Vertex newVertex = new Vertex();
                                newVertex.pos = posList[posIndex];
                                vertexList.Add(newVertex);
                                indexs[i] = vertexList.Count - 1;
                                cache[cacheString] = vertexList.Count - 1;
                            }
                            //triangleIndexList.Add(indexs);
                        }
                        else if (IndexAry.Length == 2)
                        {
                            int posIndex = Convert.ToInt32(IndexAry[0]) - 1;
                            int uvIndex = Convert.ToInt32(IndexAry[1]) - 1;
                            string cacheString = "pos" + posIndex.ToString() + "uv" + uvIndex.ToString();
                            if (cache.ContainsKey(cacheString))
                            {
                                int index = cache[cacheString];
                                indexs[i] = index;
                            }
                            else
                            {
                                Vertex newVertex = new Vertex();
                                newVertex.pos = posList[posIndex];
                                newVertex.uv = uvList[uvIndex];
                                vertexList.Add(newVertex);
                                indexs[i] = vertexList.Count - 1;
                                cache[cacheString] = vertexList.Count - 1;
                            }
                            //triangleIndexList.Add(indexs);
                        }
                        else if (IndexAry.Length == 3)
                        {
                            int posIndex = Convert.ToInt32(IndexAry[0]) - 1;
                            int uvIndex = Convert.ToInt32(IndexAry[1]) - 1;
                            int normalIndex = Convert.ToInt32(IndexAry[2]) - 1;
                            string cacheString = "pos" + posIndex.ToString() + "uv" + uvIndex.ToString() + "normal"+normalIndex.ToString();
                            if (cache.ContainsKey(cacheString))
                            {
                                int index = cache[cacheString];
                                indexs[i-1] = index;
                            }
                            else
                            {
                                Vertex newVertex = new Vertex();
                                newVertex.pos = posList[posIndex];
                                newVertex.uv = uvList[uvIndex];
                                newVertex.normal = normalList[normalIndex];
                                vertexList.Add(newVertex);
                                indexs[i-1] = vertexList.Count - 1;
                                cache[cacheString] = vertexList.Count - 1;
                            }
                        }
                    }
                    int index1 = indexs[0];
                    int index2 = indexs[1];
                    int index3 = indexs[2];

                    indexs[0] = index3;
                    indexs[1] = index2;
                    indexs[2] = index1;
                    triangleIndexList.Add(indexs);
                }
            }

            if (data != null)
            {
                data.triangleIndexs = triangleIndexList;
                data.vertexs = vertexList.ToArray();
                data.materail = m;
                datalist.Add(data);
            }

            return datalist;
        }
Пример #2
0
        static void Main(string[] args)
        {
            int width = 1024;
            int height = 1024;
            float near = 1f;
            float far = 100.0f;

            SRDevice.Device.Init(width, height);

            Camera c = new Camera(near, far, (float)Math.PI/2,width, height);
            Culler cull = new Culler();
            SRDevice.Device.Camera = c;
            SRDevice.Device.Cull = cull;

            c.setLookAt(new Vector3(20, 20, 0), new Vector3(-0.1f, 0.5f, 1), new Vector3(0, 1, 0));

            Vertex[] vertexs;
            List<int[]> trianglesIndex;

            ObjPaser p = new ObjPaser();
            List<Pass.PassData> dataList = p.PaserObj("media/cube.obj");

            Model m1 = new Model(dataList);

            Vertex v1 = new Vertex();
            v1.pos = new Vector4(-0.5f, -0.5f, 11f, 0);
            v1.color = new Color4(1.0f, 0.5f, 0.5f, 0.5f);
            Vertex v2 = new Vertex();
            v2.pos = new Vector4(-0.5f, 0.5f, 11f, 0);
            v2.color = new Color4(1.0f, 0.5f, 0.5f, 0.5f);
            Vertex v3 = new Vertex();
            v3.pos = new Vector4(0.5f, -0.5f, 11f, 0);
            v3.color = new Color4(1.0f, 0.5f, 0.5f, 0.5f);
            Vertex v4 = new Vertex();
            v4.pos = new Vector4(0.5f, 0.5f, 11f, 0);
            v4.color = new Color4(1.0f, 0.5f, 0.5f, 0.5f);

            Pass.PassData data = new Pass.PassData();
            data.vertexs = new Vertex[4];
            data.vertexs[0] = v1;
            data.vertexs[1] = v2;
            data.vertexs[2] = v3;
            data.vertexs[3] = v4;

            data.triangleIndexs = new List<int[]>();
            int[] triangle1 = new int[3] { 0, 3, 2 };
            int[] triangle2 = new int[3] { 3, 0, 1 };
            data.triangleIndexs.Add(triangle1);
            data.triangleIndexs.Add(triangle2);

            data.materail = new Material();

            Model m2 = new Model(new List<Pass.PassData>() { data });
            m2.Render(Matrix.Invert(SRDevice.Device.Camera.getViewMatrix()));

            m1.transform.localPosition = new Vector4(0.0f, 0.0f, 10.0f, 0.0f);
            m1.transform.localScale = new Vector4(10.0f, 10.0f, 10.0f, 0.0f);
            //m1.transform.localRotation = new Vector4(0.0f, (float)Math.PI / 4, 0.0f, 0.0f);

            //m1.Render();
            SRDevice.Device.Present();
        }