private void InitVertexes(OpenGL gl, UnmanagedArray <Vertex> vertexes, UnmanagedArray <ColorF> colorArray, UnmanagedArray <float> visibles)
        {
            uint[] vao = new uint[1];
            gl.GenVertexArrays(vao.Length, vao);
            gl.BindVertexArray(vao[0]);

            this.vertexArrayObject = vao[0];
            uint[] vboVertex = new uint[1];
            gl.GenBuffers(vboVertex.Length, vboVertex);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVertex[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexes.ByteLength, vertexes.Header, OpenGL.GL_STATIC_DRAW);
            gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION);
            this.vertexsBufferObject = vboVertex[0];


            uint[] vboColor = new uint[1];
            gl.GenBuffers(vboColor.Length, vboColor);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboColor[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray.ByteLength, colorArray.Header, OpenGL.GL_DYNAMIC_DRAW);
            gl.VertexAttribPointer(ATTRIB_INDEX_COLOUR, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(ATTRIB_INDEX_COLOUR);
            this.colorsBufferObject = vboColor[0];

            uint[] vboVisual = new uint[1];
            gl.GenBuffers(vboVisual.Length, vboVisual);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVisual[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, visibles.ByteLength, visibles.Header, OpenGL.GL_DYNAMIC_READ);
            gl.VertexAttribPointer(ATTRIB_INDEX_VISIBLE, 1, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(ATTRIB_INDEX_VISIBLE);
            this.visiblesBufferObject = vboVisual[0];

            gl.BindVertexArray(0);
        }
        //private void InitVertexes(OpenGL gl, float  [] vertexes, float[] colorArray)
        //{
        //    uint ATTRIB_INDEX_POSITION = 0;
        //    uint ATTRIB_INDEX_COLOUR = 1;

        //    uint[] vao = new uint[1];
        //    gl.GenVertexArrays(vao.Length, vao);
        //    gl.BindVertexArray(vao[0]);

        //    uint[] vboVertex = new uint[1];
        //    gl.GenBuffers(vboVertex.Length, vboVertex);
        //    gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVertex[0]);
        //    gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexes, OpenGL.GL_STATIC_DRAW);
        //    gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
        //    gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION);

        //    uint[] vboColor = new uint[1];
        //    gl.GenBuffers(vboColor.Length, vboColor);
        //    gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboColor[0]);
        //    gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray, OpenGL.GL_DYNAMIC_DRAW);
        //    gl.VertexAttribPointer(ATTRIB_INDEX_COLOUR, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
        //    gl.EnableVertexAttribArray(ATTRIB_INDEX_COLOUR);

        //    gl.BindVertexArray(0);

        //    gl.InvalidateBufferData(vboVertex[0]);
        //    gl.InvalidateBufferData(vboColor[0]);
        //    gl.DeleteBuffers(1, vboVertex);
        //    gl.DeleteBuffers(1, vboColor);
        //    gl.DeleteVertexArrays(1, vao);
        //}
        private void InitVertexes(OpenGL gl, UnmanagedArray <Vertex> vertexes, UnmanagedArray <ColorF> colorArray)
        {
            uint ATTRIB_INDEX_POSITION = 0;
            uint ATTRIB_INDEX_COLOUR   = 1;

            uint[] vao = new uint[1];
            gl.GenVertexArrays(vao.Length, vao);
            gl.BindVertexArray(vao[0]);

            uint[] vboVertex = new uint[1];
            gl.GenBuffers(vboVertex.Length, vboVertex);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVertex[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexes.ByteLength, vertexes.Header, OpenGL.GL_STATIC_DRAW);
            gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION);

            uint[] vboColor = new uint[1];
            gl.GenBuffers(vboColor.Length, vboColor);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboColor[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray.ByteLength, colorArray.Header, OpenGL.GL_DYNAMIC_DRAW);
            gl.VertexAttribPointer(ATTRIB_INDEX_COLOUR, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(ATTRIB_INDEX_COLOUR);

            gl.BindVertexArray(0);

            //gl.InvalidateBufferData(vboVertex[0]);
            //gl.InvalidateBufferData(vboColor[0]);
            gl.DeleteBuffers(1, vboVertex);
            gl.DeleteBuffers(1, vboColor);
            gl.DeleteVertexArrays(1, vao);
        }
Beispiel #3
0
        /// <summary>
        /// Creation of vertex and color buffer from a List<Vertex>. This function reset all values once a new file is loaded
        /// </summary>
        private void createInitialBuffers()
        {
            //  Get the OpenGL object.
            OpenGL gl = openGLControl.OpenGL;

            //create
            if (l_sizes == null)
            {
                l_sizes = new List <int>();  //create list to store points of each buffer's list
            }
            l_sizes.Add(l_vertex.Count);

            uint [] ids = new uint[2];
            l_vboId.Add(ids);
            //create buffers
            gl.GenBuffers(2, l_vboId[0]);
            //vertex buffer
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[0][0]);
            IntPtr ptr1 = GCHandle.Alloc(l_vertex.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject();

            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * l_vertex.Count * 3, ptr1, OpenGL.GL_STATIC_DRAW);
            //color buffer
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[0][1]);
            IntPtr ptr2 = GCHandle.Alloc(l_color.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject();

            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * l_color.Count * 3, ptr2, OpenGL.GL_DYNAMIC_DRAW);
            //unbind buffers
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);
        }
