Exemple #1
0
        public void New4()
        {
            var v = new ObjVertex(2.0f, 3.0f, 4.0f, 5.0f);

            Assert.Equal(2.0f, v.Position.X);
            Assert.Equal(3.0f, v.Position.Y);
            Assert.Equal(4.0f, v.Position.Z);
            Assert.Equal(5.0f, v.Position.W);
            Assert.Null(v.Color);
        }
Exemple #2
0
 public ObjModel()
 {
     Vertices        = new List <ObjVertex>();
     ComputedNormals = new List <ObjNormal>();
     Normals         = new List <ObjNormal>();
     Textures        = new List <ObjTexture>();
     Objects         = new Dictionary <string, ObjObject>();
     Materials       = new Dictionary <string, ObjMaterial>();
     MaximumVertex   = new ObjVertex();
     MinimumVertex   = new ObjVertex();
 }
Exemple #3
0
        public void New()
        {
            var v = new ObjVertex
            {
                Position = new ObjVector4(2.0f, 3.0f, 4.0f, 5.0f),
                Color    = new ObjVector4(6.0f, 7.0f, 8.0f, 9.0f)
            };

            Assert.Equal(new ObjVector4(2.0f, 3.0f, 4.0f, 5.0f), v.Position);
            Assert.Equal(new ObjVector4(6.0f, 7.0f, 8.0f, 9.0f), v.Color);
        }
Exemple #4
0
        private void ParseVertex(string[] data)
        {
            var vert = new ObjVertex();

            vert.Position = ParseVector3(data, 1);
            if (data.Length >= 7)
            {
                vert.Color = ParseVector3(data, 4);
            }

            vertices.Add(vert);
        }
Exemple #5
0
        public void New7()
        {
            var v = new ObjVertex(2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f);

            Assert.Equal(2.0f, v.Position.X);
            Assert.Equal(3.0f, v.Position.Y);
            Assert.Equal(4.0f, v.Position.Z);
            Assert.Equal(1.0f, v.Position.W);
            Assert.Equal(5.0f, v.Color.Value.X);
            Assert.Equal(6.0f, v.Color.Value.Y);
            Assert.Equal(7.0f, v.Color.Value.Z);
            Assert.Equal(8.0f, v.Color.Value.W);
        }
Exemple #6
0
        /// <summary>
        /// Parses and loads a line from an OBJ file.
        /// Currently only supports V, VT, F and MTLLIB prefixes
        /// </summary>
        private void ProcessLine(string line)
        {
            string[] lineParts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (lineParts.Length > 0)
            {
                switch (lineParts[0])
                {
                case "usemtl":
                    UseMtl = lineParts[1];
                    break;

                case "mtllib":
                    Material = lineParts[1];
                    break;

                case "v":
                    ObjVertex v = new ObjVertex();
                    v.LoadFromStringArray(lineParts);
                    VertexList.Add(v);
                    v.Index = VertexList.Count();
                    break;

                case "f":
                    ObjFace f = new ObjFace();
                    f.LoadFromStringArray(lineParts);
                    f.UseMtl = UseMtl;
                    FaceList.Add(f);
                    break;

                case "vt":
                    ObjTextureVertex vt = new ObjTextureVertex();
                    vt.LoadFromStringArray(lineParts);
                    TextureList.Add(vt);
                    vt.Index = TextureList.Count();
                    break;
                }
            }
        }
Exemple #7
0
 public CollisionBounds(ObjVertex[] verts, float[] __)
 {
     Key = verts;
     Value = __;
 }
