opengl 库函数 glew glfw glad glut gl glu freeglut

opengl 库函数 glew glfw glad glut gl glu freeglut

前言

接触opengl 开发的时候, glew glfw glad glut gl glu freeglut整的有点蒙,在看别人的代码的时候一会儿这个函数一会儿那个函数,所以决心整理下。。。。。。

一、GLEW

The OpenGL Extension Wrangler Library (GLEW) is a cross-platform open-source C/C++ extension loading library. GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform. OpenGL core and extension functionality is exposed in a single header file. GLEW has been tested on a variety of operating systems, including Windows, Linux, Mac OS X, FreeBSD, Irix, and Solaris.

OpenGL Extension Wrangler Library(GLEW)是跨平台的开源C / C ++扩展加载库。 GLEW提供了有效的运行时机制,用于确定目标平台上支持哪些OpenGL扩展。 OpenGL核心和扩展功能在单个头文件中公开。 GLEW已在各种操作系统上进行了测试,包括Windows,Linux,Mac OS X,FreeBSD,Irix和Solaris。

网址:

http://glew.sourceforge.net/

GLEW 是Opengl扩展库,使用它可以很方便的调用OpenGL较新的特性。GLEW能自动识别你的平台所支持的全部OpenGL高级扩展涵数,也就是说,只要包含一个glew.h头文件,你就能使用gl, glu, glext, wgl, glx的全部函数

glew.h 提供了 glCreateShader 等函数的定义

在opengl 程序中编译通过,程序在 glCreateShader出崩溃,原因在于未执行glewInit(); glewInit执行应该在opengl环境就绪后,不能初始化过早,不然出现 “”Missing GL version ” 错误

glew初始化:

	GLenum err = glewInit();
	if (err != GLEW_OK) {
		std::cerr << "GLEW initialization failed" << std::endl;
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		return;
	}

https://www.khronos.org/opengl/wiki/OpenGL_Loading_Library#GLEW_.28OpenGL_Extension_Wrangler.29中glew 描述:


GLEW (OpenGL Extension Wrangler)
The OpenGL Extension Wrangler library provides access to all GL entrypoints. It supports Windows, MacOS X, Linux, and FreeBSD.

As with most other loaders, you should not include gl.h, glext.h, or any other gl related header file before glew.h, otherwise you'll get an error message that you have included gl.h before glew.h. In fact, you shouldn't be including gl.h at all; glew.h replaces it.

GLEW also provides wglew.h which provides Windows specific GL functions (wgl functions). If you include wglext.h before wglew.h, GLEW will complain. GLEW also provides glxew.h for X windows systems. If you include glxext.h before glxew.h, GLEW will complain.

The latest release as of February 2019 is version 2.1.0.

Initialization of GLEW 1.13.0 and earlier
GLEW up to version 1.13.0 has a problem with core contexts. It calls glGetString(GL_EXTENSIONS), which causes GL_INVALID_ENUM on GL 3.2+ core context as soon as glewInit() is called. It also doesn't fetch the function pointers. GLEW version 2.0.0+ uses glGetStringi instead. The only fix for earlier versions is to use glewExperimental:

  // If using GLEW version 1.13 or earlier
  glewExperimental=TRUE;
  GLenum err=glewInit();
  if(err!=GLEW_OK) {
    // Problem: glewInit failed, something is seriously wrong.
    cout << "glewInit failed: " << glewGetErrorString(err) << endl;
    exit(1);
  }
glewExperimental is a variable that is already defined by GLEW. You must set it to GL_TRUE before calling glewInit().

You might still get GL_INVALID_ENUM (depending on the version of GLEW you use), but at least GLEW ignores glGetString(GL_EXTENSIONS) and gets all function pointers.

If you are creating a GL context the old way or if you are creating a backward compatible context for GL 3.2+, then you don't need glewExperimental.
712 / 5000
翻译结果
OpenGL Extension Wrangler库提供对所有GL入口点的访问。 它支持Windows,MacOS X,Linux和FreeBSD。

