2

只是用delphi学习一些OpenGL并尝试一些简单但没有得到结果的东西,我相信我应该得到一个深绿色的形式。但是当我运行它时,我什么也得不到。也没有错误。也许错过了什么?

 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.
4

1 回答 1

5

由于您请求单个缓冲上下文,因此您必须glFinish在渲染代码的末尾调用,以将您的绘图命令提交给实现。但是,我强烈建议您切换到使用双缓冲上下文,而不是glFinish-ing 您发出 awglSwapBuffers意味着完成。

于 2012-07-31T07:28:19.750 回答