Esempio n. 1
0
        public void DrawImageColored(SmartTexture texture, Color4 color, Vector2[] verts, Vector2[] tverts)
        {
            device.VertexFormat = VertexFormat.Position
                                  | VertexFormat.Diffuse
                                  | VertexFormat.Specular
                                  | VertexFormat.Texture1;

            SetupTexture(texture);

            Vector2 toUV = new Vector2(
                (texture == null) ? 1 : 1.0f / (float)texture.Width,
                (texture == null) ? 1 : 1.0f / (float)texture.Height);

            int diffuseColor = color.ToArgb();
            int specular     = currentOverlayColor.ToArgb();

            TexturedVertex[] verts2 = new TexturedVertex[]
            {
                new TexturedVertex(new Vector3(verts[0], 0), diffuseColor, specular, Vector2.Modulate(tverts[0], toUV)),
                new TexturedVertex(new Vector3(verts[1], 0), diffuseColor, specular, Vector2.Modulate(tverts[1], toUV)),
                new TexturedVertex(new Vector3(verts[2], 0), diffuseColor, specular, Vector2.Modulate(tverts[2], toUV)),
                new TexturedVertex(new Vector3(verts[3], 0), diffuseColor, specular, Vector2.Modulate(tverts[3], toUV)),
            };

            device.DrawUserPrimitives <TexturedVertex>(PrimitiveType.TriangleFan, 2, verts2);
        }
Esempio n. 2
0
 public static (TexturedVertex[], PrimitiveType) TexRectangle(
     float width,
     float height)
 {
     width /= 2f; height /= 2f;
     TexturedVertex[] res = new TexturedVertex[]
     {
         new TexturedVertex(new Vector4(+width, +height, 0f, 1f), new Vector2(1f, 0f)),
         new TexturedVertex(new Vector4(-width, +height, 0f, 1f), new Vector2(0f, 0f)),
         new TexturedVertex(new Vector4(+width, -height, 0f, 1f), new Vector2(1f, 1f)),
         new TexturedVertex(new Vector4(-width, +height, 0f, 1f), new Vector2(0f, 0f)),
         new TexturedVertex(new Vector4(+width, -height, 0f, 1f), new Vector2(1f, 1f)),
         new TexturedVertex(new Vector4(-width, -height, 0f, 1f), new Vector2(0f, 1f))
     };
     return(res, PrimitiveType.Triangles);
 }
Esempio n. 3
0
        void SetupGeometry()
        {
            int   x_res   = 2;
            int   y_res   = 2;
            float x_scale = (float)(2 * Width);
            float y_scale = (float)(2 * Height);

            Vertices = new TexturedVertex[x_res * y_res];
            Indices  = new int[6 * x_res * y_res];
            //Texcoords = new Vector2[x_res * y_res];

            int i = 0;

            //for (int y = -y_res / 2; y < y_res / 2; y++)
            for (int y = 0; y < y_res; y++)
            {
                //for (int x = -x_res / 2; x < x_res / 2; x++)
                for (int x = 0; x < x_res; x++)
                {
                    Vertices[i]._position.X        = x_scale * (float)x / (float)x_res;
                    Vertices[i]._position.Y        = y_scale * (float)y / (float)y_res;
                    Vertices[i]._position.Z        = 0;
                    Vertices[i]._normal.X          = 0;
                    Vertices[i]._normal.Y          = 0;
                    Vertices[i]._normal.Z          = 1;
                    Vertices[i]._textureCoordinate = new Vector2((float)(x) / (float)(x_res - 1), (float)(1 - y) / (float)(y_res - 1));

                    i++;
                }
            }

            i = 0;
            for (int y = 0; y < y_res - 1; y++)
            {
                for (int x = 0; x < x_res - 1; x++)
                {
                    Indices[i++] = (y + 0) * x_res + x;
                    Indices[i++] = (y + 1) * x_res + x;
                    Indices[i++] = (y + 0) * x_res + x + 1;

                    Indices[i++] = (y + 0) * x_res + x + 1;
                    Indices[i++] = (y + 1) * x_res + x;
                    Indices[i++] = (y + 1) * x_res + x + 1;
                }
            }
        }
