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.

157 lines
3.6 KiB

  1. unit uMainForm;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  6. uglcContext, uglcShader, uglcArrayBuffer, uglcTypes, uglcBitmap;
  7. type
  8. TMainForm = class(TForm)
  9. ApplicationProperties: TApplicationProperties;
  10. LogLB: TListBox;
  11. RenderPanel: TPanel;
  12. procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
  13. procedure FormCreate(Sender: TObject);
  14. procedure FormDestroy(Sender: TObject);
  15. procedure RenderPanelResize(Sender: TObject);
  16. private
  17. fContext: TglcContext;
  18. fShader: TglcShaderProgram;
  19. fVBO: TglcArrayBuffer;
  20. fTexture: TglcBitmap2D;
  21. procedure Log(aSender: TObject; const aMsg: String);
  22. procedure Render;
  23. public
  24. { public declarations }
  25. end;
  26. var
  27. MainForm: TMainForm;
  28. implementation
  29. {$R *.lfm}
  30. uses
  31. dglOpenGL, ugluVector;
  32. const
  33. SHADER_FILE = 'shader.glsl';
  34. TEXTURE_FILE = 'data\texture.png';
  35. LAYOUT_LOCATION_POS = 0;
  36. LAYOUT_LOCATION_TEX = 1;
  37. UNIFORM_NAME_TEXTURE = 'uTexture';
  38. type
  39. TVertex = packed record
  40. pos: TgluVector3f;
  41. tex: TgluVector2f;
  42. end;
  43. PVertex = ^TVertex;
  44. procedure TMainForm.FormCreate(Sender: TObject);
  45. var
  46. pf: TglcContextPixelFormatSettings;
  47. p: PVertex;
  48. texData: TglcBitmapData;
  49. begin
  50. pf := TglcContext.MakePF();
  51. fContext := TglcContext.GetPlatformClass.Create(RenderPanel, pf);
  52. fContext.BuildContext;
  53. fShader := TglcShaderProgram.Create(@Log);
  54. fShader.LoadFromFile(ExtractFilePath(Application.ExeName) + SHADER_FILE);
  55. fShader.Compile;
  56. fShader.Uniform1i(UNIFORM_NAME_TEXTURE, 0);
  57. fVBO := TglcArrayBuffer.Create(TglcBufferTarget.btArrayBuffer);
  58. fVBO.BufferData(4, sizeof(TVertex), TglcBufferUsage.buStaticDraw, nil);
  59. p := fVBO.MapBuffer(TglcBufferAccess.baWriteOnly);
  60. try
  61. p^.pos := gluVector3f(-0.5, -0.5, 0);
  62. p^.tex := gluVector2f( 0.0, 1.0);
  63. inc(p);
  64. p^.pos := gluVector3f( 0.5, -0.5, 0);
  65. p^.tex := gluVector2f( 1.0, 1.0);
  66. inc(p);
  67. p^.pos := gluVector3f( 0.5, 0.5, 0);
  68. p^.tex := gluVector2f( 1.0, 0.0);
  69. inc(p);
  70. p^.pos := gluVector3f(-0.5, 0.5, 0);
  71. p^.tex := gluVector2f( 0.0, 0.0);
  72. inc(p);
  73. finally
  74. fVBO.UnmapBuffer;
  75. end;
  76. fTexture := TglcBitmap2D.Create;
  77. texData := TglcBitmapData.Create;
  78. try
  79. texData.LoadFromFile(ExtractFilePath(Application.ExeName) + TEXTURE_FILE);
  80. fTexture.UploadData(texData);
  81. finally
  82. FreeAndNil(texData);
  83. end;
  84. end;
  85. procedure TMainForm.ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
  86. begin
  87. Render;
  88. Done := false;
  89. end;
  90. procedure TMainForm.FormDestroy(Sender: TObject);
  91. begin
  92. FreeAndNil(fVBO);
  93. FreeAndNil(fShader);
  94. FreeAndNil(fContext);
  95. end;
  96. procedure TMainForm.RenderPanelResize(Sender: TObject);
  97. begin
  98. if Assigned(fContext) then begin
  99. glViewport(0, 0, RenderPanel.ClientWidth, RenderPanel.ClientHeight);
  100. end;
  101. end;
  102. procedure TMainForm.Log(aSender: TObject; const aMsg: String);
  103. begin
  104. LogLB.Items.Add(aMsg);
  105. end;
  106. procedure TMainForm.Render;
  107. begin
  108. glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  109. fVBO.Bind;
  110. fTexture.Bind;
  111. fShader.Enable;
  112. glEnableVertexAttribArray(LAYOUT_LOCATION_POS);
  113. glVertexAttribPointer(LAYOUT_LOCATION_POS, 3, GL_FLOAT, False, SizeOf(TVertex), @PVertex(nil)^.pos);
  114. glEnableVertexAttribArray(LAYOUT_LOCATION_TEX);
  115. glVertexAttribPointer(LAYOUT_LOCATION_TEX, 2, GL_FLOAT, False, SizeOf(TVertex), @PVertex(nil)^.tex);
  116. glDrawArrays(GL_QUADS, 0, fVBO.DataCount);
  117. glDisableVertexAttribArray(LAYOUT_LOCATION_POS);
  118. glDisableVertexAttribArray(LAYOUT_LOCATION_TEX);
  119. fShader.Disable;
  120. fTexture.Unbind;
  121. fVBO.Unbind;
  122. fContext.SwapBuffers;
  123. end;
  124. end.