1.7. 绘制几何体 Drawing Geometry

1.7. 绘制几何体 Drawing Geometry

As you can see from Figure 1.1, data for drawing geometry (points, lines, and polygons) starts

off in application-controlled memory (1). This memory may be on the host CPU, or, with the

help of some recent additions to OpenGL or under-the-covers data caching by the OpenGL

implementation, it may actually reside in video memory on the graphics accelerator. Either way,

the fact is that it is memory that contains geometry data that the application can cause to be

drawn.

1.7.1. Geometry Specification

The geometric primitives supported in OpenGL are points, lines, line strips, line loops, polygons,

triangles, triangle strips, triangle fans, quadrilaterals, and quadrilateral strips. There are three

main ways to send geometry data to OpenGL. The first is the vertex-at-a-time method, which

calls glBegin to start a primitive and calls
glEnd to end it. In between are commands that set

specific VERTEX ATTRIBUTES such as vertex position, color, normal, texture coordinates, secondary

color, edge flags, and fog coordinates, using calls such as
glVertex, glColor, glNormal, and

glTexCoord. (A number of variants of these function calls allow the application to pass these

values with various data types as well as to pass them by value or by reference.) Up through

version 1.5 of OpenGL, there was no way to send arbitrary (user-defined) pervertex data. The

only per-vertex attributes allowed were those specifically defined in the OpenGL specification.

OpenGL 2.0 added a method for sending arbitrary per-vertex data; that method is described in

Section 7.7 "Specifying Vertex Attributes."

When the vertex-at-a-time method is used, the call to
glVertex signals the end of the data

definition for a single vertex, and it may also define the completion of a primitive. After
glBegin is

called and a primitive type is specified, a graphics primitive is completed whenever
glVertex is

called enough times to completely specify a primitive of the indicated type. For independent

triangles, a triangle is completed every third time
glVertex is called. For triangle strips, a triangle

is completed when glVertex is called for the third time, and an additional connecting triangle is

completed for each subsequent call to glVertex.

The second method of drawing primitives is to use vertex arrays. With this method, applications

store vertex attributes in user-defined arrays, set up pointers to the arrays, and use

glDrawArrays, glMultiDrawArrays,
glDrawElements, glMultiDrawElements,
glDrawRangeElements, or

glInterleavedArrays to draw a large number of primitives at once. Because these entry points can

efficiently pass large amounts of geometry data to OpenGL, application developers are

encouraged to use them for portions of code that are extremely performance critical. Using

glBegin and glEnd requires a function call to specify each attribute of each vertex, so the function

call overhead can become substantial when objects with thousands of vertices are drawn. In

contrast, vertex arrays can be used to draw a large number of primitives with a single function

call after the vertex data is organized into arrays. Processing the array data in this fashion can

be faster because it is often more efficient for the OpenGL implementation to deal with data

organized in this way. The current array of color values is specified with
glColorPointer, the

current array of vertex positions is specified with
glVertexPointer, the current array of normal

vectors is specified with glNormalPointer, and so on. The function
glInterleavedArrays can specify and

enable several interleaved arrays simultaneously (e.g., each vertex might be defined with three

floating-point values representing a normal followed by three floating-point values representing

a vertex position.)

The preceding two methods are referred to as drawing in
IMMEDIATE MODE because primitives are

rendered as soon as they have been specified. The third method involves storing either the

vertex-at-a-time function calls or the vertex array calls in a
DISPLAY LIST, an OpenGL-managed

data structure that stores commands for later execution. Display lists can include commands to

set state as well as commands to draw geometry. Display lists are stored on the server side and

can be processed later with glCallList or
glCallLists. This is not illustrated in Figure 1.1, but it is

another way that data can be provided to the OpenGL processing pipeline. The definition of a

display list is initiated with glNewList, and display list definition is completed with
glEndList. All the

commands issued between those two calls become part of the display list, although certain

OpenGL commands are not allowed within display lists. Depending on the implementation,

DISPLAY LIST MODE can provide a performance advantage over immediate mode. Storing

commands in a display list gives the OpenGL implementation an opportunity to optimize the

commands in the display list for the underlying hardware. It also gives the implementation the

chance to store the commands in a location that enables better drawing performance, perhaps

even in memory on the graphics accelerator. Of course, some extra computation or data

movement is usually required to implement these optimizations, so applications will typically

see a performance benefit only if the display list is executed more than once.

New API calls in version 1.5 of OpenGL permitted vertex array data to be stored in server-side

memory. This mechanism typically provides the highest performance rendering because the