Esempio n. 4
0
 public static (TexturedVertex[], PrimitiveType) TexRectangle(
     float width,
     float height,
     RectangleF region)
 {
     width /= 2f; height /= 2f;
     TexturedVertex[] res = new TexturedVertex[]
     {
         new TexturedVertex(new Vector4(+width, +height, 0f, 1f), new Vector2(region.Right, region.Top)),
         new TexturedVertex(new Vector4(-width, +height, 0f, 1f), new Vector2(region.Left, region.Top)),
         new TexturedVertex(new Vector4(+width, -height, 0f, 1f), new Vector2(region.Right, region.Bottom)),
         new TexturedVertex(new Vector4(-width, +height, 0f, 1f), new Vector2(region.Left, region.Top)),
         new TexturedVertex(new Vector4(+width, -height, 0f, 1f), new Vector2(region.Right, region.Bottom)),
         new TexturedVertex(new Vector4(-width, -height, 0f, 1f), new Vector2(region.Left, region.Bottom))
     };
     return(res, PrimitiveType.Triangles);
 }
Esempio n. 5
0
        public void FillTriangle(Color color, PointF p0, PointF p1, PointF p2)
        {
            device.VertexFormat = VertexFormat.Position | VertexFormat.Diffuse | VertexFormat.Specular | VertexFormat.Texture1;

            SetupTexture(null);

            int diffuseColor = color.ToArgb();
            int specular     = currentOverlayColor.ToArgb();

            TexturedVertex[] verts2 = new TexturedVertex[]
            {
                new TexturedVertex(new Vector3(p0.X, p0.Y, 0), diffuseColor, specular, Vector2.Zero),
                new TexturedVertex(new Vector3(p1.X, p1.Y, 0), diffuseColor, specular, Vector2.Zero),
                new TexturedVertex(new Vector3(p2.X, p2.Y, 0), diffuseColor, specular, Vector2.Zero),
            };

            device.DrawUserPrimitives <TexturedVertex>(PrimitiveType.TriangleFan, 1, verts2);
        }
Esempio n. 6
0
        public void SetupMesh()
        {
            Vertices = new TexturedVertex[x_res * z_res];
            Indices  = new int[6 * x_res * z_res];
            int i = 0;

            for (float z = 0; z < z_res; z++)
            {
                Vector3d zpl = Vector3d.Lerp(Boundary.TL, Boundary.BL, z / (float)(z_res - 1));
                Vector3d zpr = Vector3d.Lerp(Boundary.TR, Boundary.BR, z / (float)(z_res - 1));
                for (double x = x_res; x > 0; x--)
                {
                    Vector3d xp = Vector3d.Lerp(zpr, zpl, x / (float)(x_res - 1)) - transform.Position;
                    BoundingBox.Expand(xp);
                    // Console.WriteLine("x:" + x + ",z:" + z + " = " + xp);
                    Vertices[i]._position.X        = (float)(xp.X);
                    Vertices[i]._position.Y        = (float)(xp.Y);
                    Vertices[i]._position.Z        = (float)(xp.Z);
                    Vertices[i]._normal.X          = 0;
                    Vertices[i]._normal.Y          = 1;
                    Vertices[i]._normal.Z          = 0;
                    Vertices[i]._textureCoordinate = new Vector2((float)(x_res - x) / (float)(x_res - 1), (float)z / (float)(z_res - 1));
                    i++;
                }
            }

            i = 0;
            for (int y = 0; y < z_res - 1; y++)
            {
                for (int x = 0; x < x_res - 1; x++)
                {
                    Indices[i++] = (y + 0) * x_res + x;
                    Indices[i++] = (y + 1) * x_res + x;
                    Indices[i++] = (y + 0) * x_res + x + 1;

                    Indices[i++] = (y + 0) * x_res + x + 1;
                    Indices[i++] = (y + 1) * x_res + x;
                    Indices[i++] = (y + 1) * x_res + x + 1;
                }
            }
            //base.Setup();
        }