Exemple #8
0
    private static void TranslateModelToObj(string path)
    {
        string tempName    = path;
        int    counter     = 1;
        bool   nameChanged = false;

        if (File.Exists(tempName + ".obj"))
        {
            while (File.Exists($"{tempName}{counter}.obj"))
            {
                counter++;
            }
            nameChanged = true;
            tempName    = tempName + counter.ToString();
        }
        tempName += ".obj";
        int vertexIndex = 1;
        Dictionary <Vector3, ObjVertex> vertices  = new Dictionary <Vector3, ObjVertex>();
        HashSet <ObjTriangle>           triangles = new HashSet <ObjTriangle>();

        //get triangles from GPUMesh
        foreach (var l in LayerManager.Instance.layers)
        {
            foreach (var chunk in l.chunks)
            {
                if (chunk.voxels.Initialized)
                {
                    var tris = chunk.gpuMesh.GetTriangles();
                    for (int i = 0; i < tris.Length; i++)
                    {
                        ObjVertex[] tmp = new ObjVertex[3];
                        tmp[0].coord = chunk.ModelMatrix.MultiplyPoint(tris[i].vertexA);
                        tmp[1].coord = chunk.ModelMatrix.MultiplyPoint(tris[i].vertexB);
                        tmp[2].coord = chunk.ModelMatrix.MultiplyPoint(tris[i].vertexC);

                        tmp[0].coord.x = -tmp[0].coord.x;
                        tmp[1].coord.x = -tmp[1].coord.x;
                        tmp[2].coord.x = -tmp[2].coord.x;
                        var         currentNorm = Vector3.Cross(tmp[2].coord - tmp[0].coord, tmp[1].coord - tmp[0].coord);
                        ObjTriangle tri;
                        tri.verts = new int[3];
                        for (int j = 0; j < tmp.Length; j++)
                        {
                            if (vertices.ContainsKey(tmp[j].coord))
                            {
                                var v = vertices[tmp[j].coord];
                                v.normal              += currentNorm;
                                tri.verts[j]           = v.index;
                                vertices[tmp[j].coord] = v;
                            }
                            else
                            {
                                tmp[j].normal = currentNorm;
                                tmp[j].index  = vertexIndex;
                                vertices.Add(tmp[j].coord, tmp[j]);
                                tri.verts[j] = vertexIndex;
                                vertexIndex++;
                            }
                        }
                        triangles.Add(tri);
                    }
                }
            }
        }
        int normInd = 1;

        int[] normalsArray = new int[vertices.Count];
        Dictionary <float3, int> normalsDict = new Dictionary <float3, int>();

        //reduce unnecessary normal vectors
        foreach (var item in vertices)
        {
            float3 n = new float3()
            {
                coord = item.Value.normal
            };
            int realIndex;
            if (normalsDict.TryGetValue(n, out realIndex))
            {
                normalsArray[item.Value.index - 1] = realIndex;
            }
            else
            {
                normalsDict.Add(n, normInd);
                normalsArray[item.Value.index - 1] = normInd;
                normInd++;
            }
        }
        StreamWriter sw = new StreamWriter(tempName);

        sw.WriteLine("#AlabasterVR" + Environment.NewLine);

        sw.WriteLine("#vertices");
        StringBuilder stringBuilder = new StringBuilder();

        foreach (var item in vertices)
        {
            stringBuilder.AppendLine(item.Value.ToString());
        }
        sw.Write(stringBuilder.ToString());
        sw.WriteLine("#vertices count: " + vertices.Count + Environment.NewLine);

        sw.WriteLine("#vertex normals");
        stringBuilder = new StringBuilder();
        foreach (var item in normalsDict)
        {
            stringBuilder.AppendLine(item.Key.ToString());
        }
        sw.Write(stringBuilder.ToString());
        sw.WriteLine("#vertex normals count: " + normalsDict.Count + Environment.NewLine);

        sw.WriteLine("#faces");
        stringBuilder = new StringBuilder();
        foreach (var item in triangles)
        {
            stringBuilder.AppendLine(item.ToStringCustom(normalsArray[item.verts[0] - 1], normalsArray[item.verts[1] - 1], normalsArray[item.verts[2] - 1]));
        }
        sw.Write(stringBuilder.ToString());
        sw.WriteLine("#faces count: " + triangles.Count + Environment.NewLine);
        sw.Close();

        if (!nameChanged)
        {
            UIController.Instance.ShowMessageBox("Model exported to " + Path.GetFileName(tempName));
        }
        else
        {
            UIController.Instance.ShowMessageBox($"File {Path.GetFileName(path)}.obj already exists.\nModel exported to {Path.GetFileName(tempName)}.");
        }
    }
