Chapter 4 - State Management
Chapter 4 - State Management
Chapter 4
State Management
Chapter 4 State Management
Normal Vector
Vector Arrays
2
Basic State Management
OpenGL maintains many states and state variables.
3
Cont’d
To turn many of these states on and off, use these two simple
commands:
void glEnable(GLenum capability);
void glDisable(GLenum capability);
glEnable() turns on a capability.
glDisable() turns it off a capability.
More than 60 enumerated values can be passed as parameters to
glEnable() or glDisable().
4
Cont’d
Some examples are:
GL_BLEND
GL_DEPTH_TEST
GL_FOG
GL_LINE_STIPPLE
GL_LIGHTING
You can also check whether a state is currently enabled or
disabled.
GLboolean glIsEnabled(GLenum capability);
Returns GL_TRUE or GL_FALSE, depending on whether or
not the queried capability is currently activated.
5
Displaying Points Lines and Polygons
By default, a point is drawn as a single pixel on the screen, a line is
drawn solid and 1 pixel wide, and polygons are drawn solidly filled in.
The following discuss the details of how to change these default display
modes.
1. Point Details
To control the size of a rendered point, use glPointSize() and supply the
desired size in pixels as the argument.
void glPointSize(GLfloat size);
Sets the width in pixels for rendered points.
Size must be greater than 0.0 and by default is 1.0.
6
Cont’d
The actual collection of pixels on the screen which are drawn for various point
widths depends on whether antialiasing is enabled.
Antialiasing is a technique for smoothing points and lines as they are rendered.
If antialiasing is disabled (the default), fractional widths are rounded to integer
widths, and a screen−aligned square region of pixels is drawn.
Thus, if the width is 1.0,the square is 1 pixel by 1 pixel;
If the width is 2.0, the square is 2 pixels by 2 pixels, and so on.
With antialiasing enabled, a circular group of pixels is drawn, and the pixels on
the boundaries are typically drawn at less than full intensity to give the edge a
smoother appearance.
In this mode, non−integer widths aren’t rounded.
7
Cont’d
8
Wide Lines
10
Stippled Lines
11
Cont’d
To make stippled (dotted or dashed) lines, you use the command
glLineStipple() to define the stipple pattern, and then you enable line
With the preceding example and the pattern 0x3F07 (which translates
If factor had been 2, the pattern would have been elongated: 6 pixels
15
Cont’d
16
Cont’d
17
Cont’d
18
Normal Vector
A normal vector (or normal, for short) is a vector that points in a direction
that’s perpendicular to a surface.
For a flat surface
o one perpendicular direction is the same for every point on the surface
But for a general curved surface
o the normal direction might be different at each point on the surface
With OpenGL, you can specify a normal for each polygon or for each vertex.
Vertices of the same polygon might share the same normal (for a flat
surface) or have different normal (for a curved surface).
You can’t assign normal anywhere other than at the vertices.
19
Cont’d
Normal vectors define the orientation of its surface in space
in particular, its orientation relative to light sources.
These vectors are used by OpenGL to determine how much
light the object receives at its vertices.
An object’s normal vectors define the orientation of its surface
in space
in particular, its orientation relative to light sources.
These vectors are used by OpenGL to determine how much
light the object receives at its vertices.
Lighting is large topic by itself.
You use glNormal*() to set the current normal to the value of
the argument passed in.
20
Cont’d
Subsequent calls to glVertex*() cause the specified vertices to
be assigned the current normal.
Often, each vertex has a different normal, which necessitates a
series of alternating calls, as in the below Example.
glBegin (GL_POLYGON);
glNormal3fv(n0); glVertex3fv(v1);
glVertex3fv(v0); glNormal3fv(n2);
glNormal3fv(n1); glVertex3fv(v2);
glNormal3fv(n3);
glVertex3fv(v3);
glEnd();
21
Cont’d
22
Vector Arrays
You may have noticed that OpenGL requires many function calls to render geometric primitives.
Drawing a 20-sided polygon requires at least 22 function calls:
o one call to glBegin(),
o one call for each of the vertices, and
o a final call to glEnd().
For some systems, function calls have a great deal of overhead and can hinder performance.
An additional problem is the redundant processing of vertices that are shared between adjacent
polygons.
23
Cont’d
Using vertex arrays can reduces the number of function calls, which
improves performance.
Also, using vertex arrays may allow reuse of already processed shared
vertices.
There are three steps to using vertex arrays to render geometry:
1. Enabling Arrays
Activate (enable) the appropriate arrays, with each storing a different
type of data:
vertex coordinates fog coordinates
surface normals texture coordinates
RGBA colors polygon edge flags or
Secondary colors vertex attributes for use in a vertex
Color indices shader
24
Cont’d
25
Cont’d
If you use lighting, you may want to define a surface normal for every vertex.
To use vertex arrays for that case, you activate both the surface normal and
vertex coordinate arrays:
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
To deactivate the above functions: use the glEnableClientState() with the
same above symbolic constants
void glDisableClientState(GLenum array);
26
2.Specifying Data for the Arrays
void glVertexPointer(GLint size, GLenum type, GLsizei
stride, const GLvoid *pointer);
Specifies where spatial coordinate data can be accessed.
pointer is the memory address of the first coordinate of the
first vertex in the array.
Type specifies the data type (GL_SHORT, GL_INT,
GL_FLOAT, or GL_DOUBLE) of each coordinate in the
array.
size is the number of coordinates per vertex, which must be 2,
3, or 4. stride is the byte offset between consecutive vertices.
If stride is 0, the vertices are understood to be tightly packed
in the array.
27
Other similar methods are:
void glColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid
*pointer);
void glSecondaryColorPointer(GLint size, GLenum type, GLsizei stride, const
GLvoid *pointer);
void glIndexPointer(GLenum type, GLsizei stride, const GLvoid *pointer);
void glNormalPointer(GLenum type, GLsizei stride, const GLvoid *pointer);
void glFogCoordPointer(GLenum type, GLsizei stride, const GLvoid *pointer);
void glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid
*pointer);
void glEdgeFlagPointer(GLsizei stride, const GLvoid *pointer);
28
3. Dereferencing and Rendering
In Step 3, contents of the arrays are obtained, sent to the server, and
then sent down the graphics processing pipeline for rendering.
Dereferencing a Single Array Element:
void glArrayElement(GLint ith)
Obtains the data of one (the ith) vertex for all currently enabled arrays.
For the vertex coordinate array, the corresponding command would be
glVertex[size][type]v(), where size is one of [2, 3, 4], and type is one
of [s,i,f,d] for GLshort, GLint, GLfloat, and GLdouble, respectively.
Both size and type were defined by glVertexPointer().
For other enabled arrays, glArrayElement() calls glEdgeFlagv(),
glTexCoord[size][type]v(), glColor[size][type]v(),
glSecondaryColor3[type]v(), glIndex[type]v(), glNormal3[type]v(),
and glFogCoord[type]v().
glArrayElement() is usually called between glBegin() and glEnd().
29
Cont.…
30
Cont.…
Example Using glArrayElement() to Define Colors and Vertices:
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colors);
glVertexPointer(2, GL_INT, 0, vertices);
glBegin(GL_TRIANGLES);
glArrayElement(2);
glArrayElement(3);
glArrayElement(5);
glEnd();
31
32