Simple OpenGL Code not working

Tag: delphi , opengl , delphi-xe2 Author: sicker1 Date: 2012-07-14

Just learning some OpenGL with delphi and trying something simple but not getting a result, I belive i should get a dark green form. But when i run this i get nothing. No errors either. maybe missing something?

 unit First1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls,OpenGL, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  private
    { Private declarations }
    GLContext : HGLRC;
    ErrorCode: GLenum;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.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(Canvas.Handle,@pfd);
  SetPixelFormat(Canvas.Handle,FormatIndex,@pfd);
  GLContext := wglCreateContext(Canvas.Handle);
  wglMakeCurrent(Canvas.Handle,GLContext);
end; {FormCreate}

procedure TForm2.FormDestroy(Sender: TObject);
begin
  wglMakeCurrent(Canvas.Handle,0);
  wglDeleteContext(GLContext);
end;

procedure TForm2.FormPaint(Sender: TObject);
begin
    {background}
    glClearColor(0.0,0.4,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
{error checking}
    errorCode := glGetError;
    if errorCode<>GL_NO_ERROR then
      raise Exception.Create('Error in Paint'#13+
    gluErrorString(errorCode));
end;

end.

Best Answer

Since you request a single buffered context, you must call glFinish at the end of the rendering code, to commit your drawing commands to the implementation. However I strongly suggest you switch to using a double buffered context and instead of glFinish-ing you issue a wglSwapBuffers which implies a finish.

comments:

I am trying to learn from a book, but nice to know. Ill try adding the glfinish as soon as i get home. Cheers
GLFinish worked, but now iam intrested in knowing about this wglswapBuffers..
@GlenMorse: You enable double buffering by adding the flag PFD_DOUBLEBUFFER to the PFD.dwFlags bits. This should give you a double buffered window. Then you do OpenGL as usual, but you finish with wglSwapBuffers instead. BTW: The way you create a OpenGL context is seriously outdated, you normally want to use the method using wglCreateContextAttribsARB – Google finds you plenty tutorials.
Thanks, i can add the pfd_dubblebuffer to flags, but when i try to usewglSwapBuffers it says its an undecleared... is there another uses i have to add besides opengl? also the book i am using is old, but its only one i found that works opengl with delphi.
@GlenMorse: Unfortunately this is Delphi and my knowledge about Delphi streches only as far as my knowledge of good old Turbo Pascal overlaps with it :) According to this tutorial edn.embarcadero.com/article/26401 the function is called just SwapBuffers in Delphi.