310 likes | 780 Views
Geometric Primitives. Geometric Primitives: Points, Lines and Polygons, Text Introduction Points, Lines, Polygons Rendering Primitives Begin and End Blocks Restrictions on Begin End Blocks Other Useful Primitive Drawing Functions Rendering Bitmapped and Stroke Text Characters.
E N D
Geometric Primitives • Geometric Primitives: Points, Lines and Polygons,Text • Introduction • Points, Lines, Polygons • Rendering Primitives • Begin and End Blocks • Restrictions on Begin End Blocks • Other Useful Primitive Drawing Functions • Rendering Bitmapped and Stroke Text Characters
Points, Lines and Polygons - Introduction • The meaning of terms such as point, line, and polygon have a similar meaning in OpenGL as in math, but not quite the same. • There are limits in the computer-based calculations which support the rendering of these primitives.In any OpenGL implementation floating-point calculations are of finite precision and have round-off errors. Coordinates of OpenGL primitives suffer from these problems. • There are also limits to raster graphics display.Raster graphics are graphics that involve determining which squares of an integer grid in window coordinates are occupied by a given primitive and then assigning other values to each such square.
Points • A point is represented by a set of floating-point numbers called a vertex. All internal calculations are performed using three-dimensional coordinates. Vertices specified as two dimensional are assigned a z-coordinate equal to zero by OpenGL • OpenGL internally uses homogeneous coordinates, for internal calculations, all vertices are represented with four floating point coordinates (x,y,z,w). If w is different from zero, there are coordinates corresponding to the Euclidean three-dimensional point (x/w, y/w, z/w). If the w coordinate isn't specified, it's understood to be 1.
Lines and Polygons • Lines in OpenGL refer to line segments. In all cases, lines are specified in terms of the vertices at their endpoints. • Polygons are the areas enclosed by single closed loops of line segments, where the line segments are specified by the vertices at their endpoints. Polygons are typically drawn with the pixels in the interior filled in, but you can also draw them as outlines or a set of points. • OpenGL places restrictions on what constitutes a primitive polygon. First, the edges of OpenGL polygons can't intersect. Second, OpenGL polygons must be convex, meaning that they cannot have indentations. • The reason for these restrictions on valid polygon types is that it is simpler to provide fast polygon-rendering hardware for that restricted class of polygons.
Polygons • Polygons have two sides, a front and a back. • The OpenGL function glFrontFace( GLenum mode ) is used to indicate which vertex order (relative to your eye) specifies the front face of a polygon. • Counterclockwise vertex ordering is assumed if this function is not called. By default, both front and back polygons are drawn.
Rendering Primitives • Specifying vertices for the purpose of rendering a line or polygon is accomplished using the glVertex*() function call. • This function is called once for each vertex that you want to specify. The glVertex*() functions are of type GLvoid. • There are many forms of the function glVertex*() : 2, 3, or 4 coordinate specification, short, int, float, double types. • There are scalar forms of the glVertex() function that will take x,y, z coordinates to map a vertex into World space. • Some of the Scalar Forms are followed:
Scalar Forms of glVertex*() glVertex2s(x,y) glVertex2i(x,y) glVertex2f(x,y) glVertex2d(x,y) glVertex3s(x,y,z) glVertex3i(x,y,z) glVertex3f(x,y,z) glVertex3d(x,y,z)
Vectors Forms of the glVertex*() • The Vector forms accept a parameter that represents a 2 or 3 dimensional vector quantity: Some of the Vector forms of the glVertex*() function include: • glVertex2sv(v) • glVertex2iv(v) • glVertex2fv(v) • glVertex2dv(v) • glVertex3sv(v) • glVertex3iv(v) • glVertex3fv(v) • glVertex3dv(v) • In the case of 2 dimensional coordinates, the z value is set at zero and the homogeneous coordinate w = 1. For 3 dimensional coordinates, the homogeneous coordinate w=1; • The vector argument is represented as a short, integer, float or double array of 2 or 3 vertices.
Begin/End Blocks • Drawing a collection of primitives to form an object such as a line or polygon is performed using the glBegin and glEnd function calls. The functions are defined as follows: • GLvoid glBegin( GLenum mode) - Marks the gebinining of a vertex-data list that describes a geometric primitive. The type of primitive is indicated by mode, which can be any of the values shown below. • GLvoid glEnd( GLvoid)- Marks the end of a vertex-data list
Begin/End Blocks • The parameter mode defines the type of primitive that is to be rendered. Within the Begin End block, vertices are specified using the glVertex*() function call. The various primitives that can be rendered and specified through glBegin() are as follows: • GL_POINTS individual points • GL_LINES pairs of vertices interpreted as individual line segments • GL_LINE_STRIP series of connected line segments • GL_LINE_LOOP same as above, with a segment added between last and first vertices • GL_TRIANGLES triples of vertices interpreted as triangles • GL_TRIANGLE_STRIP linked strip of triangles • GL_TRIANGLE_FAN linked fan of triangles • GL_QUADS quadruples of vertices interpreted as four-sided polygons • GL_QUAD_STRIP linked strip of quadrilaterals • GL_POLYGON boundary of a simple, convex polygon
Restrictions on using glBegin()/glEnd() • The most important information about vertices is their coordinates. You can also supply additional vertex-specific data for each vertex - a color, a normal vector, texture coordinates, or any combination of these. The following table list a complete set of commands that may be specified within a Begin/End block: • glVertex*() set vertex coordinates • glColor*() set current color • glIndex*() set current color index • glNormal*() set normal vector coordinates • glTexCoord*() set texture coordinates • glEdgeFlag*() control drawing edges • glMaterial*() set material properties • glArrayElement() extract vertex array data • glEvalCoord*(),glEvalPoint*() generate coordinates • glCallList(),glCallLists() execute display Lists(s)
Other Useful Primitive Drawing Functions • void glPointSize(GLfloat size); Sets the width in pixels for rendered points; size must be greater than 0.0 and by default is 1.0. • void glLineWidth(GLfloat width); Sets the width in pixels for rendered lines; width must be greater than 0.0 and by default is 1.0. • To draw stippled (dotted or dashed) lines, use the command void glLineStipple(GLint factor, GLushort pattern); to define the stipple pattern, and then enable line stippling with glEnable(); • GLvoid glColor3f( GLfloat red, GLfloat green, GLfloat blue ); Set the current color used to draw the primitive and remains in effect until the next call to glColor*(). • GLvoid glRectf( GLfloat x1, GLfloat y1, GLfloat x2, GLfoat y2 ); Draw a filled rectangle with upper right hand coordinates at x2,y2 and lower left hand coordinates at x1,y1.
Geometric Primitives: Points, Lines and Polygons Example #ifdef __FLAT__ #include <windows.h> #endif #include <gl/glut.h> // The initialization function void init(void) { glutInitWindowSize( glutGet( GLUT_SCREEN_WIDTH)/3, glutGet( GLUT_SCREEN_HEIGHT)/3 ); glutInitWindowPosition( 0, 0 ); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutCreateWindow("Rendering Primitives") ; glClearColor(1.0, 1.0, 1.0, 0.0) ; glShadeModel(GL_FLAT) ; }
Geometric Primitives: Points, Lines and Polygons Example // The display callback function void display(void) { static float v[] = { 0.1, 0.1 }; glClear(GL_COLOR_BUFFER_BIT) ; glColor3f(0.0, 0.0, 0.0 ); // Set the point color to black glPointSize( 3.5 ); glBegin( GL_POINTS); glVertex2fv( v ); glVertex2f( 0.05, 0.2); glVertex2f( 0.05, 0.3); glVertex2f( 0.05, 0.4); glVertex2f( 0.05, 0.5); glVertex2f( 0.1, 0.2); glVertex2f( 0.1, 0.3); glVertex2f( 0.1, 0.4); glVertex2f( 0.1, 0.5); glVertex2i( 0, 0 ); glVertex2f( -0.1, -0.1);
Geometric Primitives: Points, Lines and Polygons Example glVertex2f( -0.05,-0.2); glVertex2f( -0.05,-0.3); glVertex2f( -0.05,-0.4); glVertex2f( -0.05,-0.5); glVertex2f( -0.1,-0.2); glVertex2f( -0.1,-0.3); glVertex2f( -0.1,-0.4); glVertex2f( -0.1,-0.5); glEnd(); glBegin( GL_LINES ); glColor3f(0.0, 0.0, 1.0); // Set the point color to blue glVertex2f( 0.5, 0.5 ); glVertex2f( 0.1, 0.1); glEnd();
Geometric Primitives: Points, Lines and Polygons Example glBegin( GL_LINES ); glColor3f(0.0, 0.0, 1.0); // Set the point color to blue glVertex2f( -0.5, -0.5 ); glVertex2f( -0.1, -0.1); glEnd(); glBegin ( GL_LINE_STRIP ); glColor3f(0.0, 1.0, 0.0); // Set the point color to green glVertex2f( -0.5, 0.2 ); glVertex2f( -0.4, 0.5 ); glVertex2f( -0.3, 0.1 ); glVertex2f( -0.2, 0.9 ); glEnd(); glBegin( GL_LINE_LOOP ); glColor3f(1.0, 1.0, 0.0); // Set the point color to yellow glVertex2f( 0.8, -0.2 ); glVertex2f( 0.4, -0.5 ); glVertex2f( 0.3, -0.1 ); glVertex2f( 0.2, -0.9 ); glEnd();
Geometric Primitives: Points, Lines and Polygons Example glBegin( GL_TRIANGLES ); glColor3f( 1.0, 0.0, 1.0); // Set the point color to magenta glVertex2f( -0.8, 0.3); glVertex2f( -0.7, 0.5); glVertex2f( -0.6, 0.1); glEnd(); glBegin( GL_TRIANGLE_STRIP ); // Set the point color to acqua glColor3f( 0.0, 1.0, 1.0); glVertex2f(0.9, 0.8); glVertex2f(0.8, 0.5); glVertex2f(0.6, 0.9); glVertex2f(0.7, -0.2); glVertex2f(0.6, 0.9); glVertex2f(0.5, 0.1); glEnd(); glBegin( GL_LINE_STRIP ); glColor3f( 1.0, 1.0, 1.0); // line strips in white glVertex2f(0.8, 0.5); glVertex2f(0.7, -0.2); glVertex2f(0.6, 0.9); glEnd();
Geometric Primitives: Points, Lines and Polygons Example glBegin( GL_QUADS ); glColor3f( 1.0, 0.0, 0.0); // Draw the quad in red glVertex2f( -0.8, -0.8); glVertex2f( -0.5, -0.3); glVertex2f( -0.2, -0.9); glVertex2f( -0.4, -1.0); glEnd(); glBegin( GL_POLYGON ); glColor3f( 0.0, 0.0, 0.0); // Set point color to magenta glVertex2f( -0.9, 0.6); glVertex2f( -0.8, 0.55); glVertex2f( -0.7, 0.6); glVertex2f( -0.6,0.8); glVertex2f( -0.85, 0.9); glEnd(); glColor3f(0.2, 0.9, 0.1); glRectf(-.1, .6, .3, .9); glutSwapBuffers(); }
Geometric Primitives: Points, Lines and Polygons Example // The main function int main(int argc, char** argv) { glutInit(&argc, argv) ; init(); glutDisplayFunc(display) ; glutMainLoop() ; return 0 ; }
Text- Rendering Bitmapped and Stroke Text Characters • Text is supported in OpenGL in two principal forms: • bitmap characters • stroke characters • Bitmap characters can be displayed with the gluBitMapCharacter() function: void glutBitMapCharacter( void *font, int char) • This function renders the character char, given by an ASCII code using the font specified by the parameter font.
Text- Rendering Bitmapped and Stroke Text Characters • Stoke characters are generated using standard OpenGL primitives, such as lines, polygons, and curves. These characters can be modified by the same set of transformations that can be applied to geometric objects. • Render stroke characters using the glutStrokeCharacter() function. void glutStrokeCharacter( void *font, int character) • Where font is a symbolic constant specifying which stroke font to use (e.g., GLUT_STROKE_ROMAN or GLUT_STROKE_MONO_ROMAN). and character is the character to render
Text- Rendering Bitmapped and Stroke Text Characters • Where the characters are drawn must be specified. The glRasterPos() function specifies were the text will appear. This position specifies the lower-left corner of the next bitmap that is rendered on the display. void glRasterPos{234}{sifd}( Type x, Type y, Typez, Type w) void glRasterPos{234}{sifd}v(TYPE *array) • This function specifies the raster position which is mapped to screen coordinates using the current model view and projection matrices.