用VS 2005 openGL开发

<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta content="Word.Document" name="ProgId"> <meta content="Microsoft Word 11" name="Generator"> <meta content="Microsoft Word 11" name="Originator"> <link href="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/02/clip_filelist.xml" rel="File-List"> <smarttagtype downloadurl="" name="chmetcnv" namespaceuri="urn:schemas-microsoft-com:office:smarttags"></smarttagtype><!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!--[if !mso]><object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui></object> <style> st1/:*{behavior:url(#ieooui) } </style> <![endif]--><style type="text/css"> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} span.a {mso-style-name:a; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:297147598; mso-list-type:hybrid; mso-list-template-ids:-386086096 1849458768 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-text:%1.; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-36.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

关于opengl的相关函数库:

OpenGL函数库相关的API有核心库(gl)、实用库(glu)、辅助库(aux)、实用工具库(glut)、窗口库(glxaglwgl)和扩展函数库等。gl是核心,glu是对gl的部分封装。glxaglwgl 是针对不同窗口系统的函数。glut是为跨平台的OpenGL程序的工具包,比aux功能强大。扩展函数库是硬件厂商为实现硬件更新利用OpenGL的扩展机制开发的函数。下面逐一对这些库进行详细介绍。

<!--[if !supportLists]-->1. <!--[endif]-->OpenGL核心库

核心库包含有115个函数,函数名的前缀为gl 这部分函数用于常规的、核心的图形处理。此函数由gl.dll来负责解释执行。由于许多函数可以接收不同数以下几类。据类型的参数,因此派生出来的函数原形多达300多个。 2 OpenGL实用库The OpenGL Utility Library (GLU) 包含有43个函数,函数名的前缀为glu OpenGL提供了强大的但是为数不多的绘图命令,所有较复杂的绘图都必须从点。线、面开始。Glu 为了减轻繁重的编程工作,封装了OpenGL函数,Glu函数通过调用核心库的函数,为开发者提供相对简单的用法,实现一些较为复杂的操作。此函数由glu.dll来负责解释执行。OpenGL中的核心库和实用库可以在所有的OpenGL平台上运行。 3 OpenGL辅助库 包含有31个函数,函数名前缀为aux 这部分函数提供窗口管理、输入输出处理以及绘制一些简单三维物体。此函数由glaux.dll来负责解释执行。创建aux库是为了学习和编写OpenGL程序,它更像是一个用于测试创意的预备基础接管。Aux库在windows实现有很多错误,因此很容易导致频繁的崩溃。在跨平台的编程实例和演示中,aux很大程度上已经被glut库取代。OpenGL中的辅助库不能在所有的OpenGL平台上运行。 4 OpenGL工具库 OpenGL Utility Toolkit 包含大约30多个函数,函数名前缀为glut glut是不依赖于窗口平台的OpenGL工具包,由Mark KLilgradSGI编写(现在在Nvidia),目的是隐藏不同窗口平台API的复杂度。 函数以glut开头,它们作为aux库功能更强的替代品,提供更为复杂的绘制功能,此函数由glut.dll来负责解释执行。由于glut中的窗口管理函数是不依赖于运行环境的,因此OpenGL中的工具库可以在X-Window, Windows NT, OS/2等系统下运行,特别适合于开发不需要复杂界面的OpenGL示例程序。对于有经验的程序员来说,一般先用glut理顺3D图形代码,然后再集成为完整的应用程序。 xp用的就是opengl 1.1,而VISTA用的版本更新些。所以我们还需要什么的,第一就是gl的头文件,第二就是glu,第三是glut

下面一步步来介绍安装的方法:

[目录] include: <VCDIR>/PlatformSDK/Include/GL lib: <VCDIR>/PlatformSDK/Lib dll: <WINDIR>/system32

[VS 2005 自带的OpenGL文件] include: GL/gl.h GL/glu.h GL/glaux.h lib: opengl32.lib glu32.lib glaux.lib(* Deprecated) dll: opengl32.dll glu32.dll [SGI OpenGL 文件(* OPT)] lib: opengl.lib glu.lib

dll: opengl.dll glu.dll

[GLUT utility toolkit 文件 ] include: GL/glut.h lib: glut.lib glut32.lib

dll: glut.dll glut32.dll

最后是glutGLUT library)的安装:

GLUT 允许我们开发可移植的OpenGL窗口程序,它包装了底层的窗口系统接口。www.opengl.org的网站上有source code下载,这样就需要我们自己去编译。openGL官方是没有提供windows下编译好的dll供我们使用的,但是好心人Nate Robins and Paul Mayfield Layne Christensen 的帮助下,编译了可以直接使用的文件供我们下载。他们的主页是:http://www.xmission.com/~nate/glut.html openGL官方也提供这个包的下载,在http://www.opengl.org/resources/libraries/glut/下载。把下载下来的zip包解压会得到5个文件:glut.dll glut.h glut.lib glut32.dll glut32.lib。其中glut.hglut的头文件,.lib文件和.dll文件带32的是微软的版本,不带32的是SGI的版本。其实差不多,只是两个不一样的API

VC++

glut.h C:/Program Files/Microsoft Visual Studio/VC98/Include/GL

glut.lib glut32.lib C:/Program Files/Microsoft Visual Studio/ VC98/Lib

VS2005

glut.h C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK /Include/GL

glut.lib glut32.lib C:/Program Files/Microsoft Visual Studio 8/VC /PlatformSDK/lib

最后把glut.dll glut32.dll放到操作系统目录下面的system32文件夹内。如果系统盘为C盘则位置为:C:/Windows/System32

这样以后在Visual Studio 2005下面openGL的环境就设置好了

现在新建一个空的windows console application,加入:

#include<GL/glut.h>

就可以开发自己的OpenGL程序了 如果在老版本的VS 6.0 中开发,需要将连接库添加在项目设置属性中。VC++下面还有多一个步骤-- console能显示win32窗口程序。选择 Project -> settings -> Link 选项卡 -> Category 中选择 Output 然后 Entry-point synmbol 文本框中键入 mainCRTStartup 这样就可以开始写openGL程序!

第三步,建立一个OpenGL工程

这里以VisualStudio2005为例。

选择File->New->Project,然后选择Win32 Console Application,选择一个名字,然后按OK

在谈出的对话框左边点Application Settings,找到Empty project并勾上,选择Finish

然后向该工程添加一个代码文件,取名为“OpenGL.c”,注意用.c来作为文件结尾。 搞定了,就跟平时的工程没什么两样的。 第一个OpenGL程序 一个简单的OpenGL程序如下:

#include <GL/glut.h>

void myDisplay(void)

{ glClear(GL_COLOR_BUFFER_BIT); glRectf(<chmetcnv w:st="on" unitname="F" sourcevalue=".5" hasspace="False" negative="True" numbertype="1" tcsc="0">-0.5f</chmetcnv>, <chmetcnv w:st="on" unitname="F" sourcevalue=".5" hasspace="False" negative="True" numbertype="1" tcsc="0">-0.5f</chmetcnv>, <chmetcnv w:st="on" unitname="F" sourcevalue=".5" hasspace="False" negative="False" numbertype="1" tcsc="0">0.5f</chmetcnv>, <chmetcnv w:st="on" unitname="F" sourcevalue=".5" hasspace="False" negative="False" numbertype="1" tcsc="0">0.5f</chmetcnv>); 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工具包所提供的函数,下面对用到的几个函数进行介绍。 1glutInit,对GLUT进行初始化,这个函数必须在其它的GLUT使用之前调用一次。其格式比较死板,一般照抄这句glutInit(&argc, argv)就可以了。 2glutInitDisplayMode,设置显示方式,其中GLUT_RGB表示使用RGB颜色,与之对应的还有GLUT_INDEX(表示使用索引颜色)。GLUT_SINGLE表示使用单缓冲,与之对应的还有GLUT_DOUBLE(使用双缓冲)。更多信息,请自己Google。当然以后的教程也会有一些讲解。 3glutInitWindowPosition,这个简单,设置窗口在屏幕中的位置。 4glutInitWindowSize,这个也简单,设置窗口的大小。 5glutCreateWindow,根据前面设置的信息创建窗口。参数将被作为窗口的标题。注意:窗口被创建后,并不立即显示到屏幕上。需要调用glutMainLoop才能看到窗口。 6glutDisplayFunc,设置一个函数,当需要进行画图时,这个函数就会被调用。(这个说法不够准确,但准确的说法可能初学者不太好理解,暂时这样说吧)。 7glutMainLoop,进行一个消息循环。

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

附注:

The following applies specifically to C/C++ usage.

To compile and link OpenGL programs, you'll need OpenGL header files and libraries. To run OpenGL programs you may need shared or dynamically loaded OpenGL libraries, or a vendor-specific OpenGL Installable Client Driver (ICD) specific to your device. Also, you may need include files and libraries for the GLU and GLUT libraries. Where you get these files and libraries will depend on which OpenGL system platform you're using.

OpenG.org maintains a list of links to OpenGL Utility libraries. You can download most of what you need from there.

Under Microsoft Windows 9x, NT, and 2000:

If you're using Visual C++, your compiler comes with include files for OpenGL and GLU, as well as .lib files to link with.

For GLUT, download these files. Install glut.h in your compiler's include directory, glut32.lib in your compiler's lib directory, and glut32.dll in your Windows system directory (c:/windows/system for Windows 9x, or c:/winnt/system32 for Windows NT/2000).

In summary, a fully installed Windows OpenGL development environment will look like this:

File

Location

gl.h glut.h glu.h

[compiler]/include/gl

Opengl32.lib glut32.lib glu32.lib

[compiler]/lib

Opengl32.dll glut32.dll glu32.dll

[system]

where [compiler] is your compiler directory (such as c:/Program Files/Microsoft Visual Studio/VC98) and [system] is your Windows 9x/NT/2000 system directory (such as c:/winnt/system32 or c:/windows/system).

If you're on a hardware platform that accelerates OpenGL, you'll need to install the ICD for your device. This may have shipped with your hardware, or you can download it from your hardware vendor's Web page. Your vendor may also provide a replacement or addition for gl.h, which provides definitions and declarations for vendor-specific OpenGL extensions. See the extensions section in this FAQ for more information.

If you see files such as opengl.lib and glut.lib, these are SGI's unsupported libraries for Microsoft Windows. They should not be used. To use hardware acceleration, the Microsoft libraries are recommended. More info on the SGI libraries can be found here. Always link with either all Microsoft libraries (e.g., glu32.lib, glut32.lib, and opengl32.lib) or all SGI libraries (e.g., glu.lib, glut.lib, and opengl.lib). You can't use a combination of both Microsoft libarires and SGI libraries. However, you can install both sets of libraries on the same system. If you use SGI's .lib files, you'll need the corresponding .dll files installed in your system folder. (i.e., linking against opengl.lib requires that opengl.dll is installed at run time).

You'll need to instruct your compiler to link with the OpenGL, GLU, and GLUT libraries. In Visual C++ 6.0, you can accomplish this with the Project menu's Settings dialog box. Scroll to the Link tab. In the Object/library modules edit box, add glut32.lib, glu32.lib, and opengl32.lib to the end of any text that is present.

For UNIX or UNIX-like operating systems:

If you don't find the header files and libraries that you need to use in standard locations, you need to point the compiler and linker to their location with the appropriate -I and -L options. The libraries you link with must be specified at link time with the -l option; -lglut -lGLU -lGL -lXmu -lX11 is typical.

If you want to use GLUT, you need to download it. If you can't find the precompiled binaries, you'll want to download the source and compile it. GLUT builds easily on many platforms, and comes with many README files explaining how to do a build. The GLUT compiler uses the imake utility, which makes it easy to build GLUT on new platforms.

For Linux, Macintosh, and other systems:

Mesa is a free OpenGL-like library that is available on a number of platforms. You might also check the Developer section at The OpenGL Organization's Web page for information about OpenGL for your specific platform.

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