GLEW擴展庫【轉】


http://blog.sina.com.cn/s/blog_4aff14d50100ydsy.html

 

一、關於GLEW擴展庫:

GLEW是一個跨平台的C++擴展庫,基於OpenGL圖形接口。使用OpenGL的朋友都知道,window目前只支持OpenGL1.1的涵數,但 OpenGL現在都發展到2.0以上了,要使用這些OpenGL的高級特性,就必須下載最新的擴展,另外,不同的顯卡公司,也會發布一些只有自家顯卡才支 持的擴展函數,你要想用這數涵數,不得不去尋找最新的glext.h,有了GLEW擴展庫,你就再也不用為找不到函數的接口而煩惱,因為GLEW能自動識 別你的平台所支持的全部OpenGL高級擴展涵數。也就是說,只要包含一個glew.h頭文件,你就能使用gl,glu,glext,wgl,glx的全 部函數。GLEW支持目前流行的各種操作系統(including Windows, Linux, Mac OS X, FreeBSD, Irix, and Solaris)。



項目主頁: http://glew.sourceforge.net/ 
文檔地址: http://glew.sourceforge.net/install.html 
下載地址: https://sourceforge.net/project/downloading.php?group_id=67586&filename=glew-1.5.1-src.zip

================================================================================================================================================

在OpenGL編程中,使用glMultiTexCoord2f( GL_TEXTURE0+i, x, y ); 你會發現這個函數都沒有定義的,以及GL_TEXTURE0,這個常量。因為在vc編譯器中,OpenGL的版本是1.1,版本比較落后,下個 glew庫就可以了。

安裝的步驟,與glut一模一樣,將dll動態鏈接庫放在c:\WINDOWS:\system32;

頭文件,可以搜索一下gl.h,或者gluax.h會發現在編譯器(指vc)的安裝路徑下的,vc:\PlatformSDK:\include \gl文件夾中,將glew放在這里就ok了。

靜態庫lib文件的話,放在vc:\PlatformSDK:\Lib文件中。

如果在使用了glew,但是編譯出:沒有定義的XXX函數之類的錯誤,可以在文件開頭加上: #pragma comment(lib, "glew32.lib")

還有一個問題:查看glew.h文件,會發現這么一段話:

#if defined(__gl_h_) || defined(__GL_H__)

#error gl.h included before glew.h

#endif

這句規定不能在glew之前引用gl.h文件。否則會報錯:gl.h included before glew.h


下載地址: http://glew.sourceforge.net/


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/jisdy/archive/2008/07/29/2730432.aspx

=====================================================================================================================

OpenGL Extension Wrangler Library(轉)

http://glew.sourceforg.net

實在不知道Wrangler 應該在這里當什么意思講,不過他已經被包含到OpenGL SDK了,官方是推薦使用它的。

GLEW有什么用?你可以把它看作是windows上的OpenGL的基本庫,他取代了原來的gl.h wgl.h,讓你可以方便的調用最新的OpenGL功能,包括OpenGL眾多的Extension。

實際上我對這個OpenGL的體系也是稀里糊塗,除了核心的概念和功能,對擴展功能基本上是抓瞎的。這就是為什么我要讀GLEW的源碼。讀源碼似乎很傻,不過總比等別人出書要聰明的多。

開始吧。

基本特性:

  • 跨操作系統
  • 支持動態鏈接、靜態庫,你還可以把glew.c復制到工程中一起編譯
  • 只依賴於OPENGL32.dll和KERNEL32.dll
  • 因為不依賴於CRT所有你使用single-threaded, multi-threaded 或者 multi-threaded DLL都沒有限制

文件結構:

發布版本包括一個glew32.dll和對應的glew32.lib,頭文件glew.h和wglew.h。(windows dll版本)。源文件只有一個glew.c。

glew基本使用:

初始化

#include <GL/glew.h>放在一切和gl有關的頭文件之前,就完成了引用

GLenum err = glewInit(); 如果err為GLEW_OK(實際上返回0,其宏定義中GLEW_OK為0)就完成了初始化,應該放在程序的開始部分。現在還是不明白為什么有操作可以早於初始化執行。

檢查擴展

if (GLEW_ARB_vertex_program)
{

glGenProgramsARB(...);
}

if (GLEW_VERSION_1_3)
{

}

if (glewIsSupported("GL_VERSION_1_4 GL_ARB_point_sprite"))
{

}

if (glewGetExtension("GL_ARB_fragment_program"))
{

}

GLEW為了實現擴展檢查的功能,編制了大量的代碼。簡單的看就是即支持宏標簽又支持字符串查詢。

glew.h源碼結構:

開頭就檢測有沒有和GLEW沖突的庫

#if defined(__gl_h_) || defined(__GL_H__)
#error gl.h included before glew.h
#endif
#if defined(__glext_h_) || defined(__GLEXT_H_)
#error glext.h included before glew.h
#endif
#if defined(__gl_ATI_h_)
#error glATI.h included before glew.h
#endif

這些文件功能上和GLEW是重復的,所有開始就屏蔽掉了。

接下來的一堆宏只是為了正確的定義 GLEWAPI GLAPI GLAPIENTRY CALLBACK 等等宏。這些定義本來已經在其他的頭文件中定義了,比如WINGDIAPI就在winnt.h中定義過了。這里畫蛇添足的做法只是為了保證GLEW的純潔性,為了不依賴任何不必要的庫和頭文件。

再下面就是OpenGL1.1的定義了。這里很簡單就是類型定義和宏定義,API使用外部函數方式,這就是為什么不需要gl.h了。