Exemple #9
0
    public bool testCollision(float x, float y, float z)
    {
        ObjVertex[] asarray = new ObjVertex[0];
        List<ObjVertex> currentArray = new List<ObjVertex>();

        if(collisionTree.Count == 0) {
        foreach(ObjTriangle e in triangles) {
            List<ObjVertex> usedverticies = new List<ObjVertex>();
        usedverticies.Add(vertices[e.Index0]);
            usedverticies.Add(vertices[e.Index1]);
                              usedverticies.Add(vertices[e.Index2]);
                currentArray.AddRange(usedverticies.ToArray());
        if(currentArray.Count>1) {
            foreach (ObjVertex vtx in currentArray)
            {
                vtx.Vertex = new Vector3(vtx.Vertex.X + translation.X, vtx.Vertex.Y + translation.Y, vtx.Vertex.Z + translation.Z);
            }
                 asarray = currentArray.ToArray();
                collisionTree.Add(new CollisionBounds(asarray,getBounds(asarray)));

                    currentArray.Clear();
                }
        }
        foreach (ObjVertex vtx in currentArray)
        {
            vtx.Vertex = new Vector3(vtx.Vertex.X + translation.X, vtx.Vertex.Y + translation.Y, vtx.Vertex.Z + translation.Z);
        }
            asarray = currentArray.ToArray();
                collisionTree.Add(new CollisionBounds(asarray,getBounds(asarray)));

                    currentArray.Clear();

            //Quads
            foreach(ObjQuad e in quads) {
            List<ObjVertex> usedverticies = new List<ObjVertex>();
        Vector3 transform = new Vector3(5,5,5);
            //Original verticies
            usedverticies.Add(vertices[e.Index0]);
            usedverticies.Add(vertices[e.Index1]);
            usedverticies.Add(vertices[e.Index2]);
            usedverticies.Add(vertices[e.Index3]);

            currentArray.AddRange(usedverticies.ToArray());
            if(currentArray.Count>1) {
                foreach (ObjVertex vtx in currentArray)
                {
                    vtx.Vertex = new Vector3(vtx.Vertex.X + translation.X, vtx.Vertex.Y + translation.Y, vtx.Vertex.Z + translation.Z);
                }
                 asarray = currentArray.ToArray();
                collisionTree.Add(new CollisionBounds(asarray,getBounds(asarray)));

                    currentArray.Clear();
                }

        }
            foreach (ObjVertex vtx in currentArray)
            {
                vtx.Vertex = new Vector3(vtx.Vertex.X + translation.X, vtx.Vertex.Y + translation.Y, vtx.Vertex.Z + translation.Z);
            }
        asarray = currentArray.ToArray();
                collisionTree.Add(new CollisionBounds(asarray,getBounds(asarray)));

                    currentArray.Clear();
        }
        foreach(CollisionBounds et in collisionTree) {
        //minx, miny, minz, maxx,maxy,maxz
            if(x>et.Value[0] & y>et.Value[1] & z>et.Value[2] & x<et.Value[3] & y<et.Value[4]&z<et.Value[5]) {
            if(testCollisionTriangle(et.Key,new Vector3(x,y,z))) {
                return true;
                }
            }
        }
        return false;
            //End quads
    }
Exemple #10
0
    bool testCollisionTriangle(ObjVertex[] verti, Vector3 coord)
    {
        Vector3 max = new Vector3(-5000,-5000,-5000);
        Vector3 min = new Vector3(5000,5000,5000);

        foreach(ObjVertex et in verti) {
            ObjVertex e = new ObjVertex();
            e.Vertex.X = et.Vertex.X*-1;
            e.Vertex.Y = et.Vertex.Y*-1;
            e.Vertex.Z = et.Vertex.Z*-1;
        if(e.Vertex.X<min.X) {
            min.X = e.Vertex.X;
            }
            if(e.Vertex.X>max.X) {
            max.X = e.Vertex.X;
            }
            if(e.Vertex.Y<min.Y) {
            min.Y = e.Vertex.Y;
            }
            if(e.Vertex.Y>max.Y) {
            max.Y = e.Vertex.Y;
            }
            if(e.Vertex.Z>max.Z) {
            max.Z = e.Vertex.Z;
            }
            if(e.Vertex.Z<min.Z) {
            min.Z = e.Vertex.Z;
            }

        }
           Vector3 sval = new Vector3(.9f,.9f,.9f);

        min-=sval;
        max+=sval;

        if(coord.X<max.X& coord.X>min.X&coord.Y>min.Y&coord.Y<max.Y&coord.Z>min.Z&coord.Z<max.Z) {
        return true;

        } else {

        return false;
        }
    }
