如何为 Windows C++ 安装当前版本的 OpenGL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17957325/
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
How do I install a current version of OpenGL for Windows C++?
提问by ExOttoyuhr
I'm running Windows 7 with Visual Studio 2010. The version of OpenGL included (#include ) is version 1.1, and I'd like to be working with a reasonably current version -- some sort of version 3 or 4.
我正在使用 Visual Studio 2010 运行 Windows 7。包含的 OpenGL 版本 (#include) 是 1.1 版,我希望使用合理的当前版本 - 某种版本 3 或 4。
What do I need to do in order to get to that state? The OpenGL SDK page at http://www.opengl.org/sdk/seems to say that you're not allowed to download the SDK, and the OpenGL wiki at http://www.opengl.org/wiki/Getting_Startedsays that you're expected to already have it, and if you don't have it, it points you to sites where you can download graphics-card manufacturers' DLLs. But surely I don't need to build a different version of the game I'm working on for each graphics card I'm going to be working with.
我需要做什么才能达到那个状态?http://www.opengl.org/sdk/ 上的 OpenGL SDK 页面似乎说你不允许下载 SDK,而http://www.opengl.org/wiki/Getting_Started 上的 OpenGL wiki说您应该已经拥有它,如果您没有,它会将您指向可以下载图形卡制造商的 DLL 的站点。但我肯定不需要为我将要使用的每张显卡构建我正在开发的游戏的不同版本。
StackOverflow also doesn't seem to have anything, at least not phrased in a way that I can follow. I just want a download link to an installer that I can run, which will leave me with a reasonably up-to-date OpenGL API... Where do I go to get it?
StackOverflow 似乎也没有任何内容,至少没有以我可以遵循的方式表达。我只想要一个我可以运行的安装程序的下载链接,这会给我一个合理的最新 OpenGL API...我去哪里得到它?
Update:OpenGL appears to have an ideosyncratic idiom of some sort which doesn't involve having an SDK -- i.e., a package of .DLL, .lib, and headers. I'm using DirectX, which does. (In fact, the DirectX SDK even includes documentation!)
更新:OpenGL 似乎具有某种不涉及 SDK 的异质习语——即.DLL、.lib 和标头的包。我正在使用 DirectX,它确实如此。(事实上,DirectX SDK 甚至包括文档!)
回答by datenwolf
First of all OpenGL is not some centrally managed library and implementation (opposed to DirectX), that's why you can't download theSDK, because that's not how OpenGL works. OpenGL itself is just a bunch of documents that describe an API that drivers provide and programs can use. However, the actual implementation of the API lives in the context of an operating system. And that makes things a little bit difficult if you want your API to be independent of an OS. DirectX has it easy, because it's designed for only one particular OS. That OS is Windows and that means DirectX can be written against parts of the underlying OS. Which makes development of an SDK manageable.
首先所有的OpenGL的是不是有些集中管理库和实现(反对的DirectX),这就是为什么你不能下载的SDK,因为这是OpenGL的不是如何工作的。OpenGL 本身只是一堆描述驱动程序提供和程序可以使用的 API 的文档。但是,API 的实际实现存在于操作系统的上下文中。如果您希望 API 独立于操作系统,这会让事情变得有点困难。DirectX 很简单,因为它只为一种特定的操作系统而设计。该操作系统是 Windows,这意味着 DirectX 可以针对底层操作系统的一部分编写。这使得 SDK 的开发变得易于管理。
So what does OpenGL do then? Well, it requires that some part of the OS will be so generous and make it available to the program. In the most simple form (and in hindsight this would have been the better option, because it would save so many questions like yours) this interface would have provided exactly one function: GetProcAddress
. For each and every function found in the OpenGL spec you could have got a pointer to the actual thing in your OpenGL driver by that function. But, lazy as most programmers are they went the easy way and said: "Oh well, the current spec of OpenGL it at version 1.1, how about we expose all of OpenGL-1.1 on the interfacing library's surface. And everything that comes after we expose through extensions that are to be loaded by GetProcAddress
; after all, how much new functionality will there be…". Turns out: A lot.
那么 OpenGL 会做什么呢?好吧,它要求操作系统的某些部分如此慷慨并使其可供程序使用。在最简单的形式中(事后看来,这将是更好的选择,因为它可以省去很多像您这样的问题)这个界面将提供一个功能:GetProcAddress
. 对于 OpenGL 规范中的每个函数,您都可以通过该函数获得指向 OpenGL 驱动程序中实际内容的指针。但是,像大多数程序员一样懒惰,他们走了一条简单的路并说:“哦,好吧,OpenGL 的当前规范是 1.1 版,我们如何在接口库的表面上公开 OpenGL-1.1 的所有内容。以及我们之后的所有内容通过要加载的扩展公开GetProcAddress
;毕竟,会有多少新功能......”. 事实证明:很多。
Anyway, every compiler offering support for an OS is supposed to provide interface libraries for every API the OS ships with. Which for Windows is OpenGL-1.1. If you want an actual version bump on the OpenGL interface library, that would mean an OS update. But that's not so different from DirectX, where new DirectX versions are shipping with OS updates. It's just that Microsoft doesn't see a reason to support OpenGL. So OpenGL-1.1 is what's visible on the surface and we have to deal with it. And since it's included in what compilers ship there's no reason for providing an actual SDK to download, because everything necessary is already right there on your compiler installation.
无论如何,每个为操作系统提供支持的编译器都应该为操作系统附带的每个 API 提供接口库。对于 Windows,它是 OpenGL-1.1。如果您想要 OpenGL 接口库上的实际版本提升,那将意味着操作系统更新。但这与 DirectX 并没有太大不同,DirectX 的新 DirectX 版本随操作系统更新一起提供。只是微软没有看到支持 OpenGL 的理由。所以 OpenGL-1.1 是表面可见的,我们必须处理它。而且由于它包含在编译器附带的内容中,因此没有理由提供实际的 SDK 以供下载,因为所需的一切都已在编译器安装中。
Okay, so how to get those functions from laterversions of OpenGL then? Well: GetProcAddress
. That's the official way to do it. And because the actual details depend on the OS in question, but OpenGL is OS independent there simply can not be a definitiveOpenGL SDK. So officially what you have to do is:
好的,那么如何从更高版本的 OpenGL 中获取这些函数呢?嗯:GetProcAddress
。这是官方的做法。并且因为实际细节取决于所讨论的操作系统,但 OpenGL 是独立于操作系统的,所以根本不可能有一个明确的OpenGL SDK。所以正式你必须做的是:
- Download the OpenGL headers for later versions from http://opengl.org/registry
- For each function you want to use define a function pointer variable to hold it
- Load the function pointer using
GetProcAddress
- 从http://opengl.org/registry下载更高版本的 OpenGL 头文件
- 对于每个要使用的函数,定义一个函数指针变量来保存它
- 使用加载函数指针
GetProcAddress
Of course if you just want to use modern OpenGL this is rather tedious. So some people developed 3rd party tools that do the deed for you. And for everything that concerns you as programmer, these tools behave very much like an SDK. The most popular choice so far (but unfortunately it's not completely trouble free, with regard of the bleeding edge of OpenGL) is GLEW. Using GLEW is quite easy. I recommend using it statically linked:
当然,如果您只想使用现代 OpenGL,这会相当乏味。所以有些人开发了 3rd 方工具来为你做事。对于您作为程序员所关心的一切,这些工具的行为非常类似于 SDK。迄今为止最受欢迎的选择(但不幸的是,它并非完全没有问题,就 OpenGL 的前沿而言)是 GLEW。使用 GLEW 非常简单。我建议使用它静态链接:
- Download GLEW from http://glew.sourceforge.net
- Place glew.c and glew.h alongside your projects source files.
- Add glew.c to the list of compiled sources; make sure to have the
GLEW_STATIC
preprocessor macro definition be configured into your projects global compiler flags. - Use
#include "glew.h"
instead of#include <GL/gl.h>
- Right after you created an OpenGL window in your program call
glewInit()
- 从http://glew.sourceforge.net下载 GLEW
- 将glew.c 和glew.h 与您的项目源文件放在一起。
- 将glew.c 添加到编译源列表中;确保将
GLEW_STATIC
预处理器宏定义配置到您的项目全局编译器标志中。 - 使用
#include "glew.h"
代替#include <GL/gl.h>
- 在程序调用中创建 OpenGL 窗口之后
glewInit()
Now some advice: You should really learn to readdocumentation (not just skim it). All of what I've just written is stated on the very references you linked.
现在有一些建议:您应该真正学会阅读文档(而不仅仅是略读)。我刚刚写的所有内容都在您链接的参考资料中有所说明。
回答by PeterT
Let me answer by quoting the link you provided
让我通过引用您提供的链接来回答
For most libraries you are familiar with, you simply #include a header file, make sure a library is linked into your project or makefile, and it all works. OpenGL doesn't work that way. For reasons that are ultimately irrelevant to this discussion, you must manually load functions via a platform-specific API call. This boilerplate work is done with various OpenGL loading libraries; these make this process smooth. You are strongly advised to use one.
对于您熟悉的大多数库,您只需 #include 一个头文件,确保将库链接到您的项目或 makefile 中,然后一切正常。OpenGL 不是这样工作的。由于与本讨论最终无关的原因,您必须通过特定于平台的 API 调用手动加载函数。这个样板工作是通过各种 OpenGL 加载库完成的;这些使这个过程顺利。强烈建议您使用一个。
this passage contains a handy link to a wiki page entitled OpenGL Loading Library
这篇文章包含一个方便的链接,指向名为OpenGL 加载库的 wiki 页面
回答by Ryan Butcher
As so far as I can tell with working in the OpenTK framework(C#) and the JOGL framework(Java), your OpenGL version is determined by your graphics card. I know my laptop only supports OpenGL 1.1 on my desktop it supports the most recent version.
据我所知,在 OpenTK 框架(C#)和 JOGL 框架(Java)中工作时,您的 OpenGL 版本由您的显卡决定。我知道我的笔记本电脑在我的桌面上只支持 OpenGL 1.1,它支持最新版本。
Laptop has an Intel IGP with an I5 proc.
笔记本电脑有一个带 I5 处理器的英特尔 IGP。
Desktop has a Radeon 6950HD.
桌面有一个 Radeon 6950HD。