0% found this document useful (0 votes)
2 views

chapter 6-1

Chapter Six discusses state management and drawing geometric objects in OpenGL, including clearing the window, specifying colors, and managing coordinates. It explains the representation of points, lines, and polygons, as well as the importance of state variables for rendering performance. The chapter also covers advanced topics like stippling, normal vectors, and vertex arrays to enhance graphical applications.

Uploaded by

mesimiracle95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

chapter 6-1

Chapter Six discusses state management and drawing geometric objects in OpenGL, including clearing the window, specifying colors, and managing coordinates. It explains the representation of points, lines, and polygons, as well as the importance of state variables for rendering performance. The chapter also covers advanced topics like stippling, normal vectors, and vertex arrays to enhance graphical applications.

Uploaded by

mesimiracle95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

COMPUTER GRAPHICS & MULTIMEDIA

Chapter Six

State Management and


Drawing Geometric Objects

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

 OpenGL allows you to set/change your viewing

position, so where would you put this rectangle?


 Graphics hardware can have multiple buffers,

and it is convenient to have a single command to


clear any/all of them.

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:

 RGBA -- Red, Green, Blue, Alpha

 The Red, Green, and Blue values are stored


directly int he bit planes.
 This is the most common method, and we
will use it for most of our examples.
 Index values are stored, and looked up in a

color lookup table.

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);

 OpenGL keeps track of the current clearing color

as a state variable, so if it is set to the color you


want, you don’t have to call glClearColor( ) again.

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

the currently specified coloring scheme.

 In General, you specify the color or coloring


scheme first and then draw the objects.
 This helps OpenGL achieve higher drawing

performance.

5
 To set a color, use the command glColor3f( )
 it takes 3 parameters, all floats in [0.0, 1.0]

 each specifies the amount of Red, Green, and


Blue respectively.

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.

 In GLUT, whatever routine has been registered


with glutReshapeFunc( ) will be called.
 This function must:

 Reestablish the rectangular region that will be

the new rendering canvas


 Define the coordinate system to which objects

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

square(pixel) represent one unit.

9
DESCRIBING POINTS, LINES, AND POLYGONS
 This section explains how to describe OpenGL
geometric primitives.

 All geometric primitives are eventually described in


terms of their vertices -- or coordinates that define the
points themselves.

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

 round-off error, ...

 limitation from raster graphics displays.

11
 Points
 a point is represented by a se of floating-point
numbers called a vertex.
 Internally OpenGL does everything in 3D.

 if you specify an x and a y, but leave off the z,


OpenGL assumes it is 0.
 Advanced
 OpenGL works with homogeneous coordinates of

three dimensional projective geometry.

12
 Lines
 In OpenGL the term line refers to a line segment.

 There are easy ways to specify a connected

series of line segments (even a closed loop)

 Polygons
 Polygons are areas enclosed by single closed

loops of line segments.


 They are typically drawn filled in, but you can

draw them as outlines, or a set of points.


 OpenGL Polygons have to

 Be simple. -- edges can’t intersect.

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

 If you need something that is not simple, and


convex, you can form it out of a union of simple
and convex polygons.
 This procedure, known as tessellation.

 A typical assignment in the past was to take a

sphere and tessellate it so that it is almost


smooth.
 GLU has routines to do this, so we will go on to
other things.
14
 Rectangles
 Since rectangles are so common, OpenGL

provides a filled-rectangle drawing primitive,


glRect*( )
 This function has four parameters (x1,y1,x2,y2)

to specify the two opposite corners.


 It lies in the plane z=0 with sides parallel to the x

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

arbitrary degree of accuracy- by short line segments or small


polygonal regions.

Figure: Approximating Curves


 Even though curves aren’t geometric primitives, OpenGL

provides direct support for subdividing and drawing them


16
 Specifying Vertices
 With OpenGL, every geometric object is ultimately
