public LinesBase(OpenGL gl, List<Tuple<vec3, vec3>> lines, Material material = null, OGLModelUsage usage = OGLModelUsage.StaticRead)
        {
            var verts = new vec3[lines.Count * 2];
            var normals = new vec3[lines.Count * 2];
            var indices = new uint[lines.Count * 2];

            for (int i = 0; i < lines.Count; i++)
            {
                var i2 = i * 2;
                verts[i2] = lines[i].Item1;
                verts[i2 + 1] = lines[i].Item2;

                normals[i2] = new vec3(1, 1, 1);
                normals[i2 + 1] = new vec3(1, 1, 1);

                indices[i2] = (uint)i2;
                indices[i2 + 1] = (uint)(i2 + 1);
            }

            if (material != null)
                Material = material;

            Vertices = verts;
            Normals = normals;
            Indices = indices;
            GlDrawMode = OpenGL.GL_LINES;

            if (gl != null)
                GenerateGeometry(gl, usage);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the vertices, normals and indices and creates them for the OpenGL.
        /// This method has to be called once before drawing.
        /// </summary>
        /// <param name="gl"></param>
        public void GenerateGeometry(OpenGL gl, OGLModelUsage usage)
        {
            GL     = gl;
            _usage = usage;

            // Create the data buffers.
            var buffers = OGLBufferId.CreateBufferIds(gl, 3);

            IndexBuffer  = new IBO(buffers[0]);
            NormalBuffer = new VBO(buffers[1]);
            VertexBuffer = new VBO(buffers[2]);

            if (AutoCalculateNormals)
            {
                CalculateNormals();
            }

            var vertData = Vertices.SelectMany(v => v.to_array()).ToArray();

            VertexBuffer.BindBuffer(gl);                                                         // GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer.Buffer.Value);
            VertexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, vertData, usage, 3); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, vertData, (uint)usage);

            var normData = Normals.SelectMany(v => v.to_array()).ToArray();

            NormalBuffer.BindBuffer(gl);                                                         //GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, NormalBuffer.Buffer.Value);
            NormalBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, normData, usage, 3); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, normData, (uint)usage);

            IndexBuffer.BindBuffer(gl);                                                          // GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, IndexBuffer.Buffer.Value);
            IndexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, Indices, usage, 1);   // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, Indices, (uint)usage);

            if (new OGLModelUsage[] { OGLModelUsage.StaticCopy, OGLModelUsage.StaticDraw, OGLModelUsage.StaticRead }.Contains(usage))
            {
                ClearStaticData();
            }
        }
        public void SetBufferData(OpenGL gl, OGLBufferDataTarget target, uint[] indices, OGLModelUsage usage, bool compress)
        {
            Size = indices.Length;
            Stride = 1;

            if(compress)
                if (indices.Max() < byte.MaxValue)
                {
                    // use a byte buffer instead in order to decrease the buffer size.
                    var newIdxs = indices.Select(x => (byte)x).ToArray();
                    SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, newIdxs, OGLModelUsage.StaticCopy, Stride);
                    return;
                }
                else if (indices.Max() < ushort.MaxValue)
                {
                    // use a ushort buffer instead in order to decrease the buffer size.
                    var newIdxs = indices.Select(x => (ushort)x).ToArray();
                    SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, newIdxs, OGLModelUsage.StaticCopy, Stride);
                    return;
                }

            SetBufferData(gl, SharpGLHelper.OGLBufferDataTarget.ArrayBuffer, indices, SharpGLHelper.OGLModelUsage.StaticCopy, Stride);
        }
        /// <summary>
        /// Calls the glBufferData(...). The data.Length will be used as value for Size property.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="target">The buffer data target.</param>
        /// <param name="data">The data as float.</param>
        /// <param name="usage">The usage.</param>
        /// <param name="stride">The amount of floats that form one input variable.</param>
        public virtual void SetBufferData(OpenGL gl, OGLBufferDataTarget target, float[] data, OGLModelUsage usage, int stride)
        {
            Size   = data.Length / stride;
            Stride = stride;

            BufferDataType = OGLType.Float;

            gl.BufferData((uint)target, data, (uint)usage);
        }
        /// <summary>
        /// Calls the glBufferData(...). The size will be used as value for Size property.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="target">The buffer data target.</param>
        /// <param name="size">The size of the data buffer.</param>
        /// <param name="data">The pointer to the data in memory.</param>
        /// <param name="usage">The usage.</param>
        public virtual void SetBufferData(OpenGL gl, OGLBufferDataTarget target, int size, IntPtr data, OGLModelUsage usage, int stride, OGLType bufferDataType,
                                          bool bind = false, OGLBindBufferTarget bindTarget = OGLBindBufferTarget.ArrayBuffer)
        {
            Size   = size / stride;
            Stride = stride;


            if (bind)
            {
                BindBuffer(gl, bindTarget);
            }
            gl.BufferData((uint)target, size, data, (uint)usage);
        }
        /// <summary>
        /// Calls the glBufferData(...). The data.Length will be used as value for Size property.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="target">The buffer data target.</param>
        /// <param name="data">The data as uint.</param>
        /// <param name="usage">The usage.</param>
        /// <param name="stride">The amount of uint that form one input variable.</param>
        public virtual void SetBufferData(OpenGL gl, OGLBufferDataTarget target, uint[] data, OGLModelUsage usage, int stride)
        {
            Size   = data.Length / stride;
            Stride = stride;

            BufferDataType = OGLType.UnsignedInt;

            var    dataSize   = data.Length * sizeof(uint);
            IntPtr newDataPtr = Marshal.AllocHGlobal(dataSize);
            var    intData    = new int[data.Length];

            Buffer.BlockCopy(data, 0, intData, 0, dataSize);
            Marshal.Copy(intData, 0, newDataPtr, data.Length);

            gl.BufferData((uint)target, dataSize, newDataPtr, (uint)usage);
        }
