DGL
https://delphigl.com/forum/

How to draw on PaintBox with OpenGL?
https://delphigl.com/forum/viewtopic.php?f=19&t=2834
Seite 1 von 1

Autor:  joseph74 [ Mi Mai 12, 2004 08:52 ]
Betreff des Beitrags:  How to draw on PaintBox with OpenGL?

Hi!
I'm using Delphi 7.0. I tried to draw on a TPaintBox object but failed. Before trying this I have successfully drawn on TForm and TImage. I cannot find what the problem is. Can you help me to see the code?
Thank you!

This is the simplified code to identify the problem, when I runs the program, it immediately give the error message defined in OnCreate event.

unit testPaintBox1;
interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, OpenGL;
type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
procedure PaintBox1Paint(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
GLContext : HGLRC;
ErrorCode: GLenum;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
ShowMessage('onPaint');
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glFlush;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
pfd: TPixelFormatDescriptor;
FormatIndex: integer;
begin
fillchar(pfd,SizeOf(pfd),0);
with pfd do
begin
nSize := SizeOf(pfd);
nVersion := 1; {The current version of the desccriptor is 1}
dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
iPixelType := PFD_TYPE_RGBA;
cColorBits := 24; {support 24-bit color}
cDepthBits := 32; {depth of z-axis}
iLayerType := PFD_MAIN_PLANE;
end; {with}
FormatIndex := ChoosePixelFormat(Form1.PaintBox1.Canvas.Handle,@pfd);
SetPixelFormat(Form1.PaintBox1.Canvas.Handle,FormatIndex,@pfd);
GLContext := wglCreateContext(Form1.PaintBox1.Canvas.Handle);
wglMakeCurrent(Form1.PaintBox1.Canvas.Handle,GLContext);
errorcode:= glGetError;
if errorcode<>GL_NO_ERROR then
ShowMessage('gl error occurs');
end;
end.

Autor:  Sascha Willems [ Mi Mai 12, 2004 11:15 ]
Betreff des Beitrags: 

Form1.PaintBox1.Canvas.Handle is the problem, cause you have to pass a device context and not the handle of a canvas (that are two totally different things). So you normally have to get a DC from an object via GetDC(Object.Handle), but the TPaintBox-Object has no Handle-Property. So you'll have to go for another object to draw, and I'd suggest you a TPanel instead of a TPaintBox. In the end there is no difference, as you draw GL on it.

Autor:  Flo [ Sa Mai 22, 2004 17:42 ]
Betreff des Beitrags: 

SOS beliefe it or not, the handle of TCanvas is a DC, but the problem is the same.
TCanvas need a handle of a window to create it's own DC.

Zitat:
TGraphicControl hat kein eigenes Fenster. Wenn Canvas ein Gerätekontext-Handle benötigt, wird daher der Gerätekontext des übergeordneten Steuerelements verwendet.


If you don't believe SOS, try the following code:
Code:
  1.  
  2. unit Unit1;
  3.  
  4. interface
  5.  
  6. uses
  7.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  8.    opengl, ExtCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     procedure FormCreate(Sender: TObject);
  13.     procedure FormDestroy(Sender: TObject);
  14.     procedure FormPaint(Sender: TObject);
  15.   private
  16.     FRC:HGLRC;
  17.     FDC:HDC;
  18.     procedure SetupPixelFormat(DC:HDC);
  19.     { Private-Deklarationen }
  20.   public
  21.     { Public-Deklarationen }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.DFM}
  30. procedure TForm1.SetupPixelFormat(DC:HDC);
  31. var
  32.   PFD: TPixelFormatDescriptor;
  33.   nPixelFormat: Integer;
  34.  
  35. begin
  36.      FillChar(PFD, SizeOf(PFD), 0);
  37.   with PFD do
  38.   begin
  39.     nSize     := sizeof(pfd);               // Länge der pfd-Struktur
  40.     nVersion  := 1;                         // Version
  41.     iLayerType:= PFD_MAIN_PLANE;            // Layer Type
  42.     dwFlags         := PFD_DRAW_TO_WINDOW    // Buffer erlaubt zeichenen auf Fenster
  43.                        or PFD_SUPPORT_OPENGL; // Buffer unterstützt OpenGL drawing
  44.     iPixelType      := PFD_TYPE_RGBA;        // RGBA Farbformat
  45.     cColorBits      := 32;                   // OpenGL Farbtiefe
  46.     cDepthBits      := 24;                   // OpenGL Farbtiefe
  47.   end;
  48.   nPixelFormat:= ChoosePixelFormat(DC, @PFD);
  49.   SetPixelFormat(DC, nPixelFormat, @PFD);
  50.   DescribePixelFormat(DC, nPixelFormat,sizeof(TPixelFormatDescriptor),pfd);
  51. end;
  52.  
  53. procedure TForm1.FormCreate(Sender: TObject);
  54. begin
  55.   FDC := canvas.handle ;
  56.   SetupPixelFormat(FDC);
  57.   FRC:= wglCreateContext(FDC);
  58.   wglMakeCurrent(FDC, FRC);
  59. end;
  60.  
  61. procedure TForm1.FormDestroy(Sender: TObject);
  62. begin
  63.  wglMakeCurrent(0, 0);
  64.  wgldeletecontext(FDC);
  65. end;
  66.  
  67. procedure TForm1.FormPaint(Sender: TObject);
  68. begin
  69.   glclearcolor(0,0,0.5,0);
  70.   glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  71.  
  72.   glbegin(GL_TRIANGLES);
  73.    glvertex3f(0,0,0);
  74.    glvertex3f(0,1,0);
  75.    glvertex3f(1,0,0);
  76.   glend;
  77.   glflush;
  78. end;
  79.  
  80. end.
  81.  

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/