Пример #1
0
 public void BindAttribute(VertexAttrib attribute, int offset)
 {
     if (BufferInitialized)
     {
         Vao.BindAttribute(attribute, VertexBuffer, offset);
     }
 }
Пример #2
0
 public void UnbindAttribute(VertexAttrib attribute)
 {
     if (BufferInitialized)
     {
         Vao.UnbindAttribute(attribute);
     }
 }
Пример #3
0
        public static Shape CreateCircle(VertexAttrib VertexPositionAttrib, float radius = 1, int slices = 64)
        {
            float dtheta = MathHelper.TwoPi / (slices - 1);
            var   theta  = 0f;

            Vector3[] Vertices = new Vector3[slices + 1];
            Vertices[0] = new Vector3(0, 0, 0);
            for (var i = 0; i < slices; i++)
            {
                Vertices[i + 1] = new Vector3((float)Math.Cos(theta) * radius, (float)Math.Sin(theta) * radius, 0);
                theta          += dtheta;
            }

            Buffer <Vector3> VertexBuffer = new Buffer <Vector3>();

            VertexBuffer.Init(BufferTarget.ArrayBuffer, Vertices);

            return(new DynamicShape()
                   .WithVertexAttrib(VertexPositionAttrib, VertexBuffer)
                   .WithDisposeFunction(() => {
                VertexBuffer?.Dispose();
            })
                   .SetDrawFunction((VertexArray VAO) => {
                VAO.DrawArrays(PrimitiveType.TriangleFan, 0, VertexBuffer.ElementCount);
            }));
        }
Пример #4
0
 /// <summary>
 /// Binds the buffer to the given vertex attribute.
 /// </summary>
 public void BindAttribute <T>(VertexAttrib attribute, Buffer <T> buffer, int components, VertexAttribPointerType type, int stride, int offset, bool normalized)
     where T : struct
 {
     if (!attribute.Active)
     {
         return;
     }
     BindAttribute(attribute.Index, buffer, components, type, stride, offset, normalized);
 }
Пример #5
0
        private VertexFormat(IEnumerable <VertexAttrib> attributes)
        {
            vertexAttributes = new VertexAttrib[attributes.Count()];

            for (int attributeIndex = 0; attributeIndex < attributes.Count(); attributeIndex++)
            {
                VertexAttrib attribute = attributes.ElementAt(attributeIndex);
                vertexAttributes[attributeIndex] = attribute;
                stride += attribute.GetSizeBytes();
            }
        }
Пример #6
0
        public void ApplyFormat()
        {
            VertexAttrib?prevAttribute = null;

            for (int attributeIndex = 0; attributeIndex < vertexAttributes.Length; attributeIndex++)
            {
                VertexAttrib attribute = vertexAttributes.ElementAt(attributeIndex);
                //If prevAttribute is null, this is the first attribute and the offset is 0. Otherwise the offset is the size of the previous attribute in bytes.
                int offset = prevAttribute.HasValue ? prevAttribute.Value.GetSizeBytes() : 0;
                GL.VertexAttribPointer(attributeIndex, attribute.count, attribute.pointerType, attribute.normalised, stride, offset);
                GL.EnableVertexAttribArray(attributeIndex);
                prevAttribute = attribute;
            }
        }
Пример #7
0
        private static DynamicShape CreateBasicCube(VertexAttrib VertexPositionAttrib)
        {
            Vector3[] Vertices = new[]
            {
                new Vector3(-1.0f, -1.0f, 1.0f),
                new Vector3(1.0f, -1.0f, 1.0f),
                new Vector3(1.0f, 1.0f, 1.0f),
                new Vector3(-1.0f, 1.0f, 1.0f),
                new Vector3(-1.0f, -1.0f, -1.0f),
                new Vector3(1.0f, -1.0f, -1.0f),
                new Vector3(1.0f, 1.0f, -1.0f),
                new Vector3(-1.0f, 1.0f, -1.0f)
            };

            uint[] Indices = new uint[]
            {
                // front face
                0, 1, 2, 2, 3, 0,
                // top face
                3, 2, 6, 6, 7, 3,
                // back face
                7, 6, 5, 5, 4, 7,
                // left face
                4, 0, 3, 3, 7, 4,
                // bottom face
                5, 1, 0, 0, 4, 5,
                // right face
                1, 5, 6, 6, 2, 1
            };

            Buffer <Vector3> VertexBuffer = new Buffer <Vector3>();

            VertexBuffer.Init(BufferTarget.ArrayBuffer, Vertices);
            Buffer <uint> IndexBuffer = new Buffer <uint>();

            IndexBuffer.Init(BufferTarget.ElementArrayBuffer, Indices);

            return(new DynamicShape()
                   .WithElementBuffer(IndexBuffer)
                   .WithVertexAttrib(VertexPositionAttrib, VertexBuffer)
                   .WithDisposeFunction(() => {
                VertexBuffer?.Dispose();
                IndexBuffer?.Dispose();
            })
                   .SetDrawFunction((VertexArray VAO) => {
                VAO.DrawElements(PrimitiveType.Triangles, IndexBuffer.ElementCount);
            }));
        }