Ejemplo n.º 7
0
        public LinesBase(OpenGL gl, List <Tuple <vec3, vec3> > lines, Material material = null, OGLModelUsage usage = OGLModelUsage.StaticRead)
        {
            var verts   = new vec3[lines.Count * 2];
            var normals = new vec3[lines.Count * 2];
            var indices = new uint[lines.Count * 2];

            for (int i = 0; i < lines.Count; i++)
            {
                var i2 = i * 2;
                verts[i2]     = lines[i].Item1;
                verts[i2 + 1] = lines[i].Item2;

                normals[i2]     = new vec3(1, 1, 1);
                normals[i2 + 1] = new vec3(1, 1, 1);

                indices[i2]     = (uint)i2;
                indices[i2 + 1] = (uint)(i2 + 1);
            }

            if (material != null)
            {
                Material = material;
            }

            Vertices   = verts;
            Normals    = normals;
            Indices    = indices;
            GlDrawMode = OpenGL.GL_LINES;


            if (gl != null)
            {
                GenerateGeometry(gl, usage);
            }
        }
Ejemplo n.º 8
0
        public void SetBufferData(OpenGL gl, OGLBufferDataTarget target, uint[] indices, OGLModelUsage usage, bool compress)
        {
            Size   = indices.Length;
            Stride = 1;


            if (compress)
            {
                if (indices.Max() < byte.MaxValue)
                {
                    // use a byte buffer instead in order to decrease the buffer size.
                    var newIdxs = indices.Select(x => (byte)x).ToArray();
                    SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, newIdxs, OGLModelUsage.StaticCopy, Stride);
                    return;
                }
                else if (indices.Max() < ushort.MaxValue)
                {
                    // use a ushort buffer instead in order to decrease the buffer size.
                    var newIdxs = indices.Select(x => (ushort)x).ToArray();
                    SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, newIdxs, OGLModelUsage.StaticCopy, Stride);
                    return;
                }
            }


            SetBufferData(gl, SharpGLHelper.OGLBufferDataTarget.ArrayBuffer, indices, SharpGLHelper.OGLModelUsage.StaticCopy, Stride);
        }
