Sei sulla pagina 1di 66

Computer Graphics p p

Lecture 3
Graphics Output Primitives
Somsak Walairacht, Computer Engineering, KMITL
Outline
Line Drawing Algorithms
Circle- Ellipse-Generating Algorithms Circle-, Ellipse-Generating Algorithms
Fill-Area Primitives
l ll Polygon Fill Area
Pixel-Array Primitives
Character Primitives
01074410 /13016218 Computer Graphics 2
Line Drawing Algorithms
Line Properties
DDA Algorithm DDA Algorithm
Bresenhams Line Algorithm
Curve Properties
Midpoint Algorithm
01074410 /13016218 Computer Graphics 3
A Straight-Line Segment
Defined by the coordinate positions for the
endpoints of the segment
Graphics system projects the endpoints to Graphics system projects the endpoints to
integer screen coordinates and determine the
nearest pixel positions along the line path
b t th t d i t between the two endpoints
A computed line position of (10.48, 20.51) is
converted to pixel position (10, 21) co e ted to p e pos t o ( 0, )
It causes all but horizontal and vertical lines
to be displayed with a stair-step appearance
(the jaggies)
01074410 /13016218 Computer Graphics 4
( the jaggies )
Line Properties
Line Equations
Cartesian slope-intercept equation
y = mx + b
With specific to a line with (x
0
, y
0
) and (x
end
, y
end
)
0
y y
m
end

= mx y b =
Given x interval dx, y interval dy can be found by
0
x x
m
end

=
0 0
mx y b =
Similarly
dy
dx =
mdx dy =
01074410 /13016218 Computer Graphics 5
m
Lines & Slopes
The slope of a line (m) is defined by its start and end
coordinates
The diagram below shows some examples of lines and their
slopes slopes
m = -2
m = -4
m =
m = 2
m = 4
m =
1
/
m = -1
m =
1
/
2
m = 1
m = -
1
/
3
m = -
1
/
2
m =
1
/
3
m /
2
01074410 /13016218 Computer Graphics 6
m = 0 m = 0
Scan-conversion Process for
Straight Lines
For lines with slope magnitude < 1:
dx can be set proportional to horizontal position,
dy can be calculated from dy=m(dx)
For lines with slope magnitude > 1:
dy can be set proportional to vertical position,
dx can be calculated from dx=dy/m
Sl it d 1 Slope magnitude = 1:
dx=dy
01074410 /13016218 Computer Graphics 7
Example (cont)
y
(7, 5)
5
First work out m and b:
(2, 2)
2
5
3
2 7
2 5
=