Пример #8
0
        private void AddAttribute(ref int index, ref VertexAttrib attrib, ref uint enabledVertexAttribArrays)
        {
            int attribIndex;
            if (programAttributes.TryGetValue(attrib.AttributeName, out attribIndex))
            {
                vertexAttribs[index] = attrib;
                vertexAttribs[index].Index = attribIndex;

#if SILICONSTUDIO_XENKO_GRAPHICS_API_OPENGLES
                hasDynamicStagingVB |= attrib.VertexBufferId == 0;
#endif

                if (attribIndex != -1)
                    enabledVertexAttribArrays |= 1U << attribIndex;

                index++;
            }
        }
Пример #9
0
        public VertexAttribute(string name, VertexAttrib attrib, VertexType type, VertexComponents components, bool normalized = false)
        {
            Name       = name;
            Attrib     = attrib;
            Type       = type;
            Components = components;
            Normalized = normalized;

            ComponentSize = type switch
            {
                VertexType.Byte => 1,
                VertexType.Short => 2,
                VertexType.Int => 4,
                VertexType.Float => 4,
                _ => throw new NotImplementedException(),
            };

            AttributeSize = (int)Components * ComponentSize;
        }
Пример #10
0
        public static Shape CreateTexturedQuad(VertexAttrib VertexPositionAttrib, VertexAttrib VertexTexCoordAttrib)
        {
            Vector2[] TexCoords = new[]
            {
                new Vector2(0, 0),
                new Vector2(1, 0),
                new Vector2(0, 1),
                new Vector2(1, 1)
            };

            Buffer <Vector2> TexCoordBuffer = new Buffer <Vector2>();

            TexCoordBuffer.Init(BufferTarget.ArrayBuffer, TexCoords);

            return(CreateBasicQuad(VertexPositionAttrib)
                   .WithVertexAttrib(VertexTexCoordAttrib, TexCoordBuffer)
                   .WithDisposeFunction(() => {
                TexCoordBuffer?.Dispose();
            }));
        }
        // ---- METHODS (PRIVATE) --------------------------------------------------------------------------------------

        private Vector4F[] FromRawData(VertexBuffer vertexBuffer, VertexAttrib attrib)
        {
            // Create a reader on the raw bytes of the correct endianness.
            Buffer buffer = vertexBuffer.Buffers[attrib.BufferIndex];

            using (BinaryDataReader reader = new BinaryDataReader(new MemoryStream(buffer.Data[0])))
            {
                reader.ByteOrder = ByteOrder;

                // Get a conversion callback transforming the raw data into a Vector4F instance.
                Func <BinaryDataReader, Vector4F> callback = reader.GetGX2AttribCallback(attrib.Format);

                // Read the elements.
                Vector4F[] elements = new Vector4F[vertexBuffer.VertexCount];
                for (int i = 0; i < vertexBuffer.VertexCount; i++)
                {
                    reader.Position = attrib.Offset + i * buffer.Stride;
                    elements[i]     = callback.Invoke(reader);
                }
                return(elements);
            }
        }
