Basic OpenGL Elements


Basic 2D Drawing

glBegin (mode);
glVertex2f (x1, y1);
glVertex2f (x2, y2);
glVertex2f (x3, y3);
glVertex2f (x4, y4);
glEnd ();

where mode = GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_POLYGON, GL_LINE_LOOP, etc.

Lines of zero length are not visible.

A vertex is considered to be just a location with zero mass.

Only 10 of the OpenGL functions are legal for use between glBegin and glEnd.

Some Primitive Attributes

glClearColor (red, green, blue, alpha); - Default = (0.0, 0.0, 0.0, 0.0)
glColor3f (red, green, blue); - Default = (1.0, 1.0, 1.0)
glLineWidth (width); - Default = (1.0)
glLineStipple (factor, pattern) - Default = (1, 0xffff)
glEnable (GL_LINE_STIPPLE);
glPolygonMode (face, mode) - Default = (GL_FRONT_AND_BACK, GL_FILL)
glPointSize (size); - Default = (1.0)
Obtaining Values of OpenGL State Variables

glGetBooleanv (paramname, *paramlist);
glGetDoublev (paramname, *paramlist);
glGetFloatv (paramname, *paramlist);
glGetIntegerv (paramname, *paramlist);
Saving and Restoring Attributes

glPushAttrib (group);
glPopAttrib ( );

where group = GL_CURRENT_BIT, GL_ENABLE_BIT, GL_LINE_BIT, GL_POLYGON_BIT, etc.

Projection Transformations

glMatrixMode (GL_PROJECTION);
glLoadIdentity ( );
glFrustum (left, right, bottom, top, near, far);
gluPerspective (fov, aspect, near, far);
glOrtho (left, right, bottom, top, near, far); - Default = (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
gluOrtho2D (left, right, bottom, top);
Modelview Transformations

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ( );
gluLookAt (eye_x, eye_y, eye_z, at_x, at_y, at_z, up_x, up_y, up_z);
glTranslatef (dx, dy, dz);
glScalef (sx, sy, sz);
glRotatef (angle, axisx, axisy, axisz);
Writing Bitmapped Text

glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glColor3f (red, green, blue);
glRasterPos2f (x, y);
glutBitmapCharacter (font, character);

where font = GLUT_BITMAP_8_BY_13, GLUT_BITMAP_HELVETICA_10, etc.

Managing the Frame Buffer

glutInit (&argc, argv);
glutInitDisplayMode (GLUT_RGB | mode);
glutInitWindowSize (width, height);
glutInitWindowPosition (x, y);
glutCreateWindow (label);
glClear (GL_COLOR_BUFFER_BIT);
glutSwapBuffers ( );

where mode = GLUT_SINGLE or GLUT_DOUBLE.

Registering Callbacks

glutDisplayFunc (callback);
glutReshapeFunc (callback);
glutDisplayFunc (callback);
glutMotionFunc (callback);
glutPassiveMotionFunc (callback);
glutMouseFunc (callback);
glutKeyboardFunc (callback);
id = glutCreateMenu (callback);
glutMainLoop ( );
Display Lists

glNewList (number, GL_COMPILE);
glEndList ( );
glCallList (number);
glDeleteLists (number, 1);
Managing Menus

id = glutCreateMenu (callback);
glutDestroyMenu (id);
glutAddMenuEntry (label, number);
glutAttachMenu (button);
glutDetachMenu (button);

where button = GLUT_RIGHT_BUTTON or GLUT_LEFT_BUTTON.