Presentation is loading. Please wait.

Presentation is loading. Please wait.

State Management and Drawing Geometric Objects

Similar presentations


Presentation on theme: "State Management and Drawing Geometric Objects"— Presentation transcript:

1 State Management and Drawing Geometric Objects
Open GL Lecture 10

2 Chapter 2 -- State Management and Drawing Geometric Primitives
Although you can draw complex and interesting pictures using OpenGL, they’re all constructed from a small number of primitive graphical items. At the highest level of abstraction, there are three basic drawing operations -- this chapter covers the first two. clearing the window, drawing a geometric object drawing a raster object This includes things such as: two dimensional images, bitmaps, character fonts These are covered in Chapter 8 Chapter 2 -- State Management and Drawing Geometric Primitives

3 Chapter 2 -- State Management and Drawing Geometric Primitives
One thing to keep in mind as you go through the rest of this chapter is that with OpenGL, unless you specify otherwise, every time you issue a drawing command, the specified object is drawn This might seem obvious, but in some systems, you first make a list of things to draw, and when your list is complete, you tell the graphics hardware to draw all the items on the list. Chapter 2 -- State Management and Drawing Geometric Primitives

4 Chapter 2 -- State Management and Drawing Geometric Primitives
The first style is called immediate-mode graphics and is the default OpenGL style. In addition to using immediate mode, you can save some commands in a list (called a display list) for later drawing. Differences: Immediate mode graphics are typically easier to program, but display lists are often more efficient. Chapter 7 covers display lists and why you might want to use them. Chapter 2 -- State Management and Drawing Geometric Primitives

5 Chapter 2 -- State Management and Drawing Geometric Primitives
A Drawing Survival Kit This section explains how to clear the window in preparation for drawing, set the colors of objects that are to be drawn, and force drawing to be completed. None of these subjects has anything to do with geometric objects in a direct way, but any program that draws geometric objects has to deal with these issues. Chapter 2 -- State Management and Drawing Geometric Primitives

6 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

7 Chapter 2 -- State Management and Drawing Geometric Primitives
You must also know that the colors of pixels are stored in the graphics hardware known as bitplanes. 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. we will get to Alpha in Chapter 6 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. Chapter 2 -- State Management and Drawing Geometric Primitives

8 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 4 and Chapter 10 discuss how other buffers are used. For now, all you need to know is that clearing them is simple. Chapter 2 -- State Management and Drawing Geometric Primitives

9 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

10 Chapter 2 -- State Management and Drawing Geometric Primitives
Coloring, Lighting, and Shading are all large topics with entire chapters or large sections devoted to them. To draw geometric primitives that can be seen, however, you need some basic knowledge (Chapters 4 and 5 provide the details) 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. Chapter 2 -- State Management and Drawing Geometric Primitives

11 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

12 Chapter 2 -- State Management and Drawing Geometric Primitives
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 Chapter 2 -- State Management and Drawing Geometric Primitives

13 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

14 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. Chapter 2 -- State Management and Drawing Geometric Primitives

15 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

16 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Appendix F covers more on homogeneous coordinates. Chapter 5 of Foley, vanDam, Feiner, Hughes, and Phillips gives coverage of homogeneous coordinates, and their use. Chapter 2 -- State Management and Drawing Geometric Primitives

17 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

18 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

19 Chapter 2 -- State Management and Drawing Geometric Primitives
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*( ) Chapter 2 -- State Management and Drawing Geometric Primitives

20 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 (See Chapter 12) Chapter 2 -- State Management and Drawing Geometric Primitives

21 Chapter 2 -- State Management and Drawing Geometric Primitives
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 (discussed later in the chapter) Chapter 2 -- State Management and Drawing Geometric Primitives

22 Chapter 2 -- State Management and Drawing Geometric Primitives
OpenGL Geometric Drawing Primitives glBegin(GL_POLYGON) 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. Chapter 2 -- State Management and Drawing Geometric Primitives

23 Chapter 2 -- State Management and Drawing Geometric Primitives
There are a lot of options to put in glBegin( ) Chapter 2 -- State Management and Drawing Geometric Primitives

24 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. Chapter 2 -- State Management and Drawing Geometric Primitives

25 Chapter 2 -- State Management and Drawing Geometric Primitives
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. See Appendix B for more of them. Chapter 2 -- State Management and Drawing Geometric Primitives

26 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…. Chapter 2 -- State Management and Drawing Geometric Primitives

27 Chapter 2 -- State Management and Drawing Geometric Primitives
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. (see Chapter 6) Most OpenGL implementation support very large point sizes. you can query the minimum and maximum point sizes allowed Chapter 2 -- State Management and Drawing Geometric Primitives

28 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

29 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

30 Chapter 2 -- State Management and Drawing Geometric Primitives
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 (See Chapter 6) Chapter 2 -- State Management and Drawing Geometric Primitives

31 Chapter 2 -- State Management and Drawing Geometric Primitives
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); Chapter 2 -- State Management and Drawing Geometric Primitives

32 Chapter 2 -- State Management and Drawing Geometric Primitives
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 culling can be enabled or disabled. Chapter 2 -- State Management and Drawing Geometric Primitives

33 Chapter 2 -- State Management and Drawing Geometric Primitives
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); Chapter 2 -- State Management and Drawing Geometric Primitives

34 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

35 Chapter 2 -- State Management and Drawing Geometric Primitives
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 (See Chapter 5) Chapter 2 -- State Management and Drawing Geometric Primitives

36 Chapter 2 -- State Management and Drawing Geometric Primitives
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. Chapter 2 -- State Management and Drawing Geometric Primitives

37 Chapter 2 -- State Management and Drawing Geometric Primitives
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 Chapter 2 -- State Management and Drawing Geometric Primitives

38 Chapter 2 -- State Management and Drawing Geometric Primitives
Attribute Groups Earlier we saw how to set or query and individual state or state variable. You can also save and restore the values of a collection of related state variables with a single command. Chapter 2 -- State Management and Drawing Geometric Primitives

39 Chapter 2 -- State Management and Drawing Geometric Primitives
The general functions you would use are: void glPushAttrib(Glbitfield mask); void glPopAttrib(void); If you are doing client-server computing you may want to use: void glPushClientAttrib(Glbitfield mask); void glPopClientAttrib(void); Chapter 2 -- State Management and Drawing Geometric Primitives

40 Some Hints for Building Polygonal Models of Surfaces
Constructing polygonal approximations to surfaces is an art, and there is no substitute for experience. However, if you have to tessellate surfaces yourself, this section lists a few pointers that might make it a bit easier to get started. Chapter 2 -- State Management and Drawing Geometric Primitives


Download ppt "State Management and Drawing Geometric Objects"

Similar presentations


Ads by Google