Beispiel #4
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);
        }
        protected void InitializeVAO(OpenGL gl, out uint[] vao, out BeginMode primitiveMode, out int vertexCount)
        {
            primitiveMode = BeginMode.Quads;
            vertexCount   = positions.Length;

            vao = new uint[1];
            gl.GenVertexArrays(1, vao);
            gl.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]);
                var positionArray = new UnmanagedArray <vec3>(positions.Length);
                for (int i = 0; i < positions.Length; i++)
                {
                    positionArray[i] = positions[i];
                }

                uint positionLocation = (uint)shaderProgram.GetAttributeLocation(gl, strin_Position);

                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, positionArray.ByteLength, positionArray.Header, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(positionLocation, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }
            //  Create a vertex buffer for the uv data.
            {
                uint[] ids = new uint[1];
                gl.GenBuffers(1, ids);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]);
                var uvArray = new UnmanagedArray <vec2>(uvs.Length);
                for (int i = 0; i < uvs.Length; i++)
                {
                    uvArray[i] = uvs[i];
                }

                uint uvLocation = (uint)shaderProgram.GetAttributeLocation(gl, strin_uv);

                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, uvArray.ByteLength, uvArray.Header, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(uvLocation, 2, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(uvLocation);

                uvArray.Dispose();
            }
            //  Unbind the vertex array, we've finished specifying data for it.
            gl.BindVertexArray(0);
        }
        private void LoadSceneData(OpenGL gl)
        {
            var modelLoader = Model.Load("male_head");

            modelLoader.Wait();
            Shape = modelLoader.Result;
            UI    = new Legend();

            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.Enable(OpenGL.GL_CULL_FACE);

            gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            gl.GenVertexArrays(ArrayIds.Length, ArrayIds);

            // Model data
            gl.BindVertexArray(ArrayIds[0]);
            gl.GenBuffers(ModelBufferIds.Length, ModelBufferIds);

            gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ModelBufferIds[0]);
            gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, Shape.Indices, OpenGL.GL_STATIC_DRAW);

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ModelBufferIds[1]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, Shape.Data, OpenGL.GL_STATIC_DRAW);

            gl.EnableVertexAttribArray(0);
            gl.EnableVertexAttribArray(1);
            gl.EnableVertexAttribArray(2);
            gl.VertexAttribPointer(0, Shape.GetAttribSize(0), Shape.GetAttribType(0), Shape.ShouldAttribNormalize(0), Shape.GetAttribStride(0), Shape.GetAttribOffset(0));
            gl.VertexAttribPointer(1, Shape.GetAttribSize(1), Shape.GetAttribType(1), Shape.ShouldAttribNormalize(1), Shape.GetAttribStride(1), Shape.GetAttribOffset(1));
            gl.VertexAttribPointer(2, Shape.GetAttribSize(2), Shape.GetAttribType(2), Shape.ShouldAttribNormalize(2), Shape.GetAttribStride(2), Shape.GetAttribOffset(2));

            // UI data
            gl.BindVertexArray(ArrayIds[1]);
            gl.GenBuffers(UIBufferIds.Length, UIBufferIds);

            gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, UIBufferIds[0]);
            gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, UI.Indices, OpenGL.GL_STATIC_DRAW);

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, UIBufferIds[1]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, UI.Data, OpenGL.GL_STATIC_DRAW);

            gl.EnableVertexAttribArray(0);
            gl.EnableVertexAttribArray(1);
            gl.EnableVertexAttribArray(2);
            gl.VertexAttribPointer(0, UI.GetAttribSize(0), UI.GetAttribType(0), UI.ShouldAttribNormalize(0), UI.GetAttribStride(0), UI.GetAttribOffset(0));
            gl.VertexAttribPointer(1, UI.GetAttribSize(1), UI.GetAttribType(1), UI.ShouldAttribNormalize(1), UI.GetAttribStride(1), UI.GetAttribOffset(1));
            gl.VertexAttribPointer(2, UI.GetAttribSize(2), UI.GetAttribType(2), UI.ShouldAttribNormalize(2), UI.GetAttribStride(2), UI.GetAttribOffset(2));
        }
