Beispiel #1
0
        /// <summary>
        /// format: type(0)	pX	pY	pZ(0)	sX	sY	sZ rX(0) r(Y) r(Z)
        /// </summary>
        private static string GetLine(CTree pTree)
        {
            string output = "0 ";

            ObjParser.Obj treeObj = pTree.assignedRefTreeObj;
            if (treeObj == null)
            {
                return(null);
            }

            //get coordinates relative to botleft corner of the area
            Vector3 treePos = GetMovedPoint(pTree.Center);

            //in final file Z = height, but here Y = height //changed!
            output += $"{treePos.X} {treePos.Y} 0 ";
            //scale will be same at all axix
            output += $"{treeObj.Scale.X} {treeObj.Scale.Y} {treeObj.Scale.Z} ";
            //rotation
            output += $"0 0 {treeObj.Rotation.Y} ";

            //to know which tree in OBJ is this one
            bool   debugObjName = true;
            string objName      = debugObjName ? " " + pTree.GetObjName() : "";

            output += pTree.assignedRefTree.RefTreeTypeName + objName;

            return(output);
        }
 public Triangle[] Read(string path)
 {
     var obj = new ObjParser.Obj();
     obj.LoadObj(path);
     return obj.FaceList.Select(
         v =>
         {
             var p1 = new Vector3(
                 (float) obj.VertexList[v.VertexIndexList[0] - 1].X,
                 (float) obj.VertexList[v.VertexIndexList[0] - 1].Y,
                 (float) obj.VertexList[v.VertexIndexList[0] - 1].Z);
             var p2 = new Vector3(
                 (float) obj.VertexList[v.VertexIndexList[1] - 1].X,
                 (float) obj.VertexList[v.VertexIndexList[1] - 1].Y,
                 (float) obj.VertexList[v.VertexIndexList[1] - 1].Z);
             var p3 = new Vector3(
                 (float) obj.VertexList[v.VertexIndexList[2] - 1].X,
                 (float) obj.VertexList[v.VertexIndexList[2] - 1].Y,
                 (float) obj.VertexList[v.VertexIndexList[2] - 1].Z);
             return new Triangle(
                 p1,
                 p2,
                 p3
             );
         }
     ).ToArray();
 }
Beispiel #3
0
        public void Load(string path)
        {
            string objFileName = Path.ChangeExtension(path, ".obj");
            string mtlFileName = Path.ChangeExtension(path, ".mtl");

            if (File.Exists(mtlFileName))
            {
                mtl = new ObjParser.Mtl();
                mtl.LoadMtl(mtlFileName);
                materials = ListMaterialsOfObjMaterials(mtl.MaterialList);
            }

            if (!File.Exists(objFileName))
            {
                return;
            }
            obj = new ObjParser.Obj();
            obj.LoadObj(objFileName);
            BuildFromFaces();
        }
Beispiel #4
0
        public void Save(string path)
        {
            string objFileName = Path.ChangeExtension(path, ".obj");
            string mtlFileName = Path.ChangeExtension(path, ".mtl");

            File.Delete(objFileName);
            string[] headers = new string[] { "Mesh Maker VR" };
            obj            = new ObjParser.Obj();
            obj.VertexList = ObjVerticesOfListVector3(vertices);
            if (uvs != null)
            {
                obj.TextureList = ObjTextureVerticesOfListVector2(uvs);
            }
            obj.FaceList = ObjFacesOfListInt(triangles);
            obj.WriteObjFile(objFileName, headers);

            File.Delete(mtlFileName);
            mtl = new ObjParser.Mtl();
            mtl.MaterialList = ObjMaterialsOfListMaterials(materials);
            mtl.WriteMtlFile(mtlFileName, headers);
        }
