📄 ch69.htm
字号:
Press the fly button in the menu. Hold down the left mouse button in the view area.
Move the mouse at the speed at which you want to do the fly-by, and then release
the mouse button. Remember that the rendering is slow on a 486 system and that you
might have to work slowly to get just the right effect. The Geomview application
uses a command language called the Graphical Command Language (GCL). Using GCL, you
can create your animation sequences by sending commands via pipes directly to Geomview.
The work of rendering and deciphering images is performed with the Object Oriented
Graphics Library (OOGL), also developed at the University of Minnesota. The full
documentation for the OOGL is available at the umn site at</P>
<PRE><FONT COLOR="#0066FF">http://www.geom.umn.edu/software/geomview/geomview_toc.html
</FONT></PRE>
<P>The documentation includes a great tutorial with lots of examples and code to
work with.
<H3 ALIGN="CENTER"><A NAME="Heading16<FONT COLOR="#000077">The Mesa/OpenGL
Toolkit</FONT></H3>
<P>The OpenGL standard is a software interface and libraries designed for 3D graphics
on high-end machines such as Iris, Silicon Graphics, IBM, and Sun workstations. Yes,
OpenGL libraries also exist for Windows NT products. The OpenGL libraries are generally
optimized for the hardware they are designed for, whereas the software interface
to their functions remains constant.</P>
<P>The Mesa toolkit was written primarily by Brian Paul as a software-only solution
running on top of X Window. The primary motivation for this design is to enable OpenGL
programs to run on all platforms that run X Window. This means you can run Mesa on
Linux too!</P>
<P>The cost of abstracting this software from the hardware via the layer of X Window
is loss of performance. Programs running with Mesa will almost certainly run slower
on X than on library code designed for OpenGL. The Mesa toolkit library can be called
directly from within C and FORTRAN programs. The Mesa library offers an almost complete
set of OpenGL functions. More functions are being added to the library as we go to
print. The latest version of the software and documentation can be downloaded from
the HTML page at</P>
<PRE><FONT COLOR="#0066FF">http://www.ssec.wisc.edu/~brianp/Mesa.html
</FONT></PRE>
<P>The files are found in zipped, tar archives. The latest version is 2.0, and the
source file is called <TT>Mesa-2.0.tar.gz</TT>. At the rate versions were changing
while I was writing this chapter, you will see a later version of the library.</P>
<P>Untar and unzip the archive, and change directories into the Mesa source tree.
Then run the following command:</P>
<PRE><FONT COLOR="#0066FF">$ make linux
</FONT></PRE>
<P>The <TT>make</TT> program takes a very long time to complete. So be patient.</P>
<P>After the <TT>make</TT> program terminates, you are left with the library directory
<TT>./lib</TT>. As root, link these libraries to <TT>/usr/local/lib</TT> because
the <TT>make</TT> program does not move these for you. The header files should be
linked under <TT>/usr/local/include</TT>.</P>
<P>The makefile I used for my system and setup is shown in Listing 69.2.
<H3 ALIGN="CENTER"><A NAME="Heading17<FONT COLOR="#000077">Listing 69.2. The
makefile for sample programs.</FONT></H3>
<PRE><FONT COLOR="#0066FF">INCPATH=/home/khusain/mesa/Mesa-2.0/include
LIBPATH=/home/khusain/mesa/Mesa-2.0/lib
LIBX= /usr/X11/lib
LIBS= -lMesaaux -lMesatk -lMesaGL -lMesaGLU -lXext -lX11 -lm
s1: s1.c
gcc -o s1 s1.c -I$(INCPATH) -L$(LIBPATH) -L$(LIBX) $(LIBS)
</FONT></PRE>
<P>Note that in Listing 69.2 I used the <TT>/home/khusain/mesa</TT> directory rather
than <TT>/usr/local/lib</TT>. This is simply for my convenience and pure laziness
while I write this book. You should really just create a link from the <TT>/usr/local/lib</TT>
directory to the files in the Mesa package. I just have too many software packages
on my Linux machine that are cluttering my <TT>/usr/local/lib</TT> source tree. It's
ironic that even my <TT>/usr/local</TT> tree, which is intended for use with custom
packages, is too cluttered up with weird links because I have installed and removed
so many different versions of different packages. Oh, well; I will do a spring cleanup...real
soon now. <TT>;-)</TT></P>
<P>Four libraries must be linked in with this program. All of these libraries are
located in the <TT>lib</TT> subdirectory. If you are going to port this code to non-Linux
machines, you might not have to link in the MesaGLU library because this provides
the layered interface for the underlying graphics libraries.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading18<FONT COLOR="#000077"><B>TIP: </B></FONT>If you stick with
the OpenGL standard when writing your programs, your code can be ported to other
systems with OpenGL installed in their file systems. These systems include Windows
NT, UNIX, and Silicon Graphics workstations.
<HR>
</DL>
<P>The test program I tried was the one based on the samples provided in the Mesa
package. I actually prefer to write simpler stuff because it's easier to explain
some of the examples. See Listing 69.3 for a simple OpenGL program.
<H3 ALIGN="CENTER"><A NAME="Heading19<FONT COLOR="#000077">Listing 69.3. A
simple test program.</FONT></H3>
<PRE><FONT COLOR="#0066FF"> 1
2 #include <stdlib.h>
3 #include <GL/gl.h>
4 #include <glaux.h>
5
6 float x,y;
7
8 void moveUp() { y += 10.0; }
9 void moveDown() { y -= 10.0; }
10 void moveRight() { x += 10.0; }
11 void moveLeft() { x -= 10.0; }
12
13 void forever()
14 {
15 glClearColor(1.0,1.0,1.0,0.0);
16 glClear(GL_COLOR_BUFFER_BIT);
17 glColor3f(0.0,0.3,0.0);
18 glMatrixMode(GL_PROJECTION);
19 glLoadIdentity();
20 glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
21 glRotatef(x,1.0,0.0,0.0);
22 glRotatef(y,0.0,1.0,0.0);
23 auxWireCube(1.0);
24 auxWireCube(0.5);
25 glFlush();
26 }
27
28
29 void main(int argc, char *argv[])
30 {
31 x = 0.0;
32 y = 0.0;
33 auxInitDisplayMode(AUX_SINGLE | AUX_RGB);
34 auxInitPosition(0,0,400,400);
35 auxInitWindow(argv[0]); /* program name */
36 auxKeyFunc(AUX_UP,moveUp);
37 auxKeyFunc(AUX_DOWN,moveDown);
38 auxKeyFunc(AUX_RIGHT,moveRight);
39 auxKeyFunc(AUX_LEFT,moveLeft);
40 auxMainLoop(forever);
41 }
</FONT></PRE>
<P>Two include files are required for building an application using the Mesa-enabled
program. Two global variables are declared at line 6 for the x and y angle of rotation.
Lines 8 through 11 define four callback functions that increment and decrement the
value of x and y by 10. Each of these functions is designed to be called when a key
is pressed. We will be tying these functions to presses of the arrow keys on the
keyboard. The values of the keys are defined in OpenGL as <TT>AUX_UP</TT>, <TT>AUX_DOWN</TT>,
<TT>AUX_LEFT</TT>, and <TT>AUX_RIGHT</TT>. (Only the arrow keys worked with this
program. The numeric keys with the NumLock key off did not work with these definitions.)</P>
<P>The <TT>forever()</TT> function is called whenever the window area for the application
has to be refreshed. (See line 13.) There are several function calls to each of the
libraries in Mesa. The <TT>gl</TT> functions call the Mesa libraries, whereas the
<TT>aux</TT> functions call the Mesaaux (auxiliary) library functions. The <TT>glClearColor()</TT>
function clears the buffer and color display memory. If you are going to use surfaces
and lighting in your program, you must also clear the bit buffer with a call to <TT>glClear</TT>
like this:</P>
<PRE><FONT COLOR="#0066FF">16 glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_DEPTH_BUFFER);
</FONT></PRE>
<P>All these constants are defined in the header files for Mesa. All functions that
end in the letter <TT>f</TT> take floating-point numbers as arguments. (You should
use the decimal point when specifying numbers as arguments to functions, or the gcc
compiler will complain.) The function that takes integers as arguments is defined
without the <TT>f</TT> prefix; that is, <TT>glColor(int, int, int)</TT>. Instead,
we used the floating format version in line 17 like this:</P>
<PRE><FONT COLOR="#0066FF">17 glColor3f(0.0,0.3,0.0);
</FONT></PRE>
<P>The next lines (18-22) define the mode of display as <TT>GL_PROJECTION</TT> and
set up the orthogonal projection for the viewing angle. The rotations are done from
the origin (0,0,0) and are performed along the line drawn from the point specified
in the arguments to the <TT>glRotate</TT> call. For example, <TT>glRotatef(x,1.0,0.0,0.0)</TT>
rotates about the line from (0,0,0) to (1,0,0). The next two auxiliary functions
(lines 23 and 24) are used to draw a wire cube of scale 0.5 and 1.0, respectively.
After the drawing commands are sent, we simply flush the buffers and we are done.</P>
<P>The main program does the initialization of the (x,y) coordinates and the initial
starting position for the viewing angle. It also initializes the color model (<TT>AUX_RGB</TT>)
and the windows with the program name (lines 33-35). The numeric keys are assigned
to handler functions in lines 36-39. Finally, the message loop to process events
that in turn call the function to handle user input and render the graphics is done
with the call to <TT>auxMainLoop</TT> in line 40. The function <TT>auxMainLoop</TT>
never returns, and the program must be terminated with an <TT>exit()</TT> call in
one of the handler functions.</P>
<P>This is a very simple introduction to OpenGL. A lot of the information simply
cannot be covered in one chapter. Each function I have shown has many variants and
options. The OpenGL library itself has many functions for coloring, shading, and
three-dimensional graphics tricks that will certainly make programming a joy. Unfortunately,
you need a fast Linux box to really take advantage of some the advanced features.
Speaking of advanced features, forgive me for not discussing what you can do with
OpenGL programming, but lack of time and space in a Linux book simply makes it impossible
to deviate into this arena. A topic for this type of programming would take at least
a book (or two) on its own.</P>
<P>For more information on the OpenGL standard libraries, check out the files at
the Web site at</P>
<PRE><FONT COLOR="#0066FF">http://www.sgi.com/Technology/OpenGL
</FONT></PRE>
<P>You'll find a FAQ and lots of sample code. The sample programs within the Mesa
package run quite slowly with a 486 processor running at 100MHz. The capability to
write portable OpenGL code on a Linux machine, however, makes the slow speed a minor
inconvenience when you consider the price of a Sun or Iris workstation.
<H3 ALIGN="CENTER"><A NAME="Heading20<FONT COLOR="#000077">Summary</FONT></H3>
<P>There you have it. We covered four programs and packages that are available on
Linux for performing graphical image processing. The pbm toolkit is useful when you
want to manipulate images from within programs. For interactive image editing, try
using the xv program. Three-dimensional viewing and creating of models is possible
with the Geomview program. Finally, you can use the Mesa libraries for writing OpenGL
code that is portable for other UNIX and Windows NT systems with installed OpenGL
libraries.
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -