版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、译文:OpenGL 简介OpenGL是一个底层图形库规范。它为程序员提供了一个小的几何图元(点、线、多边形、图片和位图)库和一个支持2D/3D几何对象绘图命令库,通过所提供的图元和命令来控制对象的呈现(绘图)。由于OpenGL的绘图命令仅限于画一些简单的几何图元(如点、线和多边形),所以OpenGL实用工具包(GLUT)应运而生,它能够帮助绘画出更复杂的三维对象(比如球体、圆环甚至茶壶)。如果你要构建需要利用到OpenGL全部特性的应用的话,GLUT未必适合,但是对刚学习OpenGL的人来说GLUT就非常有用。GLUT是为满足windows系统下OpenGL程序独立编程接口的需求而设计的,接口
2、被设计的非常简单而又实用。从OpenGL中移除windows系统的操作是一个非常英明的决定,因为这意味着OpenGL图形系统能够被应用于更广泛的系统中(包括功能强大但昂贵的图形工作站以及需要大量图形运算的视频游戏、互动电视机机顶盒和个人电脑)。GLUT简化了用OpenGL进行渲染的程序的实现。GLUT应用编程接口(API)只需要调用很少的接口就可以用OpenGL来渲染图形场景,并且GLUT接口所需的参数也相对较少。渲染管线大多数OpenGL实现都有着类似的操作顺序,这一系列的操作过程叫做OpenGL渲染管线。尽管OpenGL并不严格要求需要按照渲染管线这一顺序来实现,但是这样做可以为预测Ope
3、nGL下一步将要做什么提供可靠的指引。几何数据(点、线、多边形)将会沿着一条依次通过求值器、顶点操作和装配阶段的路径进行传递,而像素数据(像素,图形,位图)将会沿着另一条路径传递,在最终像素写入帧缓冲区前,像素数据和几何数据都会经历相同的最后一步栅格化处理。Display Lists:所有数据,无论是用来描述几何体还是像素的,都可以保存在一个display list中来在当下或将来使用(用以替代display list的用法是在需要时立即处理数据称为立即模式)当一个display list被触发时,保存的数据就像立即模式一样被发送至显示器。Evaluators:所有几何原语最终都是由顶点描述的
4、,但evaluator提供了一种用控制点表示面的方法。这种方法是拟合多项式,可以通过控制点提供面法线、颜色、和空间纹理坐标。基于顶点和图元的装配:对于顶点的数据,下一步就是将它们转换成图元。某些类型的顶点数据被转换为4x4的浮点型矩阵。空间坐标从三维世界中的一个位置被投影到您显示屏上的一个位置。在某些情形中,这一步之后会进行透视除法,利用它能够使远处的几何物体看起来比近处的物体小。然后,我们再对数据进行视口和景深的操作。至此,我们得到的结果是几何图元,这些图元与相关的颜色值和景深值,以及光栅化步骤的指标等一同进行转换。像素操作:在几何数据通过OpenGL渲染管线的一条路径的同时,像素数据则通过
5、了另外一条路径。系统内存中的一个数组中的像素首先被解压,从若干格式中的一种转换成拥有恰当数目的分量的那种。然后,我们对数据进行尺度放缩,偏移,再根据像素映射处理,随后这些结果被发送至光栅化步骤。光栅化:光栅化是指将几何数据和像素数据都转换成片段的过程。各方形片段对应帧缓存中的一个像素。当顶点被连接起来形成直线,或者计算填充的多边形的内部像素时,直线的宽度,点的大小,着色模型,以及用以支持抗锯齿的覆盖计算等都被考虑在内。颜色值和深度值被赋予各方形片段。然后,处理后的片段被存入合适的缓存中,在那里,它最终被转化成一个像素,从而完成了它的最终使命。库文件OpenGL提供了一组强大但原始的渲染命令,而
6、且所有的高层绘制都必须通过这些命令来完成。有若干库文件能够简化您的编程任务,包括: OpenGL Utility Library (GLU),它包含若干调用底层OpenGL命令的函数,这些函数能够进行诸如为某一特定观察方向和透视以及渲染表面设定矩阵的任务。 OpenGL Utility Toolkit (GLUT),它是一个视窗-系统分离的工具箱,由Mark Kilgard编写,用于隐藏不同视窗的API所带来的复杂性。包含文件对于所有OpenGL应用程序,你都需要在所有文件中包含gl.h。几乎所有OpenGL应用程序使用GLU,即上文提到的OpenGL实用库(OpenGL Utility Li
7、brary),它也需要包含gl.h才能使用。所以几乎所有OpenGL源文件这样开头:1#include 2#include 如果你用OpenGL实用工具集(OpenGL Utility Toolkit,即GLUT)来管理窗体事务,你需要包含:1#include 记住glut.h保证会正确包含gl.h和glut.h,所以把三个文件全包含是多此一举。为使你的GLUT程序通用性更佳,包含glut.h而不要明确包含gl.h或glu.h。设置编译器在Windows上使用MS Visual Studio C+安装GLUT1. 如果你已经安装了MS Visual Studio C+ 5.0或以上版本,那么下
8、列文件中的大多数就已经在您的机器上了。下列的GLUT文件需要复制到指定的路径下。2. 要开始安装: 右键单击各链接 选择另存为. 接受默认名称(只需点击保存即可) 库文件(放置到Visual C+下的lib子路径中) opengl32.lib glu32.lib glut32.lib 包含文件(放置到Visual C+下的includeGL子路径中) gl.h glu.h glut.h 动态链接库文件(放置到WindowsSystem路径中) opengl32.dll glu32.dll glut32.dll编译OpenGL/GLUT程序1. 创建一个新项目: 从菜单中点选文件 | 新建 点选
9、项目选项卡 点选Win32控制台应用程序 Console Application 输入您的项目名称2. 为链接器指定要用到的库文件: 从菜单中点选项目 | 设置 在对象/库模块中:输入opengl32.lib glu32.lib glut32.lib3. 向项目中添加或创建文件: 从文件菜单中点选项目 | 向项目添加 | 文件 添加需要的程序文件4. 构建并运行Silicon Graphics WorkstationMakefile:view sourceprint?01# insert the name of your source file here (omit the .c)02TARG
10、ET = progname0304LIBS = -lglut -lGLU -lGL -lXmu -lXext -lXi -lX11 -lm0506CC = /usr/gnu/bin/gcc0708default: $(TARGET)0910all:default1112.c.o:13$(CC) -c $1415$(TARGET): $.o16$(CC) $.o -Wall $(LIBS) -o $1718clean:19-rm -f *.o $(TARGET)开始3D编程前需注意的十件事:开始3D编程不是一件容易完成的任务。这里有很多新的东西可以发挥作用,从选择语言到选择合适的3d建模软件。当
11、这10个事情完成时,无论你选择何种语言,使用何种建模软件,你都可以认为自己是这方面的半个专家了。1:建立自己的图形初始化函数现在有种类繁多的3d引擎和平台,所以这个任务一般交给它们来完成。我还记得以前的事情,你必须使用windows函数来初始化OpenGL,并管理窗口句柄和资源的加载。了解事情是如何进行内部管理的会让你更深刻理解你现在在干什么,这是非常有用的。 我建议从NEHE的教程开始。在教程的第一章中包含了一个C语言编写的利用Windows API的图形初始化函数。如果这里有很多东西需要你控制,你可以试试C+的相对的函数,或者使用托管语言,像C#、Java或者Python。在网络中有很多例
12、子。2:实现自己的相机您可以从互联网上复制和粘贴摄像头的代码,使用它并没有大的问题, 但它不是自己的相机,直到你从头开始,充分了解一些概念,如向量处理,矩阵变化,角度转换等等。你应该先从一个FPS(第一人称射击)的摄像头开始,它会给你需要的一切并让你为接下来的学习做好准备。如果你以后想构建自己的游戏,你不能使用它。我建议你阅读这篇文章,以找出最适合您的需求类型的相机。3:了解初级的3D概念当我开始,我就开始听到了很多新词,如抗锯齿,各向异性过滤,阻塞测试,Z-缓冲,alpha测试,着色器语言,凹凸贴图等。如果你是一个玩家,也许你已经在配置游戏的图形设置时看过它们了。请确保你花费一些时间来阅读相
13、关内容,因为这将会给你一个3D编程的概述。4:学习所有向量和矩阵的知识这始终被低估。我强烈建议,为了正确地管理如相机,光线追踪,地形跟踪的东西,你应该知道关于这个的一切。当然,你也需要学会最基础的三角知识。现在我才明白,如果我愿意花几分钟研究这个问题,我的生活本应该很轻松。5:编写你自己的3D模型加载器我建议从使用OBJ文件或STL文件开始,因为他们用一个ASCII格式表示。之后你可以迁移到其他根伟复杂的格式上,比如3DS格式。有了这个,你不仅将了解如何保存3D模型,你还会理解如何绘制三角形,然后你就会明白图形引擎是如何绘制一切的。6:成功实现自己的碰撞算法一个事情是绘制世界,另外一个是管理其
14、他中几何形状。在一个虚拟的3D世界中,这里没有物理定律,所以你必须创建它们。如果你想要一个对象不能穿越墙壁,那么你必须在墙壁中创建一个几何图形并计算所有东西。有几种方法来管理碰撞,我建议从在一个迷宫项目实现两体碰撞开始。试试这个链接,了解更多信息。7:实现一个小的粒子引擎当我发现3D游戏中火焰、烟雾、照明和一些其他令人惊艳的效果是由粒子组成,而那些粒子是由相机面前的贴图组成。你添加更多的粒子,效果看起来更加逼真,但是性能就有所损失了。我的第一个粒子引擎是为火箭的烟雾制作的,而且我这样做的时候还没有不参看粒子引擎教程。后来我意识到了,我已经改造了车轮。通过实现这些东西,你会明白一些诸如粒子发射器
15、、粒子行为等想法。8:了解一个3D建模软件的基本知识为了修改在你的应用中使用的3D模型,你应该知道基本的操作,比如平移,缩放,旋转,变形,导出为其他格式,并制作简单的模型。如果你不这样做,你在制作第一个游戏时遇到事事依赖别人的情况。我曾用过几个建模软件,我强烈推荐3D MAX或Maya。9 :加载和播放动画我做过的最困难的事情是加载并正确播放动画。我不得不对3D max的XAF文件进行大量的逆向。我不得不学习骨骼层次,矩阵插值等东西。在最后,看到自己的模型自行移动是一件让人相当高兴的事情。我建议从一个机器人动画开始,因为动物模型什么的还需要一种名为蒙皮的技术。#10 编写2D自定义GUI控件当
16、我开始使用XNA时,由于XNA没有实现窗体控件,我不得不构建自己的图形用户接口。这也导致了两件事:第一:我有了构建自定义的GUI控件的能力。第二:我懂得了一些重要的概念,如事件控制和事件捕捉。这不是容易的事,我实现的最难的一个控件是listbox,但是一旦做出来了,就可以在很多地方使用了。结论在这个过程中你将会遇到很多问题。你不得不花费很多时间来使你的代码可以正常工作,即使你很聪明。但是我可以告诉你的是,从一个程序猿的角度来说,没有什么快乐比的上你看着你的代码能顺利的工作。我仍然不能忘记当我第一次编出OBJ模块加载器时的喜悦。当时,我想要加载个人脸图像,然后数个小时过去了,在凌晨3:50时,突
17、然一个非常诡异的人脸出现在我的屏幕,真是吓死爹了,每当我想起这件事时,就哈哈大笑.我确信当你设法完成这10件事后,你可以说你已经了解了3D编程的基础。我写篇文章是因为我花了很多时间来完成它(指走了弯路了),所以我希望每个开始接触3D编程的人,能有一个小小的指南。我的建议是开始做一个小小的游戏,并不断的完善它。我觉得这是一个很好的方式,因为如果只是单纯的去学习而看不到实际的效果的话,积极性不高。一段时间之后,你会觉得游戏对你来说不一样了,因为你会花很多时间来想它们是怎么解决你曾经碰到的技术难点的。以上,我试图使这篇文章结构保持清晰和易懂,如果你喜欢,你可以访问我的博客来获取更多的这方面的内容。下
18、附原文原文:Introduction to OpenGLOpenGL is a low-level graphics library specification. It makes available to the programmer a small set of geomteric primitives - points, lines, polygons, images, and bitmaps. OpenGL provides a set of commands that allow the specification of geometric objects in two or thr
19、ee dimensions, using the provided primatives, together with commands that control how these objects are rendered (drawn).Since OpenGL drawing commands are limited to those that generate simple geometric primitives (points, lines, and polygons), the OpenGL Utility Toolkit (GLUT) has been created to a
20、id in the development of more complicated three-dimensional objects such as a sphere, a torus, and even a teapot. GLUT may not be satisfactory for full-featured OpenGL applications, but it is a useful starting point for learning OpenGLGLUT is designed to fill the need for a window system independent
21、 programming interface for OpenGL programs. The interface is designed to be simple yet still meet the needs of useful OpenGL programs. Removing window system operations from OpenGL is a sound decision because it allows the OpenGL graphics system to be retargeted to various systems including powerful
22、 but expensive graphics workstations as well as mass-production graphics systems like video games, set-top boxes for interactive television, and PCs.GLUT simplifies the implementation of programs using OpenGL rendering. The GLUT application programming interface (API) requires very few routines to d
23、isplay a graphics scene rendered using OpenGL. The GLUT routines also take relatively few parameters.Rendering PipelineMost implementations of OpenGL have a similar order of operations, a series of processing stages called the OpenGL rendering pipeline. Although this is not a strict rule of how Open
24、GL is implemented, it provides a reliable guide for predicting what OpenGL will do. Geometric data (verices, line, and polygons) follow a path through the row of boxes that includes evaluators and per-vertex operations, while pixel data (pixels, images and bitmaps) are treated differently for part o
25、f the process. Both types of data undergo the same final step (raterization) before the final pixel data is written to the framebuffer.Display Lists:All data, whether it describes geometry or pixels, can be saved in a display list for current or later use. (The alternative to retaining data in a dis
26、play list is processing the data immediately-known as immediate mode.) When a display list is executed, the retained data is sent from the display list just as if it were sent by the application in immediate mode.Evaluators:All geometric primitives are eventually described by vertices. Evaluators pr
27、ovide a method for deviving the vertices used to represent the surface from the control points. The method is a polynomial mapping, which can produce surface normal, colors, and spatial coordinate values from the control points.Per-Vertex and Primitive Assembly:For vertex data, the next step convert
28、s the vertices into primitives. Some types of vertex data are transformed by 4x4 floating-point matrices. Spatial coordinates are projected from a position in the 3D world to a position on your screen. In some cases, this is followed by perspective division, which makes distant geometric objects app
29、ear smaller than closer objects. Then viewport and depth operations are applied. The results at this point are geometric primitives, which are transformed with related color and depth vlaues and guidelines for the rasterization step.Pixel Operations:While geometric data takes one path through the Op
30、enGL rendering papeline, pixel data takes a different route. Pixels from an array in system memory are first unpacked form one of a variety of formats into the proper number of components. Next the data is scaled, biased, processed by a pixel map, and sent to the rasterization step.Rasterization:Ras
31、terization is the conversion of both geometric and pixel data into fragments. Each fragment square corresponds to a pixel in the framebuffer. Line width, point size, shading model, and coverage calculations to support antialiasing are taken ito consideration as vertices are connected into lines or t
32、he interior pixels are calculated for a filled polygon. Color and depth values are assigned for each fragment square. The processed fragment is then drawn into the appropriate buffer, where it has finally advanced to be a pixel and achieved its final resting place.LibrariesOpenGL provides a powerful
33、 but primitive set of rendering command, and all higher-level drawing must be done in terms of these commands. There are several libraries that allow you to simplify your programming tasks, including the following: OpenGL Utility Library (GLU) contains several routines that use lower-level OpenGL co
34、mmands to perform such tasks as setting up matracies for specific viewing orientations and projections and rendering surfaces. OpenGL Utility Toolkit (GLUT) is a window-system-independent toolkit, written by Mark Kilgard, to hide the complexities of differing window APIs.Include FilesFor all OpenGL
35、applications, you want to include the gl.h header file in every file. Almost all OpenGL applications use GLU, the aforementioned OpenGL Utility Library, which also requires inclusion of the glu.h header file. So almost every OpenGL source file begins with:Setting Up Compilers Windows Using MS Visual
36、 C+Installing GLUT1. Most of the following files (ie. OpenGL and GLU) will already be present if you have installed MS Visual C+ v5.0 or later. The following GLUT files will need to be copied into the specified directories.2. To install: right-click each link chooseSave Link As. accept the default n
37、ame (just clickSave) libraries (place in thelib subdirectory of Visual C+) opengl32.lib glu32.lib glut32.lib include files (place in theincludeGL subdirectory of Visual C+) gl.h glu.h glut.h dynamically-linked libraries (place in the WindowsSystemsubdirectory opengl32.dll glu32.dll glut32.dllCompili
38、ng OpenGL/GLUT Programs1. Create a new project: chooseFile | Newfrom the File Menu select theProjectstab chooseWin32 Console Application fill in your Project name2. Designate library files for the linker to use: chooseProject | Settingsfrom the File Menu underOject/library modules:enter opengl32.lib
39、 glu32.lib glut32.lib3. Add/Create files to the project: chooseProject | Add to Project | Filesfrom the File menu add the required program files4. Build and ExecuteSilicon Graphics WorkstationMakefile:view sourceprint?01# insert the name of your source file here (omit the .c)02TARGET = progname0304L
40、IBS = -lglut -lGLU -lGL -lXmu -lXext -lXi -lX11 -lm0506CC = /usr/gnu/bin/gcc0708default: $(TARGET)0910all:default1112.c.o:13$(CC) -c $1415$(TARGET): $.o16$(CC) $.o -Wall $(LIBS) -o $1718clean:19-rm -f *.o $(TARGET)Ten Things to Achieve When Starting 3D Programming:Starting 3D programming is not an e
41、asy task to accomplish. There are a lot of new things that come into play, and they vary from choosing a programming language to selecting the correct 3D modeling software. These 10 goals are the things that when they are done, no matter in what language and with what rendering engine, you can consi
42、der yourself a semi-expert on this matter.#1: Build your own custom graphic initialization functionToday with the great variety of 3D engines and platforms, this task is always delegated to those. I still remember the times when you had to initialize OpenGL with all the windows functions, and how yo
43、u had to manage windows handles and resource loading yourself. This is useful to understand how things are managed internally and will give you more comprehension of what you are doing. My advice is to start looking atNEHE tutorials, it has a graphic initialization function written in C and with win
44、dows APIs in chapter one. If this is a lot for you to handle, you should look at C+ equivalent functions or try to port them to managed languages like C#, Java or Python. There are plenty of examples on the internet.#2: Implement your own cameraYoucan copy and paste a camera code from the internet,
45、and use it without major problems, but it is not until you make your own camera from scratch that you will fully understand some concepts like vector handling, translation matrices, angle conversion, etc. You should start by coding a FPS (First Person Shooter) camera; it has everything it needs to g
46、et you ready. Later if you want to make your own game and you cant use it, I recommend you to readthis articleto find out the type of camera that best suits your needs.#3: Understand primary 3D conceptsWhen I started, I began to hear a lot of new words like anti-aliasing, anisotropic filtering, occl
47、usion testing, z-buffer, alpha testing, shader language, bump mapping, etc. Maybe if you are a gamer, you have seen some of them while configuring the graphic settings of your game. Make sure you spent some time reading about this, because it will give an overview of what 3D programming is.#4: Learn
48、 everything you can about vectors and matricesThis is always underestimated, I strongly recommend that in order to correctly manage things like cameras, terrain following, ray tracing; you should know everything about this. You should also learn minimum trigonometry basis. Now I understand how easy
49、my life would have been if I would had spent only a few hours studying this matter.#5: Code yourself a 3D model loaderI recommend beginning with a .OBJ file or a .STL file because they have an ASCII format representation. Later, you can move to other more complex formats like .3DS. With this, you no
50、t only will understand more how 3D models are saved, you will have to draw it in its raw manner: triangles, and then you will understand how everything is drawn in a graphics engine.#6: Successfully make your own collision algorithmIt is one thing to draw a world and another thing to manage its geom
51、etry. In a virtual 3D world, there are no physics laws, so you have to create them. If you want an object not to go through a wall, then you have to create an internal geometric representation of the wall and make all the calculations yourself. There are several approaches to handle collisions; I re
52、commend starting with binary collisions with a labyrinth project. Trythis linkfor more information.#7: Implement a small particle engineI was disappointed when I found out that fire, smoke, some lighting and other stunning effects that you see in 3D games are made by particles and that particles are
53、 in essence planes with textures facing the camera. The more particles you add, the more realistic the effect looks but the performance is more compromised. The first particle engine I made was for rocket smoke and I did it without looking at a particle engine tutorial. Later I realized I had reinve
54、nted the wheel but I was really into this. By carrying out this, you will understand concepts like particle emitters, particle behavior and bill boarding techniques, among others.#8: Learn the basics in a 3D modeling softwareIn order to make changes to the 3D models you want to use in your applicati
55、on, you should at least know operations like translating, scaling, rotating, texturing, exporting to other formats and making simple models. If you dont do that, you will suffer from depending on other people to do your first game. Ive worked with a few modeling software and I strongly recommend 3D
56、Max or Maya.#9: Load and play an animationLoading and correctly playing an animation was the most difficult thing in 3D that I ever did. I had to do reverse engineering to a 3D max .XAF file. I had to also learn stuff like bone hierarchy, matrix interpolation, among others. At the end, it was very gratifying to look at your own models moving by themselves. I recommend starting with animating a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024物流配送员劳动协议3篇
- 2024版网络游戏开发与运营权转让合同2篇
- 2024押证不押车商业地产项目贷款合同范本9篇
- 2025年度建筑安全评价与施工监理一体化合同范本3篇
- 2025厂区食堂承包合同:厂区文化建设与餐饮服务融合协议3篇
- 二零二五版北京市金融行业劳动合同法实施标准2篇
- 2024离婚财产分割保险保障合同
- 2024施工现场环境信息公开与共享协议3篇
- 2025年MLB棒球帽定制加工及品牌合作框架协议3篇
- 2025年度智能制造生产线操作工劳动合同3篇 - 副本
- 2024版个人私有房屋购买合同
- 2025年山东光明电力服务公司招聘笔试参考题库含答案解析
- 2024爆炸物运输安全保障协议版B版
- 《神经发展障碍 儿童社交沟通障碍康复规范》
- 2025年中建六局二级子企业总经理岗位公开招聘高频重点提升(共500题)附带答案详解
- 2024年5月江苏省事业单位招聘考试【综合知识与能力素质】真题及答案解析(管理类和其他类)
- 注浆工安全技术措施
- 《食品与食品》课件
- 2024年世界职业院校技能大赛“食品安全与质量检测组”参考试题库(含答案)
- 读书分享会《白夜行》
- 2023上海高考英语词汇手册单词背诵默写表格(复习必背)
评论
0/150
提交评论