Programming and Graphics

(Kiana) #1

258 Introduction to C++ Programming and Graphics


Triangle


The following alternative user-defined function produces the triangle
shown in Figure 8.1.1(c):


void showme(void)
{
glClear(GLCOLORBUFFERBIT);
glBegin(GLTRIANGLES);
glVertex2f(-0.9,-0.5);
glVertex2f( 0.3,-1.0);
glVertex2f( 0.0, 1.0);
glEnd();
glFlush();
}

Note that the triangle is drawn using OpenGL functions alone. Initially, the
triangle is put into the memory buffer. TheglFlushfunction flushes the buffer
and forces stored objects to be drawn.


OpenGL function names


Many OpenGL functions have the general name:

glNameNm( ... )

whereNis 2 or 3 to indicate the number of function arguments, andmisffor
float,dfor double,sfor signed short integer,usfor unsigned short integer,i
for signed integer,uifor unsigned integer,bfor character, andubfor unsigned
character.


Thus, the 2 in the name of theglVertex2findicates two arguments, and
thefindicates that the arguments will be floating-point numbers registered in
single precision.


glBegin(TYPE) and glEnd()


TheglBegin(TYPE)andglEnd()functions mark the beginning and end
of a set ofNvertices whose interpretation depends onTYPE. In the case of a
triangle,TYPEisGLTRIANGLES. A list of possible types follows:


GLPOINTS

Each vertex is a single point.
Free download pdf