示例#1
0
 public Face(Vertice ind1, Vertice ind2, Vertice ind3)
 {
     Vertice = new Vertice[3];
     Vertice[0] = ind1;
     Vertice[1] = ind2;
     Vertice[2] = ind3;
     // Log.e("VboCube",Vi+"/"+Ti+"/"+Ni);
 }
示例#2
0
        public void loadObj(Mesh target)
        {
            List<Vector3> positionVboDataList = new List<Vector3> { };
            List<Vector3> normalVboDataList = new List<Vector3> { };
            List<Vector2> textureVboDataList = new List<Vector2> { };
            List<Face> FaceList = new List<Face> { };
            List<Vertice> FpIndiceList = new List<Vertice> { };

            // Read the file and display it line by line.
            string line;
            System.IO.StreamReader file =
               new System.IO.StreamReader(target.pointer);
            while ((line = file.ReadLine()) != null)
            {
                string[] sline = line.Split(new string[]{" "},10,StringSplitOptions.None);

                if (sline[0] == "v")
                {
                    float X = float.Parse(sline[1], nfi);
                    float Y = float.Parse(sline[2], nfi);
                    float Z = float.Parse(sline[3], nfi);
                    positionVboDataList.Add(new Vector3(X, Y, Z));

                }

                if (sline[0] == "vn")
                {
                    float X = float.Parse(sline[1], nfi);
                    float Y = float.Parse(sline[2], nfi);
                    float Z = float.Parse(sline[3], nfi);
                    normalVboDataList.Add(new Vector3(X, Y, Z));

                }

                if (sline[0] == "vt")
                {
                    float X = float.Parse(sline[1], nfi);
                    float Y = 1-float.Parse(sline[2], nfi);
                    textureVboDataList.Add(new Vector2(X, Y));

                }

                if (sline[0] == "f")
                {
                    string[] segment = sline[1].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                    if (segment.Length == 3)
                    {
                        Vertice fp1 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[2].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp2 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[3].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp3 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        FaceList.Add(new Face(fp1, fp2, fp3));
                    }
                }
            }

            file.Close();

            target.positionVboDataList = positionVboDataList;
            target.normalVboDataList = normalVboDataList;
            target.textureVboDataList = textureVboDataList;
            target.FaceList = FaceList;

            parseFaceList(ref target, false);

            target.loaded = true;
            //Meshes[target.identifier] = target;

            generateVBO(ref target);
        }