data can be stored in memory on the graphics accelerator and need not be transferred over the

I/O bus each time it is rendered. The API also supports the concept of efficiently streaming data

from client to server. The glBindBuffer command creates a buffer object, and
glBufferData and

glBufferSubData specify the data values in such a buffer.
glMapBuffer can map a buffer object into

the client's address space and obtain a pointer to this memory so that data values can be

specified directly. The command glUnmapBuffer
must be called before the values in the buffer are

accessed by subsequent GL rendering commands. glBindBuffer
can also make a particular buffer

object part of current state. If buffer object 0 is bound when calls are made to vertex array

pointer commands such as glColorPointer,
glNormalPointer, glVertexPointer, and so on, the pointer

parameter to these calls is understood to be a pointer to client-side memory. When a buffer

object other than 0 is bound, the pointer parameter is understood to be an offset into the

currently bound buffer object. Subsequent calls to one of the vertex array drawing commands

(e.g., glMultiDrawArrays) can thus obtain their vertex data from either client- or server-side

memory or a combination thereof.

OpenGL supports the rendering of curves and surfaces with evaluators. Evaluators use a

polynomial mapping to produce vertex attributes such as color, normal, and position that are

sent to the vertex processing stage just as if they had been provided by the client. See the

OpenGL specification for a complete description of this functionality.

1.7.2. Per-Vertex Operations

No matter which of these methods is used, the net result is that geometry data is transferred

into the first stage of processing in OpenGL, VERTEX PROCESSING
(2). At this point, vertex positions

are transformed by the modelview and projection matrices, normals are transformed by the

inverse transpose of the upper leftmost 3 x 3 matrix taken from the modelview matrix, texture

coordinates are transformed by the texture matrices, lighting calculations are applied to modify

the base color, texture coordinates may be automatically generated, color material state is

applied, and point sizes are computed. All of these things are rigidly defined by the OpenGL

specification. They are performed in a specific order, according to specific formulas, with

specific items of OpenGL state controlling the process.

Because the most important things that occur in this stage are transformation and lighting, the

vertex processing stage is sometimes called TRANSFORMATION AND LIGHTING, or, more familiarly,

T&L. There is no application control to this process other than modifying OpenGL state values:

turning lighting on or off with glEnable/glDisable; changing lighting attributes with
glLight and

glLightModel; changing material properties with
glMaterial; or modifying the modelview matrix by

calling matrix manipulation functions such as glMatrixMode,
glLoadMatrix, glMultMatrix,
glRotate,

glScale, glTranslate. At this stage of processing, each vertex is treated independently. The vertex

position computed by the transformation stage is used in subsequent clipping operations. The

transformation process is discussed in detail in Section 1.9.

Lighting effects in OpenGL are controlled by manipulation of the attributes of one or more of the

simulated light sources defined in OpenGL. The number of light sources supported by an

OpenGL implementation is specifically limited to GL_MAX_LIGHTS. This value can be queried

with glGet and must be at least 8. Each simulated light source in OpenGL has attributes that

cause it to behave as a directional light source, a point light source, or a spotlight. Light

attributes that can be adjusted by an application include the color of the emitted light, defined

as ambient, diffuse, and specular RGBA intensity values; the light source position; attenuation

factors that define how rapidly the intensity drops off as a function of distance; and direction,

exponent, and cutoff factors for spotlights. These attributes can be modified for any light with

glLight. Individual lights can be turned on or off by a call to
glEnable/glDisable with a symbolic

constant that specifies the affected light source.

Lighting produces a primary and secondary color for each vertex. The entire process of lighting

can be turned on or off by a call to glEnable/glDisable
with the symbolic constant GL_LIGHTING. If

lighting is disabled, the values of the primary and secondary color are taken from the last color

value set with the glColor command and the last secondary color set with the
glSecondaryColor

command.

The effects from enabled light sources are used in conjunction with surface material properties

to determine the lit color at a particular vertex. Materials are characterized by the color of light

they emit; the color of ambient, diffuse, and specular light they reflect; and their shininess.

Material properties can be defined separately for front-facing surfaces and for back-facing

surfaces and are specified with glMaterial.

Global lighting parameters are controlled with glLightModel. You can use this function to

时间: 2024-11-09 00:17:48

1.7. 绘制几何体 Drawing Geometry的相关文章

1.8. 绘制图片 Drawing Images

1.8. 绘制图片 Drawing Images As mentioned previously, OpenGL has a great deal of support for drawing images in addition to its support for drawing 3D geometry. In OpenGL parlance, images are called PIXEL RECTANGLES. The values that define a pixel rectang

《OpenGL编程指南(原书第9版)》——3.4 OpenGL的绘制命令

3.4 OpenGL的绘制命令 大部分OpenGL绘制命令都是以Draw这个单词开始的.绘制命令大致可以分为两个部分:索引形式和非索引形式的绘制.索引形式的绘制需要用到绑定GL_ELEMENT_ARRAY_BUFFER的缓存对象中存储的索引数组,它可以用来间接地对已经启用的顶点数组进行索引.另一方面,非索引的绘制不需要使用GL_ELEMENT_ARRAY_BUFFER,只需要简单地按顺序读取顶点数据即可.OpenGL当中,最基本的非索引形式的绘制命令就是glDrawArrays().void g

8天入门wpf—— 第七天 画刷

               这一篇我们聊聊wpf中的画刷,在wpf中如果想玩各种花哨,那么如何使用画刷则是我们的基本功,首先看一下类图 从图中可以看出,wpf有5种画刷和1种自定义画刷,都是继承自基类Brush,我们看看基类中有哪些好玩的东西. 这里有3个比较感兴趣的属性,分别属于"透明度"和"图像转换",好,下面我们一一解说.   一:SolidColorBrush(实心画刷)     实心画刷是我们用的最多的,也是最简单的一个,其实也就是填充色的意思,一个很简

《WebGL入门指南》——第2章,第2.4节一个真实的3D示例

2.4 一个真实的3D示例 WebGL入门指南 到目前为止,你也许在想"真是个还不错的正方形",然后开始怀疑我们什么时候开始画一些真正的3D图形.好吧,那就现在吧!在示例2-2中我们将会用更有趣的物体来代替正方形,我们将会完成一个看起来还不错.并且展示了大部分WebGL主要特性.同时还保持代码简洁的页面. 图2-2就是页面的最终效果.其中我们设置了标题文字,添加了一个表面贴有图片的立方体,然后在页面底部也添加了文字.另外值得一提的是,这个页面是可以交互的:点击画布元素,立方体就会开始或

【Web动画】CSS3 3D 行星运转 && 浏览器渲染原理

承接上一篇:[CSS3进阶]酷炫的3D旋转透视 . 最近入坑 Web 动画,所以把自己的学习过程记录一下分享给大家. CSS3 3D 行星运转 demo 页面请戳:Demo.(建议使用Chrome打开) 本文完整的代码,以及更多的 CSS3 效果,在我 Github 上可以看到,也希望大家可以点个 star. 嗯,可能有些人打不开 demo 或者页面乱了,贴几张效果图:(图片有点大,耐心等待一会) CSS3 3D 行星运转效果图 随机再截屏了一张: 强烈建议你点进 Demo页感受一下 CSS3

Windows 8 Store Apps学习(18) 绘图 Shape, Path, Stroke, Brush

介绍 重新想象 Windows 8 Store Apps 之 绘图 Shape - 图形 Path - 路径 Stroke - 笔划 Brush - 画笔 示例 1.演示如何绘制图形 Drawing/Shape.xaml <Page x:Class="XamlDemo.Drawing.Shape" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="ht

Android简明开发教程十九:线程 Bezier曲线

Android中使用线程Thread的方法和Java SE相同.和大多数OS系统一样,Android中也有称为UI Thread的主线程.UI Thread 主要用来给相应的Widget分发消息,包括绘制(Drawing)事件.UI Thread 也是用来处理用户交互事件的线程.比如:如果你 按下屏幕上某个按钮,UI 线程则将Touch 事件通知对应的控件(Widgets),Widget 则将其状态设置成"按下",并把"重绘" (Invalidate)事件发到Eve

Direct3D 9.0 SDK 文档(中文版)

版权声明: 此处提供的所有Direct3D 9.0 SDK 文档(中文版)的版权属于www.gesoftfactory.com及其所有人.在保持文档原貌(即:不对文档的内容做任何更改)的前提下,任何人可以免费下载和使用本文档,但不得以任何形式转载本文档的内容.在未经许可的情况下,禁止任何人/实体将本文档用于商业或赢利目的.如果需要在别处提供指向本站的链接,请事先用电子邮件联系并获得许可.www.gesoftfactory.com及其所有人保留对本文档的所有权利. 本页面(http://www.g

HTML5 界面元素 Canvas 参考手册

HTML5 界面元素 Canvas 参考手册 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. HTML5 界面元素 Canvas 参考手册HTML Canvas Reference 描述Desc