Beispiel #7
0
        public void BufferData(OpenGL gl,
                               uint[] indices, float[] vertices, float[] normals,
                               float[] ambs, float[] diffs, float[] specs, float[] shinis,
                               float[] htColor)
        {
            gl.BindBuffer(_iboTarget, Ibo);
            gl.BufferData(_iboTarget, indices, _usage);

            gl.BindBuffer(_vboTarget, Position);
            gl.BufferData(_vboTarget, vertices, _usage);

            gl.BindBuffer(_vboTarget, Normal);
            gl.BufferData(_vboTarget, normals, _usage);

            gl.BindBuffer(_vboTarget, AmbientMaterial);
            gl.BufferData(_vboTarget, ambs, _usage);

            gl.BindBuffer(_vboTarget, DiffuseMaterial);
            gl.BufferData(_vboTarget, diffs, _usage);

            gl.BindBuffer(_vboTarget, SpecularMaterial);
            gl.BufferData(_vboTarget, specs, _usage);

            gl.BindBuffer(_vboTarget, ShininessValue);
            gl.BufferData(_vboTarget, shinis, _usage);

            gl.BindBuffer(_vboTarget, HTColorId);
            gl.BufferData(_vboTarget, htColor, _usage);
        }
Beispiel #8
0
        protected void Initialise(OpenGL gl, string textureName)
        {
            sp = new ShaderProg(gl);

            // Load the shader's & link them to the program
            shaderProgram = sp.Loader("Shader.vert", "Shader.frag");

            VBA = new uint[1];
            gl.GenVertexArrays(1, VBA);
            gl.BindVertexArray(VBA[0]);

            // Allocate 2 buffers (verts & UV's)
            VBO = new uint[2];

            // Generate the 2 buffers
            gl.GenBuffers(2, VBO);

            // Bind the first buffer
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO[0]);
            // Copy the data from the Vertex array into the array buffer
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices, OpenGL.GL_STATIC_DRAW);
            // Align the Buffer to the Vertex shader layout location value (0)
            gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 3 * sizeof(float), IntPtr.Zero);
            // Unbind the buffer
            gl.EnableVertexAttribArray(0);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);

            // Bind the UV's buffer
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO[1]);
            // Copy the data from the Colour array into the array buffer
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, texCoords, OpenGL.GL_STATIC_DRAW);
            // Align the Buffer to the Vertex shader layout location value (1)
            gl.VertexAttribPointer(1, 2, OpenGL.GL_FLOAT, false, 2 * sizeof(float), IntPtr.Zero);
            // Unbind the buffer
            gl.EnableVertexAttribArray(1);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);

            // Unbind VBA
            gl.BindVertexArray(0);

            texture.Create(gl, textureName);
            textureValue = texture.TextureName;

            //  Specify linear filtering.
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);

            //  Create a model matrix to make the model a little bigger.
            modelMatrix = glm.scale(new mat4(1.0f), new vec3(1.5f));//2.5
        }
