(转)glut函数详解(1)--glut初始化API

*转自:http://old.blog.edu.cn/user3/zjdukang/archives/2007/1751751.shtml
(1)    void glutInit(int *argc, char **argv);
     这个函数用来初始化GLUT库.这个函数从main函数获取其两个参数.对应main函数的形式应是:int main(int argc,char* argv[]);
//*******************************************************************************************
(2)    void glutInitDisplayMode(unsigned int mode);
设置图形显示模式.参数mode的可选值为:
     GLUT_RGBA:当未指明GLUT—RGBA或GLUT—INDEX时,是默认使用的模式.表明欲建立RGBA模式的窗口.
     GLUT_RGB:与GLUT—RGBA作用相同.
     GLUT_INDEX:指明为颜色索引模式.
     GLUT_SINGLE:只使用单缓存
     GLUT_DOUBLE:使用双缓存.以避免把计算机作图的过程都表现出来,或者为了平滑地实现动画.
     GLUT_ACCUM:让窗口使用累加的缓存.
     GLUT_ALPHA:让颜色缓冲区使用alpha组件.
     GLUT_DEPTH:使用深度缓存.
     GLUT_STENCIL:使用模板缓存.
     GLUT_MULTISAMPLE:让窗口支持多例程.
     GLUT_STEREO:使窗口支持立体.
     GLUT_LUMINACE:luminance是亮度的意思.但是很遗憾,在多数OpenGL平台上,不被支持.
//******************************************************************************************
(3)    void glutInitWindowPosition(int x, int y);
设置初始窗口的位置(窗口左上角相对于桌面坐标(x,y))
//******************************************************************************************
(4)    void glutInitWindowSize(int width, int height);
设置初始窗口的大小
//******************************************************************************************
(5)    void glutMainLoop(void);
让glut程序进入事件循环.在一个glut程序中最多只能调用一次,且必须调用.一旦调用,会直到程序结束才返回.
//******************************************************************************************
(6)    void glutInitDisplayString(const char *string);
通过一个字符串初始化display mode
参数:string:display mode的描述字符串
这个描述字符串用在创建顶级窗口,子窗口和重叠层时,给将要被创建的窗口或重叠层设置display mode.
这个字符串是由0个或多个功能描述参数组成,每个功能描述参数用空格或tab键隔开.(若未给参数限制缓存精度大小,则采用默认值)

例如:
glutInitDisplayString("stencil~2 rgb double depth>=16 samples");
上例将窗口初始化为至少2位的模板缓存,RGB模式alpha占用位数为0,深度缓存至少为16位,如果平台支持的话使用mutlisampling技术.
可以使用的符号有:
=   等号
!=  不等号
<   小于号(越小越好)
>   大于号(越大越好)
<=  小于等于(越小越好)
>=  大于等于(尽可能选择大的数值,主要用于颜色缓存或深度缓存等对位数要求高的参数设置)
~   大于等于(但尽可能选择小的数值,有效利用资源,主要用于模板缓存等)
 