与大多数其他加载程序一样,您不应在glew.h之前包含gl.h,glext.h或任何其他与gl相关的头文件,否则您将得到一条错误消息,指出您在glew.h之前包含gl.h。 实际上,您根本不应该包含gl.h。 glew.h替换它。

GLEW还提供了wglew.h,它提供了Windows特定的GL函数(wgl函数)。 如果在wglew.h之前包含wglext.h,GLEW将抱怨。 GLEW还为X Windows系统提供glxew.h。 如果在glxew.h之前包含glxext.h,GLEW将抱怨。

截至20192月的最新版本是2.1.0版。 

二、glad

learnOpengl 中描述:
GLAD是用来管理OpenGL的函数指针的,所以在调用任何OpenGL的函数之前我们需要初始化GLAD
glad 初始化:

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
    std::cout << "Failed to initialize GLAD" << std::endl;
    return -1;
}
Glad is pretty similiar to glLoadGen, it generates a loader for your exact needs based on the official specifications from the Khronos SVN. This means they are always up to date! It was written in a way that you can easily extend it to other languages (e.g. at the date of writing this there are 4 different code generating backends).

You can use the glad website to generate a loader for your needs, download it and use it in your project. Another method of using glad is cloning/downloading the repository and generating your own loader. The tool itself is pretty easy to use and works with any Python version above 2.6, you can also include the source directly into your CMake project.

Glad gives you the option to also generate a very basic loader (similiar to gl3w or glxw), but it is recommended to use the loading function provided by your context creation framework, like glfwGetProcAddress. Here is how it looks:
Glad与glLoadGen非常相似,它根据Khronos SVN的官方规范生成了一个满足您确切需求的装载程序。 这意味着它们始终是最新的! 它的编写方式使您可以轻松地将其扩展到其他语言(例如,在撰写本文时,有4种不同的代码生成后端)。

您可以使用高兴的网站生成满足您需求的加载程序,下载并在您的项目中使用它。 使用happy的另一种方法是克隆/下载存储库并生成自己的加载器。 该工具本身非常易于使用,并且可以与2.6以上的任何Python版本一起使用,您也可以将源代码直接包含在CMake项目中。

Glad让您可以选择生成一个非常基本的加载器(类似于gl3w或glxw),但是建议使用上下文创建框架提供的加载功能,例如glfwGetProcAddress。 外观如下: 
// glad, include glad *before* glfw
#include <glad/glad.h>
// GLFW
#include <GLFW/glfw3.h>

// ... <snip> ...

int main()
{
    // Init GLFW
    glfwInit();
    // ... <snip> ... setup a window and a context
 
    // Load all OpenGL functions using the glfw loader function
    // If you use SDL you can use: https://wiki.libsdl.org/SDL_GL_GetProcAddress
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        std::cout << "Failed to initialize OpenGL context" << std::endl;
        return -1;
    }
    // Alternative use the builtin loader, e.g. if no other loader function is available
    /*
    if (!gladLoadGL()) {
        std::cout << "Failed to initialize OpenGL context" << std::endl;
        return -1;
    }
    */
  
    // glad populates global constants after loading to indicate,
    // if a certain extension/version is available.
    printf("OpenGL %d.%d\n", GLVersion.major, GLVersion.minor);

    if(GLAD_GL_EXT_framebuffer_multisample) {
        /* GL_EXT_framebuffer_multisample is supported */ 
    }
    if(GLAD_GL_VERSION_3_0) {
        /* We support at least OpenGL version 3 */
    }

    // ... <snip> ... more code
}
Glad is able to generate a debugging header, which allows you to hook into your OpenGL calls really easily using glad_set_pre_callback and glad_set_post_callback, you can find a more detailed guide on the github repository

在#include“glad/glad.h” 后应该将glad.c放入工程一起编译,否则报错如下:
在这里插入图片描述

三、GLFW

https://learnopengl-cn.github.io/01%20Getting%20started/02%20Creating%20a%20window/#glfw 

