Drawing Geometric Objects Drawing Primitives n Open GL
Drawing Geometric Objects
Drawing Primitives n Open. GL sets three types of drawing primitives ¨ Points ¨ Lines ¨ Polygons, n e. g, traingles All primitives are represented in terms of vertices ¨ that define the positions of the points themselves or the ends of line segments or the corners of polygons
Points n Object of zero dimension (infinitely small) n Specified by a set of floating-point numbers (coordinates) called a vertex n Displayed as a single pixel on screen n void gl. Point. Size(Glfloat size); ¨ Sets the size of a rendered point in pixels
Specifying Vertices n void gl. Vertex{234}{sifd}[v](TYPE coords); ¨ Specifies a vertex for use in describing a geometric object gl. Vertex 2 s(2, 4); n gl. Vertex 4 f(2. 3, 1. 0, -2. 2, 2. 0); n GLdouble dvect[3] = {5. 0, 9. 0, 4. 0); n gl. Vertex 3 dv(dvect); n n Open. GL works in homogeneous coordinates vertex: : (x, y, z, w) n w = 1 for default n
Displaying Vertices n Bracket a set of vertices between a call to gl. Begin() and a call to gl. End() pair ¨ The argument GL_POINTS passed to gl. Begin() means drawing vertices in the form of the points n n gl. Begin(GL_POINTS); gl. Vertex 2 f(0. 0, 0. 0); gl. Vertex 2 f(4. 0, 4. 0); gl. Vertex 2 f(0. 0, 4. 0); gl. End(); ¨ Other drawing options for vertex-data n Lines (GL_LINES) n Polygon (GL_POLYGON) list
Lines n The term line refers to a line segment n Specified by the vertices at their endpoints n Displayed solid and one pixel wide n Smooth curves from line segments
Drawing Lines n To draw a vertex-data list as lines n n n n GL_LINE_STRIP ¨A n gl. Begin(GL_LINES); gl. Vertex 2 f(0. 0, 0. 0); gl. Vertex 2 f(4. 0, 4. 0); gl. Vertex 2 f(0. 0, 4. 0); gl. End(); series of connected lines GL_LINE_LOOP ¨A closed loop
Wide and Stippled Lines n void gl. Line. Width(GLfloat width); ¨ Sets n the width in pixels for rendered lines void gl. Line. Stipple(GLint factor, GLshort pattern); ¨ Sets the current stippling pattern (dashed or dotted) for lines ¨ Pattern is a 16 -bit series of 0 s and 1 s n 1 means one pixel drawing, and 0 not drawing ¨ Factor stretches the pattern multiplying each bit ¨ Trun on and off stippling n n gl. Enable(GL_LINE_STIPPLE) gl. Disable(GL_LINE_STIPPLE)
Example of Stippled Lines n gl. Line. Stipple(1, 0 x 3 F 07); Pattern 0 x 3 F 07 translates to 0011111100000111 n Line is drawn with 3 pixels on, 5 off, 6 on, and 2 off n n • gl. Line. Stipple(2, 0 x 3 F 07); Factor is 2 Line is drawn with 6 pixels on, 10 off, 12 on, and 4 off
Polygon n Areas enclosed by singled closed loops of line segments n Specified by vertices at the corners n Displayed as solid with the pixels in the interior filled in Examples: Triangle and Pentagon
Polygon Tessellation n Simple and convex polygon ¨ Triangle ¨ Any n three points always lie on a plane Polygon tessellation ¨ Nonsimple or nonconvex polygons can be represented in the form of triangles n Curved surfaces can be approximated by polygons
Drawing Polygon Draw a vertex-data list as a polygon n n n n gl. Begin(GL_POLYGON); gl. Vertex 2 f(0. 0, 0. 0); gl. Vertex 2 f(4. 0, 4. 0); gl. Vertex 2 f(0. 0, 4. 0); gl. End(); GL_TRIANGLES n n Draws first three vertices as a triangle GL_QUADS n Quadilateral is a four-sided polygon
Drawing Polygons n ¨ Draws a series of triangles using vertices in the order n n ¨ n v 0, v 1, v 2; v 2, v 1, v 3 v 2, v 3, v 4; v 4, v 3, v 5 v 0 All triangles are drawn with the same orientation (clockwise order) v 3 v 5 v 4 v 2 v 1 GL_TRIANGLE_FAN One vertex is in common to all triangles ¨ Clockwise orientation ¨ n v 1 GL_TRIANGLE_STRIP GL_QUAD_STRIP ¨ Draws a series of quadrilaterals v 2 v 3 v 0 v 4 v 5
Polygons as Points and Outlines n void gl. Polygon. Mode(GLenum face, Glenum mode); ¨ Controls the drawing mode for a polygon’s front and back faces gl. Polygon. Mode(GL_FRONT, GL_FILL); n gl. Polygon. Mode(GL_BACK, GL_LINE); n gl. Polygon. Mode(GL_FRONT_AND_BACK, GL_POINT); n n By convention, polygons whose vertices appear in counterclockwise order are frontfacing n GL_CCW
Deciding Front- or Back Facing n Decision based the sign of the polygon’s area, a computed in window coordinates n For GL_CCW, if a>0 means the polygon be front -facing, then a<0 means the back-facing n For GL_CW, if a<0 for front-facing, then a>0 for back-facing
Reversing and Culling Polygons n void gl. Front. Face(GLenum mode); ¨ Controls how front-facing polygons are determined ¨ Default mode is GL_CCW (vertices in counterclockwise order) ¨ Needs to be enabled n void gl. Cull. Face(GLenum mode); ¨ Indicates which polygons (back-facing or front -facing) should be discarded (culled) ¨ Needs to be enabled
Stippling Polygons n Void gl. Polygon. Stipple(const GLbyte *mask); ¨ Defines the current stipple pattern for the filled polygons ¨ The argument is a pointer to a 32 x 32 bitmap (a mask of 0 s and 1 s) n Needs to be enabled and disabled gl. Enable(GL_POLYGON_STIPPLE); n gl. Disable(GL_POLYGON_STIPPLE); n
Normal Vectors n Points in a direction that’s perpendicular to a surface ¨ The normal vectors are used in lighting calculations n void gl. Normal 3(bsidf)(TYPE nx, TYPE ny, TYPE nz); ¨ Sets the current normal vector as specified by the arguments n void gl. Normal 3(bsidf)v(const TYPE *v); ¨ Vector version supplying a single array v of three element
Finding Normal Vector n Surfaces described with polygonal data Calculate normal vectors for each polygonal facet ¨ Average these normals for neighboring facets ¨ Use the averaged normal for the vertex that the neighboring facets have in common ¨ •
Finding Normal Vector n Make two vectors from any three vertices v 1, v 2 and v 3 n n Cross product of these vectors is perpendicular to polygonal surface n n P = v 1 - v 2; Q = v 2 - v 3 N = P x Q = [Px Py Pz] x [Qx Qy Qz] = [Py. Qz-Qy. Pz) (Qx. Pz-Px. Qz) (Px. Qy-Qx. Py] = [Nx Ny Nz] Normalize the vector n n n = [nx ny nz] = [Nx/L Ny/L Nz/L] where L is length of the vector [Nx Ny Nz] v 3 v 1 Q P v 2
Building Polygonal Models of Surfaces n You can approximate smooth surfaces by polygons n Important points ¨ Polygon orientations consistency (all clockwise or all anticlockwise) ¨ Caution at non-triangular polygons ¨ Trade-off between display speed and image quality
Examples n Building an icosahedron n Polygonal approximation to a sphere
- Slides: 22