* updated glBitmap
[LazOpenGLCore.git] / uglcVertexArrayObject.pas
diff --git a/uglcVertexArrayObject.pas b/uglcVertexArrayObject.pas
new file mode 100644 (file)
index 0000000..89e7ac2
--- /dev/null
@@ -0,0 +1,84 @@
+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.
+