将 OpenGL 应用程序移植到 Windows 时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/679113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:11:37  来源:igfitidea点击:

Trouble porting OpenGL app to Windows

c++windowsvisual-studioopengl

提问by Frank Krueger

I am trying to move an OpenGL app to Windows.

我正在尝试将 OpenGL 应用程序移至 Windows。

It was my understanding that Windows had a decent OpenGL implementation. But I'm starting to think that it doesn't...

我的理解是 Windows 有一个不错的 OpenGL 实现。但我开始认为它不会......

Specifically, I use array buffers and glDrawArrays.

具体来说,我使用数组缓冲区和glDrawArrays

When I tried to compile my code in Visual Studio 2008 Pro, I received the following errors:

当我尝试在 Visual Studio 2008 Pro 中编译代码时,收到以下错误:

vertexbuffers.cpp(31) : error C3861: 'glGenBuffers': identifier not found
vertexbuffers.cpp(32) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
vertexbuffers.cpp(32) : error C3861: 'glBindBuffer': identifier not found
vertexbuffers.cpp(33) : error C2065: 'GL_ARRAY_BUFFER' : undeclared identifier
vertexbuffers.cpp(33) : error C2065: 'GL_STATIC_DRAW' : undeclared identifier
vertexbuffers.cpp(33) : error C3861: 'glBufferData': identifier not found

When I examined <GL\gl.h>(contained in C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\gl), I saw:

当我检查<GL\gl.h>(包含在C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\gl)时,我看到:

/* ClientArrayType */ 
/*      GL_VERTEX_ARRAY */
/*      GL_NORMAL_ARRAY */
/*      GL_COLOR_ARRAY */

Updatebut it would seem that those contants get defined elsewhere.

更新,但这些内容似乎是在别处定义的。

How am I supposed to generate buffers if I don't have access to those functions?

如果我无权访问这些功能,我应该如何生成缓冲区?

The documentation doesn't say that those array types are disabled. How do I get access to the realimplementation on OpenGL on Windows?

文档没有说这些数组类型被禁用。如何在 Windows 上访问OpenGL的真实实现?

采纳答案by jheriko

The #defines are commented out in the header file whenever they would otherwise be repeated. Look at line 1054 of gl.h:

#defines 在头文件中被注释掉,否则它们将被重复。查看 gl.h 的第 1054 行:

/* vertex_array */
#define GL_VERTEX_ARRAY                   0x8074

If this #define is actually missing then you should probably replace the file with a fresh copy.

如果这个#define 实际上丢失了,那么你应该用一个新的副本替换这个文件。

If you look at the documentation for glGenBuffersyou will see that it is only available in OpenGL 1.5 and higher. The header file for Windows only comes with OpenGL 1.2 and you should use the extension mechanism to access the newer functionality. If you call wglGetProcAddresswith the function name, e.g.

如果您查看glGenBuffers 的文档,您会发现它仅在 OpenGL 1.5 及更高版本中可用。Windows 的头文件仅随 OpenGL 1.2 一起提供,您应该使用扩展机制来访问较新的功能。如果您使用函数名称调用wglGetProcAddress,例如

void (__stdcall *glGenBuffers)(GLsizei,GLuint*) =
    wglGetProcAddress("glGenBuffers");

then you have a pointer to the function.

那么你就有了一个指向该函数的指针。

回答by BigSandwich

You might give GLEW a shot:

你可能会给 GLEW 一个机会:

http://glew.sourceforge.net/

http://glew.sourceforge.net/

I'm pretty sure I used it at some time in the past, and makes this sort of thing a little easier and more portable.

我很确定我在过去的某个时候使用过它,并使这种事情变得更容易和更便携。

回答by Frank Krueger

It would seem that the buffer functions are only available on Windows as extension methods.

似乎缓冲区函数只能在 Windows 上作为扩展方法使用。

OpenGL provides glext.hthat declares pointers to all of these functions. It is then up to my app to use wglGetProcAddressto get pointers to the functions.

OpenGL 提供了glext.h来声明所有这些函数的指针。然后由我的应用程序使用wglGetProcAddress来获取指向函数的指针。

For example:

例如:

PFNGLGENBUFFERSPROC myglBindBuffers = 
    (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffersARB");

Thankfully, I only have to do it for about 4 functions. Unfortunately, I now have to add platform-dependent code to my app.

值得庆幸的是,我只需要为大约 4 个函数执行此操作。不幸的是,我现在必须向我的应用程序添加依赖于平台的代码。

回答by datenwolf

Microsoft's support for OpenGL stretches only as far as OpenGL-1.1 up to Windows XP, and OpenGL-1.4 starting with Vista. Any OpenGL functionality beyond those must be delivered and supported by the installable client driver (ICD), i.e. the GPU driver's OpenGL implmenentation. To access the advanced functionality, OpenGL provides the so called Extension System, formed by wglGetProcAddress, which is kind of like GetProcAddressfor DLLs, but gives access to functions of the OpenGL implementation (=driver).

Microsoft 对 OpenGL 的支持范围从 OpenGL-1.1 到 Windows XP,以及从 Vista 开始的 OpenGL-1.4。任何超出这些功能的 OpenGL 功能都必须由可安装客户端驱动程序 (ICD) 提供和支持,即 GPU 驱动程序的 OpenGL 实现。为了访问高级功能,OpenGL 提供了所谓的扩展系统,由wglGetProcAddress形成,它有点像DLL 的GetProcAddress,但可以访问 OpenGL 实现(=驱动程序)的功能。

To make things easier, nice wrapper libraries like GLEW have been developed, which do all the grunt work initializing all the available OpenGL extensions, providing them to the end user.

为了让事情变得更简单,像 GLEW 这样的好包装库已经被开发出来,它完成了初始化所有可用 OpenGL 扩展的所有繁重工作,并将它们提供给最终用户。