Recent

Author Topic: Flickering with TCustomControl using OpenGL to paint  (Read 407 times)

deadbeef

  • New Member
  • *
  • Posts: 18
Flickering with TCustomControl using OpenGL to paint
« on: March 22, 2023, 09:46:03 pm »
I'm trying to render to a panel like component which works but causes flicker everytime it is updated (e.g. calling Repaint), which is really bad when scrolling.
It seems like the default painting code is used and then the OpenGL code overrides it again.
Any idea how to fix this?

Code: Pascal  [Select][+][-]
  1. unit OpenGLPanel;
  2.  
  3. {$mode Delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Controls, Graphics, GR32, XVector, XMatrix, dglOpenGL, OpenGLContext;
  9.  
  10.  
  11. type
  12.   TOpenGLPanel = class(TCustomControl)
  13.     strict private
  14.       var
  15.         FContext  : TOpenGLContext;
  16.         FGlColor  : TXVector;
  17.         FGlMatrix : TXMatrix;
  18.  
  19.  
  20.     protected
  21.       procedure   SetColor(Value: TColor); override;
  22.       procedure   Paint; override;
  23.  
  24.     public
  25.       constructor Create(TheOwner: TComponent); override;
  26.       destructor  Destroy; override;
  27.       property    Context: TOpenGLContext read FContext;
  28.       property    Matrix : TXMatrix read FGlMatrix;
  29.  
  30.  
  31.   end;
  32.  
  33.  
  34.  
  35. implementation
  36.  
  37.  
  38.  
  39. constructor TOpenGLPanel.Create;
  40. var i: GLint;
  41. begin
  42.   inherited Create(TheOwner);
  43.  
  44.   Parent          := TheOwner as TWinControl;
  45.  
  46.   Visible         := True;
  47.   DoubleBuffered  := False; // True = does not render
  48.  
  49.   BringToFront;
  50.  
  51.   FContext        := TOpenGLContext.CreateCore3D(Handle, False, True);
  52.   FGlMatrix       := TXMatrix.CreateOrtho2D(0, Width, Height, 0);
  53.  
  54.   FContext.EnableDebugToStdOut;
  55. end;
  56.  
  57.  
  58. destructor TOpenGLPanel.Destroy;
  59. begin
  60.   FContext.Free;
  61.   inherited;
  62. end;
  63.  
  64.  
  65. procedure TOpenGLPanel.Paint;
  66. begin
  67.   FContext.Activate;
  68.  
  69.   FGlMatrix:= TXMatrix.CreateOrtho2D(0, Width, Height, 0);
  70.  
  71.   glFrontFace(GL_CCW); // Y axis is flipped
  72.  
  73.   glEnable(GL_CULL_FACE);
  74.   glCullFace(GL_BACK);
  75.  
  76.   glViewport(0, 0, Width, Height);
  77.   glClearColor(FGlColor.X, FGlColor.Y, FGlColor.Z, FGlColor.W);
  78.   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
  79.  
  80.   inherited;
  81.  
  82.   FContext.SwapBuffer;
  83. end;
  84.  
  85.  
  86. procedure TOpenGLPanel.SetColor;
  87. var c: TColor32Entry;
  88. begin
  89.   c       := TColor32Entry(Color32(Value));
  90.   FGlColor:= TXVector.Create(c.R, c.G, c.B, c.A) / TXVector.Create(255.0);
  91.   inherited;
  92. end;
  93.  
  94.  
  95.  
  96. end.
  97.  

 

TinyPortal © 2005-2018