Changeset 521 for GraphicTest/Packages/bgrabitmap/bgraopengl.pas
- Timestamp:
- Apr 17, 2019, 12:58:41 AM (5 years ago)
- Location:
- GraphicTest
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest
- Property svn:ignore
-
old new 8 8 GraphicTest.lps 9 9 GraphicTest.dbg 10 heaptrclog.trc
-
- Property svn:ignore
-
GraphicTest/Packages/bgrabitmap/bgraopengl.pas
r494 r521 9 9 Classes, SysUtils, FPimage, BGRAGraphics, 10 10 BGRAOpenGLType, BGRASpriteGL, BGRACanvasGL, GL, GLext, GLU, BGRABitmapTypes, 11 BGRAFontGL, BGRASSE ;11 BGRAFontGL, BGRASSE, BGRAMatrix3D; 12 12 13 13 type … … 43 43 property Width: integer read GetWidth; 44 44 property Height: integer read GetHeight; 45 end; 46 47 { TBGLFrameBuffer } 48 49 TBGLFrameBuffer = class(TBGLCustomFrameBuffer) 50 protected 51 FHeight: integer; 52 FMatrix: TAffineMatrix; 53 FProjectionMatrix: TMatrix4D; 54 FTexture: IBGLTexture; 55 FFrameBufferId, FRenderBufferId: GLuint; 56 FWidth: integer; 57 FSettingMatrices: boolean; 58 function GetTexture: IBGLTexture; override; 59 function GetHandle: pointer; override; 60 function GetHeight: integer; override; 61 function GetMatrix: TAffineMatrix; override; 62 function GetProjectionMatrix: TMatrix4D; override; 63 function GetWidth: integer; override; 64 procedure SetMatrix(AValue: TAffineMatrix); override; 65 procedure SetProjectionMatrix(AValue: TMatrix4D); override; 66 public 67 constructor Create(AWidth,AHeight: integer); 68 function MakeTextureAndFree: IBGLTexture; override; 69 destructor Destroy; override; 45 70 end; 46 71 … … 120 145 implementation 121 146 122 uses BGRATransform{$IFDEF BGRABITMAP_USE_LCL}, BGRAText, BGRATextFX{$ENDIF} 123 ,BGRAMatrix3D; 147 uses BGRABlurGL, BGRATransform{$IFDEF BGRABITMAP_USE_LCL}, BGRAText, BGRATextFX{$ENDIF}; 124 148 125 149 type … … 210 234 procedure ToggleFlipY; override; 211 235 procedure Bind(ATextureNumber: integer); override; 236 function FilterBlurMotion(ARadius: single; ABlurType: TRadialBlurType; ADirection: TPointF): IBGLTexture; override; 237 function FilterBlurRadial(ARadius: single; ABlurType: TRadialBlurType): IBGLTexture; override; 212 238 213 239 end; … … 247 273 procedure InternalStartPolygon(const pt: TPointF); override; 248 274 procedure InternalStartTriangleFan(const pt: TPointF); override; 249 procedure InternalContinueShape(const pt: TPointF); over ride;250 251 procedure InternalContinueShape(const pt: TPoint3D); over ride;252 procedure InternalContinueShape(const pt: TPoint3D_128); over ride;253 procedure InternalContinueShape(const pt, normal: TPoint3D_128); over ride;275 procedure InternalContinueShape(const pt: TPointF); overload; override; 276 277 procedure InternalContinueShape(const pt: TPoint3D); overload; override; 278 procedure InternalContinueShape(const pt: TPoint3D_128); overload; override; 279 procedure InternalContinueShape(const pt, normal: TPoint3D_128); overload; override; 254 280 255 281 procedure InternalEndShape; override; … … 268 294 function GetBlendMode: TOpenGLBlendMode; override; 269 295 procedure SetBlendMode(AValue: TOpenGLBlendMode); override; 296 297 procedure SetActiveFrameBuffer(AValue: TBGLCustomFrameBuffer); override; 270 298 public 271 299 destructor Destroy; override; … … 274 302 procedure EndZBuffer; override; 275 303 procedure WaitForGPU(AOption: TWaitForGPUOption); override; 304 function GetImage(x, y, w, h: integer): TBGRACustomBitmap; override; 305 function CreateFrameBuffer(AWidth, AHeight: integer): TBGLCustomFrameBuffer; override; 276 306 end; 277 307 … … 307 337 function GetUniformVariable(AProgram: DWord; AName: string): DWord; override; 308 338 function GetAttribVariable(AProgram: DWord; AName: string): DWord; override; 309 procedure SetUniformSingle(AVariable: DWord; const AValue; A Count: integer); override;310 procedure SetUniformInteger(AVariable: DWord; const AValue; A Count: integer); override;339 procedure SetUniformSingle(AVariable: DWord; const AValue; AElementCount, AComponentCount: integer); override; 340 procedure SetUniformInteger(AVariable: DWord; const AValue; AElementCount, AComponentCount: integer); override; 311 341 procedure BindAttribute(AAttribute: TAttributeVariable); override; 312 342 procedure UnbindAttribute(AAttribute: TAttributeVariable); override; 313 343 end; 344 345 { TBGLFrameBuffer } 346 347 procedure TBGLFrameBuffer.SetMatrix(AValue: TAffineMatrix); 348 begin 349 if FSettingMatrices then Exit; 350 FSettingMatrices := true; 351 FMatrix:=AValue; 352 if FCanvas <> nil then 353 TBGLCustomCanvas(FCanvas).Matrix := AValue; 354 FSettingMatrices := false; 355 end; 356 357 function TBGLFrameBuffer.GetMatrix: TAffineMatrix; 358 begin 359 result := FMatrix; 360 end; 361 362 function TBGLFrameBuffer.GetTexture: IBGLTexture; 363 begin 364 result := FTexture.FlipY; 365 end; 366 367 function TBGLFrameBuffer.GetHandle: pointer; 368 begin 369 result := @FFrameBufferId; 370 end; 371 372 function TBGLFrameBuffer.GetHeight: integer; 373 begin 374 result := FHeight; 375 end; 376 377 function TBGLFrameBuffer.GetProjectionMatrix: TMatrix4D; 378 begin 379 result := FProjectionMatrix; 380 end; 381 382 function TBGLFrameBuffer.GetWidth: integer; 383 begin 384 result := FWidth; 385 end; 386 387 procedure TBGLFrameBuffer.SetProjectionMatrix(AValue: TMatrix4D); 388 begin 389 if FSettingMatrices then Exit; 390 FSettingMatrices := true; 391 FProjectionMatrix:= AValue; 392 if FCanvas <> nil then 393 TBGLCustomCanvas(FCanvas).ProjectionMatrix := AValue; 394 FSettingMatrices := false; 395 end; 396 397 constructor TBGLFrameBuffer.Create(AWidth, AHeight: integer); 398 var frameBufferStatus: GLenum; 399 begin 400 if not Load_GL_version_3_0 then 401 raise exception.Create('Cannot load OpenGL 3.0'); 402 403 FWidth := AWidth; 404 FHeight := AHeight; 405 406 FTexture := BGLTextureFactory.Create(nil,AWidth,AHeight,AWidth,AHeight); 407 408 //depth and stencil 409 glGenRenderbuffers(1, @FRenderBufferId); 410 glBindRenderbuffer(GL_RENDERBUFFER, FRenderBufferId); 411 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, AWidth,AHeight); 412 glBindRenderbuffer(GL_RENDERBUFFER, 0); 413 414 glGenFramebuffers(1, @FFrameBufferId); 415 glBindFramebuffer(GL_FRAMEBUFFER, FFrameBufferId); 416 417 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, PGLuint(FTexture.Handle)^, 0); 418 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, FFrameBufferId); 419 420 frameBufferStatus:= glCheckFramebufferStatus(GL_FRAMEBUFFER); 421 glBindFramebuffer(GL_FRAMEBUFFER, 0); 422 423 if frameBufferStatus <> GL_FRAMEBUFFER_COMPLETE then 424 begin 425 glDeleteFramebuffers(1, @FFrameBufferId); 426 glDeleteRenderbuffers(1, @FRenderBufferId); 427 FTexture := nil; 428 raise exception.Create('Error ' + inttostr(frameBufferStatus) + ' while initializing frame buffer'); 429 end; 430 431 UseOrthoProjection; 432 Matrix := AffineMatrixIdentity; 433 end; 434 435 function TBGLFrameBuffer.MakeTextureAndFree: IBGLTexture; 436 begin 437 result := FTexture; 438 FTexture := nil; 439 Free; 440 end; 441 442 destructor TBGLFrameBuffer.Destroy; 443 begin 444 glDeleteFramebuffers(1, @FFrameBufferId); 445 glDeleteRenderbuffers(1, @FRenderBufferId); 446 FTexture := nil; 447 448 inherited Destroy; 449 end; 314 450 315 451 procedure ApplyBlendMode(ABlendMode: TOpenGLBlendMode); … … 776 912 777 913 procedure TBGLLighting.SetUniformSingle(AVariable: DWord; 778 const AValue; A Count: integer);914 const AValue; AElementCount, AComponentCount: integer); 779 915 begin 780 916 NeedOpenGL2_0; 781 glUniform1fv(AVariable, ACount, @AValue); 917 case AComponentCount of 918 1: glUniform1fv(AVariable, AElementCount, @AValue); 919 2: glUniform2fv(AVariable, AElementCount, @AValue); 920 3: glUniform3fv(AVariable, AElementCount, @AValue); 921 4: glUniform4fv(AVariable, AElementCount, @AValue); 922 9: glUniformMatrix3fv(AVariable, AElementCount, GL_FALSE, @AValue); 923 16: glUniformMatrix4fv(AVariable, AElementCount, GL_FALSE, @AValue); 924 else 925 raise exception.Create('Unexpected number of components'); 926 end; 782 927 end; 783 928 784 929 procedure TBGLLighting.SetUniformInteger(AVariable: DWord; 785 const AValue; A Count: integer);930 const AValue; AElementCount, AComponentCount: integer); 786 931 begin 787 932 NeedOpenGL2_0; 788 glUniform1iv(AVariable, ACount, @AValue); 933 case AComponentCount of 934 1: glUniform1iv(AVariable, AElementCount, @AValue); 935 2: glUniform2iv(AVariable, AElementCount, @AValue); 936 3: glUniform3iv(AVariable, AElementCount, @AValue); 937 4: glUniform4iv(AVariable, AElementCount, @AValue); 938 else 939 raise exception.Create('Unexpected number of components'); 940 end; 789 941 end; 790 942 … … 849 1001 function TBGLCanvas.GetMatrix: TAffineMatrix; 850 1002 begin 851 result := FMatrix; 1003 if ActiveFrameBuffer <> nil then 1004 result := ActiveFrameBuffer.Matrix 1005 else 1006 result := FMatrix; 852 1007 end; 853 1008 … … 858 1013 m := AffineMatrixToMatrix4D(AValue); 859 1014 glLoadMatrixf(@m); 860 FMatrix := AValue; 1015 1016 if ActiveFrameBuffer <> nil then 1017 ActiveFrameBuffer.Matrix := AValue 1018 else 1019 FMatrix := AValue; 861 1020 end; 862 1021 863 1022 function TBGLCanvas.GetProjectionMatrix: TMatrix4D; 864 1023 begin 865 result := FProjectionMatrix; 1024 if ActiveFrameBuffer <> nil then 1025 result := ActiveFrameBuffer.ProjectionMatrix 1026 else 1027 result := FProjectionMatrix; 866 1028 end; 867 1029 868 1030 procedure TBGLCanvas.SetProjectionMatrix(const AValue: TMatrix4D); 869 1031 begin 870 FProjectionMatrix := AValue;871 1032 glMatrixMode(GL_PROJECTION); 872 1033 glLoadMatrixf(@AValue); 873 1034 glMatrixMode(GL_MODELVIEW); 1035 1036 if ActiveFrameBuffer <> nil then 1037 ActiveFrameBuffer.ProjectionMatrix := AValue 1038 else 1039 FProjectionMatrix := AValue; 874 1040 end; 875 1041 … … 1015 1181 end; 1016 1182 1183 function TBGLCanvas.GetImage(x, y, w, h: integer): TBGRACustomBitmap; 1184 begin 1185 NeedOpenGL2_0; 1186 result := BGRABitmapFactory.Create(w,h); 1187 if TBGRAPixel_RGBAOrder then 1188 glReadPixels(x,self.Height-y-h, w,h, GL_RGBA, GL_UNSIGNED_BYTE, result.Data) 1189 else 1190 glReadPixels(x,self.Height-y-h, w,h, GL_BGRA, GL_UNSIGNED_BYTE, result.Data); 1191 end; 1192 1193 function TBGLCanvas.CreateFrameBuffer(AWidth, AHeight: integer): TBGLCustomFrameBuffer; 1194 begin 1195 Result:= TBGLFrameBuffer.Create(AWidth,AHeight); 1196 end; 1197 1017 1198 procedure TBGLCanvas.EnableScissor(AValue: TRect); 1018 1199 begin … … 1034 1215 begin 1035 1216 FBlendMode := AValue; 1217 end; 1218 1219 procedure TBGLCanvas.SetActiveFrameBuffer(AValue: TBGLCustomFrameBuffer); 1220 var 1221 m: TMatrix4D; 1222 begin 1223 if AValue = ActiveFrameBuffer then exit; 1224 inherited SetActiveFrameBuffer(AValue); 1225 if AValue = nil then 1226 glBindFramebuffer(GL_FRAMEBUFFER, 0) 1227 else 1228 glBindFramebuffer(GL_FRAMEBUFFER, PGLuint(AValue.Handle)^); 1229 1230 glViewPort(0,0,Width,Height); 1231 1232 glMatrixMode(GL_PROJECTION); 1233 m := ProjectionMatrix; 1234 glLoadMatrixf(@m); 1235 1236 glMatrixMode(GL_MODELVIEW); 1237 m := AffineMatrixToMatrix4D(Matrix); 1238 glLoadMatrixf(@m); 1036 1239 end; 1037 1240 … … 1377 1580 procedure TBGLTexture.ToggleFlipY; 1378 1581 begin 1379 FFlip X:= not FFlipY;1582 FFlipY := not FFlipY; 1380 1583 end; 1381 1584 … … 1395 1598 end; 1396 1599 1600 function TBGLTexture.FilterBlurMotion(ARadius: single; ABlurType: TRadialBlurType; ADirection: TPointF): IBGLTexture; 1601 var shader: TBGLCustomShader; 1602 blurName: string; 1603 begin 1604 blurName := 'TBGLBlurShader(' + RadialBlurTypeToStr[ABlurType] + ')'; 1605 shader := BGLCanvas.Lighting.Shader[blurName]; 1606 if shader = nil then 1607 begin 1608 shader := TBGLBlurShader.Create(BGLCanvas, ABlurType); 1609 BGLCanvas.Lighting.Shader[blurName] := shader; 1610 end; 1611 with (shader as TBGLBlurShader) do 1612 begin 1613 Radius := ARadius; 1614 Direction := ADirection; 1615 result := FilterBlurMotion(self); 1616 end; 1617 end; 1618 1619 function TBGLTexture.FilterBlurRadial(ARadius: single; ABlurType: TRadialBlurType): IBGLTexture; 1620 var shader: TBGLCustomShader; 1621 blurName: String; 1622 begin 1623 blurName := 'TBGLBlurShader(' + RadialBlurTypeToStr[ABlurType] + ')'; 1624 shader := BGLCanvas.Lighting.Shader[blurName]; 1625 if shader = nil then 1626 begin 1627 shader := TBGLBlurShader.Create(BGLCanvas, ABlurType); 1628 BGLCanvas.Lighting.Shader[blurName] := shader; 1629 end; 1630 with (shader as TBGLBlurShader) do 1631 begin 1632 Radius := ARadius; 1633 result := FilterBlurRadial(self); 1634 end; 1635 end; 1636 1397 1637 procedure TBGLTexture.Init(ATexture: TBGLTextureHandle; AWidth, 1398 1638 AHeight: integer; AOwned: boolean);
Note:
See TracChangeset
for help on using the changeset viewer.