Exemple #11
0
    /// <summary>
    ///Gets the bounds of this mesh 
    /// </summary>
    /// <returns>
    /// A <see cref="System.Single[]"/>
    /// </returns>
    public float[] getBounds()
    {
        float minx = 5000000;
                float miny = 5000000;
                float minz = 5000000;
                float maxx = -5000000;
                float maxy = -5000000;
                float maxz = -5000000;

        //Experimental - Simulate GPU processing of verticies in triangles/quads
        Console.WriteLine("Parsed "+quads.Length+" quads");
        for(int i = 0;i<quads.Length;i++) {

            ObjQuad et = quads[i];
        ObjVertex e = vertices[et.Index0];
        ObjVertex ey = new ObjVertex();

            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
            e = ey;
        if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }
            e = vertices[et.Index1];
            ey = new ObjVertex();
            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
        e = ey;
            if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }
            e = vertices[et.Index2];
        ey = new ObjVertex();
            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
            e = ey;
        if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }
            e = vertices[et.Index3];
        ey = new ObjVertex();

            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
        e = ey;
            if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }

        }
        //And do the same for each triangle
        foreach(ObjTriangle et in triangles) {
        ObjVertex e = vertices[et.Index0];
        ObjVertex ey = new ObjVertex();
            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
        e = ey;
            if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }
            e = vertices[et.Index1];
        ey = new ObjVertex();
            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
            e = ey;
        if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }
            e = vertices[et.Index2];
        ey = new ObjVertex();
            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
        e = ey;
            if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }

            e = vertices[et.Index0];
        ey = new ObjVertex();
            ey.Vertex.X = e.Vertex.X*-1;
            ey.Vertex.Y = e.Vertex.Y*-1;
            ey.Vertex.Z = e.Vertex.Z*-1;
            e = ey;
        if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }

        }
        minx-=1;
        miny-=1;
        minz-=1;
        maxx+=1;
        maxy+=1;
        maxz+=1;
        #if DEBUG
                Console.WriteLine("MinY: "+miny.ToString());
        #endif
        #if DEBUG
                Console.WriteLine("MaxY: "+maxy.ToString());
        #endif
        return new float[] {minx,miny,minz,maxx,maxy,maxz};
    }
Exemple #12
0
    public float[] getBounds(ObjVertex[] verts)
    {
        float minx = 5000000;
                float miny = 5000000;
                float minz = 5000000;
                float maxx = -5000000;
                float maxy = -5000000;
                float maxz = -5000000;

        //Experimental - Simulate GPU processing of verticies in triangles/quads
        ObjVertex ey;
        ObjVertex e;
        foreach(ObjVertex et in verts) {
        ey = new ObjVertex();
            ey.Vertex.X = et.Vertex.X*-1;
            ey.Vertex.Y = et.Vertex.Y*-1;
            ey.Vertex.Z = et.Vertex.Z*-1;
        e = ey;
            if(e.Vertex.X<minx) {
            minx = e.Vertex.X;
            }
            if(e.Vertex.X>maxx) {
            maxx = e.Vertex.X;
            }
            if(e.Vertex.Y<miny) {
            miny = e.Vertex.Y;

            }
            if(e.Vertex.Y>maxy) {
                maxy = e.Vertex.Y;

            }
            if(e.Vertex.Z<minz) {
            minz = e.Vertex.Z;
            }
            if(e.Vertex.Z>maxz) {
            maxz = e.Vertex.Z;
            }
        }
        minx-=1;
        miny-=1;
        minz-=1;
        maxx+=1;
        maxy+=1;
        maxz+=1;
        #if DEBUG
                Console.WriteLine("MinY: "+miny.ToString());
        #endif
        #if DEBUG
                Console.WriteLine("MaxY: "+maxy.ToString());
        #endif
        return new float[] {minx,miny,minz,maxx,maxy,maxz};
    }
