Пример #1
0
        private void BufferData(Vector3[] coordinates, ushort[] indices)
        {
            this.glBuffers = new[] { GL.GenVertexArray(), GL.GenBuffer(), GL.GenBuffer() };

            GL.BindVertexArray(glBuffers[0]);

            GL.BindBuffer(BufferTarget.ArrayBuffer, glBuffers[1]);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, glBuffers[2]);

            // assign colours
            var colourPallet = new[] 
            {
                selectedAxis.HasFlag(SelectedAxis.U) ? Colours.Selection : Colours.Red,
                selectedAxis.HasFlag(SelectedAxis.V) ? Colours.Selection : Colours.Green,
                selectedAxis.HasFlag(SelectedAxis.W) ? Colours.Selection : Colours.Blue
            };
            var colours = colourPallet.SelectMany(x => x.ToFloatRgb()).Concat(colourPallet.SelectMany(x => x.ToFloatRgb()));

            var colourCount = coordinates.Length - 6;
            var palletDivisor = colourCount / 3;
            for (int i = 0; i < colourCount; ++i)
            {
                var index = (i / palletDivisor) % palletDivisor;
                colours = colours.Concat(colourPallet[index].ToFloatRgb());
            }

            var colourArray = colours.ToArray();

            GL.BufferData<ushort>(
                BufferTarget.ElementArrayBuffer,
                (IntPtr)(sizeof(ushort) * indices.Length),
                indices,
                BufferUsageHint.StreamDraw);
            GL.BufferData(
                BufferTarget.ArrayBuffer,
                (IntPtr)(Vector3.SizeInBytes * coordinates.Length + sizeof(float) * colourArray.Length),
                IntPtr.Zero,
                BufferUsageHint.StreamDraw);

            BufferPositionData(coordinates);

            BufferColourData(Vector3.SizeInBytes * coordinates.Length, colourArray);

            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, (IntPtr)(Vector3.SizeInBytes * coordinates.Length));

            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);

            GL.BindVertexArray(0);
        }