Пример #12
0
        private static DynamicShape CreateBasicQuad(VertexAttrib VertexPositionAttrib)
        {
            Vector3[] Vertices = new[]
            {
                new Vector3(-1, -1, 0),
                new Vector3(1, -1, 0),
                new Vector3(-1, 1, 0),
                new Vector3(1, 1, 0)
            };

            Buffer <Vector3> VertexBuffer = new Buffer <Vector3>();

            VertexBuffer.Init(BufferTarget.ArrayBuffer, Vertices);

            return(new DynamicShape()
                   .WithVertexAttrib(VertexPositionAttrib, VertexBuffer)
                   .WithDisposeFunction(() => {
                VertexBuffer?.Dispose();
            })
                   .SetDrawFunction((VertexArray VAO) => {
                VAO.DrawArrays(PrimitiveType.TriangleStrip, 0, VertexBuffer.ElementCount);
            }));
        }
Пример #13
0
        public VertexArrayObjectInstance(GraphicsDevice graphicsDevice, EffectInputSignature effectInputSignature, VertexAttrib[] sharedVertexAttribs, int indexBufferId)
        {
            this.graphicsDevice = graphicsDevice;
            this.indexBufferId = indexBufferId;
            programAttributes = effectInputSignature.Attributes;

            int vertexAttributeCount = 0;
            for (int i = 0; i < sharedVertexAttribs.Length; i++)
            {
                if (programAttributes.ContainsKey(sharedVertexAttribs[i].AttributeName))
                {
                    vertexAttributeCount++;
                }
            }

            vertexAttribs = new VertexAttrib[vertexAttributeCount];

            int j = 0;
            for (int i = 0; i < sharedVertexAttribs.Length; i++)
            {
                AddAttribute(ref j, ref sharedVertexAttribs[i], ref enabledVertexAttribArrays);
            }
        }
Пример #14
0
        public static Shape CreateColoredCube(VertexAttrib VertexPositionAttrib, VertexAttrib VertexColorAttrib)
        {
            uint[] Colors = new List <Color>
            {
                Color.DarkRed,
                Color.DarkRed,
                Color.Gold,
                Color.Gold,
                Color.DarkRed,
                Color.DarkRed,
                Color.Gold,
                Color.Gold
            }.Select(_ => _.ToRgba32()).ToArray();

            Buffer <uint> ColorBuffer = new Buffer <uint>();

            ColorBuffer.Init(BufferTarget.ArrayBuffer, Colors);

            return(CreateBasicCube(VertexPositionAttrib)
                   .WithVertexAttrib(VertexColorAttrib, ColorBuffer)
                   .WithDisposeFunction(() => {
                ColorBuffer?.Dispose();
            }));
        }
Пример #15
0
 /// <summary>
 /// Disable the given vertex attribute.
 /// </summary>
 public void UnbindAttribute(VertexAttrib attribute)
 {
     UnbindAttribute(attribute.Index);
 }
Пример #16
0
 /// <summary>
 /// Binds the buffer to the given vertex attribute.
 /// </summary>
 public void BindAttribute <T>(VertexAttrib attribute, Buffer <T> buffer, int components, int stride, int offset, bool normalized)
     where T : struct
 {
     BindAttribute(attribute, buffer, components, attribute.Type, stride, offset, normalized);
 }
Пример #17
0
 /// <summary>
 /// Binds the buffer to the given vertex attribute. Uses the buffers element size as the stride parameter and the given offset.
 /// The other parameters, namely components, type and normalized are chosen according to the corresponding <see cref="VertexAttribAttribute"/> attribute.
 /// </summary>
 public void BindAttribute <T>(VertexAttrib attribute, Buffer <T> buffer, int offset)
     where T : struct
 {
     BindAttribute(attribute, buffer, attribute.Components, attribute.Type, buffer.ElementSize, offset, attribute.Normalized);
 }
