You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.9 KiB

  1. unit uglcArrayBuffer;
  2. { Package: OpenGLCore
  3. Prefix: glc - OpenGL Core
  4. Beschreibung: diese Unit enthält eine Klassen-Kapselung für OpenGL Array Buffer
  5. Beispiel:
  6. type
  7. TVertex = packed record
  8. pos: TgluVector3f; // vertex position
  9. tex: TgluVector2f; // texture coordinates
  10. nor: TgluVector3f; // normal
  11. end;
  12. PVertex = ^TVertex;
  13. var
  14. vBuffer: TglcArrayBuffer;
  15. iBuffer: TglcArrayBuffer;
  16. p: Pointer;
  17. vBuffer := TglcArrayBuffer.Create(TglcBufferTarget.btArrayBuffer);
  18. iBuffer := TglcArrayBuffer.Create(TglcBufferTarget.btElementArrayBuffer);
  19. try
  20. // write vertex data to vertex buffer
  21. vBuffer.Bind;
  22. vBuffer.BufferData(4, SizeOf(TresMeshVertex), TglcBufferUsage.buStaticDraw, nil);
  23. p := vBuffer.MapBuffer(TglcBufferAccess.baWriteOnly);
  24. try
  25. PVertex(p).pos := gluVertex3f(0.0, 0.0, 0.0);
  26. PVertex(p).tex := gluVertex2f(0.0, 0.5);
  27. PVertex(p).nor := gluVertex3f(0.0, 1.0, 0.0);
  28. inc(p, SizeOf(TVertex));
  29. // ...
  30. finally
  31. vBuffer.UnmapBuffer;
  32. vBuffer.Unbind;
  33. end;
  34. // write indices to index buffer
  35. iBuffer.Bind;
  36. iBuffer.BufferData(4, SizeOf(GLuint), TglcBufferUsage.buStaticDraw, nil);
  37. p := iBuffer.MapBuffer(TglcBufferAccess.baWriteOnly);
  38. try
  39. PGLuint(p) := 0;
  40. // ...
  41. finally
  42. iBuffer.UnmapBuffer;
  43. iBuffer.Unbind;
  44. end;
  45. // use array buffers to draw primitive
  46. vBuffer.Bind;
  47. iBuffer.Bind;
  48. glEnableClientState(GL_VERTEX_ARRAY);
  49. glVertexPointer(3, GL_FLOAT, 8, Pointer(0));
  50. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  51. glTexCoordPointer(2, GL_FLOAT, 8, Pointer(3));
  52. glEnableClientState(GL_NORMAL_ARRAY);
  53. glNormalPointer(GL_FLOAT, 8, Pointer(5));
  54. glEnableClientState(GL_INDEX_ARRAY);
  55. glIndexPointer(GL_INT, 0, nil);
  56. glDrawElements(GL_QUADS, iBuffer.DataCount, GL_UNSIGNED_INT, nil);
  57. glDisableClientState(GL_INDEX_ARRAY);
  58. glDisableClientState(GL_VERTEX_ARRAY);
  59. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  60. glDisableClientState(GL_NORMAL_ARRAY);
  61. fIndexBuffer.Unbind;
  62. fVertexBuffer.Unbind;
  63. finally
  64. FreeAndNil(vBuffer);
  65. FreeAndNil(iBuffer);
  66. end; }
  67. {$mode objfpc}{$H+}
  68. interface
  69. uses
  70. dglOpenGL, sysutils, uglcTypes;
  71. type
  72. EglcArrayBuffer = class(Exception);
  73. TglcArrayBuffer = class(TObject)
  74. private
  75. fID: GLuint;
  76. fTarget: TglcBufferTarget;
  77. fUsage: TglcBufferUsage;
  78. protected
  79. fDataCount: Integer;
  80. fDataSize: Integer;
  81. public
  82. property ID: gluInt read fID;
  83. property Target: TglcBufferTarget read fTarget;
  84. property Usage: TglcBufferUsage read fUsage;
  85. property DataCount: Integer read fDataCount;
  86. property DataSize: Integer read fDataSize;
  87. procedure BufferData(const aDataCount, aDataSize: Cardinal; const aUsage: TglcBufferUsage; const aData: Pointer);
  88. function MapBuffer(const aAccess: TglcBufferAccess): Pointer;
  89. procedure UnmapBuffer;
  90. procedure Bind;
  91. procedure Unbind;
  92. constructor Create(const aTarget: TglcBufferTarget);
  93. destructor Destroy; override;
  94. end;
  95. implementation
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. //TglcArrayBuffer///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  99. procedure TglcArrayBuffer.BufferData(const aDataCount, aDataSize: Cardinal; const aUsage: TglcBufferUsage; const aData: Pointer);
  100. begin
  101. glGetError(); //clear Errors
  102. Bind;
  103. fDataCount := aDataCount;
  104. fDataSize := aDataSize;
  105. fUsage := aUsage;
  106. glBufferData(GLenum(fTarget), fDataCount * fDataSize, aData, GLenum(fUsage));
  107. glcCheckAndRaiseError;
  108. end;
  109. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  110. function TglcArrayBuffer.MapBuffer(const aAccess: TglcBufferAccess): Pointer;
  111. begin
  112. glGetError();
  113. result := nil;
  114. if (fDataCount * fDataSize) <= 0 then
  115. exit;
  116. result := glMapBuffer(GLenum(fTarget), GLenum(aAccess));
  117. glcCheckAndRaiseError;
  118. end;
  119. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  120. procedure TglcArrayBuffer.UnmapBuffer;
  121. begin
  122. glUnmapBuffer(GLenum(fTarget));
  123. end;
  124. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  125. procedure TglcArrayBuffer.Bind;
  126. begin
  127. glBindBuffer(GLenum(fTarget), fID);
  128. end;
  129. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  130. procedure TglcArrayBuffer.Unbind;
  131. begin
  132. glBindBuffer(GLenum(fTarget), 0);
  133. end;
  134. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  135. constructor TglcArrayBuffer.Create(const aTarget: TglcBufferTarget);
  136. begin
  137. if not GL_ARB_Vertex_Buffer_Object then
  138. raise EglcArrayBuffer.Create('Create - VertexBuffer: not supported');
  139. inherited Create;
  140. glGenBuffers(1, @fID);
  141. fDataCount := 0;
  142. fDataSize := 0;
  143. fTarget := aTarget;
  144. end;
  145. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[c]
  146. destructor TglcArrayBuffer.Destroy;
  147. begin
  148. glDeleteBuffers(1, @fID);
  149. inherited Destroy;
  150. end;
  151. end.