Beispiel #5
0
        void PerformExportTransaction()
        {
            // Report the progress of the export to 0%
            m_PublisherClient.ReportProgress(0);

            // Start a transaction ; note that the publisher client can only run one transaction at a time.
            PublisherTransaction transaction = m_PublisherClient.StartTransaction();

            // add sample plane //
            {
                // Build a SyncMesh and send it to the server
                var mesh = BuildMesh();
                transaction.Send(mesh);

                // Build a SyncMaterial and send it to the server
                var material = BuildMaterial(SyncColor.White);
                transaction.Send(material);

                // Report in between parsing progress
                m_PublisherClient.ReportProgress(50);

                // Build a SyncObject and send it to the server.
                var obj = BuildObject(mesh, material);
                transaction.Send(obj);

                // Build a SyncObjectInstance and send it to the server
                var instance = BuildObjectInstance(obj);
                transaction.Send(instance);
            }
            // add sample plane //

            // objファイルを開く //
            DataUtil meshData = new DataUtil();

            meshData.fileName = @"D:\model\house.obj";
            meshData.OpenObj(@"D:\model\house.obj");

            int num = meshData.obj.MeshList.Count;

            for (int i = 0; i < num; i++)
            {
                ObjParser.Obj currentObj = meshData.obj.MeshList[i];
                Console.WriteLine("Mesh: " + currentObj.Name);

                SyncMesh mesh = meshData.BuildMesh(currentObj);
                transaction.Send(mesh);
                Console.WriteLine("\tMesh Id: " + mesh.Id);
                Console.WriteLine("\tMesh Name: " + mesh.Name);

                SyncMaterial material = meshData.BuildMaterial(currentObj, transaction);
                transaction.Send(material);
                Console.WriteLine("\tMaterial Id: " + material.Id);
                Console.WriteLine("\tMaterial Name: " + material.Name);

                // Report in between parsing progress
                m_PublisherClient.ReportProgress(i * num / 100);

                // Build a SyncObject and send it to the server.
                var obj = meshData.BuildObject(mesh, material, i);
                transaction.Send(obj);
                Console.WriteLine("\tObject Id: " + obj.Id);
                Console.WriteLine("\tObject Name: " + obj.Name);

                // Build a SyncObjectInstance and send it to the server
                var instance = meshData.BuildObjectInstance(obj);
                transaction.Send(instance);
                Console.WriteLine("\tObject Instance Id: " + instance.Id);
                Console.WriteLine("\tObject Instance Name: " + instance.Name);
            }

#if false
            for (int i = 0; i < meshData.obj.MeshList.Count; i++)
            {
                List <SyncMesh> meshAry = meshData.BuildMesh();
                foreach (SyncMesh mesh in meshAry)
                {
                    transaction.Send(mesh);
                }

                List <SyncMaterial> materialAry = meshData.BuildMaterial();
                foreach (SyncMaterial material in materialAry)
                {
                    transaction.Send(material);
                }

                // Report in between parsing progress
                //m_PublisherClient.ReportProgress(50);

                // Build a SyncObject and send it to the server.
                foreach (SyncMesh mesh in meshAry)
                {
                    var obj = meshData.BuildObject(mesh);
                    transaction.Send(obj);

                    // Build a SyncObjectInstance and send it to the server
                    var instance = meshData.BuildObjectInstance(obj);
                    transaction.Send(instance);
                }
            }
#endif

            // Commit the transaction, then detach it from the publisher client.
            transaction.Commit();

            // Report the completion of the export
            m_PublisherClient.ReportProgress(100);
        }
        public static void LoadObj(bool enable, string filename, Matrix transform, int materialIndex, out IEnumerable <Vector3> vertices,
                                   out IEnumerable <Vector2> uvs, out IEnumerable <Vector3> normals, out IEnumerable <Primitive> faces)
        {
            vertices = new List <Vector3>();
            faces    = new List <Primitive>();
            uvs      = new List <Vector2>();
            normals  = new List <Vector3>();

            if (!enable)
            {
                return;
            }

            List <Vector3>   v    = new List <Vector3>();
            List <Vector2>   vuv  = new List <Vector2>();
            List <Vector3>   vn   = new List <Vector3>();
            List <Primitive> tris = new List <Primitive>();

            var obj        = new ObjParser.Obj();
            var fileStream = new FileStream(filename, FileMode.Open);

            obj.LoadObj(fileStream);

            for (int i = 0; i < obj.VertexList.Count; i++)
            {
                var vt = obj.VertexList[i];
                v.Add(new Vector3((float)vt.X, (float)vt.Y, (float)vt.Z));
            }

            for (int i = 0; i < obj.NormalList.Count; i++)
            {
                var n = obj.NormalList[i];
                vn.Add(new Vector3((float)n.X, (float)n.Y, (float)n.Z));
            }

            for (int i = 0; i < obj.TextureList.Count; i++)
            {
                var uv = obj.TextureList[i];
                vuv.Add(new Vector2((float)uv.X, (float)uv.Y));
            }

            foreach (var f in obj.FaceList)
            {
                Primitive triangle = new Primitive(PrimitiveType.Triangle, materialIndex, Vector4.Zero, transform);
                triangle.Va = f.VertexIndexList[0] - 1;
                triangle.Vb = f.VertexIndexList[1] - 1;
                triangle.Vc = f.VertexIndexList[2] - 1;

                triangle.UVa = f.TextureVertexIndexList[0] - 1;
                triangle.UVb = f.TextureVertexIndexList[1] - 1;
                triangle.UVc = f.TextureVertexIndexList[2] - 1;

                triangle.Na = f.NormalIndexList[0] - 1;
                triangle.Nb = f.NormalIndexList[1] - 1;
                triangle.Nc = f.NormalIndexList[2] - 1;

                Vertex  a  = obj.VertexList[triangle.Va];
                Vector3 va = new Vector3((float)a.X, (float)a.Y, (float)a.Z);

                Vertex  b  = obj.VertexList[triangle.Vb];
                Vector3 vb = new Vector3((float)b.X, (float)b.Y, (float)b.Z);

                Vertex  c  = obj.VertexList[triangle.Vc];
                Vector3 vc = new Vector3((float)c.X, (float)c.Y, (float)c.Z);

                Vector3 ta = (Vector3)Vector3.Transform(va, transform);
                Vector3 tb = (Vector3)Vector3.Transform(vb, transform);
                Vector3 tc = (Vector3)Vector3.Transform(vc, transform);

                BoundingBox aabb = BoundingBox.FromPoints(new Vector3[] { ta, tb, tc });
                triangle.MinBounds = aabb.Minimum;
                triangle.MaxBounds = aabb.Maximum;

                triangle.MaterialIndex = f.MtlIndex;

                tris.Add(triangle);
            }

            vertices = v;
            uvs      = vuv;
            normals  = vn;
            faces    = tris;
        }