示例#3
0
        public void parseFaceList(ref Mesh target, bool genNormal)
        {
            List <Vector3> positionVboDataList = target.positionVboDataList;
            List <Vector3> normalVboDataList   = target.normalVboDataList;
            List <Vector2> textureVboDataList  = target.textureVboDataList;

            float[][] boneWeightList = target.boneWeightList;
            int[][]   boneIdList     = target.boneIdList;

            int affBones = 0;

            if (boneWeightList != null)
            {
                affBones = boneWeightList.Length;
            }

            List <Face> faceList = target.FaceList;

            removeTemp(ref faceList);
            convertToTri(ref faceList);

            Vector3[] tmpnormalVboData   = new Vector3[normalVboDataList.Count];
            Vector3[] tmptangentVboData  = new Vector3[normalVboDataList.Count];
            Vector3[] normalPositionData = new Vector3[normalVboDataList.Count];
            Vector2[] normalUvData       = new Vector2[normalVboDataList.Count];

            List <Vector3> normalHelperList   = new List <Vector3> {
            };
            List <Vector3> tangentHelperList  = new List <Vector3> {
            };
            List <Vector2> normalUvHelperList = new List <Vector2> {
            };
            List <Vector3> positionHelperlist = new List <Vector3> {
            };

            int faceCount = faceList.Count;

            for (int i = 0; i < faceCount; i++)
            {
                // get all the information from Lists into Facelist
                Vector3[] vposition = new Vector3[3];
                Vector3[] vnormal   = new Vector3[3];
                Vector2[] vtexture  = new Vector2[3];
                for (int j = 0; j < 3; j++)
                {
                    vposition[j] = positionVboDataList[faceList[i].Vertice[j].Vi];
                    vnormal[j]   = normalVboDataList[faceList[i].Vertice[j].Ni];
                    vtexture[j]  = textureVboDataList[faceList[i].Vertice[j].Ti];

                    int id = i * 3 + j;

                    //FaceList[i].Vertice[j].position = vposition;
                    //FaceList[i].Vertice[j].normal = vnormal;
                    //FaceList[i].Vertice[j].texture = vtexture;
                }
                // calculating face normal and tangent
                Vector3 v1 = vposition[1] - vposition[0];
                Vector3 v2 = vposition[2] - vposition[0];

                Vector2 vtexture1 = vtexture[1] - vtexture[0];
                Vector2 vtexture2 = vtexture[2] - vtexture[0];

                Vector3 fnormal = Vector3.Cross(v1, v2);

                float s = 1f / (vtexture2.X - vtexture1.X * vtexture2.Y / vtexture1.Y);
                float r = 1f / (vtexture1.X - vtexture2.X * vtexture1.Y / vtexture2.Y);

                Vector3 tangent = Vector3.Normalize(r * v1 + s * v2);

                if (tangent == Vector3.Zero)
                {
                    gameWindow.log("tangent generation ERROR");
                }

                Face curFace = faceList[i];

                // finding out if normal/tangent can be smoothed
                for (int j = 0; j < 3; j++)
                {
                    Vertice curVert = curFace.Vertice[j];

                    // if Normal[Normalindice] has not been assigned a uv coordinate do so and set normal
                    if (normalUvData[curVert.Ni] == Vector2.Zero)
                    {
                        normalUvData[curVert.Ni]       = vtexture[j];
                        normalPositionData[curVert.Ni] = vposition[j];

                        tmpnormalVboData[curVert.Ni]  = fnormal;
                        tmptangentVboData[curVert.Ni] = tangent;
                    }
                    else
                    {
                        // if Normal[Normalindice] is of the same Uv and place simply add
                        if (normalUvData[curVert.Ni] == vtexture[j] && normalPositionData[curVert.Ni] == vposition[j])
                        {
                            tmpnormalVboData[curVert.Ni]  += fnormal;
                            tmptangentVboData[curVert.Ni] += tangent;
                        }
                        else
                        {
                            int helperCount = normalUvHelperList.Count;
                            for (int k = 0; k < helperCount; k++)
                            {
                                // if Normalhelper[Normalindice] is of the same Uv and position simply add
                                if (normalUvHelperList[k] == vtexture[j] && positionHelperlist[k] == vposition[j])
                                {
                                    tangentHelperList[k] += tangent;
                                    normalHelperList[k]  += fnormal;

                                    curVert.Normalihelper = k;
                                }
                            }
                            // if matching Normalhelper has not been found create new one
                            if (faceList[i].Vertice[j].Normalihelper == -1)
                            {
                                normalUvHelperList.Add(vtexture[j]);

                                tangentHelperList.Add(tangent);
                                normalHelperList.Add(fnormal);
                                positionHelperlist.Add(vposition[j]);
                                curVert.Normalihelper = normalUvHelperList.Count - 1;
                            }
                        }
                    }
                }
            }

            // put Faces into DataSets (so we can easyly compare them)
            List <VerticeDataSet> vertList = new List <VerticeDataSet> {
            };

            for (int i = 0; i < faceCount; i++)
            {
                Face curFace = faceList[i];
                for (int j = 0; j < 3; j++)
                {
                    Vertice oldVert = curFace.Vertice[j];

                    VerticeDataSet curVert = new VerticeDataSet();

                    curVert.position = positionVboDataList[oldVert.Vi];
                    curVert.normal   = normalVboDataList[oldVert.Ni];
                    if (oldVert.Normalihelper != -1)
                    {
                        if (genNormal)
                        {
                            curVert.normal = Vector3.Normalize(normalHelperList[oldVert.Normalihelper]); //-dont use calculated normal
                        }
                        curVert.tangent = Vector3.Normalize(tangentHelperList[oldVert.Normalihelper]);
                    }
                    else
                    {
                        if (genNormal)
                        {
                            curVert.normal = Vector3.Normalize(tmpnormalVboData[oldVert.Ni]); //-dont use calculated normal
                        }
                        curVert.tangent = Vector3.Normalize(tmptangentVboData[oldVert.Ni]);
                    }
                    if (affBones > 0)
                    {
                        curVert.boneWeight = new float[affBones];
                        curVert.boneId     = new int[affBones];
                    }

                    for (int k = 0; k < affBones; k++)
                    {
                        curVert.boneWeight[k] = boneWeightList[k][oldVert.Vi];
                        curVert.boneId[k]     = boneIdList[k][oldVert.Vi];
                    }
                    curVert.texture = textureVboDataList[oldVert.Ti];

                    vertList.Add(curVert);
                }
            }

            //Remove unneded verts
            int noVerts = vertList.Count;
            List <VerticeDataSet> newVertList = new List <VerticeDataSet> {
            };
            List <int> newIndiceList          = new List <int> {
            };

            for (int i = 0; i < noVerts; i++)
            {
                VerticeDataSet curVert         = vertList[i];
                int            curNewVertCount = newVertList.Count;
                int            index           = -1;

                for (int j = curNewVertCount - 1; j >= 0; j--)
                {
                    if (newVertList[j].Equals(curVert))
                    {
                        index = j;
                    }
                }
                if (index < 0)
                {
                    index = curNewVertCount;
                    newVertList.Add(curVert);
                }

                newIndiceList.Add(index);
            }

            //put Faces into Arrays
            int newIndiceCount = newIndiceList.Count;

            int[] indicesVboData = new int[newIndiceCount];

            int newVertCount = newVertList.Count;

            Vector3[] positionVboData = new Vector3[newVertCount];
            Vector3[] normalVboData   = new Vector3[newVertCount];
            Vector3[] tangentVboData  = new Vector3[newVertCount];
            Vector2[] textureVboData  = new Vector2[newVertCount];

            int[][]   boneIdVboData     = new int[affBones][];
            float[][] boneWeightVboData = new float[affBones][];

            gameWindow.log("removed Verts" + (noVerts - newVertCount));

            for (int i = 0; i < affBones; i++)
            {
                boneIdVboData[i]     = new int[newVertCount];
                boneWeightVboData[i] = new float[newVertCount];
            }

            for (int i = 0; i < newVertCount; i++)
            {
                VerticeDataSet curVert = newVertList[i];

                positionVboData[i] = curVert.position;
                normalVboData[i]   = curVert.normal;
                tangentVboData[i]  = curVert.tangent;
                textureVboData[i]  = curVert.texture;

                for (int k = 0; k < affBones; k++)
                {
                    boneWeightVboData[k][i] = curVert.boneWeight[k];
                    boneIdVboData[k][i]     = curVert.boneId[k];
                }
            }

            for (int i = 0; i < newIndiceCount; i++)
            {
                indicesVboData[i] = newIndiceList[i];
            }

            //calculate a bounding Sphere
            float sphere = 0;

            foreach (var vec in positionVboData)
            {
                float length = vec.Length;
                if (length > sphere)
                {
                    sphere = length;
                }
            }

            //deleting unneded
            target.positionVboDataList = null;
            target.normalVboDataList   = null;
            target.tangentVboData      = null;
            target.indicesVboData      = null;

            target.boneWeightList = null;
            target.boneIdList     = null;

            //returning mesh info ... DONE :D
            target.positionVboData = positionVboData;
            target.normalVboData   = normalVboData;
            target.tangentVboData  = tangentVboData;
            target.textureVboData  = textureVboData;
            target.indicesVboData  = indicesVboData;

            target.boneIdVboData     = new int[affBones][];
            target.boneWeightVboData = new float[affBones][];
            for (int i = 0; i < affBones; i++)
            {
                target.boneIdVboData[i]     = boneIdVboData[i];
                target.boneWeightVboData[i] = boneWeightVboData[i];
            }

            target.boundingSphere = sphere;
        }
