This
lesson covers drawing points, lines, and faces in 3D. Some basic user input functionality
is also introduced. Use the same procedures for setting up Pelles
C compiler for Win32 as outlined in the previous lesson, Getting
Started with OpenGL GLUT.
The
OpenGL cartesian
coordinate system is right-handed, meaning +X is to the right, +Y is up, and
+Z points out of the screen. The center (X,Y,Z) point is the origin point, or
(0,0,0). When we draw basic geometric
primitives we specify the coordinates of multiple vertices,
a single vertex
at a time. 
As
shown above, OpenGL has 10 functions for drawing points, lines, and faces. They
all have a similar syntax and are very easy to use. All you need to do is use
the glVertex3f() function
flanked by calls to glBegin()
and glEnd().
Example
of drawing two points glBegin(GL_POINTS);
glVertex3f(4.0, 0.0, 4.0); glVertex3f(1.0, -4.0, -2.0); glEnd();
Example
of drawing three lines glBegin(GL_LINES);
glVertex3f(6.0,
4.0, 2.0); glVertex3f(2.0, -4.0, 3.3);
glVertex3f(5.0, 8.0, 8.0); glVertex3f(-4.7, 5.0, -3.0);
glVertex3f(0.0, 0.0, 0.0); glVertex3f(6.0, -1.0, -7.0); glEnd();
Example
of drawing a single triangle glBegin(GL_TRIANGLES);
glVertex3f(-3.0, -3.0, 2.0); glVertex3f(3.0, -3.0, -1.0); glVertex3f(0.0,
3.0, 4.0);
glEnd(); The
rest are quite simple to figure out, here is a table of all the available OpenGL
point, line, and face drawing functions: | Points | GL_POINTS | | Lines | GL_LINES,
GL_LINE_STRIP, GL_LINE_LOOP | | Faces | GL_TRIANGLES,
GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_POLYGON |
Now
for a note about color. Notice in the code below the use of glColor()
before some of the vertex definitions. When OpenGL sees different colors for different
vertices of polygons it blends the colors across the face.
Common Color Reference
| OpenGL
Color Function | Color
| Color
HEX | Color
RGB | | glColor3f(0.0,
0.0, 0.0); | Black |
#000000 | rgb(0,0,0) | | glColor3f(1.0,
0.0, 0.0); | Red | #FF0000 | rgb(255,0,0) | | glColor3f(0.0,
1.0, 0.0); | Green | #00FF00 |
rgb(0,255,0) | | glColor3f(0.0,
0.0, 1.0); | Blue | #0000FF | rgb(0,0,255) | | glColor3f(1.0,
1.0, 0.0); | Yellow | #FFFF00 |
rgb(255,255,0) | | glColor3f(0.0,
1.0, 1.0); | Cyan | #00FFFF |
rgb(0,255,255) | | glColor3f(1.0,
0.0, 1.0); | Magenta | #FF00FF | rgb(255,0,255) | | glColor3f(1.0,
1.0, 1.0); | White | #FFFFFF | rgb(255,255,255) |
Now
it's time to try things out! As you can see, the code below is based on the previous
lesson with some additions. Some simple calls to drawing functions have been added
along with a keyboard routine using the glutKeyboardFunc()
function. The
glRotatef() function is
used to rotate the data on the screen in 3 dimensions. //--------------------------------------------------------------------- //
Drawing in 3D with openGL GLUT // Points, Lines and Faces // // Includes
basic user keyboard input //--------------------------------------------------------------------- #include
<windows.h> #include <gl\gl.h> #include <gl\glut.h> void
init(void); void display(void); void keyboard(unsigned char, int, int); int
main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE
| GLUT_RGB); glutInitWindowSize(200, 200); glutInitWindowPosition(50, 50); glutCreateWindow("Drawing
in 3D"); init(); glutDisplayFunc(display); glutKeyboardFunc(keyboard);
//set keyboard handler glutMainLoop(); return 0; } void
init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-15.0,
15.0, -15.0, 15.0, -15.0, 15.0); } void
display(void) { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3f(1.0,
0.0, 0.0); //red glVertex3f(-3.0, -3.0, 0.0); glVertex3f(3.0, -3.0, 0.0); glColor3f(1.0,
0.0, 1.0); //blue glVertex3f(3.0, 3.0, 0.0); glVertex3f(-3.0, 3.0, 0.0); glEnd(); glBegin(GL_TRIANGLES); glColor3f(1.0,
0.0, 0.0); glVertex3f(0.0, 6.0, 0.0); glColor3f(0.0, 0.0, 0.9f); glVertex3f(-3.0,
3.0, 0.0); glVertex3f(3.0, 3.0, 0.0); glEnd(); glBegin(GL_LINES); glColor3f(0.0,
0.0, 1.0); //blue glVertex3f(0.0,
7.0, 0.0); glVertex3f(0.0, -4.0, 0.0); glEnd(); glutSwapBuffers(); }
void
keyboard(unsigned char key, int x, int y) { /* this is the keyboard event
handler the x and y parameters are the mouse coordintes when the key was
struck */ switch (key) { case 'u': case 'U': glRotatef(3.0, 1.0,
0.0, 0.0); // rotate up break; case 'd': case 'D': glRotatef(-3.0,
1.0, 0.0, 0.0); // rotate down break; case 'l': case 'L': glRotatef(3.0,
0.0, 1.0, 0.0); // rotate left break; case 'r': case 'R': glRotatef(-3.0,
0.0, 1.0, 0.0); // rotate right } display(); // repaint the window } //---------------------------------------------------------------------  Drawing
output
There
is a reason drawing only occurs on a single plane
in this example. Experiment and you will see faces do not intersect
properly. This effect is due to a lack of depth
buffering, also known as Z-buffering,
which will be covered in the next lesson. END
OF LESSON BACK |