chapter 6-1
chapter 6-1
Chapter Six
1
Clearing the Window
Before you begin to draw, you usually need to clear
the window (memory to hold the picture).
You don’t want to just draw a rectangle of the
appropriate color large enough to color the window
because
You want something that can do it efficiently
2
You must also know that the colors of pixels are
stored in the graphics hardware known as
biplanes.
There are two methods for this storage:
3
As an example, these lines of code clear an RGBA
mode window to black:
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
4
Specifying a Color
With OpenGL, the description of the shape of an
object being drawn is independent of the
description of its color.
Whenever an object is drawn, it’s drawn using
performance.
5
To set a color, use the command glColor3f( )
it takes 3 parameters, all floats in [0.0, 1.0]
6
Forcing Completion of Drawing
void glFlush(void);
Forces previously issued OpenGL commands to
begin execution.
This guarantees that they will complete in some
finite time in the future.
void glFinish(void);
Forces all previously issued OpenGL commands
to complete.
This command doesn’t return until all effects from
previous commands are fully realized.
7
Coordinate System Survival Kit
Whenever you initially open a window or later
move or resize a window, the window system will
send an event to notify you.
will be drawn
8
The function you register to do this may look like this reshape
function:
void reshape( int w, int h){
glViewport(0,0,(GLsizei) w,(Glsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(0.0, (Gldouble) w, 0.0, (Gldouble) h);
}
GLUT will pass it the width and the height
glViewport adjusts the pixel rectangle for drawing to be the entire
new window.
Lower left is (0,0) Upper right is (w,h)
glOrtho2D sets the origin at the lower left, and makes each
9
DESCRIBING POINTS, LINES, AND POLYGONS
This section explains how to describe OpenGL
geometric primitives.
10
What are Points, Lines, and Polygons?
You probably have a fairly good idea of what a
mathematician means by the terms point, line, and
polygon.
The OpenGL meanings are similar, but not quite
the same.
Differences
limitations of computer-based calculations
11
Points
a point is represented by a se of floating-point
numbers called a vertex.
Internally OpenGL does everything in 3D.
12
Lines
In OpenGL the term line refers to a line segment.
Polygons
Polygons are areas enclosed by single closed
Be Convex. -- no indentations.
13
The reason for the restrictions on polygons is the
desire for speed.
It is faster to render simple and convex polygons.
and y axes.
There is also a vector form of glRect*( )
15
Curves and Curved Surfaces
Any smoothly curved line or surface can be approximated - to any
vertex
On some machines the vector form of glVertex*(
) is more efficient.
If this is the case on your machine, you may
gain some performance by using vertex array
operations of OpenGL
17
OpenGL Geometric Drawing Primitives
glBegin(GL_POLYGON)
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 0.0);
glEnd( );
19
BASIC STATE MANAGEMENT
We have already seen how RGBA color is a state
variable, but there are many more that will impact how
a primitive is rendered
lighting, texturing, hidden surface removal, fog,
By default most of these states are inactive
20
These are some of the functions to check, change, and set
the states
void glEnable(Glenum cap);
void glDisable(Glenum cap);
Glboolean glIsEnabled(Glenum (capability);
21
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.
22
Point Details
void glPointSize(Glfloat size);
sets the width in pixels for rendered points.
allowed
23
Line Details
Wide Lines
void glLineWidth(Glfloat width);
1.0
Advanced
With nonantialiased wide lines, the width isn’t
measured perpendicular to the line.
Instead, it is measured in the y-direction (if the
25
Polygon Details
Polygons are typically drawn by filling in all the pixels
enclosed within the boundary, but you can also draw them
as outlined polygons or simply points at the vertices
a filled polygon might be solidly filled or stippled with a
certain pattern.
In OpenGL polygons are drawn in such a way that if
26
Polygons as Points, Outlines, or Solids
A Polygon has two sides -- front and back -- and might be
rendered differently depending on which side is facing the
viewer.
To change this, or to draw only outlines or vertices use:
GL_FRONT, or GL_BACK.
mode can be GL_POINT, GL_LINE, or GL_FILL
Example:
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE); 27
Reversing and Culling Polygon Faces
By convention, polygons whose vertices appear in
polygons with:
void glCullFace(Glenum mode);
28
culling can be enabled or disabled.
Stippling Polygons
By default, filled polygons are drawn with a solid
29
NORMAL VECTORS
30
In OpenGL you can specify normals for each polygon
or for each vertex.
You use glNormal*( ) to set the current normal to
the value of the argument passed in.
it takes either 3 coordinates (x,y,z) or a vector
31
VERTEX ARRAYS
OpenGL has vertex array routines that allow you to
specify a lot of vertex-related data with just a few
arrays and to access that data with equally few
function calls.
Arranging data in vertex arrays may increase the
performance of your application.
32
There are three basic steps for using vertex arrays:
Step 1: Enabling Arrays
you may have to enable up to 6 arrays
33
THANK YOU FOR YOUR ATTENTION
34