Esempio n. 7
0
        public void SetupPlane(int x_res, int y_res, float x_scale, float y_scale)
        {
            Vertices = new TexturedVertex[x_res * y_res];
            Indices  = new int[6 * x_res * y_res];
            //Texcoords = new Vector2[x_res * y_res];

            int i = 0;

            //for (int y = -y_res / 2; y < y_res / 2; y++)
            for (int y = 0; y < y_res; y++)
            {
                //for (int x = -x_res / 2; x < x_res / 2; x++)
                for (int x = 0; x < x_res; x++)
                {
                    Vertices[i]._position.X        = x_scale * (float)x / (float)x_res;
                    Vertices[i]._position.Z        = y_scale * (float)y / (float)y_res;
                    Vertices[i]._position.Y        = 0;
                    Vertices[i]._normal.X          = 0;
                    Vertices[i]._normal.Y          = 1;
                    Vertices[i]._normal.Z          = 0;
                    Vertices[i]._textureCoordinate = new Vector2((float)x / (float)x_res, (float)y / (float)y_res);

                    i++;
                }
            }

            i = 0;
            for (int y = 0; y < y_res - 1; y++)
            {
                for (int x = 0; x < x_res - 1; x++)
                {
                    Indices[i++] = (y + 0) * x_res + x;
                    Indices[i++] = (y + 1) * x_res + x;
                    Indices[i++] = (y + 0) * x_res + x + 1;

                    Indices[i++] = (y + 0) * x_res + x + 1;
                    Indices[i++] = (y + 1) * x_res + x;
                    Indices[i++] = (y + 1) * x_res + x + 1;
                }
            }
        }
