Scott's GL Notes

On-Line Computer Graphics Notes

Scott's GL Notes


Greetings, and welcome to ECS 175. You're likely to be very busy in this class, so for those of you unfamiliar with the SGI's, I recommend you begin using them as soon as you can.

If you wish to get a poscript version of these notes, look here


Basics

For starters, SGI stands for Silicon Graphics Inc. It's the name of the company which makes computer graphics workstations. Although the workstations we have are called Irises, Indigos or Indys, most people often refer to them as the SGI's.

The SGI labs are located in rooms 067 and 069 of Engineering II. You'll be doing all your work on the SGI's. The good news is that graphics is very easy to do on these machines. The bad news is that we've only got about 25 of them. Since there appear to be more than 25 people enrolled, it looks like we might have some contention.

So I advise you all to start your assigned projects in this class as soon as possible. If you get an assignment done a week ahead of schedule, then you won't have to worry about fighting for a machine during the nights immediately before the deadline and you won't have late points taken off your assignments.

In order to assure that everyone gets a shot at using the machines, there is a two hour usage limit when the lab is full. We also maintain a waiting list during these times. There weren't any major problems when we last used this protocol, and I'm hoping that we won't have any this time around.

I know tempers tend to get a bit short when you're up against a deadline, but please try to remain calm: you and your classmates are all in the same boat.

Hmm, I guess this has taken on a grim tone. So let me stress now that this is a very fun class. You get to draw cool pictures and you'll get a real feeling of accomplishment each time you finish a project.

So without further ado, here's a brief introduction to the gl graphics library, which allows you to do all the great things that we do in this course.


Location of important files.


Read the man Pages

If you have a question about a specific gl command, then the man pages are an excellent source of information. These have been set up for you in your .cshrc file and through xman.


How to Open a Window

Before you can draw any graphics, you'll need to open a window in which to draw your graphics. To do this, you'll use the winopen command.

Try compiling and running the example file ch00/green.c. Remember, you'll need to copy it and its Makefile to your own directory. Then you can just type `` make'' and it should compile without error. The full directory name is:

            /usr/people/4Dgifts/examples/glpg/ch00/green.c

When you run it, a red outline will appear. Position this where you want it on the screen, and press the left mouse button. A green window should appear. Let's look at the source code.

        #include <gl/gl.h>
        main ()
           {
                  prefsize ( 400, 400 ) ;
                  winopen ( "green" ) ;
                  color ( GREEN ) ;
                  clear () ;
                  sleep ( 10 ) ;
                  gexit () ;
                  return 0;
           }

That first command, prefsize, indicates that we want to open a window 400 pixels by 400 pixels. Next, winopen says we want to open a window with the title ``green''. The color command sets the current color to green ( more on this later) and clear () clears the window to the current color, in this case, green.

The gexit command will close the window and do some other cleanups which allow us to gracefully exit the graphics program.


How to Set Color.