示例#4
0
        public Face(int vCount, int position)
        {
            Vertice = new Vertice[vCount];
            this.position = position;

            for (int i = 0; i < vCount; i++)
                Vertice[i] = new Vertice();
        }
示例#5
0
        public void loadObj(Mesh target)
        {
            gameWindow.log("load Wavefront: " + target.pointer);
            List <Vector3> positionVboDataList = new List <Vector3> {
            };
            List <Vector3> normalVboDataList   = new List <Vector3> {
            };
            List <Vector2> textureVboDataList  = new List <Vector2> {
            };
            List <Face> FaceList        = new List <Face> {
            };
            List <Vertice> FpIndiceList = new List <Vertice> {
            };

            // Read the file and display it line by line.
            string line;

            System.IO.StreamReader file =
                new System.IO.StreamReader(target.pointer);

            while ((line = file.ReadLine()) != null)
            {
                string[] sline = line.Split(new string[] { " " }, 10, StringSplitOptions.None);

                if (sline[0] == "v")
                {
                    float X = float.Parse(sline[1], nfi);
                    float Y = float.Parse(sline[2], nfi);
                    float Z = float.Parse(sline[3], nfi);
                    positionVboDataList.Add(new Vector3(X, Y, Z));
                }

                if (sline[0] == "vn")
                {
                    float X = float.Parse(sline[1], nfi);
                    float Y = float.Parse(sline[2], nfi);
                    float Z = float.Parse(sline[3], nfi);
                    normalVboDataList.Add(new Vector3(X, Y, Z));
                }

                if (sline[0] == "vt")
                {
                    float X = float.Parse(sline[1], nfi);
                    float Y = 1 - float.Parse(sline[2], nfi);
                    textureVboDataList.Add(new Vector2(X, Y));
                }

                if (sline[0] == "f")
                {
                    string[] segment = sline[1].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                    if (segment.Length == 3)
                    {
                        Vertice fp1 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[2].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp2 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[3].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp3 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        FaceList.Add(new Face(fp1, fp2, fp3));
                    }
                    else if (segment.Length == 3)
                    {
                        Vertice fp1 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[2].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp2 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[3].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp3 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        segment = sline[4].Split(new string[] { "/" }, 10, StringSplitOptions.None);
                        Vertice fp4 = new Vertice(int.Parse(segment[0]) - 1, int.Parse(segment[1]) - 1, int.Parse(segment[2]) - 1);

                        FaceList.Add(new Face(fp1, fp2, fp3, fp4));
                    }
                }
            }

            file.Close();

            target.positionVboDataList = positionVboDataList;
            target.normalVboDataList   = normalVboDataList;
            target.textureVboDataList  = textureVboDataList;
            target.FaceList            = FaceList;

            parseFaceList(ref target, false);

            target.loaded = true;
            //Meshes[target.identifier] = target;

            generateVBO(ref target);

            if (target.identifier != -1)
            {
                meshes[target.identifier] = target;
            }
        }