gpt4 book ai didi

delphi - 如何在Windows上的Delphi中加载OpenGL glGenBuffers方法?

转载 作者:行者123 更新时间:2023-12-02 20:45:57 24 4
gpt4 key购买 nike

以下代码尝试加载 OpenGL glGenBuffers 方法,但失败。您能帮忙评论一下原因是什么以及如何解决吗?

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses
StrUtils, SysUtils, Windows;

var
Library_OpenGL: HMODULE;

begin
try
Library_OpenGL := LoadLibrary('opengl32.dll');
Writeln(Ifthen(GetProcAddress(Library_OpenGL, 'glGenBuffers') <> nil, ' glGenBuffers Success', ' glGenBuffers Failed'));
Writeln(Ifthen(GetProcAddress(Library_OpenGL, 'glGenBuffersARB') <> nil, ' glGenBuffersARB Success', ' glGenBuffersARB Failed'));
FreeLibrary(Library_OpenGL);

Library_OpenGL := LoadLibrary('atioglxx.dll'); // ATI 4850
Writeln(Ifthen(GetProcAddress(Library_OpenGL, 'glGenBuffers') <> nil, ' glGenBuffers Success', ' glGenBuffers Failed'));
Writeln(Ifthen(GetProcAddress(Library_OpenGL, 'glGenBuffersARB') <> nil, ' glGenBuffersARB Success', ' glGenBuffersARB Failed'));
FreeLibrary(Library_OpenGL);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.

最佳答案

如果我们正确检查 API 的返回,而不是转储它是否失败,我们很快就能得到失败原因的答案。

替换

Writeln(Ifthen(GetProcAddress(Library_OpenGL, 'glGenBuffers') <> nil, ' glGenBuffers Success', ' glGenBuffers Failed'));

Win32Check(GetProcAddress(Library_OpenGL, 'glGenBuffers') <> nil);

我们有一个异常(exception):

EOSError: System Error. Code: 127.
The specified procedure could not be found

所以 GetProcAddress 失败的原因是 opengl 库没有导出 glGenBuffers 函数。带有“取决于”的检查证实了这一点。


快速搜索 opengl 中缺失的函数,我们会找到 wglGetProcAddress :

The wglGetProcAddress function returns the address of an OpenGL extension function for use with the current OpenGL rendering context.

如果您仔细阅读文档,您会发现仅调用提供 glGenBuffersgwlGetProcAddress 将会失败。您需要一个渲染上下文才能返回函数的地址。

下面是我最短的项目,它成功地获取了函数的地址。它使用 GUI 应用程序,因为渲染上下文需要一个窗口,并且默认的 VCL 表单可以满足要求(请参阅 SetPixelFormat 中的“备注”)。 (免责声明:我对 opengl 一无所知,下面的代码不一定正确,它还省略了您应该真正避免的错误检查)。

uses
opengl;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Library_OpenGL: HMODULE;
DC: HDC;
PxFmt: TPixelFormatDescriptor;
GLRC: HGLRC;
glGenBuffers: procedure(n: GLsizei; buffers: PGLuint); cdecl;

begin
Library_OpenGL := LoadLibrary('opengl32.dll');

DC := GetDC(Handle);
ZeroMemory(@PxFmt, SizeOf(PxFmt));
PxFmt.nSize := SizeOf(PxFmt);
PxFmt.nVersion := 1;
PxFmt.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
PxFmt.iPixelType := PFD_TYPE_RGBA;
PxFmt.cColorBits := 32;
PxFmt.cDepthBits := 24;
PxFmt.cStencilBits := 8;
SetPixelFormat(DC, ChoosePixelFormat(DC, @PxFmt), @PxFmt);

GLRC := wglCreateContext(DC);
wglMakeCurrent(DC, GLRC);
@glGenBuffers := wglGetProcAddress('glGenBuffers');
Assert(@glGenBuffers <> nil);
...

end;

关于delphi - 如何在Windows上的Delphi中加载OpenGL glGenBuffers方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25587587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com