Beispiel #9
0
        public void BufferData(OpenGL gl,
                               uint[] indices, float[] vertices, float[] colors)
        {
            if (indices != null)
            {
                gl.BindBuffer(_iboTarget, Ibo);
                gl.BufferData(_iboTarget, indices, _usage);
            }

            gl.BindBuffer(_vboTarget, Position);
            gl.BufferData(_vboTarget, vertices, _usage);

            gl.BindBuffer(_vboTarget, ColorValue);
            gl.BufferData(_vboTarget, colors, _usage);
        }
        public void InitializeGrid(float duration)
        {
            InitializeBuffers();

            var linesCount = ((int)duration + 1) * SubSegmentsInUnit + 1;

            _verticesCount    = (linesCount + 1) * 2;
            _linesCoordinates = new float[_verticesCount * 2];
            for (int i = 0; i < linesCount; i++)
            {
                var yValue = 0.15f;
                if (i % SubSegmentsInUnit == 0)
                {
                    yValue *= 3;
                }

                _linesCoordinates[i * 4]     = (float)i / SubSegmentsInUnit;
                _linesCoordinates[i * 4 + 1] = -yValue;

                _linesCoordinates[i * 4 + 2] = (float)i / SubSegmentsInUnit;
                _linesCoordinates[i * 4 + 3] = yValue;
            }

            //x axis line
            _linesCoordinates[linesCount * 4]     = 0;
            _linesCoordinates[linesCount * 4 + 1] = 0;
            _linesCoordinates[linesCount * 4 + 2] = duration;
            _linesCoordinates[linesCount * 4 + 3] = 0;

            _gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vbo);
            _gl.BufferData(OpenGL.GL_ARRAY_BUFFER, _linesCoordinates, OpenGL.GL_STATIC_DRAW);
            _gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, OpenGLUtils.NO_BUFFER);
        }
Beispiel #11
0
        protected override void DoInitialize()
        {
            base.DoInitialize();

            {
                // velocity
                var velocity_buffer = new uint[1];
                OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, velocity_buffer);
                OpenGL.BindBuffer(BufferTarget.ArrayBuffer, velocity_buffer[0]);
                var velocities = new UnmanagedArray <vec4>(SunshineParticleModel.particleCount);
                unsafe
                {
                    var random = new Random();
                    var array  = (vec4 *)velocities.Header.ToPointer();
                    for (int i = 0; i < SunshineParticleModel.particleCount; i++)
                    {
                        array[i] = new vec4(
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            (float)(random.NextDouble() - 0.5) * 0.2f,
                            0);
                    }
                }
                OpenGL.BufferData(BufferTarget.ArrayBuffer, velocities, BufferUsage.DynamicCopy);
                velocities.Dispose();
                //GL.GetDelegateFor<GL.glVertexAttribPointer>()(0, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                //GL.GetDelegateFor<GL.glEnableVertexAttribArray>()(0);
                //
                OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0);
                this.VelocityBufferPtrId = velocity_buffer[0];
            }

            this.PositionBufferPtr = this.bufferable.GetProperty(SunshineParticleModel.strPosition, null);
            this.VertexArrayObject = this.vertexArrayObject;
        }
Beispiel #12
0
 public void SetData(OpenGL gl, uint attributeIndex, float[] rawData, bool isNormalised, int stride)
 {
     //  Set the data, specify its shape and assign it to a vertex attribute (so shaders can bind to it).
     gl.BufferData(OpenGL.GL_ARRAY_BUFFER, rawData, OpenGL.GL_STATIC_DRAW);
     gl.VertexAttribPointer(attributeIndex, stride, OpenGL.GL_FLOAT, isNormalised, 0, IntPtr.Zero);
     gl.EnableVertexAttribArray(attributeIndex);
 }
