【问题标题】:Simple OpenGL Code not working简单的 OpenGL 代码不起作用
【发布时间】:2023-03-22 07:30:02
【问题描述】:

刚刚用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.

【问题讨论】:

  • 不是专家,因此发表评论:再次调用 wglMakeCurrent 并使用 '0' 作为两个参数以使渲染上下文不是当前,这将刷新你所拥有的画。这可能意味着您也必须在 OnPaint 中创建上下文(并删除)。在 MSDN 上搜索“渲染上下文”。

标签: delphi opengl delphi-xe2


【解决方案1】:

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

【讨论】:

  • 我正在尝试从一本书中学习,但很高兴知道。我一回到家就会尝试添加 glfinish。干杯
  • @GlenMorse:您可以通过将标志 PFD_DOUBLEBUFFER 添加到 PFD.dwFlags 位来启用双缓冲。这应该给你一个双缓冲窗口。然后你像往常一样做 OpenGL,但你用wglSwapBuffers 结束。顺便说一句:你创建 OpenGL 上下文的方式已经严重过时了,你通常想使用 wglCreateContextAttribsARB 的方法——谷歌找到了很多教程。
  • 谢谢,我可以将 pfd_dubblebuffer 添加到标志中,但是当我尝试使用 wglSwapBuffers 时,它说它未清除...除了 opengl 之外,我还需要添加其他用途吗?我使用的书也很旧,但我发现只有它可以与 delphi 一起使用 opengl。
  • @GlenMorse:不幸的是,这是 Delphi,我对 Delphi 的了解仅限于我对旧 Turbo Pascal 的了解与它重叠:) 根据本教程 edn.embarcadero.com/article/26401,该函数仅称为 @ 987654327@ 在德尔福。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多