Beispiel #7
0
        // I'm doing this with too many loops but whatever.
        // Also I'm shattering the ability to put multiple meshes in one file.
        void LoadObjFile(string filename)
        {
            ObjParser.Obj obj = new ObjParser.Obj();
            obj.LoadObj(filename);
            mGeometry        = new float[3 * 3 * obj.FaceList.Count];
            mGeometryIndices = new int[3 * obj.FaceList.Count];
            mNormals         = new float[3 * 3 * obj.FaceList.Count];
            mUvs             = new float[2 * 3 * obj.FaceList.Count];

            // This is the smart indexed way, but I need flat shading right now.
            //int i = 0;
            //foreach (var v in obj.VertexList)
            //{
            //    mGeometry[3 * i + 0] = (float)v.X;
            //    mGeometry[3 * i + 1] = (float)v.Y;
            //    mGeometry[3 * i + 2] = (float)v.Z;
            //    i++;
            //}
            //i = 0;
            //foreach (var f in obj.FaceList)
            //{
            //    mGeometryIndices[3 * i + 0] = f.VertexIndexList[0] - 1;
            //    mGeometryIndices[3 * i + 1] = f.VertexIndexList[1] - 1;
            //    mGeometryIndices[3 * i + 2] = f.VertexIndexList[2] - 1;
            //    i++;
            //}

            mNumPrimitives = obj.FaceList.Count;
            primitiveType  = OpenTK.Graphics.OpenGL4.BeginMode.Triangles;
            int i = 0;

            Vector3[] normalCreators = new Vector3[3];
            Vector3   n;

            foreach (var f in obj.FaceList)
            {
                for (int v = 0; v < 3; v++)
                {
                    normalCreators[v]            = new Vector3((float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).X, (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Y, (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Z);
                    mGeometry[9 * i + 3 * v + 0] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).X;
                    mGeometry[9 * i + 3 * v + 1] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Y;
                    mGeometry[9 * i + 3 * v + 2] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Z;
                }
                n = Math.calculateFaceNormal(normalCreators[0], normalCreators[1], normalCreators[2]);
                for (int j = 0; j < 3; j++)
                {
                    mNormals[9 * i + 3 * j + 0] = n.X;
                    mNormals[9 * i + 3 * j + 1] = n.Y;
                    mNormals[9 * i + 3 * j + 2] = n.Z;
                }

                if (obj.TextureList.Count > 0)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        mUvs[6 * i + 2 * j + 0] = (float)obj.TextureList[f.TextureVertexIndexList.ElementAt(j) - 1].X;
                        mUvs[6 * i + 2 * j + 1] = (float)obj.TextureList[f.TextureVertexIndexList.ElementAt(j) - 1].Y;
                    }
                }
                i++;
            }

            for (i = 0; i < mGeometryIndices.Length; i++)
            {
                mGeometryIndices[i] = i;
            }
        }