Recent

Author Topic: Content is distorting / warping?  (Read 254 times)

knuckles

  • Full Member
  • ***
  • Posts: 128
Content is distorting / warping?
« on: June 16, 2026, 01:47:39 am »
This is happening in Delphi too so i'm unsure if it's OpenGL related or Windows.

Basically I have dropped a TOpenGLControl on the form and in the Paint event:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.OpenGLControl1Paint(Sender: TObject);
  2. begin
  3.   glViewport(0, 0, OpenGLControl1.ClientWidth, OpenGLControl1.ClientHeight);
  4.   glMatrixMode(GL_PROJECTION);
  5.   glLoadIdentity;
  6.   glOrtho(0, OpenGLControl1.ClientWidth, OpenGLControl1.ClientHeight, 0, -1, 1);
  7.   glMatrixMode(GL_MODELVIEW);
  8.   glLoadIdentity;
  9.  
  10.   glClearColor(0.3, 0.3, 0.3, 1.0);
  11.   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  12.  
  13.   glColor3f(0.0, 0.0, 1.0);
  14.   glBegin(GL_QUADS);
  15.     glVertex2f(0, 0);
  16.     glVertex2f(256, 0);
  17.     glVertex2f(256, 256);
  18.     glVertex2f(0, 256);
  19.   glEnd;
  20.  
  21.   OpenGLControl1.SwapBuffers;
  22. end;

It's just a simple rectangle shown in the top left, 256x256 in size.

When resizing the Window it causes the rectangle to distort / flicker.

OpenGLControl1 is aligned to client and parented on the form itself, setting AutoResizeViewport makes no difference (Windows 11 ).

Not sure if this is a code issue or Windows 11 issue, i have a Nvidia graphics card if that matters.

Any ideas as to what may be causing it? Unfortunately i'm unable to screenshot the effect.

jamie

  • Hero Member
  • *****
  • Posts: 7829
Re: Content is distorting / warping?
« Reply #1 on: June 16, 2026, 02:55:30 am »
I am not a GL expert but I can see you are constantly resetting everything on each paint.

I Think the View port etc. should only be done once, the Paint event should only be changing whatever is to be changed when you clear your buffer, draw the items in it, and then swap the buffers.
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Content is distorting / warping?
« Reply #2 on: June 16, 2026, 03:53:02 pm »
I didn't test it on Windows but I believe I managed to solved it by preventing the painting event to run in middle of the form resizing by detecting the program idle/busy state.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, ExtCtrls,  OpenGLContext, GL;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     ApplicationProperties1: TApplicationProperties;
  16.     IdleTimer1: TIdleTimer;
  17.     OpenGLControl1: TOpenGLControl;
  18.     procedure ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
  19.     procedure ApplicationProperties1IdleEnd(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure OpenGLControl1Paint(Sender: TObject);
  22.   private
  23.     procedure RefreshGLControl(Sender: TObject);
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. var
  32.   Idle: Boolean;
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.RefreshGLControl(Sender: TObject);
  39. begin
  40.   glViewport(0, 0, OpenGLControl1.ClientWidth, OpenGLControl1.ClientHeight);
  41.   glMatrixMode(GL_PROJECTION);
  42.   glLoadIdentity;
  43.   glOrtho(0, OpenGLControl1.ClientWidth, OpenGLControl1.ClientHeight, 0, -1, 1);
  44.   glMatrixMode(GL_MODELVIEW);
  45.   glLoadIdentity;
  46.  
  47.   glClearColor(0.3, 0.3, 0.3, 1.0);
  48.   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  49.  
  50.   glColor3f(0.0, 0.0, 1.0);
  51.   glBegin(GL_QUADS);
  52.     glVertex2f(0, 0);
  53.     glVertex2f(256, 0);
  54.     glVertex2f(256, 256);
  55.     glVertex2f(0, 256);
  56.   glEnd;
  57.  
  58.   OpenGLControl1.SwapBuffers;
  59. end;
  60.  
  61. procedure TForm1.FormCreate(Sender: TObject);
  62. begin
  63.   OpenGLControl1.Align := alClient;
  64. end;
  65.  
  66. procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
  67. begin
  68.   Idle := True;
  69.   OpenGLControl1.Invalidate;
  70. end;
  71.  
  72. procedure TForm1.ApplicationProperties1IdleEnd(Sender: TObject);
  73. begin
  74.   Idle := False;
  75. end;
  76.  
  77. procedure TForm1.OpenGLControl1Paint(Sender: TObject);
  78. begin
  79.   if not(Idle) then Exit;
  80.   RefreshGLControl(nil);
  81. end;
  82.  
  83. end.

 

TinyPortal © 2005-2018