described as an ordered set of vertices.
 You use the glVertex*( ) command to specify a

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( );

 This draws a filled in polygon. If we had specified


GL_POINTS in the glBegin( ), we would have just had the
vertices drawn.
18
 There are a lot of options to put in glBegin( )

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

 They may be costly to activate, like texture


mapping,
 So there are commands to turn them on and off.

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);

 There are more than 40 enumerated capabilities that can be


passed to these and other functions.
 More than 60 enumerated values can be passed as
parameters to glEnable() or glDisable(). Some examples are
GL_BLEND, GL_DEPTH_TEST, GL_FOG,
GL_LINE_STIPPLE and GL_LIGHTING.

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.

 But it is possible to change this,


 and here is how….

22
 Point Details
 void glPointSize(Glfloat size);
 sets the width in pixels for rendered points.

 size must be greater than 0.0 and is by default 1.0

 How things are drawn depends upon whether antialiasing


is on or not.

 Most OpenGL implementation support very large point


sizes.
 you can query the minimum and maximum point sizes

allowed

23
 Line Details
 Wide Lines
 void glLineWidth(Glfloat width);

 Sets the width in pixels for rendered lines.

 width must be greater than 0.0 and default is

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

absolute value of the slope is less than 1)


otherwise it is in the x-direction.
24
 Stippled Lines
 void glLineStipple(Glint factor, Glushort pattern);

 sets the current stippling pattern for lines

 the pattern argument is a 16 bit series of 0s and 1s,


and it is repeated as necessary
 the pattern can be stretched out by using factor
(factor is clamped between 1 and 256)
 Stippling can be disabled with the glDisable( ) function.

 Note that stippling can be used in combination with wide


lines to produce wide stippled lines.

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

adjacent polygons share an edge or vertices, the pixels


that make up that edge or vertex are drawn exactly once.
 This is done with transparency in mind.

 Antialiasing is more complicated for polygons than for


points or lines

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:

 void glPolygonMode(Glenum face, Glenum mode);

 for face you have: GL_FRONT_AND_BACK,

GL_FRONT, or GL_BACK.
 mode can be GL_POINT, GL_LINE, or GL_FILL

 by default both the front and back are drawn filled.

 Example:
 glPolygonMode(GL_FRONT, GL_FILL);

 glPolygonMode(GL_BACK, GL_LINE); 27
 Reversing and Culling Polygon Faces
 By convention, polygons whose vertices appear in

counterclockwise order on the screen are called front-


facing.
 You can swap front and back with:

 void glFrontFace(Glenum mode);

 mode is either GL_CCW or GL_CW

 You can ignore rendering the back (or front) faces of

polygons with:
 void glCullFace(Glenum mode);

 mode is either GL_FRONT, GL_BACK, or


GL_FRONT_AND_BACK

28
 culling can be enabled or disabled.
 Stippling Polygons
 By default, filled polygons are drawn with a solid

pattern, but they can also be stippled like lines.

 void glPolygonStipple(const Glubyts *mask);


 mask is a pointer to a 32 x 32 bitmap

 this bitmap is interpreted as a pattern of 0’s


and 1’s to tell where to draw or not to draw.

 In addition to defining the current polygon


stippling pattern, you must enable stippling with
 glEnable(GL_POLYGON_STIPPLE);

29
NORMAL VECTORS

 A normal vector is a vector that points in a direction


that’s perpendicular to the surface.

 For a flat surface, one perpendicular direction is the


same for every point on the surface

 For other surfaces the normal might be different at


every point on the surface.

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

with three things in it.

 An objet’s normal vectors define the orientation of


its surface in space
 how it reacts to light sources and other things

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

 vertex coordinates, RGBA colors, color indices,

surface normals, texture coordinates, or


polygon edge flags.
 Step 2: Specifying Data for the Arrays
 Step 3: Derefrencing and Rendering

33
THANK YOU FOR YOUR ATTENTION

34

You might also like