Glen Morse
Glen Morse

Reputation: 2593

Simple OpenGL Code not working

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.

Upvotes: 2

Views: 781

Answers (1)

datenwolf
datenwolf

Reputation: 162164

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.

Upvotes: 5

Related Questions