德尔福OpenGL测试

标签 delphi opengl

我正在阅读《Delphi Developer's Guide to OpenGL》一书,这段代码应该设置窗口的背景颜色,但它不起作用,谁能告诉我出了什么问题??

type
   TForm1 = class(TForm)
      procedure Form_Create(Sender: TObject);
    procedure Form_Destroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
      private
         glContext   : HGLRC;
         glDC        : HDC;
         errorCode   : GLenum;
         openGLReady : Boolean;
      public
   end;

var
   Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormPaint(Sender: TObject);
begin
   if not openGLReady then
      exit;
   {background}
   glClearColor(0.1,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;

procedure TForm1.Form_Create(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}
   glDC := getDC(handle);
   FormatIndex := ChoosePixelFormat(glDC,@pfd);
   if FormatIndex=0 then
      raise Exception.Create('ChoosePixelFormat failed '+IntToStr(GetLastError));
   if not SetPixelFormat(glDC,FormatIndex,@pfd) then
      raise Exception.Create('SetPixelFormat failed '+IntToStr(GetLastError));
   GLContext := wglCreateContext(glDC);
   if GLContext=0 then
      raise Exception.Create('wglCreateContext failed '+IntToStr(GetLastError));
   if not wglMakeCurrent(glDC,GLContext) then
      raise Exception.Create('wglMakeCurrent failed '+IntToStr(GetLastError));
   OpenGLReady := true;
end;

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

最佳答案

在FormPaint opengl程序末尾添加命令:

glFlush();

关于德尔福OpenGL测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1924368/

相关文章:

delphi - 基于 PNG 图像创建丰富的 UI

delphi - 我们可以增加delphi IDE的字体大小吗,比如选项卡、菜单等(不仅仅是编辑器)

macos - macOS 上的 OpenGL 是否已弃用?

opengl - 在 GL 中组合多个模板

c - Opengl 统一变量不起作用?

c++ - 如何在 Qt5 中使 QOpenGLContext 成为当前没有表面的?

c++ - QOpenglWidget 和 QML Quick

delphi - 西里尔字母域名

delphi - Firemonkey float 键动画,转到特定键

delphi - 同步通用 TList 和 TListBox 时出现问题