#define GLEW_VERSION_1_1 GLEW_GET_VAR(__GLEW_VERSION_1_1)

唯一讓人有點不解的是這句,解析了所有的宏之后GLEW_VERSION_1_1被定義為一個GLboolean類型的變量。之后就讓人很煩了 __GLEW_VERSION_1_1被初始化為GL_FALSE,然后作者又在檢測到支持1.1之后把GLEW_VERSION_1_1賦值為真。宏來宏去的讓人郁悶。不過想想也是有道理的,GLEW_VERSION_1_1作為檢測標志必須是個宏,而宏是不能修改值的,既然他可能為真也可能為假,那就只有一個辦法,找一個變量作為他的存儲空間,然后這個宏就可以當變量使用了。

再下面#include <GL/glu.h>

再下面是OpenGL1.2的定義,和1.1的定義方式類似,只是函數定義方式不同了。

typedef void (GLAPIENTRY * PFNGLCOPYTEXSUBIMAGE3DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);
typedef void (GLAPIENTRY * PFNGLDRAWRANGEELEMENTSPROC) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
typedef void (GLAPIENTRY * PFNGLTEXIMAGE3DPROC) (GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
typedef void (GLAPIENTRY * PFNGLTEXSUBIMAGE3DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels);

#define glCopyTexSubImage3D GLEW_GET_FUN(__glewCopyTexSubImage3D)
#define glDrawRangeElements GLEW_GET_FUN(__glewDrawRangeElements)
#define glTexImage3D GLEW_GET_FUN(__glewTexImage3D)
#define glTexSubImage3D GLEW_GET_FUN(__glewTexSubImage3D)

4個函數指針定義對應4個define,看名字就知道他們是一一對應的,但是就是對不上,郁悶吧。但是在文件最后的部分能找到這樣的代碼。

GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;
GLEW_FUN_EXPORT PFNGLDRAWRANGEELEMENTSPROC __glewDrawRangeElements;
GLEW_FUN_EXPORT PFNGLTEXIMAGE3DPROC __glewTexImage3D;
GLEW_FUN_EXPORT PFNGLTEXSUBIMAGE3DPROC __glewTexSubImage3D;

也是4個,這下對上號了吧。PFNGLCOPYTEXSUBIMAGE3DPROC 是一個函數指針類型定義 __glewCopyTexSubImage3D是實際被定義的指針函數,#define glCopyTexSubImage3D GLEW_GET_FUN(__glewCopyTexSubImage3D)又指向了glCopyTexSubImage3D 。到最后 glCopyTexSubImage3D 才是真正的API接口。真費勁。

再往下1.3 1.4 1.5 2.0 2.1 3.0結構完全和1.2一致

再往下就是各種擴展了,形式上還是和1.2定義的方式一致

再往下就是GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;等等了。

再往下是GLEW_VAR_EXPORT GLboolean __GLEW_VERSION_1_1;等等。

最后才是GLEW的主要函數接口。這里支持一個GLEW_MX的定義,就是對GLEW MX的支持。

glew.c源碼結構:

相對簡單的多,除了一些函數的實現以外,全是初始化函數。

全部都是從glewInit開始的,一路下去初始化各個版本和所有擴展。

wglew

看明白上面的,這里就不用說了,比葫蘆看瓢吧.

 

二、生產並添加靜態庫:

vs2008  生成靜態鏈接庫lib
 
1. 靜態鏈接庫的創建
 
   在solution中 new project ->win32 project -> static library 即可創建生成靜態鏈接庫的項目。 在該項目的properties中可看到:
 
Configuration Properties -->General --> Configuration type 中可以看到是lib
Configuration Properties --> General --> Output Directory 是生成lib的目錄
Library -->General -->Output File 是生成的文件名字
 
將GLEW鏈接為靜態庫,在你的project preprocessor settings中,需要定義GLEW_STATIC 。否則,GLEW會認為使用的是DLL版本。
 
2. 添加靜態鏈接庫
 
包含頭文件
引入lib文件

參見:http://technet.microsoft.com/zh-cn/library/ms235627.aspx

To use the functionality from the static library in the application
1). After you create a console application, the wizard creates an empty program for you. The name for the source file will be the same as the name that you chose for the project earlier. In this example, it is named MyExecRefsLib.cpp.
2).You must reference the static library you created to use its math routines. To do this, select References from the Projectmenu. From the MyExecRefsLib Property Pages dialog box, expand the Common Properties node and then click Add New Reference. For more information about the References dialog box, see Framework and References, Common Properties, <Projectname> Property Pages Dialog Box.
3). The Add Reference dialog box is displayed. The Projects tab lists the projects in the current solution and any libraries that you can reference. On the Projects tab, select MathFuncsLib.Click OK.
4).To reference the MathFuncsLib.h header file, you must modify the include directories path. In the MyExecRefsLib Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. In theAdditional Include Directories property value, type the path of the MathFuncsLib directory or browse for it.
5). To browse for the directory path, in the property value drop-down list box, click Edit. In the Additional Include Directoriesdialog box, in the text box, select a blank line and then click the ellipsis button (…) at the end of the line. In the Select Directory dialog box, select the MathFuncsLib directory and then click Select Folder to save your selection and close the dialog box. In the Additional Include Directories dialog box, click OK.
6).You can now use the MyMathFuncs class in this application. To do this, replace the contents of MyExecRefsLib.cpp with the following code.

調用lib庫提供的函數
    注意,debug 和 release 模式下生的的lib文件是不一樣的,在其他項目引用生成的lib文件時,處於debug模式下必須引用debug模式下生成的lib文件,release模式下必須引用release模式下生成的文件。否則可能會出問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM