Professional Documents
Culture Documents
glutIdleFunc( idle );
void idle( void )
{
/* change something */
t +=dt;
glutPostRedisplay();
}
Mouse Callback
glutMouseFunc(myMouse);
void myMouse(GLint button, GLint state, GLint x, GLint y)
- button specifies which mouse button was pressed :
GLUT_LEFT_BUTTON,
GLUT_RIGHT_BUTTON, or
GLUT_MIDDLE_BUTTON
- state of that button
GLUT_UP, GLUT_DOWN
- position in window
x and y: screen coordinates of mouse position
(origin at top-left corner) when the event occurred
Captures mouse press and release events
glutMouseFunc( my_mouse);
GL_LINE_STRIP
Connected line segments
(a polyline)
GL_LINE_LOOP
Connected line segments, and last
point connected to first point
Line Attributes
Line Attribute
- Line Style
- Line Width
- Line Color
- Pen & Brush options
Line Drawing
Description:
Given the specification for a straight line, find the collection of addressable
pixels which most closely approximates this line.
Goals:
Straight lines should appear straight
lines should start and end accurately, matching endpoints with
connecting line
Lines should have constant brightness
Lines should be drawn as rapidly as possible
Problems:
How do we determine which pixels to illuminate to satisfy the above
goals
Vertical, horizontal, and lines with slope = + / - 1, are easy to draw.
Other create problems: stair-case/jaggies/aliasing
It is difficult to determine whether a pixel belongs to an object or not.
Direct Solution:
Solve y = mx + b, where (0, b) is the y-intercept and m is the slope.
Go from 𝑥0 to 𝑥1 :
calculate round(y) from the equation.
Take an example, b = 1 (starting point (0,1)) and m = 3/5.
Then
x = 1, y = 2 = round(8/5)
x = 2, y = 2 = round(11/5)
x = 3, y = 3 = round(14/5)
x = 4, y = 3 = round(17/5)
x = 5, y = 4 = round(20/5)
ideal case of line drawn in graph paper
Choice of pixels in the raster, as integer values:
x = 1, y = 2 = round(8/5)
x = 2, y = 2 = round(11/5)
x = 3, y = 3 = round(14/5)
x = 4, y = 3 = round(17/5)
x = 5, y = 4 = round(20/5)
Rewrite as:
Gives:
Choice: E or NE?
If F(x, y) > 0; then mid point is available below the line
If F(x, y) < 0; then mid point is available above the line
Algorithm for next choice:
if F(M) > 0 /* Q is above M*//* mid point is below line*/
then select NE pixel
else /* Q is below M*/ /*mid point is above line*/
select E
In this figure: Q is above the midpoint
That means line path is near to the NE
Pixel so based on algorithm we can
Choose the NE pixel to draw the line.
Texture Fill
Solid Fill Pattern Fill
Fill-area primitives are normally polygons, as they can be
filled more efficiently by graphics packages
Fill-Area algorithms are used to fill the interior of a
polygonal shape
If the polygon is to be filled we can specify a fill style
Options for filling a defined region include
- choice between a solid color or a pattern fill and
- choices for particular colors and patterns
Polygons are 2-D shapes whose boundary is formed by any
number of connected straight-line segments
- defined by 3 or more coplanar vertices (points positioned on the
same plane)
- Each pair of adjacent vertices is connected in sequence by edges
OpenGL Polygon Fill-Area Functions
To fill polygons with a fill-area pattern:
- Define the pattern
- Enable polygon-fill feature of OpenGL
- Draw polygons
A number of different ways:
glRect*
6 different symbolic constants
For all:
Polygons must be convex (all interior angles ≤ 180o)
Must specify vertices in anti-clockwise order when
viewing the polygon from “outside”
Default fill-style is solid color, determined by current
color settings
glRect*
OpenGL provides special routine that takes 2-D points only
glRect* (x1, y1, x2, y2)
(x1,y1) and (x2,y2) define opposite corners of the rectangle
when we call the glRect* routine, OpenGL will construct a
polygon with vertices defined in the following order:
(x1,y1), (x2,y1), (x2,y2), (x1,y2).
e.g: glRecti(200,100,50,250);
We can draw rectangles with
other functions, but glRect*can
be more efficient
All other fill-area functions use the functions
glBegin… glEnd:
GL_POLYGON
GL_TRIANGLES
GL_TRIANGLE_STRIP
GL_TRAINGLE_FAN
GL_QUADS
GL_QUAD_STRIP
GL_POLYGON:
Displays a single convex polygon
Vertices of the polygon are specified in anti-clockwise
direction
E.g.
glBegin(GL_POLYGON);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
GL_TRIANGLES:
Vertex list treated as groups of 3 triangle vertices
Vertices must be specified in anti-clockwise order
E.g.
glBegin(GL_TRIANGLES);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p6);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd();
GL_TRIANGLE_STRIP:
Displays set of connected triangles
First triangle vertices must be anti-clockwise
E.g.
glBegin(GL_TRIANGLE_STRIP);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p6);
glVertex2iv(p3);
glVertex2iv(p5);
glVertex2iv(p4);
glEnd();
GL_TRIANGLE_FAN:
First vertex is the ‘source’ of the fan
Subsequent pairs of vertices form triangles with the
first one
E.g.
glBegin(GL_TRIANGLE_FAN);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
GL_QUADS:
Vertex list treated as groups of 4 quadrilateral
vertices
Vertices must be specified in anti-clockwise order,
glBegin(GL_QUADS);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glVertex2iv(p7);
glVertex2iv(p8);
glEnd();
GL_QUAD_STRIP:
One quadrilateral drawn for each pair of vertices
after the first two
glBegin(GL_QUAD_STRIP);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glVertex2iv(p7);
glVertex2iv(p8);
glEnd();
Character Primitives
Many pictures require text
attributes: Font size, Color and Orientation
Attributes can be set both for entire character strings
(text) and for individual characters
Most graphics packages have some support for
displaying character primitives
Type Faces (fonts) can be divided into two:
Serif – has small lines or accents at the ends of the main
character stroke. And so makes readable.
Sans-Serif – does not have accents.
Arial is a sans-serif font
Verdana is a sans-serif font
Times Roman is a serif font
Garamond is a serif font
Another category of fonts
Monospace font: always take up the same width on the display,
regardless of which character is being drawn
Proportional fonts: width used on the display will be
proportional to the actual width of the character
00111000
01101100
11000110
11000110
11111110
11000110
11000110
00000000
Stroke(outline)
Defined using line/curve primitives
Each character represented (stored) as a series of line
segments
Takes longer time draw than bitmap fonts
we can change the font, colour, and also line width and
line style
width of these lines can be changed using
glLineWidth
style using
glLineStipple
OpenGL Character Primitives
glut library supports display of
character primitives
All characters displayed at current raster position:
glRasterPos2i(x, y);
Bitmap characters are drawn using
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, „a‟);
To change colour of the character use
glColor* routine
Stroke characters, e.g.
glutStrokeCharacter(GLUT_STROKE_ROMAN_10, „a‟);