예제 #1
0
 public bool Equals(VerticeDataSet vert)
 {
     if (GenericMethods.estSize(this.position - vert.position) < 0.0001f &&
         GenericMethods.estSize(this.texture - vert.texture) < 0.0001f &&
         GenericMethods.estSize(this.normal - vert.normal) < 0.001f)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #2
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;
        }
예제 #3
0
 public bool Equals(VerticeDataSet vert)
 {
     if (GenericMethods.estSize(this.position - vert.position) < 0.0001f &&
         GenericMethods.estSize(this.texture - vert.texture) < 0.0001f &&
         GenericMethods.estSize(this.normal - vert.normal) < 0.001f)
         return true;
     else
         return false;
 }
예제 #4
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;
        }