Beispiel #13
0
        private VertexBufferArray CreateVertexBufferArray(OpenGL gl, PrimordialObject primordialObject)
        {
            vertexBufferArray = new VertexBufferArray();
            vertexBufferArray.Create(gl);
            vertexBufferArray.Bind(gl);

            vertexDataBuffer = new VertexBuffer();
            vertexDataBuffer.Create(gl);
            vertexDataBuffer.Bind(gl);
            //  for(int i = 0; i<100; i++) {
            //  var sendDataTime = _stopwatch.ElapsedMilliseconds;
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, primordialObject.VertexData, OpenGL.GL_STATIC_DRAW);
            //  File.AppendAllText(fileTitle + "Data.txt", (_stopwatch.ElapsedMilliseconds - sendDataTime).ToString() + "\n");
            // }
            gl.VertexAttribPointer(attributeIndexPosition, 4, OpenGL.GL_FLOAT, false, 3 * 4 * sizeof(float), IntPtr.Zero);
            gl.EnableVertexAttribArray(0);


            gl.VertexAttribPointer(attributeIndexColour, 4, OpenGL.GL_FLOAT, false, 3 * 4 * sizeof(float), IntPtr.Add(IntPtr.Zero, 4 * sizeof(float)));
            gl.EnableVertexAttribArray(1);

            gl.VertexAttribPointer(attributeIndexNormal, 4, OpenGL.GL_FLOAT, false, 3 * 4 * sizeof(float), IntPtr.Add(IntPtr.Zero, 8 * sizeof(float)));
            gl.EnableVertexAttribArray(2);
            vertexDataBuffer.Unbind(gl);
            vertexBufferArray.Unbind(gl);
            return(vertexBufferArray);
        }
Beispiel #14
0
        public void Create(OpenGL gl, Viewport viewport)
        {
            _viewport = viewport;
            GC        = new GraphicsContext();
            GC.GL     = gl;
            shader    = new Shader(GC, "vertex.vert", "frag.frag");

            float[] vertices =
            {
                -1f, -1f,   0f,
                -1f,  1f,   0f,
                1f,  -1f,   0f,
                1f,   1f, 0.0f,
            };
            gl.GenBuffers(1, VBO);
            gl.GenVertexArrays(1, VAO);

            gl.BindVertexArray(VAO[0]);

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices, OpenGL.GL_STATIC_DRAW);

            gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
            gl.EnableVertexAttribArray(0);

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);

            gl.BindVertexArray(0);
        }
Beispiel #15
0
        private void UpdateItem(int index)
        {
            numVertices = 0;

            if (index >= 0 && index < Variables.Count)
            {
                ObservedVariable variable = Variables[index];

                if (DebugHandler.Debugger.CurrentMode == dbgDebugMode.dbgBreakMode)
                {
                    PointCloud pointCloud = null;

                    if (variable.MemberData == null)
                    {
                        pointCloud = DebugHandler.Load(variable.Name);
                    }
                    else
                    {
                        pointCloud = DebugHandler.Load(variable.Name, variable.MemberData);
                    }

                    if (pointCloud != null)
                    {
                        OpenGL gl = openGLControl.OpenGL;

                        vao.Bind(gl);

                        vbo.Bind(gl);
                        gl.BufferData(OpenGL.GL_ARRAY_BUFFER, pointCloud.vertices, OpenGL.GL_STATIC_DRAW);
                        numVertices = pointCloud.numVertices;
                    }
                }
            }
        }
 public void SetData(OpenGL gl, uint attributeIndex, float[] rawData, bool isNormalised, int stride)
 {
     //  Set the data, specify its shape and assign it to a vertex attribute (so shaders can bind to it).
     gl.BufferData(OpenGL.GL_ARRAY_BUFFER, rawData, OpenGL.GL_STATIC_DRAW);
     gl.VertexAttribPointer(attributeIndex, stride, OpenGL.GL_FLOAT, isNormalised, 0, IntPtr.Zero);
     gl.EnableVertexAttribArray(attributeIndex);
 }