= m
4
2
3
2 - b
x
2 3 4 5 6 7
5
2
5
2 = - = b
Now for each x value work out the y value: Now for each x value work out the y value:
5
3
2
5
4
3
5
3
) 3 ( = + = y
5
1
3
5
4
4
5
3
) 4 ( = + = y
4
3
4
5
3
) 5 (
2
4
4
6
3
) 6 (
01074410 /13016218 Computer Graphics 8
5
3
5
5
5
3
) 5 ( = + = y
5
4
5
6
5
3
) 6 ( = + = y
Example (cont)
3
Now just round off the results and turn on these pixels to draw
our line
3
5
3
2 ) 3 ( ~ = y
1
5
6
7
3
5
1
3 ) 4 ( ~ = y
4
2
3
4
4
5
4
3 ) 5 ( ~ = y
2
0
1
2
01074410 /13016218 Computer Graphics 9
4
5
2
4 ) 6 ( ~ = y
0 1 2 3 4 5 6 7 8
DDA Algorithm
Digital Differential Analyzer (DDA)
Scan-conversion line algorithm Scan conversion line algorithm
Based on calculating either dy or dx
A line is sampled at unit intervals in one A line is sampled at unit intervals in one
coordinate and the corresponding values
nearest to line path are determined for the p
other coordinate
01074410 /13016218 Computer Graphics 10
DDA (2)

The line is processed from left
endpoint to right endpoint

The line is processed from left
endpoint to right endpoint
e dpo t to g t e dpo t
Slope is less than or equal to 1
dx = 1, y
k+1
= y
k
+ m
Slope is greater than 1
p g p
Abs of slope is less than 1
dx = +1, y
k+1
= y
k
- |m|
Abs of slope is greater than 1
d 1 1/| |
dy = 1, x
k+1
= x
k
+ 1/m
The line is processed from right
endpoint to left endpoint
Slope is less than or equal to 1
dy = -1, x
k+1
= x
k
+ 1/|m|
The line is processed from right
endpoint to left endpoint
Abs of slope is less than 1
Slope is less than or equal to 1
dx = -1, y
k+1
= y
k
- m
Slope is greater than 1
dy = -1, x
k+1
= x
k
- 1/m
Abs of slope is less than 1
dx = -1, y
k+1
= y
k
+ |m|
Abs of slope is greater than 1
dy = +1, x
k+1
= x
k
- 1/|m|
01074410 /13016218 Computer Graphics 11
y
k+1 k
Sample Code
inline int round (const float a) { return int (a + 0.5); }
void lineDDA (int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd x0 dy = yEnd y0 steps k; int dx = xEnd - x0, dy = yEnd - y0, steps, k;
float xIncrement, yIncrement, x = x0, y = y0;
if (fabs (dx) > fabs (dy))
steps = fabs (dx);
else
steps = fabs (dy);
xIncrement = float (dx) / float (steps);
yIncrement = float (dy) / float (steps);
setPixel (round (x), round (y));
for (k = 0; k < steps; k++) { for (k = 0; k < steps; k++) {
x += xIncrement;
y += yIncrement;
setPixel (round (x), round (y));
}
}
01074410 /13016218 Computer Graphics 12
}
DDA (3)
Fast method
Accumulation of round-off errors can Accumulation of round-off errors can
cause pixel position to drift away from
the true line path for long segment the true line path for long segment
Rounding operation and floating-point
arithmetic are expensive arithmetic are expensive
01074410 /13016218 Computer Graphics 13
Integer DDA
dy=y
end
-y
start
, dx=x
end
-x
start
error=dy-dx
4
0<m<1 dx,dy
d error , E error = error+dy
error , NE error = error+(dy-dx)
m>1 dx,dy
error , NE error = error+(dy-dx)
error , N error = error-dx
-1<m<0 dx dy
error , W error = error+dy , y
error , NW error = error+(dx+dy)
-1<m dx dy
error , NW error = error+dx+dy
error , N error = error+dx
01074410 /13016218 Computer Graphics 14
error , N error error+dx
Bresenhams Line Algorithm
Accurate and efficient raster-line generating
algorithm
Only use integer for calculations
Determine the nearest pixel position to the p p
line
By using the sign of integer parameter
Parameter is proportional to the difference
between the vertical separation of the two pixel
positions from the actual line path
01074410 /13016218 Computer Graphics 15
positions from the actual line path
Bresenhams Line Algorithm (2)
Reference for Bresenhams Line
Algorithm Algorithm
01074410 /13016218 Computer Graphics 16
Bresenhams Line Algorithm (3)
Proving Bresenhams Line Algorithm
Find distance between the line to the upper and
l i l lower pixels
y b x m
y y d
k k
k lower
+ + =
=
) 1 (
ff
b x m y
y y d
k k
k upper
+ + =
+ =
) 1 ( 1
) 1 (
Find the difference between two pixel, to
determine which one is closest to the path
1 2 2 ) 1 ( 2 + + = b y x m d d
01074410 /13016218 Computer Graphics 17
1 2 2 ) 1 ( 2 + + = b y x m d d
k k upper lower
Bresenhams Line Algorithm (4)
To convert the parameter to an integer
d d x p
upper lower k
A A
A =
) ( 2 ) ( 2
) (
) 1 2 ( 2 A + A b x y c
For k=k+1
c y x x y
k k
+ A A = ) ( 2 ) ( 2
) 1 2 ( 2 A + A = b x y c
] ) ( 2 ) ( 2 [ ) ( 2 ) ( 2
1 1 1
c y x x y c y x x y p p
k k k k k k
+ A A + A A =
+ + +
But x
k+1
= x
k
+1
) ( 2 ) ( 2
1 1 1 k k k k k k
y y x x x y p p A A + =
+ + +
) ( 2 2 A A
] ) ( ) ( [ ) ( ) (
1 1 1
y y y y p p
k k k k k k + + +
Starting point (x
0
, y
0
) and m=dy/dx
) ( 2 2
1 1 k k k k
y y x y p p A A + =
+ +
x y p A A = 2
0
b mx y + =
01074410 /13016218 Computer Graphics 18
x y p A A 2
0
0 0
x
x
y
y b
y
A
A
=
Bresenhams Line Algorithm (5)
) ( 2 2
1 1 k k k k
y y x y p p A A + =
+ +
01074410 /13016218 Computer Graphics 19
Bresenhams Line Algorithm (6)
01074410 /13016218 Computer Graphics 20
OpenGL Curve Functions
Routines for generating basic curves are
not included in OpenGL core library not included in OpenGL core library
GLU has routines for 3D Quadrics, such
as spheres cylinders B-splines as spheres, cylinders, B-splines
Simple curve can be approximated
using a polyline using a polyline
01074410 /13016218 Computer Graphics 21
Circle-Generating Algorithms
Properties of Circles
Pythagorean Theorem
2 2 2
) ( ) ( r y y x x
c c
= +
Stepping along x from x
c
-r to x
c
+r and calculate y
) ( ) ( y y
c c
2 2
) ( x x r y y
c c
=
Expressing circle equation in polar form
o
o
cos
cos
r y y
r x x
c
c
+ =
+ =
Symmetry of circle
o y y
c
01074410 /13016218 Computer Graphics 22
Curves Properties
Properties of Ellipses
Ellipse equation
1 ) ( ) (
2 2
=

c c
y y x x
Expressing ellipse equation in polar form
1 ) ( ) ( = +
x x
r r
o cos r x x
c
+ =
Other curves
Parabolic trajectory
2
o cos r y y
c
+ =
Hyperbola
) ( ) (
2
o o o
x x b x x a y y + + =
1 ) ( ) (
2 2
=
y x
01074410 /13016218 Computer Graphics 23
x x
r r
A Simple Circle Drawing p g
Algorithm 2 2
) ( x x r y y
c c
=
20 0 20
2 2
0
~ = y
20 1 20
2 2
1
~ = y
20 2 20
2 2
2
~ = y 20 2 20
2
y
6 19 20
2 2
19
~ = y
0 20 20
2 2
01074410 /13016218 Computer Graphics 24
0 20 20
2 2
20
~ = y
Midpoint Circle Algorithm
Midpoint Algorithm
Useful in drawing curves
Determine the closest pixel to the curve path by
using the midpoint between two candidate pixels
Example, midpoint at x
k
+1 along a circular path
01074410 /13016218 Computer Graphics 25
Midpoint Circle Algorithm (2)
Example: Midpoint Circle Algorithm
Define circle function as
2 2 2
) ( r y x y x f + =
By putting a point (x
o
, y
o
) into the circle function
(x
o
, y
o
) is in the circle boundary, if f
circ
< 0
( ) i th i l b d if f 0
) , ( r y x y x f
circ
+ =
(x
o
, y
o
) is on the circle boundary, if f
circ
= 0
(x
o
, y
o
) is outside the circle boundary, if f
circ
> 0
Decision parameter can be evaluated from the circle function Decision parameter can be evaluated from the circle function
at the midpoint
)
2
1
, 1 ( + =
k k circ k
y x f p
01074410 /13016218 Computer Graphics 26
Midpoint Circle Algorithm (3)
Successive decision
parameters,
x +1 = x +2
)
2
1
, 1 (
1 1 1
+ =
+ + + k k circ k
y x f p
x
k+1
+1 = x
k
+2
y
k
+1 is either y
k
or
y
k
-1 depending on the
sign of p
k
1 ) ( ) ( ) 1 ( 2
1
2 2
1
+ + + + =
+ + k k k k k k
y y y y x p
g p
k
When k=k+1,
if p
k
< 0, plot (x
k+1
, y
k
)
) 1 ( 1 2 + + = x p p
p
k
, p (
k+1
, y
k
)
and use Eq. 1
if p
k
> 0, plot (x
k+1
, y
k
-1)
and use Eq. 2
) 2 .( .......... 2 1 2
) 1 ...( .......... .......... 1 2
1 1 1
1 1
+ + +
+ +
+ + =
+ + =
k k k k
k k k
y x p p
x p p
01074410 /13016218 Computer Graphics 27
Midpoint Circle Algorithm (4)
01074410 /13016218 Computer Graphics 28
Midpoint Circle Algorithm (5)
Given a circle radius r=10
01074410 /13016218 Computer Graphics 29
Ellipse-Generating Algorithm
An ellipse is an elongated circle
Properties of ellipses
The distances f om an point on the ellipse to t o fi ed The distances from any point on the ellipse to two fixed
positions, called the foci of the ellipse
The sum of these two distances is the same value for all
points on the ellipse points on the ellipse
1 ) ( ) (
2 2
=

x
c
x
c
r
y y
r
x x
x x
o cos r x x
c
+ =
Polar form
01074410 /13016218 Computer Graphics 30
o cos r y y
c
+ =
Midpoint Ellipse Algorithm
An ellipse function
2 2 2 2 2 2
) , (
y x x y ellipse
r r y r x r y x f + =
Function has following properties
f
ellipse
< 0, (x, y) is inside the ellipse boundary
f
ellipse
= 0, (x, y) is on the ellipse boundary
f > 0 (x y) is outside the ellipse boundary f
ellipse
> 0, (x, y) is outside the ellipse boundary
Ellipse processing regions are Region 1 and 2
Start (0,r
y
) and step clockwise along elliptical part in the first (
y
) p g p p
quadrant, when slope less than -1.0
Alternatively, start (r
x
,0) and step counterclockwise, when slope
becomes greater than -1.0
01074410 /13016218 Computer Graphics 31
Midpoint Ellipse Algorithm (2)
01074410 /13016218 Computer Graphics 32
Midpoint Ellipse Algorithm (3)
01074410 /13016218 Computer Graphics 33
01074410 /13016218 Computer Graphics 34
Fill-Area Primitives
Area filled with solid color or pattern
M t hi i fill b l Most graphics require a fill area be a polygon
Curved surfaces can be approximated with
a set of polygon patches
Sometimes called as surface tessellation
01074410 /13016218 Computer Graphics 35
Polygon
A polygon is a plane figure specified by a set
of three or more coordinate positions, called
ti vertices
A polygon must have all its vertices within a
single plane and there can be no edge single plane and there can be no edge
crossings
To avoid ambiguous object references, g j ,
polygon refers only to those planar shapes
that have a closed-polyline boundary and no
edge crossings
01074410 /13016218 Computer Graphics 36
edge crossings
Convex Polygon
All interior angles of a polygon are less than
or equal to 180
Its interior lies completely on one side of the
infinite extension line of any one of its edges
Any two points in the interior of a convex Any two points in the interior of a convex
polygon, the line segment joining the two
points is also in the interior p
A polygon that is not convex is called a
concave polygon
01074410 /13016218 Computer Graphics 37
Concave Polygon
Implementations of fill algorithms and
other graphics routines are more g p
complicated
Splits a concave polygon into a set of g
convex polygons before processing
(polygon tessellation)
OpenGL require all fill polygons to be
convex
01074410 /13016218 Computer Graphics 38
Identifying Concave Polygons
A concave polygon has at least one interior angle
greater than 180
Extension of some edges intersect other edges Extension of some edges intersect other edges
Line segment by a pair of interior points intersects
the polygon boundary
The cross product of adjacent edges is used to test The cross product of adjacent edges is used to test
for concavity
01074410 /13016218 Computer Graphics 39
Splitting Concave Polygons
Using edge vectors and edge cross
products p
If any cross product has a negative z,
we can split it along the line of the first g
edge vector in the cross-product pair
01074410 /13016218 Computer Graphics 40
Splitting Concave Polygons (2)
Using a rotational method
Proceeding counterclockwise around the Proceeding counterclockwise around the
polygon edges, shift the position of the
polygon so that each vertex V
k
in turn is g
k
at the coordinate origin
Rotate the polygon about the origin in a
clockwise direction so that the next
vertex V
k+1
is on the x-axis
01074410 /13016218 Computer Graphics 41
Splitting Concave Polygons (3)
If the following vertex, V
k+2
, is below the
x-axis, then split the polygon along the x-
axis axis
Steps are repeated until all vertices in the
polygon list are tested p yg
01074410 /13016218 Computer Graphics 42
Splitting a Convex Polygon
into a Set of Triangles
Defining any sequence of 3 consecutive
vertices to be a new polygon (a vertices to be a new polygon (a
triangle)
The middle triangle vertex is then The middle triangle vertex is then
deleted from the original vertex list
Repeat until the original polygon is Repeat until the original polygon is
reduced to just three vertices
01074410 /13016218 Computer Graphics 43
Inside-Outside Tests
Odd-even rule, also called the odd-parity rule or the
even-odd rule
Identifying interior areas of a plane figure Identifying interior areas of a plane figure
Drawing a line from any position P to a distant point
outside the coordinate extents of the closed polyline
Then count the number of line-segment crossings
along this line
01074410 /13016218 Computer Graphics 44
Inside-Outside Tests (2)
Nonzero winding-number rule
Counts the number of times the boundary of an
object winds around a particular point in the object winds around a particular point in the
counterclockwise direction
The interior points of a 2D object can be defined by
h i l f th i di b having a nonzero value for the winding number
01074410 /13016218 Computer Graphics 45
Nonzero Winding-Number
Rule Mechanism
Initializing the winding number to 0
A line drawn from any position P to a distant
point beyond the coordinate extents of the
object
Count the number of object line segments Count the number of object line segments
that cross the reference line in each direction
Add 1 when intersects a segment that crosses g
from right to left
Subtract 1 when intersects a segment that crosses
from left to right
01074410 /13016218 Computer Graphics 46
from left to right
Variations of the Nonzero
Winding-Number Rule
01074410 /13016218 Computer Graphics 47
Polygon Tables
Objects in scene are described as a set of polygon
surface facets
Geometric data for objects in scene are arranged in
Vertex table, store the coordinate value of vertex
Edge table contains pointers back into the vertex table Edge table, contains pointers back into the vertex table
to identify the vertices for each polygon edge
Surface-facet table, contains pointers back into the
edge table to identify the edge for each polygon edge g y g p yg g
Edge table can be expanded to include forward
pointers into the surface-facet table
01074410 /13016218 Computer Graphics 48
p
Polygon Tables (2)
Polygon table for two adjacent polygon surface facets
Convenient reference
Object can be displayed efficiently
01074410 /13016218 Computer Graphics 49
Polygon Tables (3)
Tables can be organized into two groups:
geometric tables and attribute tables
Geometric tables contain vertex coordinates
and parameters to identify the spatial
orientation of the polygon surfaces
Attribute tables specify the degree of
f h bj d i f transparency of the object and its surface
reflectivity and texture characteristics
Somsak Walairacht, Computer Engineering, KMITL 50 01074410 /13016218 Computer Graphics 50
Plane Properties
0
0
2 2 2
1 1 1
= + + +
= + + +
d cz by ax
d cz by ax
General equation of a plane
Use Cramers rule to compute polygon facets data
0 = + + + d cz by ax
1 1 1
0
3 3 3
2 2 2
= + + + d cz by ax
y
3 3 3
2 2 2
1 1 1
3 3
2 2
1 1
3 3
2 2
1 1
3 3
2 2
1 1
,
1
1
1
,
1
1
1
,
1
1
1
z y x
z y x
z y x
D
y x
y x
y x
D
d
c
z x
z x
z x
D
d
b
z y
z y
z y
D
d
a = = = =
Distance from a point to a plane
1 1 1
1 1 1 1
Di t
) , , ( , 0
d cz by ax
z y x p d cz by ax
+ + +
= = + + +
Front and back polygon faces
Back face, side of polygon faces into the object interior
f h bl d d
2 2 2
1 1 1
Distance
c b a
y
+ +
=
01074410 /13016218 Computer Graphics 51
Front face, the visible or outward side
Plane Properties (2)
Identify a point behind or in front of a polygon
surface
Put the point into the plane equation p p q
if Ax + By + Cz +D < 0, the point (x,y,z) is behind the plane
if Ax + By + Cz +D > 0, the point (x,y,z) is in front of the
plane
O i t ti f l f i d ib d b Orientation of a polygon surface in space, described by
normal vector
01074410 /13016218 Computer Graphics 52
OpenGL Polygon Fill-Area p yg
Functions
Specification of polygon in OpenGL
Fill area must be a convex polygon
A polygon fill area can be defined only by one A polygon fill area can be defined only by one
vertex list
Each polygon specified as two faces:
a back face and a front face a back face and a front face
Supporting functions
Fill rectangle, triangle, quadrilaterals, polygon
To fill a object with polygon mesh by using specify
primitives
E.g. GL_TRIANGLE_STRIP
01074410 /13016218 Computer Graphics 53
OpenGL Polygon Fill-Area p yg
Functions (2)
Example: Filling a triangle mesh with
primitive
GL_TRIANGLE_STRIP
glBegin (GL_TRIANGLE_STRIP);
p6
p5 g g ( );
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p6);
p1
p4
glVertex2iv (p3);
glVertex2iv (p5);
glVertex2iv (p4);
glEnd();
p2 p3
01074410 /13016218 Computer Graphics 54
glEnd();
01074410 /13016218 Computer Graphics 55
Other Supporting OpenGL pp g p
Functions (1)
OpenGL vertex-array
A mechanism for reducing the number of function calls
Steps p
Invoke function glEnableClientState (GL_VERTEX_ARRAY)
to activate
Specify location and data format of vertex by glVertexPointer
e.x., glVertexPointer (3, GL INT, 0, pt); e.x., glVertexPointer (3, GL_INT, 0, pt);
Display scene using routine
e.x., glDrawElements ();
OpenGL pixel-array function
Bitmap function
glBitmap (width, height, x0, y0, xOffset, yOffset, bitShape);
Pixmap function
glDrawPixels (width, height, dataFormat, dataType, pixMap);
01074410 /13016218 Computer Graphics 56
g ( , g , , yp , p p);
GLintpoints[8][3] = {{0,0,0}, {0,1,0}, {1,0,0}, {1,1,0}, {0,0,1}, {0,1,1}, {1,0,1}, {1,1,1}};
typedef GLintvertex3 [3];
OpenGL vertex-array
GLintpoints[8][3] = {{0,0,0}, {0,1,0}, {1,0,0}, {1,1,0}, {0,0,1}, {0,1,1}, {1,0,1}, {1,1,1}};
typedef GLintvertex3 [3];
t 3 t[8] {{0 0 0} {0 1 0} {1 0 0} {1 1 0} {0 0 1} {0 1 1} {1 0 1} {1 1 1}}
vertex3pt[8] = {{0,0,0}, {0,1,0}, {1,0,0}, {1,1,0}, {0,0,1}, {0,1,1}, {1,0,1}, {1,1,1}};
void quad (GLintn1, GLintn2, GLintn3, GLintn4) {
glBegin (GL_QUADS);
glVertex3iv (pt[n1]);
vertex3pt[8] = {{0,0,0}, {0,1,0}, {1,0,0}, {1,1,0}, {0,0,1}, {0,1,1}, {1,0,1}, {1,1,1}};
void quad (GLintn1, GLintn2, GLintn3, GLintn4) {
glBegin (GL_QUADS);
glVertex3iv (pt[n1]);
glVertex3iv (pt[n1]);
glVertex3iv (pt[n2]);
glVertex3iv (pt[n3]);
glVertex3iv (pt[n4]);
glEnd ();
}
g (p [ ]);
glVertex3iv (pt[n2]);
glVertex3iv (pt[n3]);
glVertex3iv (pt[n4]);
glEnd ();
}
}
void cube () {
quad (6,2,3,7);
quad (5,1,0,4);
quad (7,3,1,5);
}
void cube () {
quad (6,2,3,7);
quad (5,1,0,4);
quad (7,3,1,5);
q ( , , , );
quad (4,0,2,6);
quad (2,0,1,3);
quad (7,5,4,6);
}
quad (4,0,2,6);
quad (2,0,1,3);
quad (7,5,4,6);
}
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_INT, 0, pt);
GLubyte vertIndex[] = (6,2,3,7,5,1,0,4,7,3,1,5,4,0,2,6,2,0,1,3,7,5,4,6);
glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, vertIndex);
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_INT, 0, pt);
GLubyte vertIndex[] = (6,2,3,7,5,1,0,4,7,3,1,5,4,0,2,6,2,0,1,3,7,5,4,6);
glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, vertIndex);
01074410 /13016218 Computer Graphics 57
glDisableClientState (GL_VERTEX_ARRAY); //deactivate vertex-array
glDisableClientState (GL_VERTEX_ARRAY); //deactivate vertex-array
OpenGL pixel-array function p p y
Bitmap function
GLubyte bitShape[20] = {
0x1c, 0x00, 0x1c, 0x00, 0x1c, 0x00, 0x1c, 0x00, 0x1c, 0x00,
0xff, 0x80, 0x7f, 0x00, 0x3e, 0x00, 0x1c, 0x00, 0x08, 0x00 };
glPixelStorei (GL_UNPACK_ALIGNMENT,1); //Set pixel storage mode.
glRasterPos2i (30,40);
glBitmap (9, 10, 0.0, 0.0, 20.0, 15.0, bitShape);
01074410 /13016218 Computer Graphics 58
OpenGL pixel-array function p p y
Pixmap function
void display () {
//Create some nice colours (3 floats per pixel)
float* pixels = new float[size*3]; p [ ];
for(int i=0; i<size; i++) {
colour (10.0-((i*20.0)/size), &pixels[i*3]);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glDrawPixels writes a block of pixels to the framebuffer.
glDrawPixels(window_width,window_height,GL_RGB,GL_FLOAT,pixels);
glutSwapBuffers();
}
01074410 /13016218 Computer Graphics 59
Other Supporting OpenGL pp g p
Functions (2)
OpenGL Character functions
Bitmap font, representing character with a pattern
f bi l t l id of binary values on rectangle grid
glutBitmapCharacter (font, character);
Outline font (stroke font), using straight-line and
curve sections to describe characters
glutStrokeCharater (font, Character);
Example: Example:
glRasterPosition2i (x, y);
for (k =0; k <36; k++)
glutBitmapCharacter (GLUT_BITMAP_9_BY_15, text [k]);
01074410 /13016218 Computer Graphics 60
Bitmap Font & Outline Font
01074410 /13016218 Computer Graphics 61
OpenGL Character Functions
Bitmapped GLUT fonts
glutBitmapCharacter (font, character);
We can select a fixed-width font by
GLUT_BITMAP_8_BY_13
GLUT_BITMAP_9_BY_15
GLUT BITMAP_TIMES ROMAN_10
GLUT BITMAP_HELVETICA_10
or, 12-point Times-Roman, 12-point and 18-point
Helvetica fonts
01074410 /13016218 Computer Graphics 62
Helvetica fonts
OpenGL Character Functions (2)
Outline character
glutStrokeCharacter (font, character);
Assign parameter font either Assign parameter font either
GLUT STROKE ROMAN
or, GLUT STROKE MONO ROMAN ,
Example:
glRasterPosition2i (x, y);
for (k = 0; k < 36; k++) for (k 0; k < 36; k++)
glutBitmapCharacter (GLUT_BITMAP_9_BY_15, text [k]);
01074410 /13016218 Computer Graphics 63
Other Supporting OpenGL pp g p
Functions (3)
OpenGL display list
Store an object as a named sequence of
statements statements
Format:
glNewList (listID, listMode);

glEndList();
Functions:
glGenList (); // generate display list id glGenList (); // generate display list id
glCallList (listID); // execute display list
glDeleteLists (startID, nLists);
01074410 /13016218 Computer Graphics 64
g ( , );
Example of OpenGL Display
List
const double TWO_PI = 6.2831853;
GLuint regHex;
GLdouble theta;
GLint x, y, k;
/* S t di l li t f l h V ti f th h i ll /* Set up a display list for a regular hexagon. Vertices for the hexagon are six equally
spaced points around the circumference of a circle. */

regHex = glGenLists (1); // Get an identifier for the display list.
glNewList (regHex, GL_COMPILE);
glBegin (GL_POLYGON);
for (k = 0; k < 6; k++) {
theta = TWO_PI * k / 6.0;
x = 200 + 150 * cos (theta);
y = 200 + 150 * sin (theta); y = 200 + 150 sin (theta);
glVertex2i (x, y);
}
glEnd ( );
glEndList ( );
lC llLi t ( H )
01074410 /13016218 Computer Graphics 65
glCallList (regHex);
End of Lecture 3
01074410 /13016218 Computer Graphics 74

Potrebbero piacerti anche