Пример #18
0
        private void CreateAttributes()
        {
            int vertexAttribCount = 0;
            for (int i = 0; i < vertexBufferBindings.Length; ++i)
            {
                vertexAttribCount += vertexBufferBindings[i].Declaration.VertexElements.Length;
            }

            vertexAttribs = new VertexAttrib[vertexAttribCount];
            int j = 0;
            for (int i = 0; i < vertexBufferBindings.Length; ++i)
            {
                var inputSlot = vertexBufferBindings[i];
                var vertexBuffer = vertexBufferBindings[i].Buffer;

                foreach (var vertexElementAndOffset in inputSlot.Declaration.EnumerateWithOffsets())
                {
                    var vertexElement = vertexElementAndOffset.VertexElement;
                    var attributeName = "a_" + vertexElement.SemanticName + vertexElement.SemanticIndex;

                    var vertexElementFormat = ConvertVertexElementFormat(vertexElement.Format);
#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                    IntPtr bufferStart = vertexBuffer.ResourceId == 0
                        ? vertexBuffer.StagingData
                        : (IntPtr)vertexBufferBindings[i].Offset;
#else
                    var bufferStart = (IntPtr)vertexBufferBindings[i].Offset;
#endif
                    vertexAttribs[j] = new VertexAttrib
                    {
                        VertexBufferId = vertexBuffer.ResourceId,
                        Index = -1,
                        IsInteger = IsInteger(vertexElementFormat.Type),
                        Size = vertexElementFormat.Size,
                        Type = vertexElementFormat.Type,
                        Normalized = vertexElementFormat.Normalized,
                        Stride = inputSlot.Declaration.VertexStride,
                        Offset = bufferStart + vertexElementAndOffset.Offset,
                        AttributeName = attributeName
                    };

                    j++;
                }
            }

            if (indexBufferBinding != null)
            {
                indexBufferId = indexBufferBinding.Buffer.resourceId;

#if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
                if (GraphicsDevice.IsOpenGLES2 && indexBufferBinding.Is32Bit)
                    throw new PlatformNotSupportedException("32 bits index buffer are not supported on OpenGL ES 2.0");
                indexBufferOffset = (indexBufferId == 0 ? indexBufferBinding.Buffer.StagingData : IntPtr.Zero) +
                                    indexBufferBinding.Offset;
#else
                
                indexBufferOffset = (IntPtr)indexBufferBinding.Offset;
#endif
                drawElementsType = indexBufferBinding.Is32Bit ? DrawElementsType.UnsignedInt : DrawElementsType.UnsignedShort;
                indexElementSize = indexBufferBinding.Is32Bit ? 4 : 2;
            }

            // If we have a signature, we can already pre-create the instance
            if (preferredInputSignature != null)
            {
                preferredInstance = GetInstance(preferredInputSignature);
            }

            currentShaderSignature = preferredInputSignature;
            currentInstance = preferredInstance;
        }
Пример #19
0
        public SmModel(Model model, ByteOrder byteOrder)
        {
            foreach (String shapeKey in model.Shapes.Keys)
            {
                Shape shape = model.Shapes[shapeKey];

                // Get the vertex buffer for this FSHP
                VertexBuffer vertexBuffer = model.VertexBuffers[shape.VertexBufferIndex];

                // Initialize a variable that will be used to hold vertex data
                float[] verticesArray = null;

                // Find the position attributes
                foreach (string attribKey in vertexBuffer.Attributes.Keys)
                {
                    VertexAttrib attrib = vertexBuffer.Attributes[attribKey];

                    if (attribKey.Equals("_p0"))
                    {
                        // Get the buffer with positions
                        Syroot.NintenTools.Bfres.Buffer positionBuffer = vertexBuffer.Buffers[attrib.BufferIndex];

                        // Open a reader to make things easier
                        using (MemoryStream stream = new MemoryStream(positionBuffer.Data[0]))
                            using (BinaryDataReader reader = new BinaryDataReader(stream))
                            {
                                // Set the byte order to what the bfres specifies
                                reader.ByteOrder = byteOrder;

                                switch (attrib.Format)
                                {
                                case GX2AttribFormat.Format_32_32_32_32_Single:
                                case GX2AttribFormat.Format_32_32_32_Single:
                                    // Read in the whole buffer as floats
                                    List <float> verticesList = new List <float>();

                                    for (long pos = 0; pos < positionBuffer.Data[0].Length; pos += positionBuffer.Stride)
                                    {
                                        reader.Seek(pos, SeekOrigin.Begin);
                                        verticesList.Add(reader.ReadSingle());
                                        verticesList.Add(reader.ReadSingle());
                                        verticesList.Add(reader.ReadSingle());
                                        if (attrib.Format == GX2AttribFormat.Format_32_32_32_32_Single)
                                        {
                                            float temp = reader.ReadSingle();
                                            verticesList.Add(temp);
                                        }
                                    }

                                    // Convert the list into an array
                                    verticesArray = verticesList.ToArray();
                                    for (int i = 0; i < verticesArray.Length; i++)
                                    {
                                        Vector3 temp = new Vector3(verticesArray[i], verticesArray[i + 1], verticesArray[i + 2]);
                                        objVerts.Add(temp);
                                        i += 2;
                                    }

                                    break;

                                default:
                                    throw new Exception("Unsupported attribute format (" + attrib.Format + ")");
                                }
                            }

                        break;
                    }
                }

                // Generate the VBO for this FSHP
                int vboId;
                GL.GenBuffers(1, out vboId);
                GL.BindBuffer(BufferTarget.ArrayBuffer, vboId);
                GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * verticesArray.Length, verticesArray, BufferUsageHint.StaticDraw);

                // Use LoD 0 as the mesh
                meshes.Add(new SmMesh(shape.Meshes[0], vboId));
            }
        }