主要参数:
alpha : alpaha缓存精度, 默认值>=1,即大于等于1位;
acca  : red, green, blue, 和alpha累积缓存精度, 默认值>=1
acc  :  red, green, blue累积缓存精度, 默认值>=1,alpha累积缓存精度为0;
blue :  blue颜色缓存精度, 默认值>=1;
buffer: 颜色索引缓存精度, 默认值>=1;
conformant :布尔值,指示帧缓存配置是否一致,该值基于GLX的EXT_visual_rationg扩展的支持,若不支持,则默认为一致, 默认值=1;
depth : 深度缓存精度, 默认值>=12;
double: 布尔值,指示颜色缓存是否是双倍缓存. 默认值=1;
green : green颜色缓存精度,默认值>=1;
index : 布尔值,指示是否为颜色索引,true表示是颜色索引, 默认值>=1;
num  :  专用名词,指示数值表示的第n个帧缓存配置与这个描述字符串相符合的地方,当没有指定,则glutInitDisplayString也返回初始(最佳符合)配置.
red :   red颜色缓存精度, 默认值>=1;
rgba :  rgba模式,颜色缓存精度默认值>=1;
rgb :   rgb模式,颜色缓存精度默认值>=1,alpha精度=0;
luminance: 设置red颜色缓存精度,默认值>=1,其他颜色缓存精度=0(alpha没有指定);
stencil: 模板缓存精度
single: 布尔值,指示颜色缓存是否为单缓存, 默认值=1;
stereo :布尔值,标示颜色缓存支持OpenGL的三维系统,默认值=1;
samples: 标示multisamples的值,这个值是基于GLX的SGIS_multisample的扩展.默认值<=4.这个默认值表示如果支持的话可以在glutInitDisplayString中添加描述参数让glut请求multipsampling;
slow :  布尔值,标示帧缓存配置是否是slow的.对于X11对glut的执行,slow信息是基于GLX的EXT_visual_rating扩展的支持,如果不支持,就认为是fast;对于win32对glut的执行,slow是基于像素格式描述(pixel format Descriptor即PFD)被标记为"generic"并且未被标记为"accelerated",这说明Microsoft关于slow的OpenGL执行只用在PFD中.这个参数的作用是帮助程序避免对于当前机器的帧缓存配置越来越慢.默认值>=0,表示slow visuals优先于fast visuals,但fast visuals仍然被允许.
win32pdf : 只在win32系统中识别glut的请求,这个参数与win32中的像素格式(pixel format)相匹配,它的值是个数字
xvisual: 只在X Window系统中识别glut的请求,这个参数与X visual ID相匹配,它的值是个数字
xstaticgray:  只在X Window系统中识别glut请求,是个布尔值,标示帧缓存配置的X visual是否是StaticGray. 默认值=1
xgrayscale  : 只在X Window系统中识别glut的请求,是个布尔值,标示帧缓存配置的X visual是否是GrayScale. 默认值=1;
xstaticcolor:  只在X Window系统中识别glut的请求,是个布尔值,标示帧缓存配置的X visual是否是StaticColor. 默认值=1;
xpseudocolor : 只在X Window系统中识别glut的请求,是个布尔值,标示帧缓存配置的X visual是否是PsuedoColor. 默认值=1;
xtruecolor  :  只在X Window系统中识别glut的请求,是个布尔值,标示帧缓存配置的X visual是否是TrueColor. 默认值=1;
xdirectcolor : 只在X Window系统中识别glut的请求,是个布尔值,标示帧缓存配置的X visual是否是DirectColor. 默认值=1;
//*******************************************************************************************
网上没有搜到关于glut的完整资料,于是下狠心自己花时间整理了一下.这里只是glut初始化的API,其中void glutInitDisplayString(const char *string);在国内网站上没搜到具体解释,是从国外网站上翻译过来的,本人英文水平有限,可能有错误,下面贴上英文原文,望达人指点.(其他API 会在以后分类后陆续给出).
 
void glutInitDisplayString(const char *string)  - sets the initial display mode via a string.
string:Display mode description string, see below.
Description:
The initial display mode description string is
used  when creating top-level windows,subwindows,
and overlays to determine the OpenGL display mode
for the to-be-created window or overlay.
The string is a list of zero or more capability
descriptions separated by spaces and tabs.Each
capability description is a capability name that
is optionally followed by a comparator and a
numeric value.For example,"double" and "depth>=12"
are both valid criteria.
The capability descriptions are translated into
a set of criteria used to select the appropriate
frame buffer  configuration.
The criteria are matched in strict left to right
order of precdence.That is,the first specified
criteria(leftmost) takes precedence over the later
criteria for nonexact criteria (greater than,less
than,etc.comparators).Exact criteria (equal, not
equal compartors) must match exactly so precedence
is not relevant.
The numeric value is an integer that is parsed
according to ANSI C's strtol(str,strptr,0) behavior.
This means that decimal,octal(leading 0),and
hexidecimal values(leading 0x) are accepeted.
The valid compartors are:
=  Equal.
!= Not equal.
<  Less than and preferring larger difference
   (theleast is best).
>  Greater than and preferring larger differences
   (the most is best).
<= Less than or equal and preferring larger
   difference(the least is best).
>= Greater than or equal and preferring more
   instead of less. This comparator is useful for
   allocating resources like color precsion or
   depth buffer precision where the maximum
   precison is generally preferred.Contrast with
   the tilde (~) comprator.
~  Greater than or equal but preferring less
   instead of more.This compartor is useful for
   allocating resources such as stencil bits or
   auxillary color buffers where you would rather
   not over allocate.
   When the compartor and numeric value are not
   specified,each capability name has a different
   default(one default is to require a compartor
   and numeric value).