There are two ways to specify color. Via color maps and RGB mode. The default is color maps. You specify color using the color command. ( i.e. color ( MAGENTA).) There are many limitations to this method, but it will suffice for your first assignment. Eventually, however, you're going to want to use RGB mode. To do this, we first have to tell the machine that we want to use RGB mode. ( No surprise). We'll use the command RGBmode () to do this. ( Isn't this a great language?) Now when we specify colors, we do it by specifying the amounts of the red, green, and blue components.

        ASIDE: RGB color theory
        We can define nearly any color as a red-green-blue 
        triple.  For instance, R=255, G=000, B=000 would 
        be red.  Other colors:
                R=000, G=255, B=000     green
                R=000, G=000, B=255     blue
                R=000, G=000, B=000     black
                R=255, G=255, B=255     white
                R=100, G=000, B=100     dark purple

There are several different commands for setting the current color in RGB mode. The easiest is probably RGBcolor (). It takes three arguments, each of them a short. ( see the man pages for more info) To set the current color to green, we'd use `` RGBcolor ( 0,255,0)''.

The command I always use to set colors is cpack. You aren't required to use this command, but I'll be using it in all my sample programs, so you may wish to understand how it works.

Again, the man pages describe it pretty well, but I'll give a brief description. cpack () takes a single argument- an unsigned long integer. On the SGI's, an unsigned long int is 4 bytes long. In cpack, the highest order byte is used for transparency ( you don't need to worry about that) while the remaining bytes are for blue, green, and red components. Typically, we use hexadecimal notation in C for readability. In hex, each byte will be two digits. Here's some examples:

        cpack ( 0x0000ff)         red
        cpack ( 0x00ff00)         green
        cpack ( 0xff0000)         blue
        cpack ( 0x000000)         black
        cpack ( 0)                black
        cpack ( 0xffffff)         white
        cpack ( 0x640064)         dark purple

How about an example?

         #include <gl/gl.h>
         main ()
            {
               prefsize ( 400, 400 ) ;
               winopen ( "RGB green" ) ;
               RGBmode () ;
               gconfig () ;
               cpack ( 0x00ff00 ) ;
               clear () ;
               sleep ( 10 ) ;
               gexit () ;
               return 0;
            }

RGBmode indicates that we want to use RGB mode. Note: be sure to call this only after winopen, otherwise you'll get a nasty error. Also, RGBmode won't go into effect until you call gconfig ().

gconfig reconfigures the current modes of the window. In this case we're using it to set the RGBmode. Be sure to call config () after RGBmode ().

cpack sets our color in RGB mode.


How to Draw

Well, drawing a green window is a good start, but we're going to want to do more before we finish this class. Specifically, we want to draw lines and polygons and curves and, in general, pretty pictures. Again, there are several different ways to do this.

We'll start with a simple one: move and draw.

As you might guess, the move command moves the current graphics position, and the draw command draws from the previous graphics position to the new graphics position.

Let's learn by example: ch02/bluebox.c

         #include <gl/gl.h>
         main ()
            {
                prefsize ( 400, 400 ) ;    /* we want a 400x400 window */
                winopen ( "bluebox" ) ;    /* open the window, call it bluebox */
                color ( BLACK ) ;          /* set color to black */
                clear () ;                 /* clear the screen  ( to black) */
                color ( BLUE ) ;           /* set color to blue */
                move2i ( 200, 200 ) ;      /* move to center of the screen */
                draw2i ( 200, 300 ) ;      /* draw a side of the box */
                draw2i ( 300, 300 ) ;      /* draw a side of the box */
                draw2i ( 300, 200 ) ;      /* draw a side of the box */
                draw2i ( 200, 200 ) ;      /* draw a side of the box */
                sleep ( 10 ) ;
                gexit () ;
                return 0;
            }

The coordinate system corresponds to the pixels on the screen and has (0,0) in the lower left hand corner, (400,400) in the upper right. (400,0) should be the lower right hand corner, and (0,400) the upper left.

move2i and draw2i are just one flavor of the move and draw commands. They take two integer (x,y) arguments. There are other variations which take shorts, longs, or even floats. Plus some variations will allow you to specify coordinates in three dimensions.

The fastest and most flexible way to draw is by using commands such as bgnline, endline, and v2i, v2f, or v2d. If you want to look at them in the sample programs before then, go for it.


Handling events ( mouse buttons, et. al.)

Once you're fairly comfortable with working with what we've gone over so far, you'll need to take the next step. For those of you not familiar with handling X events, this is going to be a pretty good-sized step. I refer to you ch05/input.c. The program allows you to draw lines to the screen by holding down the left mouse button. It's a good introduction to handling events ( events by the way are such things as key presses, mouse button presses, and mouse movement). You'll need to be able to deal with events in order to create any interactive graphics applications.

You might want to sit down and stare at input.c until it starts to make sense. You'll probably want to cross reference the man pages quite frequently.


Scott Janus -- janus@cs.ucdavis.edu
April 8, 1994


This document maintained by Ken Joy

Comments to the Author

All contents copyright (c) 1996, 1997
Computer Science Department,
University of California, Davis
All rights reserved.



Ken Joy Mon Dec 9 09:41:08 PST 1996