fDynTexture.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. unit fDynTexture;
  2. interface
  3. uses
  4. Winapi.Windows,
  5. Winapi.OpenGL,
  6. System.SysUtils,
  7. System.Classes,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.ExtCtrls,
  11. GLS.Scene,
  12. GLS.SceneViewer,
  13. GLS.Objects,
  14. GLS.Texture,
  15. GLS.Cadencer,
  16. GLS.Material,
  17. GLS.Coordinates,
  18. GLS.BaseClasses,
  19. GLS.RenderContextInfo,
  20. GLS.Context,
  21. GLS.DynamicTexture,
  22. GLS.Utils;
  23. type
  24. TFormDynamicTexture = class(TForm)
  25. GLScene1: TGLScene;
  26. GLSceneViewer1: TGLSceneViewer;
  27. GLMaterialLibrary1: TGLMaterialLibrary;
  28. GLCamera1: TGLCamera;
  29. GLLightSource1: TGLLightSource;
  30. GLDummyCube1: TGLDummyCube;
  31. GLCube1: TGLCube;
  32. GLDirectOpenGL1: TGLDirectOpenGL;
  33. GLCadencer1: TGLCadencer;
  34. Timer1: TTimer;
  35. procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  36. procedure FormResize(Sender: TObject);
  37. procedure FormCreate(Sender: TObject);
  38. procedure GLDirectOpenGL1Render(Sender: TObject; var rci: TGLRenderContextInfo);
  39. procedure Timer1Timer(Sender: TObject);
  40. procedure GLCadencer1Progress(Sender: TObject; const DeltaTime, newTime: Double);
  41. private
  42. frame: Integer;
  43. partial: boolean;
  44. public
  45. end;
  46. var
  47. FormDynamicTexture: TFormDynamicTexture;
  48. implementation
  49. {$R *.dfm}
  50. procedure TFormDynamicTexture.FormCreate(Sender: TObject);
  51. begin
  52. GLSceneViewer1.Align := alClient;
  53. end;
  54. procedure TFormDynamicTexture.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  55. var
  56. tex: TGLTexture;
  57. img: TGLDynamicTextureImage;
  58. begin
  59. tex := GLMaterialLibrary1.TextureByName('Anim');
  60. if not (tex.Image is TGLDynamicTextureImage) then
  61. Exit;
  62. img := TGLDynamicTextureImage(tex.Image);
  63. case Key of
  64. VK_F2:
  65. begin
  66. img.UsePBO := False;
  67. GLSceneViewer1.ResetPerformanceMonitor;
  68. frame:= 0;
  69. end;
  70. VK_F3:
  71. begin
  72. img.UsePBO := True;
  73. GLSceneViewer1.ResetPerformanceMonitor;
  74. frame:= 0;
  75. end;
  76. VK_F4:
  77. begin
  78. partial:= not partial;
  79. end;
  80. end;
  81. end;
  82. procedure TFormDynamicTexture.FormResize(Sender: TObject);
  83. begin
  84. GLCamera1.SceneScale := GLSceneViewer1.ClientWidth / 400;
  85. end;
  86. procedure TFormDynamicTexture.GLCadencer1Progress(Sender: TObject; const DeltaTime, newTime: Double);
  87. begin
  88. GLSceneViewer1.Invalidate;
  89. end;
  90. procedure TFormDynamicTexture.GLDirectOpenGL1Render(Sender: TObject; var rci: TGLRenderContextInfo);
  91. var
  92. tex: TGLTexture;
  93. img: TGLDynamicTextureImage;
  94. p: PRGBQuad;
  95. X, Y: Integer;
  96. begin
  97. tex := GLMaterialLibrary1.TextureByName('Anim');
  98. if tex.Disabled then
  99. begin
  100. tex.ImageClassName := TGLDynamicTextureImage.ClassName;
  101. img := TGLDynamicTextureImage(tex.Image);
  102. img.Width := 256;
  103. img.Height := 256;
  104. tex.TextureFormat := tfRGBA;
  105. tex.TextureMode := tmReplace;
  106. tex.Disabled := False;
  107. end;
  108. img := TGLDynamicTextureImage(tex.Image);
  109. img.BeginUpdate;
  110. // draw some silly stuff
  111. p := img.Data;
  112. frame := frame + 1;
  113. // first frame must always be drawn completely
  114. if partial and (frame > 1) then
  115. begin
  116. // do partial update, set the dirty rectangle
  117. // note that we do NOT offset the p pointer,
  118. // since it is relative to the dirty rectangle,
  119. // not the complete texture
  120. // also note that the right/bottom edge is not included
  121. // in the upload
  122. img.DirtyRectangle:= Rect(
  123. img.Width div 4,
  124. img.Height div 4,
  125. img.Width * 3 div 4,
  126. img.Height * 3 div 4);
  127. end;
  128. for Y := img.DirtyRectangle.Top to img.DirtyRectangle.Bottom - 1 do
  129. begin
  130. for X := img.DirtyRectangle.Left to img.DirtyRectangle.Right - 1 do
  131. begin
  132. p^.rgbRed := ((X xor Y) + frame) and 255;
  133. p^.rgbGreen := ((X + frame) xor Y) and 255;
  134. p^.rgbBlue := ((X - frame) xor (Y + frame)) and 255;
  135. Inc(p);
  136. end;
  137. end;
  138. img.EndUpdate;
  139. end;
  140. procedure TFormDynamicTexture.Timer1Timer(Sender: TObject);
  141. const
  142. PBOText: array[Boolean] of string = ('PBO disabled', 'PBO enabled');
  143. var
  144. tex: TGLTexture;
  145. img: TGLDynamicTextureImage;
  146. s: string;
  147. begin
  148. tex := GLMaterialLibrary1.TextureByName('Anim');
  149. if (tex.Image is TGLDynamicTextureImage) then
  150. begin
  151. img := TGLDynamicTextureImage(tex.Image);
  152. s := PBOText[img.UsePBO];
  153. end;
  154. Caption := Format('Dynamic Texture '+'%s - %s',
  155. [GLSceneViewer1.FramesPerSecondText, s + ' F2,F3,F4']);
  156. GLSceneViewer1.ResetPerformanceMonitor;
  157. end;
  158. end.