Exemple #13
0
 /// <summary>
 /// Writes a vertex out to the file.
 /// </summary>
 /// <param name="vertex">The vertex to write.</param>
 private void WriteVertex(ObjVertex vertex)
 {
     _writer.WriteLine("v {0} {1} {2}", vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
     _writer.WriteLine("vn {0} {1} {2}", vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z);
     _writer.WriteLine("vt {0} {1}", vertex.TexCoords.X, 1 - vertex.TexCoords.Y);
 }
Exemple #14
0
    public static ObjMesh deserialize(BinaryReader mreader)
    {
        ObjMesh mymesh = new ObjMesh();
        bool hasimage = mreader.ReadBoolean();
        if(hasimage) {
        int len = (int)mreader.ReadInt64();
            byte[] image = mreader.ReadBytes(len);
            MemoryStream mstream = new MemoryStream();
            mstream.Write(image,0,image.Length);
            mstream.Position = 0;
            try
            {
                mymesh.Material = new Bitmap(mstream);
            }
            catch (Exception)
            {

            }
        }

        mymesh.name = mreader.ReadString();
        mymesh.quads = new ObjMesh.ObjQuad[mreader.ReadInt32()];
        for(int i = 0;i<mymesh.quads.Length;i++) {
        ObjQuad currentQuad = new ObjQuad();
            currentQuad.Index0 = mreader.ReadInt32();
            currentQuad.Index1 = mreader.ReadInt32();
            currentQuad.Index2 = mreader.ReadInt32();
            currentQuad.Index3 = mreader.ReadInt32();
            mymesh.quads[i] = currentQuad;
        }
        mymesh.triangles = new ObjMesh.ObjTriangle[mreader.ReadInt32()];
        for(int i = 0;i<mymesh.triangles.Length;i++) {
        ObjTriangle currentQuad = new ObjTriangle();
            currentQuad.Index0 = mreader.ReadInt32();
            currentQuad.Index1 = mreader.ReadInt32();
            currentQuad.Index2 = mreader.ReadInt32();
            mymesh.triangles[i] = currentQuad;
        }
        mymesh.vertices = new ObjMesh.ObjVertex[mreader.ReadInt32()];
        for(int i = 0;i<mymesh.vertices.Length;i++) {
        ObjVertex mtex = new ObjVertex();
            mtex.Normal.X = mreader.ReadSingle();
            mtex.Normal.Y = mreader.ReadSingle();
            mtex.Normal.Z = mreader.ReadSingle();
            mtex.TexCoord.X = mreader.ReadSingle();
            mtex.TexCoord.Y = mreader.ReadSingle();
            mtex.Vertex.X = mreader.ReadSingle();
            mtex.Vertex.Y = mreader.ReadSingle();
            mtex.Vertex.Z = mreader.ReadSingle();
            mymesh.vertices[i] = mtex;
        }

        return mymesh;
    }
Exemple #15
0
 public ObjVertex(ObjVertex obj, Vector3 tangent) : this(obj.TexCoord, tangent, obj.Normal, obj.Vertex)
 {
 }
Exemple #16
0
            bool testCollisionTriangle(Vector3D[] verti, Vector3D coord)
            {
                Vector3D max = new Vector3D(-999999999, -999999999, -999999999);
                Vector3D min = new Vector3D(999999999, 999999999, 999999999);

                foreach (Vector3D et in verti)
                {
                    ObjVertex e = new ObjVertex();
                    e.Vertex.X = et.X * -1;
                    e.Vertex.Y = et.Y * -1;
                    e.Vertex.Z = et.Z * -1;
                    if (e.Vertex.X < min.X)
                    {
                        min.X = e.Vertex.X;
                    }
                    if (e.Vertex.X > max.X)
                    {
                        max.X = e.Vertex.X;
                    }
                    if (e.Vertex.Y < min.Y)
                    {
                        min.Y = e.Vertex.Y;
                    }
                    if (e.Vertex.Y > max.Y)
                    {
                        max.Y = e.Vertex.Y;
                    }
                    if (e.Vertex.Z > max.Z)
                    {
                        max.Z = e.Vertex.Z;
                    }
                    if (e.Vertex.Z < min.Z)
                    {
                        min.Z = e.Vertex.Z;
                    }

                }
                Vector3D sval = new Vector3D(.9f, .9f, .9f);

                min -= sval;
                max += sval;

                if (coord.X < max.X & coord.X > min.X & coord.Y > min.Y & coord.Y < max.Y & coord.Z > min.Z & coord.Z < max.Z)
                {
                    return true;

                }
                else
                {

                    return false;
                }
            }
Exemple #17
0
 /// <summary>
 /// Writes a vertex out to the file.
 /// </summary>
 /// <param name="vertex">The vertex to write.</param>
 private void WriteVertex(ObjVertex vertex)
 {
     _writer.WriteLine("v {0} {1} {2}", vertex.Position.I, vertex.Position.J, vertex.Position.K);
     _writer.WriteLine("vn {0} {1} {2}", vertex.Normal.I, vertex.Normal.J, vertex.Normal.K);
     _writer.WriteLine("vt {0} {1}", vertex.TexCoords.I, 1 - vertex.TexCoords.J);
 }
Exemple #18
0
 private static void WriteVert(BinaryWriter b, ObjVertex vert)
 {
     b.Write(vert.Position.X);
     b.Write(vert.Position.Y);
     b.Write(vert.Position.Z);
 }
Exemple #19
0
        public HeightMap(string fileName) : base()
        {
            // Load heightmap
            Bitmap bitmap;

            using (Stream imageStream = File.Open(fileName, FileMode.Open))
            {
                Image image = Image.FromStream(imageStream);
                bitmap = new Bitmap(image);
            }
            int h = bitmap.Height, w = bitmap.Width;

            Vector3[,] pos = new Vector3[h, w]; // all the positions
            Vector2[,] tex = new Vector2[h, w]; // all the texture coordinates

            // Read the height position from the image:
            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    Color col = bitmap.GetPixel(i, j);
                    // 0f < brightness < 1f
                    pos[i, j] = 2 * new Vector3(1f * i / (h - 1), .5f * col.GetBrightness() + .5f, 1f * j / (w - 1)) - Vector3.One;
                    // everything is evenly divided over the texture.
                    tex[i, j] = new Vector2(1f * i / (h - 1), 1f * j / (w - 1));
                }
            }
            // Now we will make the mesh:
            if (MAKE_QUADS)
            {
                vertices  = new ObjVertex[4 * (h - 1) * (w - 1)];
                triangles = new ObjTriangle[0];
                quads     = new ObjQuad[(h - 1) * (w - 1)];
                for (int i = 0; i + 1 < h; i++)
                {
                    for (int j = 0; j + 1 < w; j++)
                    {
                        int       offset = 4 * ((w - 1) * i + j);
                        Vector3[] vv     = new Vector3[] { pos[i, j], pos[i, j + 1], pos[i + 1, j], pos[i + 1, j + 1] };
                        Vector2[] uv     = new Vector2[] { tex[i, j], tex[i, j + 1], tex[i + 1, j], tex[i + 1, j + 1] };

                        Vector3[] ts = new Vector3[] { Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero };
                        // calculate the tangents as an average over all the possible triangles to make with others of the quad.
                        for (int k = 0; k < 4; k++)
                        {
                            Vector3[] tinc = new Vector3[] { Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero };
                            MeshLoader.GetTangents(vv[(k + 1) % 4], vv[(k + 2) % 4], vv[(k + 3) % 4], uv[(k + 1) % 4], uv[(k + 2) % 4], uv[(k + 3) % 4], out tinc[1], out tinc[2], out tinc[3]);
                            for (int l = 1; l < 4; l++)
                            {
                                ts[(k + l) % 4] += tinc[l] / 3f;
                            }
                        }

                        for (int k = 0; k < 4; k++)
                        {
                            vertices[offset + k] = new ObjVertex(uv[k], ts[k], Vector3.UnitY, vv[k]);
                        }
                        quads[offset / 4] = new ObjQuad(offset + 0, offset + 1, offset + 2, offset + 3);
                    }
                }
            }
            else
            {
                vertices  = new ObjVertex[6 * (h - 1) * (w - 1)];
                triangles = new ObjTriangle[2 * (h - 1) * (w - 1)];
                quads     = new ObjQuad[0];
                for (int i = 0; i + 1 < h; i++)
                {
                    for (int j = 0; j + 1 < w; j++)
                    {
                        for (int k = 0; k < 2; k++)
                        {
                            Vector3 v0 = pos[i, j], v1 = pos[i + 1, j + 1], v2 = pos[i + k, j + 1 - k];
                            Vector2 uv0 = tex[i, j], uv1 = tex[i + 1, j + 1], uv2 = tex[i + k, j + 1 - k];

                            Vector3 t0, t1, t2;
                            MeshLoader.GetTangents(v0, v1, v2, uv0, uv1, uv2, out t0, out t1, out t2);

                            int offset = 6 * ((w - 1) * i + j) + 3 * k;
                            vertices[offset + 0]  = new ObjVertex(uv0, t0, Vector3.UnitY, v0);
                            vertices[offset + 1]  = new ObjVertex(uv1, t1, Vector3.UnitY, v1);
                            vertices[offset + 2]  = new ObjVertex(uv2, t2, Vector3.UnitY, v2);
                            triangles[offset / 3] = new ObjTriangle(offset + 0, offset + 1, offset + 2);
                        }
                    }
                }
            }
        }
