Primitives And Attributes

<< Windowing And Viewports | Table of Contents | Display Lists >>
(:toc:)

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.

  • Other OpenGL functions can be used between {f:glBegin} and {f:glEnd}.
  • Calculations can be performed or attributes can be changed.

Vertices

OpenGL 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

glVertex2f( GLfloat x, GLfloat y )
glVertex2f( GLfloat x, GLfloat y )
glVertex2d( GLdouble x, GLdouble y )
glVertex2i( GLint x, GLint y )
glVertex3f( GLfloat x, GLfloat y, GLfloat z )

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

glVertex2fv( const GLfloat *v )
glVertex2dv( const GLdouble *v )
glVertex2iv( const GLint *v )
glVertex3fv( const GLfloat *v )

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 Segments

Line 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.

{v
GL_LINES}: draws disconnected line segments between successive pairs of vertices.
{v
GL_LINE_STRIP}: draws connected line segments between vertices without connecting back to the first vertex.
{v
GL_LINE_LOOP}: same as GL_LINE_STRIP but the line segment is drawn between the first vertex and the last.
{v
GL_POINTS}: the set of vertices are treated and drawn as individual raster points.

The image below illustrates the results of using the line primitive types.

Polygons

glPolygonMode()

Attributes

There 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

glColor*( r, g, b )
select the rendering color for the current object. The data type of the three values depends on the type specifiers given in place of the asterisk. Some of the more common ones include
(:source lang=cpp:)
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
glPointSize( size )
sets the pixel size for drawing points. {a:size} is a real value.
glLineWidth( size )
sets the line width or pixel size of line segments. {a:size} is a real value.


<< Windowing And Viewports | Table of Contents | Display Lists >>

Print - Changes - Search
Last modified: January 17, 2008, at 10:39 PM.