Primitives And Attributes<< Windowing And Viewports | Table of Contents | Display Lists >>
Open GL provides primitives at two levels. The basic library (GL) provides a small set of primitives, while the GLU library provides a larger range of objects derived from the basic library. The basic OpenGL primitives are defined using points or vertices. The basic template for defining a primitive is (:source lang=cpp:)glBegin( type ); glVertex*( ... ); : : glVertex*( ... ); glEnd(); where type is the type of object being defined and the asterisk (*) indicates the number of coordinate components and their corresponding data types.
VerticesOpenGL requires vertices or coordinates when building and various objects. The {f:glVertex*} function, which is used to specify a vertex, has a number of variations. The difference between the variations has to do with the type and number of coordinates required. Some of the more common ones include
You can also provide coordinates stored in an array directly as a pointer using one of the {f:glVertex*v} functions. Some of the common ones include
For example, suppose you store two points or vertices in arrays. You can supply those points to OpenGL instead of having to reference individual array elements. (:source lang=cpp:)/* Define two points as 3-element arrays. */ GLfloat pointA[] = { 1, 1, 1 }; GLfloat pointB[] = { 15, 7, 9 }; glVertex2fv( pointA ); glVertex2fv( pointB ); Points and Line SegmentsLine segments can be drawn by specifying the end point pairs with {f:glVertex*}. For example, the code segment below produces the image to the right.
(:source lang=cpp:) /* Draw 4 line segments from the indicated vertices. */ glBegin( GL_LINES ); glVertex2f( 0.0, 1.0 ); glVertex2f( -1.0, 0.0 ); glVertex2f( -0.75, -0.25 ); glVertex2f( -0.25, -0.75 ); glVertex2f( 0.0, -1.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 0.75, 0.25 ); glVertex2f( 0.25, 0.75 ); glEnd(); OpenGL provides three ways to draw line segments and one for drawing points.
The image below illustrates the results of using the line primitive types. Polygons
AttributesThere are a number of attributes that can be changed in the graphics context of a window. When an attribute is set, it remains active for the given window's graphics context until explicitly changed. The basic attributes are described below
glColor3i( GLint r, GLint g, GLint b) // r, g, b range: [0..255] glColor3f( GLfloat r, GLfloat g, GLfloat b ) // r, g, b range: [0.0...1.0] glColor3iv( GLint *v ) // r, g, b in array elements glColor3fv( GLfloat *v ) // r, g, b in array elements
|