The valid capability names are:
alpha:Alpha color buffer precision in bits.Default
      is ">=1".
acca:Red,green,blue,and alpha accumulation buffer
     precision in bits.Default is ">=1" for red,
     green,blue,and alpha capabilities.
acc:Red,green,and green accumulation buffer
    precision in bits and zero bits of alpha
    accumulation buffer precision.Default is ">=1"
    for red,green,and blue capabilities,and "~0"
    for the alpha capability.
blue:Blue color buffer precision in bits.Default
     is ">=1".
buffer:Number of bits in the color index color
       buffer.Default is ">=1".
conformant:
       Boolean indicating if the frame buffer
       configuration is conformant or not.
       Conformance information is based on GLX's
       EXT_visual_rating extension if supported.
       If the extension is not supported,all
       visuals are assumed conformat.Default is
       "=1".
depth:Number of bits of precsion in the depth
      buffer.Default is ">=12".
double:Boolean indicating if the color buffer is
       double buffered.Default is "=1".
green:Green color buffer precision in bits.Default
      is ">=1".
index:Boolean if the color model is color index or
      not.True is color index.Default is ">=1".
num:A special capability name indicating where the
    value represents the Nth frame buffer
    configuration matching the description string.
    When  not specified,glutInitDisplayString also
    returns the first(best matching) configuration.
    num requires a compartor and numeric value.
red;Red color buffer precision in bits.Default is
        ">=1".
rgba:Number of bits of red,green,blue,and  alpha
     in the RGBA color buffer.Default is ">=1"
     for red,green,blue,and alpha capabilities,
     and "=1"  for the RGBA color model capability.
rgb:Number of bits of red,green,and blue in the
    RGBA color buffer and zero bits of alpha color
    buffer precision.Default  is ">=1" for the red,
    green,and blue capabilities,and "~0" for alpha
    capability,and "=1" for the RGBA color model
    capability.
luminance:
    Number of bits of red in the RGBA and zero
    bits of green,blue(alpha not specified) of
    color buffer precision.Default is ">=1"
    for the red capabilitis,and "=0" for the green
    and blue capabilities,and "=1" for the RGBA
    color model capability,and,for X11,"=1" for the
    StaticGray ("xstaticgray")capability.SGI
    InfiniteReality(and other future machines)
    support a 16-bit luminance (single channel)
    display mode(an additional 16-bit alpha channel
    can also be requested).The red channel maps to
    gray scale and green and blue channels are not
    available.A 16-bit precision luminance display
    mode is often appropriate for medical imaging
    applications.Do not expect many machines to
    support extended precision luminance display
    modes.
stencil:Number of bits in the stencil buffer.
single:Boolean indicate the color buffer is single
       buffered.Double buffer capability "=1".
stereo:Boolean indicating the color buffer is
       supports OpenGL-style stereo.Default is "=1".
samples:Indicates the number of multisamples to use
        based on GLX's SGIS_multisample extension(for
        antialiasing).Default is "<=4". This default
        means that a GLUT application can request
        multipsampling if available by simply
        specifying "samples".
slow:Boolean indicating if the frame buffer
     configuration is slow or not.For the X11
     implementation of GLUT,slowness information is
     based on GLX's EXT_visual_rating extension if
     supported.If the EXT_visual_rating extension is
     not supported,all visuals are assumed fast.For
     the Win32 implementation of GLUT,slowness is
     based on if the underlying Pixel Format Descriptor
     (PFD)is marked "generic" and not "accelerated".
     This implies that Microsoft's relatively slow
     software OpenGL implementation is used by this
     PFD.Note that slowness is a relative designation
     relative to other frame buffer configurations
     available.The intent of the slow capability is to
     help programs avoid frame buffer configurations
     that are slower(but perhaps higher precision) for
     the current machine.Default is ">=0" if not
     comparator and numeric value are provided.This
     default means that slow visuals are used in
     preference to fast visuals,but fast visuals will
     still be allowed.
win32pfd:
     Only recognized on GLUT implementations for Win32,
     this  capability name matches the Win32 Pixel For-
     mat Descriptor by numer.  win32pfd requires a com-
     partor and numeric value.
xvisual:Only recongized on GLUT implementations for the
        X Window System,this capability name matches
        the X visual ID by number.xvisual requires a
        compartor and numeric value.