GLFW是一个专门针对OpenGL的C语言库,它提供了一些渲染物体所需的最低限度的接口。它允许用户创建OpenGL上下文,定义窗口参数以及处理用户输入,这正是我们需要的。

GLFW 是配合 OpenGL 使用的轻量级工具程序库,缩写自 Graphics Library Framework(图形库框架)。GLFW 的主要功能是创建并管理窗口和 OpenGL 上下文,同时还提供了处理手柄、键盘、鼠标输入的功能。

四、GLUT freeglut

glut是基本的窗口界面,是独立于gl和glu的,如果不喜欢用glut可以用MFC和Win32窗口等代替,但是glut是跨平台的,这就保证了我们编出的程序是跨平台的,如果用MFC或者Win32只能在windows操作系统上使用。选择OpenGL的一个很大原因就是因为它的跨平台性,所以我们可以尽量的使用glut库

GLUT/freeglut 是什么? OpenGL 和它们有什么关系?

OpenGL只是一个标准,它的实现一般自带在操作系统里,只要确保显卡驱动足够新就可以使用。如果需要在程序里直接使用OpenGL,会有很多非常恶心的预备工作要做,而且可能还要专门为平台的差异写一些代码。要跳过这些工作,可以用一个utility库,直接使用它提供的函数,就不用操心那些细节了。这样的库新一点的有GLEW,因为开源所以安装相对方便(大不了丢进去一起编译),但各种教程和书里常见的是闭源的GLUT。由于GLUT的作者已经很久没更新过了(最后更新于2000年!= =),所以其他人另外做了一个接口兼容GLUT的freeglut,开源而且一直在维护中。

五、GL GLU

OpenGL中的gl库是核心库,glu是实用库,glut是实用工具库, gl是核心,glu是对gl的部分封装,glut是OpenGL的跨平台工具库,gl中包含了最基本的3D函数,而glu似乎对gl的辅助,如果算数好,不用glu的情况下,也是可以做出同样的效果

opengl 实用库 : 43个函数,以glu开头,包括纹理映射、坐标变换、多边形分化、绘制一些如椭球、圆柱、茶壶等简单多边形实体 部分函数象核心函数一样在任何OpenGL平台都可以应用。
opengl辅助库: 31个函数,以aux 开头,

总结

以下包可以用来创建并管理 OpenGL 窗口,也可以管理输入,但几乎没有除此以外的其它功能:
GLFW——跨平台窗口和键盘、鼠标、手柄处理;偏向游戏
freeglut——跨平台窗口和键盘、鼠标处理;API 是 GLUT API 的超集,同时也比 GLUT 更新、更稳定
GLUT——早期的窗口处理库,已不再维护
支持创建 OpenGL 窗口的还有一些“多媒体库”,同时还支持输入、声音等类似游戏的程序所需要的功能:
Allegro 5——跨平台多媒体库,提供针对游戏开发的 C API
SDL——跨平台多媒体库,提供 C API
SFML——跨平台多媒体库,提供 C++ API;同时也提供 C#、Java、Haskell、Go 等语言的绑定
窗口包
FLTK——小型的跨平台 C++ 窗口组件库
Qt——跨平台 C++ 窗口组件库,提供许多OpenGL辅助对象;@CryEngine
wxWidgets——跨平台 C++ 窗口组件库
函数加载
经典版:glew
时尚版:glad

(1) glfw+glew
(2) glfw+glad
(3) freeglut

资料参考网站:
https://zhuanlan.zhihu.com/p/57879987

https://www.khronos.org/opengl/wiki/OpenGL_Loading_Library#glad_.28Multi-Language_GL.2FGLES.2FEGL.2FGLX.2FWGL_Loader-Generator.29

https://learnopengl-cn.github.io/01%20Getting%20started/02%20Creating%20a%20window/#glfw

https://blog.csdn.net/deniece1/article/details/102642934

https://www.cnblogs.com/huty/p/8517265.html

https://blog.csdn.net/fanhenghui/article/details/52882837

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值