OpenGL是一种APl不是语言 【振动数据反演图像设计】英文文献集译文.doc_第1页
OpenGL是一种APl不是语言 【振动数据反演图像设计】英文文献集译文.doc_第2页
OpenGL是一种APl不是语言 【振动数据反演图像设计】英文文献集译文.doc_第3页
OpenGL是一种APl不是语言 【振动数据反演图像设计】英文文献集译文.doc_第4页
OpenGL是一种APl不是语言 【振动数据反演图像设计】英文文献集译文.doc_第5页
免费预览已结束,剩余4页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

OpenGL是一种APl,不是语言 在极大程度上,OpenGL并不是一种编程语言,而是一种API(Application Programming Interface,应用程序编程接口)。每当我们说一个程序是以OpenGL为基础的或者说它是一个OpenGL应用程序时,其意思是指它是用某种编程语言(比如C或C+)编写的,在程序中调用了一个或几个OpenGL库。我们并不是指这个程序专业地使用OpenGL进行绘图。它可能是结合了两个不同的图形软件包的各自最好的功能,或者它可能使用OpenGL来做一些特定的任务,同时使用特定环境的图形学(例如Windows GDI)来执行其他任务。当然,这种规则的唯一例外就是将要在第二部分中介绍到的OpenGL阴影法语言。作为一种API,OpenGL库遵循了C的调用习惯。这就意味着,这种调用习惯的选择使得在多数其它语言中,直接调用OpenGL都是简单可行的。在本书中,例子的程序都是用C+编写的。除了很少的一些次要因素外,C+程序能够和C使用一样的方式很容易地进入C函数和API。C+是目前大多数应用程序编程所选择的语言。一些很基础的C+类也能极显著地简化大部分编程任务。我们迫切地希望把对象的使用保持到最小,没有模板、过载运算符、元之类的。像VB这样的能调用C库文件中的函数的其它编程语言,也都能够使用OpenGL,而且OpenGL的汇集都可被很多其他编程语言利用。然而,超出这本书的范围之外,在其它语言中使用OpenGL会多少有点难以解释。为了使事情简单和轻便,我们坚持用C+来作为例子。库和头文件标准尽管OpenGL是一个“标准”程序库,这个库有很多种实现和版本。例如,在Microsoft窗口中,实际的Microsoft软件的实现是位于设置在Windows system目录下的动态链接库opengl32.dll里面的。在大多数平台上,OpenGL库是附属于OpenGL实用库(GLU)的。这个实用库在Windows中就是位于也设置在Windows system目录下的glu32.dll里面的。这个实用库是一套用于完成普通的(但有时很复杂的)工作的实用函数,例如特殊的矩阵计算,或者是给曲线和曲面的公共类型提供支持。在Mac OSX操作系统上,OpenGL和GLU的库文件都包含在OpenGL架构中。这种基于OSX操作系统的架构在很多方面跟Windows中的动态链接库都极为相似。为了使用正确的OpenGL头文件和链接到正确的OpenGL库文件,安装编译工具的步骤随着工具和工作平台的不同而不同。当这些工具的新版本发布时,这些安装步骤也会随之改变。假设如果你正在读一本有关于3D图形编程的书,那么在你对开发环境的准备当中,你已经知道了如何实际地编译程序,这种情况总是安全的。应当经常记录句子中的斜体字。由于这个原因,在源代码的分配中,都将要为Windows操作系统上的Visual Studio、 Mac OSX操作系统的X代码和Linux上一些通用的“制作”文件查找预配置的工程。在必要的时候,你可以在我们的网站(/superbible)上,找到一些更多的详细的自学材料来帮助你。在所有的工作平台上,所有OpenGl函数、类型和宏的原型都(按照规定)被包含在头文件gl.h中。实用库函数的原型在另一个文件glu.h中。当你安装开发工具的时候,这些文件总是自动的安装设置在包含路径的一个特殊目录下。例如,下面的代码显示了一个使用了OpenGL的基础Windows程序的初始头文件包含:#include#include#include在苹果的OSX系统上,包含的文件可能是这样的:#include #include #include API的规定OpenGL是被一些聪明人设计的,他们具有设计图形编程API的丰富经验。他们对函数命名和变量声明的方式指定一些标准规则。API简单清晰,而且使厂商很容易对其进行扩展。OpenGL试着尽可能地避免策略。策略是指假定设计者知道程序员将如何使用API。策略的例子是假定你总是明确指定顶点数据是浮点数值,假定雾化总是在进行绘制前被启用,或者假定场景中的所有的对象都受到相同的光照参数的影响。进行这些类型的假定将会消除很多已经开发了很长时间的、流行的绘制技术。这个基本定律已经对OpenGL的寿命和演变做出了贡献。然而,随着时间的推移,硬件的性能及研发人员和硬件厂商的创造力有了所没有预料到的发展进步,这给已经发展了这些年的OpenGL带来了损失。尽管如此,OpenGL的基础API已经显示了在应对新的、未预料到的特点上惊人的复原能力。这种可以编译十年来几乎没有变化的源代码的能力,对于应用程序研发人员来说是实质性的有利因素,而且OpenGL几年来在尽可能不对旧代码产生影响的前提下,已经增加了新的特点。未来的OpenGL的版本将会针对低可塑性的和平均性的侧面,而这些侧面中一些旧的特征和模板最终可能会被放弃。数据类型 为了使OpenGL代码从一个平台导入到另一个平台更加容易,OpenGL定义了它自己的数据类型。如果你愿意的话,这些数据类型将映射为常规的C/C+数据类型,你可以使用C/C+数据类型来代替它们。然而,不同的编译器和环境,对于不同的变量类型,都有它们各自的大小和内存安排的规则。通过使用OpenGL定义变量类型,你能从这些类型的变化中隔离出你的代码。表2.1列出了OpenGL的数据类型、它们在大多数32位环境(Win32、OSX等)下相对应的C/C+数据类型以及字面上所用的合适的后缀。在本书中,我们对所有的字面值使用这些后缀。在后面你将会看到这些后缀也用在很多(但不是全部)OpenGL的函数名称中。不管是机器大小或者是编译器的使用,其内部的表示在所有的平台(甚至是64位的 OSs)上都是一样的(只要你拥有一个合适的SDK!)。 表2.1 OpenGL变量类型及其相应的C数据类型OpenGL数据类型内部表示定义为C类型C字面值后缀GLbyte8位整数signed charbGLshort16位整数shortsGLint,GLsizei32位整数longlGLfloat,GLclampf32位浮点数floatfGLdouble,GLclampd 64位浮点数doubledGLubyte,GLboolean8位无符号整数unsigned charubGLushort 16位无符号整数unsigned shortus续表2.1 OpenGL数据类型内部表示定义为C类型C字面值后缀GLuint,GLenum,GLbitfield32位无符号整数unsigned longuiGLchar8位字符charNoneGLsizeiptr,GLintptr本地指针ptrdiff_tNone所有数据类型都以表示openGL的GL作为开头。大多数后面跟的是它们相对应的C数据类型(byte, short, int, float等等)。有一些先有个u,以表示是一个无符号数据类型,比如 ubyte表示了一个无符号字节。为了方便起见,提供了更具描述性的名称,比如 size表示一个长度值或深度值。举个例子,GLsizei是一个openGL变量,它表示了一个用整数代表的尺寸参数。clamp暗指了一个被要求成为“clamped”、范围在0.01.0之内的变量。GLboolean变量用于表示真或假的条件,GLenum用于枚举变量,而GLbitfield是用于包含二进制位字段的变量。 这里没有对指针和数组提供任何特殊的考虑。一个由10个GLshort变量组成的数组只需声明为 GLshort shorts10;而由10个指向GLdouble变量的指针组成的数组则声明为GLdouble *doubles10;一些其他的指针对象类型用于NURBS曲线和曲面。它们需要更多的说明,这将会在稍后的章节中涉及到。OpenGL: An API, Not a LanguageFor the most part, OpenGL is not a programming language; it is an application programming interface (API). Whenever we say that a program is OpenGL-based or an OpenGL application, we mean that it was written in some programming language(such as C or C+) that makes calls to one or more of the OpenGL libraries. We are not saying that the program uses OpenGL exclusively to do drawing. It might combine the best features of two different graphics packages. Or it might use OpenGL for only a few specific tasks and environment-specific graphics (such as the Windows GDI) for others. The only exception to this rule of thumb is, of course, the OpenGL Shading Language, which is covered in Part II.As an API, the OpenGL library follows the C calling convention. As it turns out, thischoice of calling convention makes it possible to easily call OpenGL directly from most other languages as well. In this book, the sample programs are written in C+. C+ programs can easily access C functions and APIs in the same manner as C, with only some minor considerations. C+ is the modern language of choice for most performanceminded applications. Very basic C+ classes can dramatically simplify most programming tasks as well. We promise to keep the object usage to a minimum, no STL/Template/Operator Overloaded/Meta blah blahwe promise!Other programming languagessuch as Visual Basicthat can call functions in C libraries can also make use of OpenGL, and OpenGL bindings are available for many other programming languages. Using OpenGL from these other languages is, however, outside the scope of this book and can be somewhat tedious to explain. To keep things simple and easily portable, well stick with C+ for our examples.Standard Libraries and HeadersAlthough OpenGL is a “standard” programming library, this library has many implementations and versions. On Microsoft Windows, for example, the actual Microsoft software implementation is in the opengl32.dll dynamic link library, located in the Windows system directory. On most platforms, the OpenGL library is accompanied by the OpenGL utility library (GLU), which on Windows is in glu32.dll, also located in the system directory. The utility library is a set of utility functions that perform common (but sometimes complex) tasks, such as special matrix calculations, or provide support for common types of curves and surfaces. On Mac OS X, OpenGL and the GLU libraries are both included in the OpenGL Framework. Frameworks on OS X are similar in many respects to Windows DLLs.The steps for setting up your compiler tools to use the correct OpenGL headers and to linkto the correct OpenGL libraries vary from tool to tool and from platform to platform. They also change over time as newer versions of these tools are released. It is usually safe to assume that if you are reading a book on programming 3D graphics, you already know how to actually compile programs with your preferred development environment. Note the italics on the word usually! For this reason, in the source code distribution, youll find preconfigured projects for Visual Studio on Windows, XCode on Mac OS X, and some generic “make” files for Linux. On our Web site (/superbible) youll find some more detailed tutorials to walk you through this if necessary.On all platforms, the prototypes for all OpenGL functions, types, and macros are contained (by convention) in the header file gl.h. The utility library functions are prototyped in a different file, glu.h. These files are usually located in a special directory in your include path, set up automatically when you install your development tools. For example,the following code shows the initial header inclusions for a basic Windows program that uses OpenGL:#include#include#includeOn an Apple OS X system, your include files might look more like this:#include #include #include API SpecificsOpenGL was designed by some clever people who had a lot of experience designing graphics programming APIs. They applied some standard rules to the way functions were named and variables were declared. The API is simple and clean and easy for vendors to extend.OpenGL tries to avoid as much policy as possible. Policy refers to assumptions that the designers make about how programmers will use the API. Examples of policies include assuming that you always specify vertex data as floating-point values, assuming that fog is always enabled before any rendering occurs, or assuming that all objects in a scene are affected by the same lighting parameters. Making these kinds of assumptions would eliminate many of the popular rendering techniques that have developed over time.This philosophy has contributed to the longevity and evolution of OpenGL. Still, as time marches on, unanticipated advances in hardware capabilities, and the creativity of developers and hardware vendors, has taken its toll on OpenGL as it has progressed through the years. Despite this, OpenGLs basic API has shown surprising resilience to new unanticipated features. The ability to compile ten-year-old source code with little to no changes is a substantial advantage to application developers, and OpenGL has managed for years to add new features with as little impact on old code as possible. Future versions of OpenGL are in the works with “lean and mean” profiles, where some older features and models may eventually be dropped.Data TypesTo make it easier to port OpenGL code from one platform to another, OpenGL defines its own data types. These data types map to normal C/C+ data types that you can useinstead, if you want. The various compilers and environments, however, have their own rules for the size and memory layout of various variable types. By using the OpenGL defined variable types, you can insulate your code from these types of changes.Table 2.1 lists the OpenGL data types, their corresponding C/C+ data types under most 32-bit environments (Win32/OS X, etc.), and the appropriate suffix for literals. In this book, we use the suffixes for all literal values. You will see later that these suffixes are also used in many (but not all) OpenGL function names. The internal representation is the same on all platforms (even 64-bit OSs), regardless of machine size or compiler used (provided you have an appropriate SDK!).TABLE 2.1 OpenGL Variable Types Corr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论