xstaticgray:
        Only recongized on GLUT implementations for the
        X Window System,boolean indicating if the frame
        buffer configuration's X visual is of type Stat-
        icGray.Default is "=1".
xgrayscale:
        Only recongized on GLUT implementations for the
        X Window System,boolean indicating if the frame
        buffer configuration's X visual is of type
        GrayScale.Default is "=1".
xstaticcolor:
        Only recongized on GLUT implementations for the
        X Window System,boolean indicating if the frame
        buffer configuration's X visual is of type
        StaticColor.Default is "=1".
xpseudocolor:
        Only recongized on GLUT implementations for the
        X Window System,boolean indicating if the frame
        buffer configuration's X visual is of type
        PsuedoColor.Default is "=1".
xtruecolor:
        Only recongized on GLUT implementations for the
        X Window System,boolean indicating if the frame
        buffer configuration's X visual is of type
        TrueColor.Default is "=1".
xdirectcolor:
        Only recongized on GLUT implementations for the
        X Window System,boolean indicating if the frame
        buffer configuration's X visual is of type
        DirectColor.Default is "=1".
Unspecifed capability descriptions will result in
unspecified criteria being generated.These unspecified
criteria help glutInitDisplayString behave sensibly
with terse display mode description strings.For
example,if no "slow" capability description is provided,
fast frame buffer configurations will be choosen in
preference to slow frame buffer configurations,but slow
frame  buffer  configurations will still be choosen if
no better fast frame buffer configuration is available.
Example:
Here is an examples using glutInitDisplayString:
glutInitDisplayString("stencil~2 rgb double depth>=16 samples");
The above call requests a window with an RGBA color
model(but requesting no bits of alpha),a depth buffer
with at least 16 bits of precsion but preferring more,
mutlisampling if available,and at least 2 bits of
stencil(favoring less stencil to more as long as 2 bits
are available).

posted @ 2009-03-03 15:15 小虫虫 阅读(873) | 评论 (0)编辑 收藏

(转)glut中去掉控制台

*文章转自:http://old.blog.edu.cn/user3/zjdukang/archives/2007/1765297.shtml
    glut是OpenGL应用工具包(OpenGL Utility Toolkit),它为OpenGL提供了一个简易的窗口平台,使程序员不用过多的关心与平台相关的窗口系统.

    建立工程时,我们一般都建立控制台程序(在vc6中即console win32 application)来使用glut库.但是在控制台程序下,运行时都会跳出控制台窗口,很不美观,我们可以用下面的方法将控制台窗口去掉:
#pragma comment( linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
在主函数所在的.cpp文件的顶部加上上面的宏,就可以轻松将控制台窗口去掉,这样就只剩下glut窗口了.

posted @ 2009-03-03 15:10 小虫虫 阅读(550) | 评论 (0)编辑 收藏

