fDynTextureD.pas 4.3 KB

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