Exemple #20
0
            public bool Contains(Vector3D point)
            {
                //Create list of triangles

                List<Vector3D[]> Triangles = new List<Vector3D[]>();
                if (interntriangles == null)
                {
                    int tricount = internverts.Length / 3;
                    int x = 0;
                    for (int i = 0; i < tricount; i++)
                    {

                        Vector3D[] triangle = new Vector3D[] { internverts[x], internverts[x + 1], internverts[x + 2] };
                        Triangles.Add(triangle);
                        x += 3;

                    }
                    interntriangles = Triangles.ToArray();
                }
                if (!boundingBox.PointWithin(point))
                {
                    return false;
                }
                if (internworld.testtype == CollisionTestType.AABB || IsCube)
                {
                    return true;
                }
                foreach (Vector3D[] et in interntriangles)
                {
                    List<ObjVertex> vt = new List<ObjVertex>();
                    foreach (Vector3D ty in et)
                    {
                        ObjVertex mm = new ObjVertex();
                        mm.Vertex = ty;
                        vt.Add(mm);
                    }
                    if (testCollisionTriangle(vt.ToArray(), point))
                    {

                        return true;
                    }
                }
                return false;
            }
 private static void WriteVert(BinaryWriter b, ObjVertex vert, float scaleFactor)
 {
     b.Write(vert.Position.X * scaleFactor);
     b.Write(vert.Position.Y * scaleFactor);
     b.Write(vert.Position.Z * scaleFactor);
 }
 /// <summary>
 /// Writes a vertex out to the file.
 /// </summary>
 /// <param name="vertex">The vertex to write.</param>
 private void WriteVertex(ObjVertex vertex)
 {
     _writer.WriteLine("v {0} {1} {2}", vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
     _writer.WriteLine("vn {0} {1} {2}", vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z);
     _writer.WriteLine("vt {0} {1}", vertex.TexCoords.X, 1 - vertex.TexCoords.Y);
 }
Exemple #23
0
        public override bool Equals(object obj)
        {
            ObjVertex tmp = (ObjVertex)obj;

            return(coord.x.Equals(tmp.coord.x) && coord.y.Equals(tmp.coord.y) && coord.z.Equals(tmp.coord.z));
        }