glut函数说明

     摘要: Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 st1\:*{behavior:url(#ieooui) } /* Style Definitions */ table.MsoNormalT...  阅读全文

posted @ 2009-03-03 15:02 小虫虫 阅读(742) | 评论 (0)编辑 收藏

VC下OpenGL配置以及glut配置

OpenGL官方网站(英文)
http://www.opengl.org

下面我将对Windows下的OpenGL编程进行简单介绍。

第一步:选择一个编译环境   

现在Windows系统的主流编译环境有Visual Studio,Broland C++ Builder,Dev-C++等,它们都是支持OpenGL的。但这里我们选择VC++ 6.0作为学习OpenGL的环境。   

第二步:安装GLUT工具包   

GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装。   

Windows环境下的GLUT下载地址:(大小约为150k)   
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip  

无法从以上地址下载的话请使用下面的连接:   
http://upload.programfan.com/upfile/200607311626279.zip    

Windows环境下安装GLUT的步骤:   
1、将下载的压缩包解开,将得到5个文件
2、以我的安装目录为例:   
(1)“d:\Program Files\Microsoft Visual Studio\VC98\include\GL文件夹”。把解压得到的glut.h放到这个GL文件夹里。没有GL文件夹可以自己建一个,一般都有的。
(2)“d:\Program Files\Microsoft Visual Studio\VC98\lib文件夹”。把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹,即lib文件夹。
(3)把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\Windows\System32)这是非常重要的动态链接库设置! 

第三步,创建工程,其步骤如下:   

(1)创建一个Win32 Console Application。(以我创建的为例,工程名为simpleGL)   
(2)链接OpenGL libraries:在Visual C++中先单击Project,再单击Settings,再找到Link单击,最后在Object/library modules 的最前面加上opengl32.lib Glut32.lib Glaux.lib glu32.lib 。   
(3)单击Project Settings中的C/C++标签,将Preprocessor definitions 中的_CONSOLE改为__WINDOWS。最后单击OK。  

现在,准备工作基本上完成了,可不要轻视这一步,如果你没有设置好在编译及运行过程中总会出错的。

对于VS2005可以如下设置:
1、把 glut.h 复制到 VC 安装路径下的 PlatFormSDK\include\gl 文件夹
2、把 glut32.lib 复制到 VC 安装路径下的 PlatFormSDK\lib 文件夹
3、把 glut32.dll 复制到 Windows\System32 文件夹
4、在 VC 中创建控制台应用程序,在选项中清除“使用预编译头”(以免影响可移植性)
5、在 VC 中打开项目->属性对话框进行如下设置:
        将“配置”下拉框选则为“所有配置”
        打开 “链接器--输入”项
        在“附加依赖项”中增加:
        OpenGL32.lib
        glu32.lib
        glut32.lib

第四步,创建一个最简单的opengl程序

#include <GL/glut.h>

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

int main(int argc, char *argv[])
{
   
glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个OpenGL程序");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
}

该程序的作用是在一个黑色的窗口中央画一个白色的矩形。下面对各行语句进行说明。

首先,需要包含头文件#include<GL/glut.h>,这是GLUT的头文件。
本来OpenGL程序一般还要包含<GL/gl.h>和<GL/glu.h>,但GLUT的头文件中已经自动将这两个文件包含了,不必再次包含。

然后看main函数。
int main(int argc, char *argv[]),这个是带命令行参数的main函数,各位应该见过吧?没见过的同志们请多翻翻书,等弄明白了再往下看。
注意main函数中的各语句,除了最后的return之外,其余全部以glut开头。这种以glut开头的函数都是GLUT工具包所提供的函数,下面对用到的几个函数进行介绍。
1、glutInit,对GLUT进行初始化,这个函数必须在其它的GLUT使用之前调用一次。其格式比较死板,一般照抄这句glutInit(&argc, argv)就可以了。
2、glutInitDisplayMode,设置显示方式,其中GLUT_RGB表示使用RGB颜色,与之对应的还有GLUT_INDEX(表示使用索引颜色)。GLUT_SINGLE表示使用单缓冲,与之对应的还有GLUT_DOUBLE(使用双缓冲)。更多信息,请自己Google。当然以后的教程也会有一些讲解。
3、glutInitWindowPosition,这个简单,设置窗口在屏幕中的位置。
4、glutInitWindowSize,这个也简单,设置窗口的大小。
5、glutCreateWindow,根据前面设置的信息创建窗口。参数将被作为窗口的标题。注意:窗口被创建后,并不立即显示到屏幕上。需要调用glutMainLoop才能看到窗口。
6、glutDisplayFunc,设置一个函数,当需要进行画图时,这个函数就会被调用。(这个说法不够准确,但准确的说法可能初学者不太好理解,暂时这样说吧)。
7、glutMainLoop,进行一个消息循环。(这个可能初学者也不太明白,现在只需要知道这个函数可以显示窗口,并且等待窗口关闭后才会返回,这就足够了。)

在glutDisplayFunc函数中,我们设置了“当需要画图时,请调用myDisplay函数”。于是myDisplay函数就用来画图。观察 myDisplay中的三个函数调用,发现它们都以gl开头。这种以gl开头的函数都是OpenGL的标准函数,下面对用到的函数进行介绍。
1、glClear,清除。GL_COLOR_BUFFER_BIT表示清除颜色,glClear函数还可以清除其它的东西,但这里不作介绍。
2、glRectf,画一个矩形。四个参数分别表示了位于对角线上的两个点的横、纵坐标。
3、glFlush,保证前面的OpenGL命令立即执行(而不是让它们在缓冲区中等待)。其作用跟fflush(stdout)类似。

posted @ 2009-03-03 14:57 小虫虫 阅读(20348) | 评论 (10)编辑 收藏

仅列出标题
共3页: 1 2 3 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(5)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