Esempio n. 8
0
        MMesh processMesh(Mesh mesh, Scene scene, string sName, Matrix4d trans)
        {
            // data to fill
            List <TexturedVertex> vertices = new List <TexturedVertex>();
            List <int>            indices  = new List <int>();
            List <MTexture>       textures = new List <MTexture>();

            // Walk through each of the mesh's vertices
            for (int i = 0; i < mesh.VertexCount; i++)
            {
                TexturedVertex vertex = new TexturedVertex();
                Vector3        vector = new Vector3(); // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first.
                                                       // positions
                vector.X         = mesh.Vertices[i].X;
                vector.Y         = mesh.Vertices[i].Y;
                vector.Z         = mesh.Vertices[i].Z;
                vertex._position = vector;
                // normals
                if (mesh.HasNormals)
                {
                    vector.X       = mesh.Normals[i].X;
                    vector.Y       = mesh.Normals[i].Y;
                    vector.Z       = mesh.Normals[i].Z;
                    vertex._normal = vector;
                }
                // texture coordinates
                if (mesh.HasTextureCoords(0)) // does the mesh contain texture coordinates?
                {
                    Vector2 vec = new Vector2();
                    // a vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't
                    // use models where a vertex can have multiple texture coordinates so we always take the first set (0).
                    vec.X = mesh.TextureCoordinateChannels[0][i].X;
                    vec.Y = 1 - mesh.TextureCoordinateChannels[0][i].Y;
                    vertex._textureCoordinate = vec;
                }
                else
                {
                    vertex._textureCoordinate = new Vector2(0.0f, 0.0f);
                }

                if (mesh.HasTangentBasis)
                {
                    // tangent
                    vector.X = mesh.Tangents[i].X;
                    vector.Y = mesh.Tangents[i].Y;
                    vector.Z = mesh.Tangents[i].Z;
                    /// vertex.Tangent = vector;
                    // bitangent
                    vector.X = mesh.BiTangents[i].X;
                    vector.Y = mesh.BiTangents[i].Y;
                    vector.Z = mesh.BiTangents[i].Z;
                    /// vertex.BiTangent = vector;
                }
                vertices.Add(vertex);
            }
            // now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
            for (int i = 0; i < mesh.FaceCount; i++)
            {
                Face face = mesh.Faces[i];
                // retrieve all indices of the face and store them in the indices vector
                for (int j = 0; j < face.IndexCount; j++)
                {
                    indices.Add(face.Indices[j]);
                }
            }
            // process materials
            Material material = scene.Materials[mesh.MaterialIndex];
            // we assume a convention for sampler names in the shaders. Each diffuse texture should be named
            // as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
            // Same applies to other texture as the following list summarizes:
            // diffuse: texture_diffuseN
            // specular: texture_specularN
            // normal: texture_normalN

            // 1. diffuse maps

            /*
             * List<MTexture> diffuseMaps = loadMaterialTextures(material, TextureType_DIFFUSE, "texture_diffuse");
             * textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
             * // 2. specular maps
             * List<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
             * textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
             * // 3. normal maps
             * List<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
             * textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
             * // 4. height maps
             * List<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
             * textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
             */

            // return a mesh object created from the extracted mesh data
            MMesh m = new MMesh(sName);

            // trans.Transpose();
            m.transform.Position = trans.ExtractTranslation();
            m.transform.Rotation = trans.ExtractRotation();
            m.transform.Scale    = trans.ExtractScale();
            m.AddMaterial(this.material);
            m.SetupMesh(vertices, indices, textures);
            return(m);
        }
        public void Render(Grid.GridRange range, Rectangle viewport)
        {
            // 1. Remove previous tiles.
            ////
            foreach (var cellList in visibleCells.Values)
            {
                cellList.Clear();
            }

            // 1. Fill
            ////
            foreach (Grid.GridCell cell in range)
            {
                if (cell.Data == null)
                {
                    continue;
                }

                BlobImage image = cell.Data.Floor.Image;

                if (!visibleCells.ContainsKey(image))
                {
                    Bitmap bitmap = image.Image;
                    using (Stream s = new MemoryStream())
                    {
                        bitmap.Save(s, ImageFormat.Png);
                        s.Seek(0, SeekOrigin.Begin);
                        Texture tex = Texture.FromStream(device, s, bitmap.Width, bitmap.Height, 0, Usage.None,
                                                         Format.Unknown,
                                                         Pool.Managed, Filter.None, Filter.None, 0);

                        textures.Add(image, tex);
                        visibleCells.Add(image, new List <TexturedVertex>());
                    }
                }

                List <TexturedVertex> currentList = visibleCells[image];

                int pixelsPerCell = 8;
                int x             = (cell.X * pixelsPerCell) - viewport.X;
                int y             = (cell.Y * pixelsPerCell) - viewport.Y;

                int w = 8;
                int h = 8;

                // Calculate the (u,v) we need to use based on the tile coordinates.
                float scaleW = 8.0f / image.Image.Width;
                float scaleH = 8.0f / image.Image.Height;

                float uStart = cell.X * scaleW;
                float vStart = cell.Y * scaleH;

                float uEnd = uStart + scaleW;
                float vEnd = vStart + scaleH;

                // Clockwise winding
                // TODO: Index these vertices! argh
                var v0 = new TexturedVertex(new Vector4(x, y, RenderingZOffset, 1.0f), new Vector2(uStart, vStart));
                var v1 = new TexturedVertex(new Vector4(x + w, y, RenderingZOffset, 1.0f), new Vector2(uEnd, vStart));
                var v2 = new TexturedVertex(new Vector4(x + w, y + h, RenderingZOffset, 1.0f),
                                            new Vector2(uEnd, vEnd));

                var v3 = new TexturedVertex(new Vector4(x, y, RenderingZOffset, 1.0f), new Vector2(uStart, vStart));
                var v4 = new TexturedVertex(new Vector4(x + w, y + h, RenderingZOffset, 1.0f),
                                            new Vector2(uEnd, vEnd));
                var v5 = new TexturedVertex(new Vector4(x, y + h, RenderingZOffset, 1.0f), new Vector2(uStart, vEnd));

                currentList.Add(v0);
                currentList.Add(v1);
                currentList.Add(v2);
                currentList.Add(v3);
                currentList.Add(v4);
                currentList.Add(v5);
            }

            // 2. Fill buffer.
            ////
            DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.Discard);

            foreach (var vertexList in visibleCells)
            {
                if (vertexList.Value.Count == 0)
                {
                    continue;
                }

                stream.WriteRange(vertexList.Value.ToArray());
            }

            vertexBuffer.Unlock();

            // 3. Draw.
            ////
            device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
            device.SetStreamSource(0, vertexBuffer, 0, TexturedVertex.Size);
            device.VertexDeclaration = vertexDecl;

            int offset = 0;

            foreach (var vertexList in visibleCells)
            {
                var texture = textures[vertexList.Key];
                device.SetTexture(0, texture);
                int tilesToDraw = vertexList.Value.Count / 3;
                device.DrawPrimitives(PrimitiveType.TriangleList, offset, tilesToDraw);
                offset += tilesToDraw * 3;
            }
        }
Esempio n. 10
0
        public override void Setup()
        {
            base.Setup();
            SetupTree();
            SetupMaterial();

            float w = 0.5f;

            Vertices = new TexturedVertex[] {
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(0.0f, 0.0f)), // bottom-left
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(0.0f, 1.0f)),

                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(0.0f, 0.0f)),

                new TexturedVertex(new Vector3(-w, w, w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(1.0f, 1.0f)),

                new TexturedVertex(new Vector3(w, w, -w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(0.0f, 1.0f)),

                new TexturedVertex(new Vector3(w, w, -w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, -w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(1.0f, 1.0f)),

                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, -w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(0.0f, 1.0f)),

                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, w, w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(1.0f, 0.0f))
            };

            GL.BindBuffer(BufferTarget.ArrayBuffer, treemesh.VBO);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, treemesh.EBO);

            GL.GenBuffers(1, out instanceVBO);
            //Helper.CheckGLError(this);
            UploadBufferFull();
            //Helper.CheckGLError(this);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindVertexArray(0);
        }