Пример #20
0
        public static Shape CreateTexturedCube(VertexAttrib VertexPositionAttrib, VertexAttrib VertexTexCoordAttrib)
        {
            var quad_uv_map = new[]
            {
                new Vector2(0, 0),
                new Vector2(0, 1),
                new Vector2(1, 1),
                new Vector2(1, 1),
                new Vector2(1, 0),
                new Vector2(0, 0),
            };


            Vector3[] Vertices = new[]
            {
                new Vector3(-1.0f, -1.0f, 1.0f),
                new Vector3(1.0f, -1.0f, 1.0f),
                new Vector3(1.0f, 1.0f, 1.0f),
                new Vector3(-1.0f, 1.0f, 1.0f),
                new Vector3(-1.0f, -1.0f, -1.0f),
                new Vector3(1.0f, -1.0f, -1.0f),
                new Vector3(1.0f, 1.0f, -1.0f),
                new Vector3(-1.0f, 1.0f, -1.0f)
            };

            uint[] Indices = new uint[]
            {
                // front face
                0, 1, 2, 2, 3, 0,
                // top face
                3, 2, 6, 6, 7, 3,
                // back face
                7, 6, 5, 5, 4, 7,
                // left face
                4, 0, 3, 3, 7, 4,
                // bottom face
                5, 1, 0, 0, 4, 5,
                // right face
                1, 5, 6, 6, 2, 1
            };

            Vertices = Indices.Select(idx => Vertices[idx]).ToArray();

            // Use predefined uv texture mapping for vertices
            Vector2[] TexCoords = Enumerable.Range(0, Vertices.Length).Select(i => quad_uv_map[i % quad_uv_map.Length]).ToArray();

            Buffer <Vector3> VertexBuffer = new Buffer <Vector3>();

            VertexBuffer.Init(BufferTarget.ArrayBuffer, Vertices);
            Buffer <Vector2> TexCoordBuffer = new Buffer <Vector2>();

            TexCoordBuffer.Init(BufferTarget.ArrayBuffer, TexCoords);

            return(new DynamicShape()
                   .WithVertexAttrib(VertexPositionAttrib, VertexBuffer)
                   .WithVertexAttrib(VertexTexCoordAttrib, TexCoordBuffer)
                   .WithDisposeFunction(() => {
                VertexBuffer?.Dispose();
                TexCoordBuffer?.Dispose();
            })
                   .SetDrawFunction((VertexArray VAO) => {
                VAO.DrawArrays(PrimitiveType.Triangles, 0, VertexBuffer.ElementCount);
            }));
        }
Пример #21
0
 public static Shape CreateCube(VertexAttrib VertexPositionAttrib)
 {
     return(CreateBasicCube(VertexPositionAttrib));
 }
Пример #22
0
 public static Shape CreateQuad(VertexAttrib VertexPositionAttrib)
 {
     return(CreateBasicQuad(VertexPositionAttrib));
 }
Пример #23
0
        private static Syroot.Maths.Vector4F[] AttributeData(VertexAttrib att, VertexBufferHelper helper, string attName)
        {
            VertexBufferHelperAttrib attd = helper[attName];

            return(attd.Data);
        }
Пример #24
0
 public DynamicShape WithVertexAttrib <T>(VertexAttrib Attrib, Buffer <T> Buffer) where T : struct
 {
     _VAO.BindAttribute(Attrib, Buffer);
     return(this);
 }