Beispiel #17
0
        protected uint CreateVertexBufferObject(uint mode, VertexBuffer bufferData, uint usage)
        {
            uint[] ids = new uint[1];
            gl.GenBuffers(1, ids);
            gl.BindBuffer(mode, ids[0]);
            gl.BufferData(mode, bufferData.SizeInBytes, bufferData.Data, usage);

            return(ids[0]);
        }
 public void Create(int bufferSize)
 {
     uint[] temp = new uint[1];
     Device.GenBuffers(1, temp);
     Device.BindBuffer(OpenGL.GL_SHADER_STORAGE_BUFFER, temp[0]);
     Device.BufferData(OpenGL.GL_SHADER_STORAGE_BUFFER, bufferSize, IntPtr.Zero, OpenGL.GL_DYNAMIC_DRAW);
     Device.BindBuffer(OpenGL.GL_SHADER_STORAGE_BUFFER, 0);
     Buffer = temp[0];
 }
 private void InitTrianglesBuffer(OpenGL gl, UnmanagedArray <uint> TriangleStrip)
 {
     uint[] triangleBuffer = new uint[1];
     gl.GenBuffers(triangleBuffer.Length, triangleBuffer);
     gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, triangleBuffer[0]);
     gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, TriangleStrip.ByteLength, TriangleStrip.Header, OpenGL.GL_STATIC_DRAW);
     this.triangleIndexCount   = TriangleStrip.Length;
     this.triangleBufferObject = triangleBuffer[0];
 }
        /// <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);
        }
Beispiel #21
0
        /// <summary>
        /// Function invoked when points and colors must be added to the render.
        /// The size of lvertex and lcolor MUST BE THE SAME. Otherwise, there will be black points (no visible)
        /// </summary>
        /// <param name="lvertex"> List of vertexes to be added</param>
        /// <param name="lcolor"> List of colors to be added</param>
        private void addPointsAndColor(List <SharpGL.SceneGraph.Vertex> lvertex, List <SharpGL.SceneGraph.Vertex> lcolor)
        {
            OpenGL gl = openGLControl.OpenGL;

            //append new points to the existing list (if exists)
            if (l_vertex == null)
            {
                l_vertex = new List <SharpGL.SceneGraph.Vertex>();
            }
            l_vertex.AddRange(lvertex);
            //append new colors to the existing list (if exists)
            if (l_color == null)
            {
                l_color = new List <SharpGL.SceneGraph.Vertex>();
            }
            l_color.AddRange(lcolor);
            //set new size of the new list (just to render, if it exists)
            if (l_sizes == null)
            {
                l_sizes = new List <int>();
            }
            l_sizes.Add(lvertex.Count);

            uint[] ids = new uint[2];
            int    pos = l_vboId.Count;

            //add ids to the existing list
            if (l_vboId == null)
            {
                l_vboId = new List <uint[]>();
            }
            l_vboId.Add(ids);

            gl.GenBuffers(2, l_vboId[pos]);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[pos][0]);
            IntPtr ptr1 = GCHandle.Alloc(lvertex.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject();

            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * lvertex.Count * 3, ptr1, OpenGL.GL_STATIC_DRAW);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[pos][1]);
            IntPtr ptr2 = GCHandle.Alloc(lcolor.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject();

            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * lcolor.Count * 3, ptr2, OpenGL.GL_DYNAMIC_DRAW);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);
        }
        public SpriteBatch(OpenGL gl, int triCount = 4000)
        {
            this.gl = gl;

            var textureLoaded = this.white1x1Tex.Create(gl, Properties.Resources.white1x1);

            if (textureLoaded == false)
            {
                throw new Exception("white1x1Tex loading failed");
            }

            _shaderProgram.Create(gl, Properties.Resources.SimpleVS, Properties.Resources.SimplePS, null);

            _shaderProgram.BindAttributeLocation(gl, VertexAttributes.Pos, nameof(VertexAttributes.Pos));
            _shaderProgram.BindAttributeLocation(gl, VertexAttributes.Color, nameof(VertexAttributes.Color));
            _shaderProgram.BindAttributeLocation(gl, VertexAttributes.TexCoord, nameof(VertexAttributes.TexCoord));

            _shaderProgram.Bind(gl);

            vertices = new VertexPosTexColor[triCount * 3];
            indices  = new int[triCount * 3];

            gl.GenBuffers(1, vertexBufferIds);
            gl.BindBuffer(GL_ARRAY_BUFFER, vertexBufferIds[0]);
            gl.BufferData(GL_ARRAY_BUFFER, vertices.Length * VertexPosTexColor.SizeOf, IntPtr.Zero, GL_DYNAMIC_DRAW);

            gl.GenBuffers(1, indexBufferIds);
            gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferIds[0]);
            gl.BufferData(GL_ELEMENT_ARRAY_BUFFER, indices.Length * sizeof(int), IntPtr.Zero, GL_DYNAMIC_DRAW);

            gl.GenVertexArrays(1, vaos);
            gl.BindVertexArray(vaos[0]);

            gl.EnableVertexAttribArray(VertexAttributes.Pos);
            gl.VertexAttribPointer(VertexAttributes.Pos, 3, GL_FLOAT, false, VertexPosTexColor.SizeOf, new IntPtr(0));

            gl.EnableVertexAttribArray(VertexAttributes.Color);
            gl.VertexAttribPointer(VertexAttributes.Color, 4, GL_UNSIGNED_BYTE, true, VertexPosTexColor.SizeOf, new IntPtr(3 * sizeof(float)));

            gl.EnableVertexAttribArray(VertexAttributes.TexCoord);
            gl.VertexAttribPointer(VertexAttributes.TexCoord, 2, GL_FLOAT, false, VertexPosTexColor.SizeOf, new IntPtr(3 * sizeof(float) + 4 * sizeof(byte)));
        }
        public override uint CreateVBO(UsageType usage)
        {
            OpenGL gl = new OpenGL();

            uint[] buffers = new uint[1];
            gl.GenBuffers(1, buffers);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, buffers[0]);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, this.SizeInBytes, this.Data, (uint)usage);

            return(buffers[0]);
        }