Ejemplo n.º 9
0
        public void SetBufferData(OpenGL gl, IEnumerable <TransformationMatrix> transformations, OGLModelUsage usage = OGLModelUsage.StaticRead)
        {
            var transCount = transformations.Count();

            // Validation.
            if (transCount > Math.Pow(2, 24))
            {
                throw new OverflowException(
                          "This shader can't handle more than 2^24 transformations while " +
                          "using 24 bit colors.");
            }

            #region get indices
            var indices = new ushort[transCount];
            for (ushort i = 0; i < indices.Length; i++)
            {
                indices[i] = i; // Do all transformations once.
            }
            #endregion get indices

            #region get transformations array
            var stride = 16; // Transformation matrix is a 4x4 = 16.

            var transformationsArray = new float[transCount * stride];
            for (int i = 0; i < transCount; i++)
            {
                float[] transAsFloats = transformations.ElementAt(i).ResultMatrix.to_array();
                for (int j = 0; j < stride; j++)
                {
                    transformationsArray[i * stride + j] = transAsFloats[j];
                }
            }
            #endregion get transformations array

            #region get color array
            int colorStride = 3;
            var colorArray  = new float[transCount * colorStride];

            for (int i = 0; i < transCount; i++)
            {
                ulong id    = transformations.ElementAt(i).UniqueId;
                var   color = new ColorF((uint)id);

                colorArray[i * stride]     = color.R;
                colorArray[i * stride + 1] = color.G;
                colorArray[i * stride + 2] = color.B;
            }
            #endregion get color array



            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _indicesBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, indices, (uint)usage);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _transformationsBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, transformationsArray, (uint)usage);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _colorsBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray, (uint)usage);
        }
        public void SetBufferData(OpenGL gl, IEnumerable<TransformationMatrix> transformations, OGLModelUsage usage = OGLModelUsage.StaticRead)
        {
            var transCount = transformations.Count();

            // Validation.
            if(transCount > Math.Pow(2, 24))
            {
                throw new OverflowException(
                    "This shader can't handle more than 2^24 transformations while "+
                    "using 24 bit colors.");
            }

            #region get indices
            var indices = new ushort[transCount];
            for (ushort i = 0; i < indices.Length; i++)
            {
                indices[i] = i; // Do all transformations once.
            }
            #endregion get indices

            #region get transformations array
            var stride = 16; // Transformation matrix is a 4x4 = 16.

            var transformationsArray = new float[transCount * stride];
            for (int i = 0; i < transCount; i++)
            {
                float[] transAsFloats = transformations.ElementAt(i).ResultMatrix.to_array();
                for (int j = 0; j < stride; j++)
                {
                    transformationsArray[i * stride + j] = transAsFloats[j];
                }
            }
            #endregion get transformations array

            #region get color array
            int colorStride = 3;
            var colorArray = new float[transCount * colorStride];

            for (int i = 0; i < transCount; i++)
            {
                ulong id = transformations.ElementAt(i).UniqueId;
                var color = new ColorF((uint) id);

                colorArray[i * stride] = color.R;
                colorArray[i * stride + 1] = color.G;
                colorArray[i * stride + 2] = color.B;
            }
            #endregion get color array

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _indicesBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, indices, (uint)usage);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _transformationsBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, transformationsArray, (uint)usage);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _colorsBufferId.Value);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray, (uint)usage);
        }
        /// <summary>
        /// Generates the vertices, normals and indices and creates them for the OpenGL.
        /// This method has to be called once before drawing. 
        /// </summary>
        /// <param name="gl"></param>
        public void GenerateGeometry(OpenGL gl, OGLModelUsage usage)
        {
            GL = gl;
            _usage = usage;

            // Create the data buffers.
            var buffers = OGLBufferId.CreateBufferIds(gl, 3);
            IndexBuffer = new IBO(buffers[0]);
            NormalBuffer = new VBO(buffers[1]);
            VertexBuffer = new VBO(buffers[2]);

            if (AutoCalculateNormals)
            {
                CalculateNormals();
            }

            var vertData = Vertices.SelectMany(v => v.to_array()).ToArray();
            VertexBuffer.BindBuffer(gl); // GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer.Buffer.Value);
            VertexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, vertData, usage, 3); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, vertData, (uint)usage);

            var normData = Normals.SelectMany(v => v.to_array()).ToArray();
            NormalBuffer.BindBuffer(gl); //GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, NormalBuffer.Buffer.Value);
            NormalBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, normData, usage, 3); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, normData, (uint)usage);

            IndexBuffer.BindBuffer(gl); // GL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, IndexBuffer.Buffer.Value);
            IndexBuffer.SetBufferData(gl, OGLBufferDataTarget.ArrayBuffer, Indices, usage, 1); // GL.BufferData(OpenGL.GL_ARRAY_BUFFER, Indices, (uint)usage);

            if (new OGLModelUsage[]{OGLModelUsage.StaticCopy, OGLModelUsage.StaticDraw, OGLModelUsage.StaticRead}.Contains(usage))
            {
                ClearStaticData();
            }
        }
        /// <summary>
        /// Calls the glBufferData(...). The size will be used as value for Size property.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="target">The buffer data target.</param>
        /// <param name="size">The size of the data buffer.</param>
        /// <param name="data">The pointer to the data in memory.</param>
        /// <param name="usage">The usage.</param>
        public virtual void SetBufferData(OpenGL gl, OGLBufferDataTarget target, int size, IntPtr data, OGLModelUsage usage, int stride, OGLType bufferDataType,
            bool bind = false, OGLBindBufferTarget bindTarget = OGLBindBufferTarget.ArrayBuffer)
        {
            Size = size / stride;
            Stride = stride;

            if(bind)
                BindBuffer(gl, bindTarget);
            gl.BufferData((uint)target, size, data, (uint)usage);
        }
        /// <summary>
        /// Calls the glBufferData(...). The data.Length will be used as value for Size property.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="target">The buffer data target.</param>
        /// <param name="data">The data as uint.</param>
        /// <param name="usage">The usage.</param>
        /// <param name="stride">The amount of uint that form one input variable.</param>
        public virtual void SetBufferData(OpenGL gl, OGLBufferDataTarget target, uint[] data, OGLModelUsage usage, int stride)
        {
            Size = data.Length / stride;
            Stride = stride;

            BufferDataType = OGLType.UnsignedInt;

            var dataSize = data.Length * sizeof(uint);
            IntPtr newDataPtr = Marshal.AllocHGlobal(dataSize);
            var intData = new int[data.Length];
            Buffer.BlockCopy(data, 0, intData, 0, dataSize);
            Marshal.Copy(intData, 0, newDataPtr, data.Length);

            gl.BufferData((uint)target, dataSize, newDataPtr, (uint)usage);
        }
        /// <summary>
        /// Calls the glBufferData(...). The data.Length will be used as value for Size property.
        /// </summary>
        /// <param name="gl">The gl.</param>
        /// <param name="target">The buffer data target.</param>
        /// <param name="data">The data as ushort.</param>
        /// <param name="usage">The usage.</param>
        /// <param name="stride">The amount of ushorts that form one input variable.</param>
        public virtual void SetBufferData(OpenGL gl, OGLBufferDataTarget target, ushort[] data, OGLModelUsage usage, int stride)
        {
            Size = data.Length / stride;
            Stride = stride;

            BufferDataType = OGLType.UnsignedShort;

            gl.BufferData((uint)target, data, (uint)usage);
        }