unit uglcVertexArrayObject; {$mode objfpc}{$H+} interface uses Classes, SysUtils, contnrs, dglOpenGL, uglcArrayBuffer; type EglcVertexArrayObject = class(Exception); TglcVertexArrayObject = class(TObject) private fID: GLuint; fArrayBuffers: TObjectList; public property ID: GLuint read fID; procedure BindArrayBuffer(const aBuffer: TglcArrayBuffer; const aOwnsObject: Boolean); procedure VertexAttribPointer(const aIndex: GLuint; const aSize: GLint; const aType: GLenum; const aNormalized: GLboolean; const aStride: GLsizei; const aOffset: GLint); procedure Bind; procedure Unbind; constructor Create; destructor Destroy; override; end; implementation ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TglcVertexArrayObject////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglcVertexArrayObject.BindArrayBuffer(const aBuffer: TglcArrayBuffer; const aOwnsObject: Boolean); begin Bind; aBuffer.Bind; if aOwnsObject and (fArrayBuffers.IndexOf(aBuffer) < 0) then fArrayBuffers.Add(aBuffer); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglcVertexArrayObject.VertexAttribPointer(const aIndex: GLuint; const aSize: GLint; const aType: GLenum; const aNormalized: GLboolean; const aStride: GLsizei; const aOffset: GLint); begin Bind; glEnableVertexAttribArray(aIndex); glVertexAttribPointer(aIndex, aSize, aType, aNormalized, aStride, Pointer(aOffset)); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglcVertexArrayObject.Bind; begin glBindVertexArray(fID); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TglcVertexArrayObject.Unbind; begin glBindVertexArray(0); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TglcVertexArrayObject.Create; begin inherited Create; if not GL_VERSION_3_0 and not GL_ARB_vertex_array_object then raise EglcVertexArrayObject.Create('vertex array objects are not supported by video vard'); glGenVertexArrays(1, @fID); fArrayBuffers := TObjectList.Create(true); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// destructor TglcVertexArrayObject.Destroy; begin glDeleteVertexArrays(1, @fID); FreeAndNil(fArrayBuffers); inherited Destroy; end; end.