Beispiel #24
0
        private void FlushVertexArray(OpenGL gl, int vertexCount, int triangleCount)
        {
            if (vertexCount == 0 || triangleCount == 0)
            {
                return;
            }

            gl.PushMatrix();

            gl.Translate(0, -300, 0);

            var bufferArrayData = vertexArray;

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBuffer);

            if (_bufferSize < sizeof(float) * vertexCount * 8)
            {
                gl.BufferData(
                    OpenGL.GL_ARRAY_BUFFER,
                    sizeof(float) * vertexCount * 8,
                    Marshal.UnsafeAddrOfPinnedArrayElement(bufferArrayData, 0),
                    OpenGL.GL_DYNAMIC_DRAW);
            }
            else
            {
                gl.BufferSubData(
                    OpenGL.GL_ARRAY_BUFFER,
                    0,
                    sizeof(float) * vertexCount * 8,
                    Marshal.UnsafeAddrOfPinnedArrayElement(bufferArrayData, 0));
            }

            _bufferSize = Math.Max(sizeof(float) * vertexCount * 8, _bufferSize);

            gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
            gl.EnableClientState(OpenGL.GL_COLOR_ARRAY);
            gl.EnableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);

            gl.VertexPointer(2, OpenGL.GL_FLOAT, 8 * sizeof(float), (IntPtr)0);
            gl.ColorPointer(4, OpenGL.GL_FLOAT, 8 * sizeof(float), (IntPtr)(2 * sizeof(float)));
            gl.TexCoordPointer(2, OpenGL.GL_FLOAT, 8 * sizeof(float), (IntPtr)(6 * sizeof(float)));

            gl.DrawElements(
                OpenGL.GL_TRIANGLES, triangleCount,
                OpenGL.GL_UNSIGNED_SHORT,
                Marshal.UnsafeAddrOfPinnedArrayElement(triangles, 0));

            gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
            gl.DisableClientState(OpenGL.GL_COLOR_ARRAY);
            gl.DisableClientState(OpenGL.GL_TEXTURE_COORD_ARRAY);

            gl.PopMatrix();
        }
