/// <summary> /// Deletes the vertex buffer and the vertex array. /// </summary> protected override void DisposeInternal() { //delete opengl buffers GL.DeleteVertexArray(_vertexArrayHandle); GL.DeleteBuffer(_vertexBufferHandle); //free certain fields?. _shader = null; CurrentTexture = null; }
/// <summary> /// Constructs a QuadRenderer with the specified max quad count and shader /// </summary> /// <param name="maxQuadCount">The maximum amount of quads that may be submitted for rendering at once.</param> /// <param name="shader">The shader the renderer will use.</param> public QuadRenderer(int maxQuadCount, QuadShader shader) { //Check that the arguments are valid _shader = shader ?? throw new ArgumentNullException(nameof(shader)); MaxQuadCount = maxQuadCount < 1 || maxQuadCount == int.MaxValue ? throw new ArgumentOutOfRangeException(nameof(maxQuadCount)) : maxQuadCount; //Generate OpenGL buffers _vertexBufferHandle = GL.GenBuffer(); _vertexArrayHandle = GL.GenVertexArray(); //Create the array to store the vertices client side _vertices = new float[MaxQuadCount * FLOATS_PER_QUAD]; int sf = sizeof(float); //Configure the vertex array and set the size of the buffer GL.BindVertexArray(_vertexArrayHandle); GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferHandle); GL.BufferData(BufferTarget.ArrayBuffer, sf * _vertices.Length, _vertices, BufferUsageHint.StreamDraw); int stride = FLOATS_PER_QUAD * sf; GL.VertexAttribPointer(_shader.PosLocation, 4, VertexAttribPointerType.Float, false, stride, 0); GL.VertexAttribPointer(_shader.TexLocation, 4, VertexAttribPointerType.Float, false, stride, 4 * sf); GL.VertexAttribPointer(_shader.ColorLocation, 4, VertexAttribPointerType.Float, false, stride, 8 * sf); GL.VertexAttribPointer(_shader.OriginLocation, 2, VertexAttribPointerType.Float, false, stride, sf * 12); GL.VertexAttribPointer(_shader.ScaleLocation, 2, VertexAttribPointerType.Float, false, stride, sf * 14); GL.VertexAttribPointer(_shader.RotLocation, 1, VertexAttribPointerType.Float, false, stride, sf * 16); GL.EnableVertexAttribArray(_shader.PosLocation); GL.EnableVertexAttribArray(_shader.TexLocation); GL.EnableVertexAttribArray(_shader.ColorLocation); GL.EnableVertexAttribArray(_shader.OriginLocation); GL.EnableVertexAttribArray(_shader.ScaleLocation); GL.EnableVertexAttribArray(_shader.RotLocation); //Give the transform the identity value initially Transform = Matrix4.Identity; }