Recent

Author Topic: OpenGLControl in ScrollBox with ScrollBar.Tracking  (Read 3586 times)

creaothceann

  • Full Member
  • ***
  • Posts: 117
OpenGLControl in ScrollBox with ScrollBar.Tracking
« on: March 07, 2017, 01:04:29 am »
I'm using an OpenGLControl for displaying OpenGL graphics. The Control is in a ScrollBox whose Horz/VertScrollBar.Tracking properties are enabled. This means that the OpenGLControl moves while the scroll bar is moved, and not only when the mouse button is released. Unfortunately this creates a strange "corruption" effect. (Maybe it's related to this problem?) Setting the form, the ScrollBox and the OpenGLControl's DoubleBuffered properties doesn't help.

I know I could probably simulate the ScrollBox with separate scrollbars, but maybe there is an easier way?

Code: Pascal  [Select][+][-]
  1. unit U_Form_Main;
  2.  
  3.  
  4. interface
  5. uses Classes, Controls, Forms, GL, Graphics, OpenGLContext, SysUtils;
  6.  
  7.  
  8. type {TForm_Main} TForm_Main = class(TForm)
  9.         procedure GUI_Form_OnCreate        (Sender : TObject);
  10.         procedure GUI_OpenGLControl_OnClick(Sender : TObject);
  11.         procedure GUI_OpenGLControl_OnPaint(Sender : TObject);
  12.         private
  13.         _OpenGLControl : TOpenGLControl;
  14.         _ScrollBox     : TScrollBox;
  15.         end;
  16.  
  17.  
  18. var Form_Main : TForm_Main;
  19.  
  20.  
  21. implementation
  22.  
  23.  
  24. {$R *.lfm}
  25.  
  26.  
  27. {TForm_Main}
  28.  
  29.  
  30. procedure TForm_Main.GUI_Form_OnCreate(Sender : TObject);
  31. begin
  32. Caption := '0';
  33. DoubleBuffered := True;
  34. _ScrollBox     := TScrollBox.Create(Self);
  35. with _ScrollBox do begin
  36.         Parent         := Self;
  37.         Align          := alClient;
  38.         Color          := clAppWorkspace;
  39.         DoubleBuffered := True;
  40. end;
  41. _OpenGLControl := TOpenGLControl.Create(_ScrollBox);
  42. with _OpenGLControl do begin
  43.         AutoResizeViewport := True;
  44.         DoubleBuffered     := True;
  45.         Parent             := _ScrollBox;
  46.         Left               := 0;
  47.         Top                := 0;
  48.         Width              := Screen.Width;
  49.         Height             := Screen.Height;
  50.         MultiSampling      := 4;
  51.         OnClick            := @GUI_OpenGLControl_OnClick;
  52.         OnPaint            := @GUI_OpenGLControl_OnPaint;
  53.         Invalidate;
  54. end;
  55. end;
  56.  
  57.  
  58. procedure TForm_Main.GUI_OpenGLControl_OnClick(Sender : TObject);
  59. begin
  60. with _ScrollBox do begin
  61.         Tag := Tag XOR 1;
  62.         Self.Caption := IntToStr(Tag);
  63.         HorzScrollBar.Tracking := ByteBool(Tag);
  64.         VertScrollBar.Tracking := HorzScrollBar.Tracking;
  65. end;
  66. end;
  67.  
  68.  
  69. procedure TForm_Main.GUI_OpenGLControl_OnPaint(Sender : TObject);
  70. begin
  71. glClearColor(0.27, 0.53, 0.71, 1.0);
  72. glClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT);
  73. glLoadIdentity;
  74. glBegin(GL_TRIANGLE_STRIP);
  75.         glColor3F(0, 0, 1);  glVertex3F(-1.0,  1.0, -1.0);
  76.         glColor3F(1, 0, 0);  glVertex3F(-1.0, -1.0, -1.0);
  77.         glColor3F(0, 1, 1);  glVertex3F( 1.0,  1.0,  1.0);
  78.         glColor3F(1, 1, 0);  glVertex3F( 1.0, -1.0,  1.0);
  79. glEnd;
  80. _OpenGLControl.SwapBuffers;
  81. end;
  82.  
  83.  
  84. end.

(This version of the code dynamically allocates the ScrollBox and the OpenGLControl so that it's clear what settings are used.)

Click on the OpenGLControl to enable the ScrollBar.Tracking.

 

TinyPortal © 2005-2018