Beispiel #25
0
        public static void SetArrayData(OpenGL gl, float[] data, int sizeInBytes, uint openGlDrawType)
        {
            unsafe
            {
                fixed(float *pointerToData = data)
                {
                    IntPtr pointerToVerticesData = new IntPtr((void *)pointerToData);

                    gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeInBytes, pointerToVerticesData, openGlDrawType);
                }
            }
        }
Beispiel #26
0
        public static void SetElementData(OpenGL gl, uint[] data, int sizeInBytes, uint openGlDrawType)
        {
            unsafe
            {
                fixed(uint *pointerToData = data)
                {
                    IntPtr pointerToIndicesData = new IntPtr((void *)pointerToData);

                    gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, sizeInBytes, pointerToIndicesData, openGlDrawType);
                    pointerToIndicesData.ToInt32();
                }
            }
        }
        /// <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);
        }
Beispiel #28
0
        // load given vertices into VBO
        private static void loadVertices(float[] vertices)
        {
            int    size   = Marshal.SizeOf(vertices[0]) * vertices.Length;
            IntPtr handle = Marshal.AllocHGlobal(size);

            try {
                Marshal.Copy(vertices, 0, handle, vertices.Length);
                var unmanagedSize = new IntPtr(size);
                OpenGL.BufferData(OpenGL.GL_ARRAY_BUFFER, unmanagedSize, handle, OpenGL.GL_STATIC_DRAW);
            }
            finally {
                Marshal.FreeHGlobal(handle);
            }
        }
Beispiel #29
0
        static void Main(string[] args)
        {
            try
            {
                window = new Window("Text", 800, 600, false);


                //float[] vert = new float[]{
                //    -0.5f,-0.5f,0.0f,
                //    -0.5f, 0.5f,0.0f,
                //     0.5f, 0.5f,0.0f,
                //     0.5f, 0.5f,0.0f,
                //     0.5f,-0.5f,0.0f,
                //    -0.5f,-0.5f,0.0f,
                //};
                float[] vert = new float[] {
                    0, 0, 0,
                    8, 0, 0,
                    0, 3, 0,
                    0, 3, 0,
                    8, 3, 0,
                    8, 0, 0,
                };

                UInt32[] vbo = new UInt32[2];


                gl.GenBuffers(1, vbo);
                gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vbo[0]);
                gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vert, OpenGL.GL_STATIC_DRAW);
                gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
                gl.EnableVertexAttribArray(0);


                shader s = new shader("minimal.vert", "minimal.frag");

                s.enable();
                s.SetUniformMatrix("pr_matrix", Matrix.CreateOrthographic(0.0, 16.0, -1.0, 1.0));
                s.SetUniformMatrix("ml_matrix", Matrix.CreateTranslation(new Vector3(2, 2, 0)));
                s.SetUniform("light_pos", new Vector2(4, 1.5f));
                s.SetUniform("colour", new Vector4(0f, 0.0f, 1f, 1.0f));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            window.SetUpdate(EventUpdate, update);
        }
Beispiel #30
0
        // load given indices into EBO
        private void loadIndices(uint[] indices)
        {
            byte[] data   = indices.ToByteArray();
            int    size   = Marshal.SizeOf(data[0]) * data.Length;
            IntPtr handle = Marshal.AllocHGlobal(size);

            try {
                Marshal.Copy(data, 0, handle, data.Length);
                var unmanagedSize = new IntPtr(size);
                OpenGL.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, unmanagedSize, handle, OpenGL.GL_STATIC_DRAW);
            }
            finally {
                Marshal.FreeHGlobal(handle);
            }
        }
        /// <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);
        }
Beispiel #32
0
 public void SetData(OpenGL gl, ushort[] rawData)
 {
     gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, rawData, OpenGL.GL_STATIC_DRAW);
 }