Esempio n. 11
0
        public override void Setup()
        {
            base.Setup();

            float w = 0.5f;

            TexturedVertex[] Vertices = new TexturedVertex[] {
                // back face


                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(0.0f, 0.0f)), // bottom-left
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(0.0f, 0.0f, -1.0f), new Vector2(0.0f, 1.0f)),

                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(0.0f, 0.0f, 1.0f), new Vector2(0.0f, 0.0f)),

                new TexturedVertex(new Vector3(-w, w, w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(-1.0f, 0.0f, 0.0f), new Vector2(1.0f, 1.0f)),

                new TexturedVertex(new Vector3(w, w, -w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, -w), new Vector3(1.0f, 0.0f, 0.0f), new Vector2(1.0f, 1.0f)),

                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, -w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, -w, w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(1.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, -w, -w), new Vector3(0.0f, -1.0f, 0.0f), new Vector2(0.0f, 1.0f)),

                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(w, w, -w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(0.0f, 1.0f)),
                new TexturedVertex(new Vector3(w, w, w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(0.0f, 0.0f)),
                new TexturedVertex(new Vector3(-w, w, -w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(1.0f, 1.0f)),
                new TexturedVertex(new Vector3(-w, w, w), new Vector3(0.0f, 1.0f, 0.0f), new Vector2(1.0f, 0.0f))
            };

            GL.GenVertexArrays(1, out VAO);

            GL.DeleteBuffer(VBO);
            GL.GenBuffers(1, out VBO);
            // fill buffer
            GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
            GL.BufferData(BufferTarget.ArrayBuffer, (Vertices.Length) * TexturedVertex.Size, Vertices, BufferUsageHint.StaticDraw);
            // link vertex attributes
            GL.BindVertexArray(VAO);
            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0);
            GL.EnableVertexAttribArray(1);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 3 * sizeof(float));
            GL.EnableVertexAttribArray(2);
            GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 8 * sizeof(float), 6 * sizeof(float));
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindVertexArray(0);
        }
