Drawing Basic Graphics Primitives Lecture 4 Wed Sep

Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003

Open. GL Functions Open. GL function names always begin with gl, glu, or glut, depending on the library. Names of functions that allow a variable number of arguments are followed by a digit indicating the number. Names of functions that allow various argument types are followed by a letter indicating the type.

State Variables Open. GL maintains a large number of state variables. These state variables are global – they are in effect and accessible within all functions. Some states are n n n Colors used in various situations. Shading effects. Lighting effects.

Setting the Color One of the states maintained is the current color used for drawing. The color is a combination of red, green, and blue. Use gl. Color*() to set the current color. n n n gl. Color 3 f(1. 0, 0. 0) – bright red. gl. Color 3 f(1. 0, 0. 0) – bright yellow. gl. Color 3 f(0. 5, 0. 5) – medium gray.

Drawing Dots To color a single pixel, use the gl. Vertex*() function. n n gl. Vertex 2 i(int, int). gl. Vertex 3 f(float, float), etc. The pixel is colored with the current color, as set by gl. Color(). The coordinates are in “world” coordinates, not screen coordinates.

Primitive Objects The pixel-drawing function is called gl. Vertex() because the pixel is typically a vertex in a polygon. The programmer must indicate to the GPU whether the point is an isolated point or part of a line or polygon. To do this, use gl. Begin() and gl. End() to enclose the vertices of the primitive objects.

Primitive Objects gl. Begin(type) defines the type of object to be drawn. Some values of type are n n GL_POINTS GL_LINES GL_TRIANGLES GL_POLYGON gl. End() marks the end of the object.

Creating Points The following program segment will draw three pixels. gl. Begin(GL_POINTS); gl. Vertex 2 i(10, 20); gl. Vertex 2 i(50, 50); gl. End():

Creating Triangles The following program segment will draw and fill a triangle. gl. Begin(GL_TRIANGLES); gl. Vertex 2 i(10, 20); gl. Vertex 2 i(50, 50); gl. End():

Creating Triangles The following program segment will draw, but not fill, a triangle. gl. Begin(GL_LINE_LOOP); gl. Vertex 2 i(10, 20); gl. Vertex 2 i(50, 50); gl. End():

Drawing Primitives For points, the size of the drawing pen is set by the function gl. Point. Size(size). For lines, the pen width is defined by the function gl. Line. Width(width). The size and width are measured in pixels.

Example: Draw. Dots. cpp n n Why does each dot disappear when the next dot is drawn? How could we modify the program so that all the dots remained? w Do not clear the buffer, but keep adding to it? w Store all the points in a list and draw each one every time? n What happens when the window is resized?

Making Line Drawings Drawing lines is similar to drawing points and triangles. Use gl. Begin(GL_LINES); Every pair of points drawn between gl. Begin() and gl. End() is rendered as a line. We may list many pairs of points.

Example: Draw an X The following program segment will draw two line segments that form an X. gl. Begin(GL_LINES); gl. Vertex 2 i(50, 50); gl. Vertex 2 i(100, 100); gl. Vertex 2 i(50, 100); gl. Vertex 2 i(100, 50); gl. End();

Example: Draw an X Draw. X. cpp

Drawing Polygons If we use GL_POLYGON in the gl. Begin() function, then the entire list of points is used to draw a single polygon. What would the last example look like? Why not use GL_POLYGON to draw several polygons in one group, just as with GL_LINES?

Example: Draw an Octagon The following program segment will draw a regular octagon. int cx = 100, cy = 100; gl. Begin(GL_POLYGON); for (int i = 0; i < 8; i++) { float dx = cos(i*PI/4); float dy = sin(i*PI/4); gl. Vertex 2 i(x + 100*dx, y + 100*dy); } gl. End();

Example: Draw an Octagon Draw. Octagon. cpp
- Slides: 18