GLS.OpenGLVCL.pas 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLS.OpenGLVCL;
  5. (*
  6. OpenGL for Vcl
  7. Adapted from https://github.com/LUXOPHIA
  8. *)
  9. interface
  10. uses
  11. Winapi.OpenGL,
  12. Winapi.OpenGLext,
  13. Winapi.Windows,
  14. OpenGLTokens,
  15. OpenGLAdapter,
  16. GLContext,
  17. Vcl.Forms;
  18. type
  19. TGLOpenGL = class
  20. private
  21. _Form: TCustomForm;
  22. _WND: HWND;
  23. _DC: HDC;
  24. protected
  25. _PFD: TPixelFormatDescriptor;
  26. _PFI: Integer;
  27. _RC: HGLRC;
  28. procedure SetPFD(const PFD_: TPixelFormatDescriptor);
  29. procedure SetPFI(const PFI_: Integer);
  30. procedure CreateWindow;
  31. procedure DestroyWindow;
  32. procedure ValidatePFD(const PFD_: TPixelFormatDescriptor);
  33. procedure ValidatePFI(const PFI_: Integer);
  34. procedure CreateDC;
  35. procedure DestroyDC;
  36. procedure CreateRC;
  37. procedure DestroyRC;
  38. public
  39. constructor Create;
  40. destructor Destroy; override;
  41. property PFD: TPixelFormatDescriptor read _PFD write SetPFD;
  42. property PFI: Integer read _PFI write SetPFI;
  43. property RC: HGLRC read _RC;
  44. class function DefaultPFD: TPixelFormatDescriptor;
  45. procedure BeginGL;
  46. procedure EndGL;
  47. procedure InitOpenGL;
  48. procedure ApplyPixelFormat(const DC_: HDC);
  49. end;
  50. TGLShader = class
  51. private
  52. protected
  53. _ID: TGLuint;
  54. public
  55. constructor Create(const Kind_: TGLuint);
  56. destructor Destroy; override;
  57. property ID: TGLuint read _ID;
  58. procedure SetSource(const Source_: String);
  59. end;
  60. TGLShaderV = class(TGLShader)
  61. private
  62. protected
  63. public
  64. constructor Create;
  65. destructor Destroy; override;
  66. end;
  67. TGLShaderG = class(TGLShader)
  68. private
  69. protected
  70. public
  71. constructor Create;
  72. destructor Destroy; override;
  73. end;
  74. TGLShaderF = class(TGLShader)
  75. private
  76. protected
  77. public
  78. constructor Create;
  79. destructor Destroy; override;
  80. end;
  81. TGLProgram = class
  82. private
  83. protected
  84. _ID: TGLuint;
  85. public
  86. constructor Create;
  87. destructor Destroy; override;
  88. procedure Attach(const Shader_: TGLShader);
  89. procedure Detach(const Shader_: TGLShader);
  90. procedure Link;
  91. procedure Use;
  92. end;
  93. TGLBuffer<_TYPE_: record > = class
  94. public type
  95. _PValue_ = ^_TYPE_;
  96. private
  97. protected
  98. _ID: TGLuint;
  99. _Kind: TGLuint;
  100. _Count: Integer;
  101. _Head: _PValue_;
  102. procedure SetCount(const Count_: Integer);
  103. public
  104. constructor Create(const Kind_: TGLuint);
  105. destructor Destroy; override;
  106. property ID: TGLuint read _ID;
  107. property Count: Integer read _Count write SetCount;
  108. procedure Bind;
  109. procedure Unbind;
  110. procedure Map;
  111. procedure Unmap;
  112. end;
  113. TGLBufferV<_TYPE_: record > = class(TGLBuffer<_TYPE_>)
  114. private
  115. protected
  116. public
  117. constructor Create;
  118. destructor Destroy; override;
  119. end;
  120. TGLBufferI<_TYPE_: record > = class(TGLBuffer<_TYPE_>)
  121. private
  122. protected
  123. public
  124. constructor Create;
  125. destructor Destroy; override;
  126. end;
  127. TGLBufferU<_TYPE_: record > = class(TGLBuffer<_TYPE_>)
  128. private
  129. protected
  130. public
  131. constructor Create;
  132. destructor Destroy; override;
  133. end;
  134. TGLArray = class
  135. private
  136. protected
  137. _ID: TGLuint;
  138. public
  139. constructor Create;
  140. destructor Destroy; override;
  141. property ID: TGLuint read _ID;
  142. procedure BeginBind;
  143. procedure EndBind;
  144. end;
  145. var
  146. GLOpenGL: TGLOpenGL;
  147. //=====================================================================
  148. implementation
  149. //=====================================================================
  150. uses
  151. System.SysUtils;
  152. procedure TGLOpenGL.SetPFD(const PFD_: TPixelFormatDescriptor);
  153. begin
  154. DestroyRC;
  155. DestroyDC;
  156. CreateDC;
  157. ValidatePFD(PFD_);
  158. CreateRC;
  159. end;
  160. procedure TGLOpenGL.SetPFI(const PFI_: Integer);
  161. begin
  162. DestroyRC;
  163. DestroyDC;
  164. CreateDC;
  165. ValidatePFI(PFI_);
  166. CreateRC;
  167. end;
  168. procedure TGLOpenGL.CreateWindow;
  169. begin
  170. _Form := TCustomForm.CreateNew(nil);
  171. _WND := _Form.Handle;
  172. end;
  173. procedure TGLOpenGL.DestroyWindow;
  174. begin
  175. _Form.Free;
  176. end;
  177. // ------------------------------------------------------------------------------
  178. procedure TGLOpenGL.ValidatePFD(const PFD_: TPixelFormatDescriptor);
  179. var
  180. I: Integer;
  181. begin
  182. _PFD := PFD_;
  183. I := ChoosePixelFormat(_DC, @_PFD);
  184. Assert(I > 0, 'Not found the PixelFormat with a close setting!');
  185. ValidatePFI(I);
  186. end;
  187. procedure TGLOpenGL.ValidatePFI(const PFI_: Integer);
  188. begin
  189. _PFI := PFI_;
  190. Assert(DescribePixelFormat(_DC, _PFI, SizeOf(TPixelFormatDescriptor), _PFD),
  191. 'Not found the PixelFormat of the index!');
  192. end;
  193. // ------------------------------------------------------------------------------
  194. procedure TGLOpenGL.CreateDC;
  195. begin
  196. _DC := GetDC(_WND);
  197. end;
  198. procedure TGLOpenGL.DestroyDC;
  199. begin
  200. ReleaseDC(0, _DC);
  201. end;
  202. // ------------------------------------------------------------------------------
  203. procedure TGLOpenGL.CreateRC;
  204. begin
  205. ApplyPixelFormat(_DC);
  206. _RC := wglCreateContext(_DC);
  207. end;
  208. procedure TGLOpenGL.DestroyRC;
  209. begin
  210. wglDeleteContext(_RC);
  211. end;
  212. constructor TGLOpenGL.Create;
  213. begin
  214. inherited;
  215. CreateWindow;
  216. CreateDC;
  217. ValidatePFD(DefaultPFD);
  218. CreateRC;
  219. InitOpenGL;
  220. end;
  221. destructor TGLOpenGL.Destroy;
  222. begin
  223. DestroyRC;
  224. DestroyDC;
  225. DestroyWindow;
  226. inherited;
  227. end;
  228. class function TGLOpenGL.DefaultPFD: TPixelFormatDescriptor;
  229. begin
  230. with Result do
  231. begin
  232. nSize := SizeOf(TPixelFormatDescriptor);
  233. nVersion := 1;
  234. dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
  235. iPixelType := PFD_TYPE_RGBA;
  236. cColorBits := 24;
  237. cRedBits := 0;
  238. cRedShift := 0;
  239. cGreenBits := 0;
  240. cGreenShift := 0;
  241. cBlueBits := 0;
  242. cBlueShift := 0;
  243. cAlphaBits := 0;
  244. cAlphaShift := 0;
  245. cAccumBits := 0;
  246. cAccumRedBits := 0;
  247. cAccumGreenBits := 0;
  248. cAccumBlueBits := 0;
  249. cAccumAlphaBits := 0;
  250. cDepthBits := 32;
  251. cStencilBits := 0;
  252. cAuxBuffers := 0;
  253. iLayerType := PFD_MAIN_PLANE;
  254. bReserved := 0;
  255. dwLayerMask := 0;
  256. dwVisibleMask := 0;
  257. dwDamageMask := 0;
  258. end;
  259. end;
  260. procedure TGLOpenGL.BeginGL;
  261. begin
  262. wglMakeCurrent(_DC, _RC);
  263. end;
  264. procedure TGLOpenGL.EndGL;
  265. begin
  266. wglMakeCurrent(_DC, 0);
  267. end;
  268. // ------------------------------------------------------------------------------
  269. procedure TGLOpenGL.InitOpenGL;
  270. begin
  271. BeginGL;
  272. gl.Enable(GL_DEPTH_TEST);
  273. gl.Enable(GL_CULL_FACE);
  274. EndGL;
  275. end;
  276. // ------------------------------------------------------------------------------
  277. procedure TGLOpenGL.ApplyPixelFormat(const DC_: HDC);
  278. begin
  279. Assert(SetPixelFormat(DC_, _PFI, @_PFD), 'SetPixelFormat() is failed!');
  280. end;
  281. constructor TGLShader.Create(const Kind_: TGLuint);
  282. begin
  283. inherited Create;
  284. _ID := gl.CreateShader(Kind_);
  285. end;
  286. destructor TGLShader.Destroy;
  287. begin
  288. gl.DeleteShader(_ID);
  289. inherited;
  290. end;
  291. procedure TGLShader.SetSource(const Source_: String);
  292. var
  293. P: PAnsiChar;
  294. N: TGLint;
  295. E: TGLint;
  296. Cs: array of PGLchar;
  297. CsN: TGLsizei;
  298. begin
  299. P := PAnsiChar(AnsiString(Source_));
  300. N := Length(Source_);
  301. gl.ShaderSource(_ID, 1, @P, @N);
  302. gl.CompileShader(_ID);
  303. gl.GetShaderiv(_ID, GL_COMPILE_STATUS, @E);
  304. if E = GL_FALSE then
  305. begin
  306. gl.GetShaderiv(_ID, GL_INFO_LOG_LENGTH, @N);
  307. SetLength(Cs, N);
  308. gl.GetShaderInfoLog(_ID, N, @CsN, @Cs[0]);
  309. Assert(False, AnsiString(Cs));
  310. end;
  311. end;
  312. constructor TGLShaderV.Create;
  313. begin
  314. inherited Create(GL_VERTEX_SHADER);
  315. end;
  316. destructor TGLShaderV.Destroy;
  317. begin
  318. inherited;
  319. end;
  320. constructor TGLShaderG.Create;
  321. begin
  322. inherited Create(GL_GEOMETRY_SHADER);
  323. end;
  324. destructor TGLShaderG.Destroy;
  325. begin
  326. inherited;
  327. end;
  328. constructor TGLShaderF.Create;
  329. begin
  330. inherited Create(GL_FRAGMENT_SHADER);
  331. end;
  332. destructor TGLShaderF.Destroy;
  333. begin
  334. inherited;
  335. end;
  336. constructor TGLProgram.Create;
  337. begin
  338. inherited;
  339. _ID := gl.CreateProgram;
  340. end;
  341. destructor TGLProgram.Destroy;
  342. begin
  343. gl.DeleteProgram(_ID);
  344. inherited;
  345. end;
  346. procedure TGLProgram.Attach(const Shader_: TGLShader);
  347. begin
  348. gl.AttachShader(_ID, Shader_.ID);
  349. end;
  350. procedure TGLProgram.Detach(const Shader_: TGLShader);
  351. begin
  352. gl.DetachShader(_ID, Shader_.ID);
  353. end;
  354. // ------------------------------------------------------------------------------
  355. procedure TGLProgram.Link;
  356. begin
  357. gl.LinkProgram(_ID);
  358. end;
  359. // ------------------------------------------------------------------------------
  360. procedure TGLProgram.Use;
  361. begin
  362. gl.UseProgram(_ID);
  363. end;
  364. procedure TGLBuffer<_TYPE_>.SetCount(const Count_: Integer);
  365. begin
  366. _Count := Count_;
  367. Bind;
  368. gl.BufferData(_Kind, SizeOf(_TYPE_) * _Count, nil, GL_DYNAMIC_DRAW);
  369. Unbind;
  370. end;
  371. constructor TGLBuffer<_TYPE_>.Create(const Kind_: TGLuint);
  372. begin
  373. inherited Create;
  374. gl.GenBuffers(1, @_ID);
  375. _Kind := Kind_;
  376. Count := 0;
  377. end;
  378. destructor TGLBuffer<_TYPE_>.Destroy;
  379. begin
  380. gl.DeleteBuffers(1, @_ID);
  381. inherited;
  382. end;
  383. procedure TGLBuffer<_TYPE_>.Bind;
  384. begin
  385. gl.BindBuffer(_Kind, _ID);
  386. end;
  387. procedure TGLBuffer<_TYPE_>.Unbind;
  388. begin
  389. gl.BindBuffer(_Kind, 0);
  390. end;
  391. // ------------------------------------------------------------------------------
  392. procedure TGLBuffer<_TYPE_>.Map;
  393. begin
  394. Bind;
  395. _Head := gl.MapBuffer(_Kind, GL_READ_WRITE);
  396. end;
  397. procedure TGLBuffer<_TYPE_>.Unmap;
  398. begin
  399. gl.UnmapBuffer(_Kind);
  400. Unbind;
  401. end;
  402. constructor TGLBufferV<_TYPE_>.Create;
  403. begin
  404. inherited Create(GL_ARRAY_BUFFER);
  405. end;
  406. destructor TGLBufferV<_TYPE_>.Destroy;
  407. begin
  408. inherited;
  409. end;
  410. constructor TGLBufferI<_TYPE_>.Create;
  411. begin
  412. inherited Create(GL_ELEMENT_ARRAY_BUFFER);
  413. end;
  414. destructor TGLBufferI<_TYPE_>.Destroy;
  415. begin
  416. inherited;
  417. end;
  418. constructor TGLBufferU<_TYPE_>.Create;
  419. begin
  420. inherited Create(GL_UNIFORM_BUFFER);
  421. end;
  422. destructor TGLBufferU<_TYPE_>.Destroy;
  423. begin
  424. inherited;
  425. end;
  426. constructor TGLArray.Create;
  427. begin
  428. inherited Create;
  429. gl.GenVertexArrays(1, @_ID);
  430. end;
  431. destructor TGLArray.Destroy;
  432. begin
  433. gl.DeleteVertexArrays(1, @_ID);
  434. inherited;
  435. end;
  436. procedure TGLArray.BeginBind;
  437. begin
  438. gl.BindVertexArray(_ID);
  439. end;
  440. procedure TGLArray.EndBind;
  441. begin
  442. gl.BindVertexArray(0);
  443. end;
  444. // ====================================================================
  445. initialization
  446. // ====================================================================
  447. GLOpenGL := TGLOpenGL.Create;
  448. GLOpenGL.BeginGL;
  449. GL.Initialize; // InitOpenGLext;
  450. finalization
  451. GLOpenGL.EndGL;
  452. GLOpenGL.Free;
  453. end.