예제 #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            this.panel1.Size = new Size(640, 480);
            this._graphics   = this.panel1.CreateGraphics();
            device           = new Device(640, 480);
            bmp = new Bitmap(640, 480);
            device.Clear(0, 0, 0, 255);
            device.DrawPoint(new OpenTK.Vector3d(0, 0, 0), new Color4(1, 1, 1, 1));
            _texture = new Texture("Suzanne.jpg", 512, 512);

            //mesh.Vertices[0] = new Vector3(-1, 1, 1);
            //mesh.Vertices[1] = new Vector3(1, 1, 1);
            //mesh.Vertices[2] = new Vector3(-1, -1, 1);
            //mesh.Vertices[3] = new Vector3(1, -1, 1);
            //mesh.Vertices[4] = new Vector3(-1, 1, -1);
            //mesh.Vertices[5] = new Vector3(1, 1, -1);
            //mesh.Vertices[6] = new Vector3(1, -1, -1);
            //mesh.Vertices[7] = new Vector3(-1, -1, -1);

            //mesh.Faces[0] = new Face { A = 0, B = 1, C = 2 };
            //mesh.Faces[1] = new Face { A = 1, B = 2, C = 3 };
            //mesh.Faces[2] = new Face { A = 1, B = 3, C = 6 };
            //mesh.Faces[3] = new Face { A = 1, B = 5, C = 6 };
            //mesh.Faces[4] = new Face { A = 0, B = 1, C = 4 };
            //mesh.Faces[5] = new Face { A = 1, B = 4, C = 5 };

            //mesh.Faces[6] = new Face { A = 2, B = 3, C = 7 };
            //mesh.Faces[7] = new Face { A = 3, B = 6, C = 7 };
            //mesh.Faces[8] = new Face { A = 0, B = 2, C = 7 };
            //mesh.Faces[9] = new Face { A = 0, B = 4, C = 7 };
            //mesh.Faces[10] = new Face { A = 4, B = 5, C = 6 };
            //mesh.Faces[11] = new Face { A = 4, B = 6, C = 7 };

            mera.Position = new Vector3d(0, 0, 10.0f);
            mera.Target   = Vector3d.Zero;
            _meshes       = device.LoadJSONFileAsync("monkey.babylon");

            //----------

            //---------------------
            //another scene
            _simpleTexture2 = new Texture("favorites32.png", 32, 32);
            //create a simple rect
            _myrectMesh = new Mesh("test", 4, 2);
            //_myrectMesh.Vertices[0] = new Vertex() { Coordinates = new Vector3d(-0.5, 0, 1), Normal = new Vector3d(-0.5, 0, -1), TextureCoordinates = new Vector2d(0, 1) };
            //_myrectMesh.Vertices[1] = new Vertex() { Coordinates = new Vector3d(-0.5, 0.5, 1), Normal = new Vector3d(-0.5, 0.5, -1), TextureCoordinates = new Vector2d(0, 0) };
            //_myrectMesh.Vertices[2] = new Vertex() { Coordinates = new Vector3d(0.5, 0.5, 1), Normal = new Vector3d(0.5, 0.5, -1), TextureCoordinates = new Vector2d(1, 0) };
            //_myrectMesh.Vertices[3] = new Vertex() { Coordinates = new Vector3d(0.5, 0, 1), Normal = new Vector3d(0.5, 0, -1), TextureCoordinates = new Vector2d(1, 1) };

            //_myrectMesh.Vertices[0] = new Vertex() { Coordinates = new Vector3d(-1, 0, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(0, 0) };
            //_myrectMesh.Vertices[1] = new Vertex() { Coordinates = new Vector3d(-1, 1, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(0, 1) };
            //_myrectMesh.Vertices[2] = new Vertex() { Coordinates = new Vector3d(0, 1, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(1, 1) };
            //_myrectMesh.Vertices[3] = new Vertex() { Coordinates = new Vector3d(0, 0, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(1, 0) };


            _myrectMesh.Vertices[0] = new Vertex()
            {
                Coordinates = new Vector3d(-1, 1, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(0, 1)
            };
            _myrectMesh.Vertices[1] = new Vertex()
            {
                Coordinates = new Vector3d(-1, -1, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(0, 0)
            };
            _myrectMesh.Vertices[2] = new Vertex()
            {
                Coordinates = new Vector3d(0, 1, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(1, 1)
            };
            _myrectMesh.Vertices[3] = new Vertex()
            {
                Coordinates = new Vector3d(0, -1, 1), Normal = new Vector3d(0, 0, 1), TextureCoordinates = new Vector2d(1, 0)
            };



            //each face is defined by a set of coords A,B,C
            _myrectMesh.Faces[0] = new Face {
                A = 0, B = 1, C = 2
            };
            _myrectMesh.Faces[1] = new Face {
                A = 2, B = 1, C = 3
            };
            //
            _myrectMesh.ComputeFaceNormals();
            _myrectMesh.Texture = _simpleTexture2;
        }
예제 #2
0
        public Mesh[] LoadJSONFileAsync(string fileName)
        {
            string           data   = System.IO.File.ReadAllText(fileName);
            var              meshes = new List <Mesh>();
            JToken           jtoken = Newtonsoft.Json.JsonConvert.DeserializeObject(data) as JToken;
            MyMeshCollection myMesh = jtoken.ToObject <MyMeshCollection>();


            List <Mesh> meshList = new List <Mesh>();

            List <MyMaterial> materials = myMesh.materials;

            Dictionary <string, MyMaterial> material_dic = new Dictionary <string, MyMaterial>();
            int meshCount = myMesh.meshes.Count;

            for (int materialIndex = 0; materialIndex < materials.Count; materialIndex++)
            {
                MyMaterial myMaterial = materials[materialIndex];

                if (myMaterial.diffuseTexture != null)
                {
                    myMaterial.DiffuseTextureName = (myMaterial.diffuseTexture.name);
                }


                material_dic.Add(myMaterial.id, myMaterial);
            }

            for (int meshIndex = 0; meshIndex < meshCount; ++meshIndex)
            {
                MyMesh   mymesh        = myMesh.meshes[meshIndex];
                double[] verticesArray = mymesh.vertices;
                int[]    indicesArray  = mymesh.indices;
                double[] position      = mymesh.position;

                int uvCount      = mymesh.uvCount;
                int verticesStep = 1;
                switch (uvCount)
                {
                case 0:
                    verticesStep = 6;
                    break;

                case 1:
                    verticesStep = 8;
                    break;

                case 2:
                    verticesStep = 10;
                    break;
                }
                // the number of interesting vertices information for us
                int verticesCount = verticesArray.Length / verticesStep;
                // number of faces is logically the size of the array divided by 3 (A, B, C)
                int facesCount = indicesArray.Length / 3;


                Mesh mesh = new Mesh(mymesh.name, verticesCount, facesCount);
                meshList.Add(mesh);
                // Filling the Vertices array of our mesh first
                for (var index = 0; index < verticesCount; index++)
                {
                    double x = verticesArray[index * verticesStep];
                    double y = verticesArray[index * verticesStep + 1];
                    double z = verticesArray[index * verticesStep + 2];
                    // Loading the vertex normal exported by Blender
                    double nx = verticesArray[index * verticesStep + 3];
                    double ny = verticesArray[index * verticesStep + 4];
                    double nz = verticesArray[index * verticesStep + 5];

                    if (uvCount > 0)
                    {   // Loading the texture coordinates
                        double u = verticesArray[index * verticesStep + 6];
                        double v = verticesArray[index * verticesStep + 7];
                        mesh.Vertices[index] = new Vertex
                        {
                            Coordinates        = new Vector3d(x, y, z),
                            Normal             = new Vector3d(nx, ny, nz),
                            TextureCoordinates = new Vector2d(u, v)
                        };
                    }
                    else
                    {   //no uv
                        mesh.Vertices[index] = new Vertex
                        {
                            Coordinates = new Vector3d(x, y, z),
                            Normal      = new Vector3d(nx, ny, nz)
                        };
                    }
                }


                // Then filling the Faces array
                for (var index = 0; index < facesCount; index++)
                {
                    int a = indicesArray[index * 3];
                    int b = indicesArray[index * 3 + 1];
                    int c = indicesArray[index * 3 + 2];
                    mesh.Faces[index] = new Face {
                        A = a, B = b, C = c
                    };
                }

                // Getting the position you've set in Blender
                mesh.Position = new Vector3d(position[0], position[1], position[2]);


                if (uvCount > 0)
                {
                    // Texture
                    string meshTextureID   = mymesh.materialId;
                    string meshTextureName = material_dic[meshTextureID].DiffuseTextureName;
                    mesh.Texture = new Texture(meshTextureName, 512, 512);
                }
                mesh.ComputeFaceNormals();
            }

            return(meshList.ToArray());
        }