Esempio n. 12
0
        private Mesh getMesh(JObject nodeJson)
        {
            int     meshId   = (int)nodeJson["mesh"];
            JObject meshJson = (JObject)_json["meshes"][meshId];

            List <Mesh.Primitive> primitives = new List <Mesh.Primitive>();

            foreach (JObject primitiveJson in (JArray)meshJson["primitives"])
            {
                // Material
                int materialId = (int)primitiveJson["material"];

                // Geometry
                if (primitiveJson["mode"] == null || (GLTFPrimitiveMode)(int)primitiveJson["mode"] != GLTFPrimitiveMode.TRIANGLES)
                {
                    throw new Exception($"Found unsupported primitive mode {(int)primitiveJson["mode"]} for {meshId}, only TRIANGLES (4) is supported.");
                }

                int positionAttributeAccessorId = (int)primitiveJson["attributes"]["POSITION"];
                var positionAccessor            = _json["accessors"][positionAttributeAccessorId];

                int verticesCount = (int)positionAccessor["count"];

                TexturedVertex[] staticVertices = new TexturedVertex[verticesCount];

                JToken       accessor, bufferView;
                int          sourceOffset, stride;
                BinaryReader buffer;

                // Position
                accessor = positionAccessor;
                if ((GLTFConst)(int)accessor["componentType"] != GLTFConst.FLOAT)
                {
                    throw new Exception($"Found unexpected component type {(int)accessor["componentType"]} for POSITION attribute of {meshId}, FLOAT was expected.");
                }

                bufferView = _json["bufferViews"][(int)accessor["bufferView"]];
                int accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                int bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                sourceOffset = bufferViewByteOffset + accessorByteOffset;
                stride       = (bufferView["byteStride"] != null) ? (int)bufferView["byteStride"] : sizeof(float) * 3;

                buffer = _buffers[(int)bufferView["buffer"]];

                for (var i = 0; i < verticesCount; i++)
                {
                    buffer.BaseStream.Position = sourceOffset + stride * i;

                    var position = new Vector3(buffer.ReadSingle(), buffer.ReadSingle(), buffer.ReadSingle());
                    staticVertices[i].Position = position;
                    staticVertices[i].Color    = Color4.White;
                }

                // Normals
                int normalAttributeAccessorId = (int)primitiveJson["attributes"]["NORMAL"];
                var normalAccessor            = _json["accessors"][normalAttributeAccessorId];
                accessor = normalAccessor;
                if ((GLTFConst)(int)accessor["componentType"] != GLTFConst.FLOAT)
                {
                    throw new Exception($"Found unexpected component type {(int)accessor["componentType"]} for NORMAL attribute of {meshId}, FLOAT was expected.");
                }

                bufferView           = _json["bufferViews"][(int)accessor["bufferView"]];
                accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                sourceOffset         = bufferViewByteOffset + accessorByteOffset;
                stride = (bufferView["byteStride"] != null) ? (int)bufferView["byteStride"] : sizeof(float) * 3;

                buffer = _buffers[(int)bufferView["buffer"]];

                for (var i = 0; i < verticesCount; i++)
                {
                    buffer.BaseStream.Position = sourceOffset + stride * i;

                    staticVertices[i].Normal = new Vector3(buffer.ReadSingle(), buffer.ReadSingle(), buffer.ReadSingle());
                }

                // Texture coordinates
                if (primitiveJson["attributes"]["TEXCOORD_0"] != null)
                {
                    int attributeAccessorId = (int)primitiveJson["attributes"]["TEXCOORD_0"];

                    if (primitiveJson["attributes"]["TEXCOORD_1"] != null)
                    {
                        throw new Exception("Dual textures primitives are not supported");
                    }

                    accessor = _json["accessors"][attributeAccessorId];
                    if ((GLTFConst)(int)accessor["componentType"] != GLTFConst.FLOAT)
                    {
                        throw new Exception($"Found unexpected component type {(int)accessor["componentType"]} for TEXCOORD_0 attribute of {meshId}, FLOAT was expected.");
                    }

                    bufferView           = _json["bufferViews"][(int)accessor["bufferView"]];
                    accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                    bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                    sourceOffset         = bufferViewByteOffset + accessorByteOffset;
                    stride = (bufferView["byteStride"] != null) ? (int)bufferView["byteStride"] : sizeof(float) * 2;

                    buffer = _buffers[(int)bufferView["buffer"]];

                    for (var i = 0; i < verticesCount; i++)
                    {
                        buffer.BaseStream.Position = sourceOffset + stride * i;

                        staticVertices[i].TextureCoord = new Vector2(buffer.ReadSingle(), buffer.ReadSingle());
                    }
                }

                // Indices
                ushort[] indices = new ushort[0];
                if (primitiveJson["indices"] != null)
                {
                    int indicesAccessorId = (int)primitiveJson["indices"];
                    var indicesAccessor   = _json["accessors"][indicesAccessorId];

                    indices  = new ushort[(int)indicesAccessor["count"]];
                    accessor = indicesAccessor;

                    bufferView           = _json["bufferViews"][(int)accessor["bufferView"]];
                    accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                    bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                    sourceOffset         = bufferViewByteOffset + accessorByteOffset;

                    buffer = _buffers[(int)bufferView["buffer"]];

                    for (var i = 0; i < indices.Length; i++)
                    {
                        buffer.BaseStream.Position = sourceOffset + sizeof(ushort) * i;
                        indices[i] = buffer.ReadUInt16();
                    }
                }

                Mesh.Primitive primitive = new Mesh.Primitive(staticVertices.ToList(), indices.ToList());
                primitive.MaterialModelId = materialId;
                primitives.Add(primitive);
            }

            Mesh mesh = new Mesh(primitives.ToArray());

            if (nodeJson["matrix"] != null)
            {
                JArray  m             = (JArray)nodeJson["matrix"];
                Matrix4 initialMatrix = new Matrix4((float)m[0], (float)m[1], (float)m[2], (float)m[3], (float)m[4], (float)m[5], (float)m[6], (float)m[7], (float)m[8], (float)m[9], (float)m[10], (float)m[11], (float)m[12], (float)m[13], (float)m[14], (float)m[15]);
                mesh.LocalMatrix = initialMatrix;
            }

            if (nodeJson["name"] != null)
            {
                mesh.Name = (string)nodeJson["name"];
            }

            return(mesh);
        }