Learning C PROGRAMMING GRAPHICS =ONTHE= AMIGA -AND- Aiarisl

Learning C PROGRAMMING GRAPHICS =ONTHE= AMIGA -AND- Aiarisl

• • II LEARNING • r-• m >- PROGRAMMING ~ Z GRAPHICS ON THE -Z C1 1 AMIGA n AND JTEI :s AIARI Sl Marc B. Sugiyama II and Christopher D. Metcalf A thorough guide to program- II ming in C. Includes tutorials on programming advanced graph- ics on the Atari ST and Amiga II personal computers. II - A COMPUTE I Books Publication $18.95 -I I I -- - ,- , II II .8 Learning C PROGRAMMING GRAPHICS =ONTHE= -AND-AMIGA AIARI Sl Marc B. Sugiyama and Christopher D. Metcalf a '..- .• 5?~~~~!!~~s~~blications/lnc One of the ABC Publishing Companies Greensboro, North Carolina 11 II II Copyright 1987, COMPUTE! Publications, Inc. All rights reserved. Reproduction or translation of any part of this work beyond that permitted by Sections 107 and 108 of the United States Copyright Act without the permission of the copyright owner is unlawful. Printed in the United States of America 10987654321 ISBN 0-87455-064-5 The authors and publisher have made every effort in the preparation of this book to insure the ac­ curacy of the programs and information. However, the information and programs in this book are sold without warranty, either express or implied. Neither the authors nor COMPUTE! Publica­ tions, Inc. will be liable for any damages caused or alleged to be caused directly, indirectly, inci­ II dentally, or consequentially by the programs or information in this book. The opinions expressed in this book are solely those of the author and are not necessarily those of COMPUTE! Publications, Inc. COMPUTE! Publications, Inc., Post Office Box 5406, Greensboro, NC 27403, (919) 275-9809, is part of ABC Consumer Magazines, Inc., one of the ABC PUblishing Com­ panies, and is not associated with any manufacturer of personal computers. Amiga and AmigaDOS are trademarks of Commodore-Amiga, Inc. Atari, ST, and TOS are trademarks of Atari Corporation. GEM is a registered trademark of Digital Research Inc. Lattice is a registered trademark of Lattice, Inc. Macintosh is a trademark licensed • to Apple Computer, Inc. MacPaint is a registered trademark of Apple Computer, Inc. Megamax is a trademark of Megamax, Inc. Microsoft and MS-DOS are registered trademarks of Microsoft Corporation. UNIX is a trademark of AT&T Bell Laboratories. ii -I Contents I Foreword ......................................... v Preface .......................................... vii 1. Introduction .................................... 1 2. Functions ...................................... 9 3. Variables, Operators, and Expressions .............. 29 4. Decision Making and Loops ...................... 53 5. Arrays ........................................ 81 6. Structures .................................... 117 7. Introduction to Graphics ........................ 159 8. Polygon Filling ................................ 173 9. Three Dimensions ............................. 199 10. Displaying Three Dimensions ................... 223 11. The z-buffer Algorithm ......................... 251 12. Clipping ..................................... 287 13. Advanced Graphics. .. 317 Appendices ..................................... 327 A. Tables of ASCII, Hex, Binary, Octal .............. 329 B. Table of C Operator Precedence ................. 333 C. Binary Numbers .............................. 335 D. Setting Up Your Programming Environment ....... 339 E. Typing and Compiling the Machine-Specific Files ... 347 F. Special Compiling Instructions .................. 361 G. Using the Graphics Library ..................... 369 H. stdio.h Functions. .. 375 I. Amiga Graphics .............................. 385 J. ST Graphics .................................. 391 K. References ................................... 399 II Glossary ........................................ 401 Index .......................................... 415 •, Disk Coupon .................................... 421 - iii -I I I -- - ,- , II II Foreword II Not only does Learning C: Programming Graphics on the Amiga and Atari ST give you the information you need to begin writ­ ing your own C programs on the ST or Amiga, but it also shows you how to translate advanced mathematical con­ cepts-the same ones professional programmers use to create graphics-into C source code. The first sections of this book explain C programming. You'll learn all about C in general and the C language con­ cepts and commands you need to program graphics on the Amiga and Atari ST. The appendices even include specific information about how to compile and link programs on a variety of compilers for the two computers. The latter sections of Learning C: Programming Graphics on the Amiga and Atari ST illustrate how mathematical concepts relate to computer graphics, and how to write C code using those concepts in creating dramatic three-dimensional graphics. All the programs work on both the Amiga and ST be­ cause of machine.c, a machine-specific library of routines that you can add to any of your C programs to create graphics. The appendices include specific instructions on how to use this powerful library. Learning C: Programming Graphics on the Amiga and Atari ST assumes you're familiar with your ST or Amiga, have a C compiler, and that you know how to program in at least one computer language. This book shows you how to write high­ quality C source code to create executable programs using the II most popular compilers for the Amiga and Atari ST. (You should, though, be familiar with your compiler's operation.) All the programs have been tested and are ready to type w in, compile, link, and use on either the Atari ST or Commo­ dore Amiga. If you prefer, you can purchase a disk which in­ cludes all the C source code and executable files by calling , 1-800-346-6767 (in N.Y. 212-887-8525) or by using the coupon in the back of this book. v II II - II II, , II - *- Preface Learning C: Programming Graphics on the Amiga II and Atari ST is intended for the programmer who is new to the C programming language. It introduces those aspects of C programming which are necessary to understand and imple­ ment the advanced graphics algorithms discussed in Chapters 7-13. In general, we've assumed you are familiar with pro­ gramming your computer-that you know how to edit a file, run programs, and use the operating system. Throughout the text we have tried to emphasize machine­ independent graphics. All of the machine-dependent functions have been isolated in the file machine.c. This means that the sample programs in this book will run on any computer, if the appropriate machine.c file has been prepared. In fact, the orig­ inal zbuf program was implemented on an Apollo graphics workstation. It should be possible to port all of the example programs to the Apple Macintosh, the IBM pc, or even other graphics workstations such as SUNs or MicroVAX lIs. You simply need the necessary machine-dependent functions. The graphics algorithms and techniques which are pre­ sented in this book are usually only found in books about ad­ vanced graphics programming. We've tried to make these algorithms and techniques more accessible to the average com­ puter programmer. You might think that we talk about every algorithm possible, but what's here is really just the tip of the iceberg. The last chapter on graphics touches on some aspects of graphics programming which haven't been discussed else­ where in the book. This gives you a glimpse of what's possi­ ble, even on a personal computer like the Atari ST or Commodore Amiga. We've listed several good sources of graphics material in Appendix K if you want to learn more. It was quite a challenge getting all of the programs to run II on both the Atari ST and the Commodore Amiga. The biggest problems were bugs in the compilers or their libraries. Often we would get the program running satisfactorily on the Amiga, only to find that it didn't compile on the Atari. After working around compiler bugs on the Atari, we would bring II vii - * the program back to the Amiga, only to find that the Atari _ "fixes" manifested new bugs in the Amiga compilers. As with any large project, there are a number of people who deserve our gratitude. We'd like to thank Orson Scott _ Card, noted science fiction author and former book editor of COMPUTE!, for his early encouragement; Steven Grady, for giving us access to USENET and the ARPA Internet during the _ summer months; our parents, for their continuing support; and . a final thanks to our readers-Michael Littman, Lisa Gragg, Steve Roth, Byron Servies, and Rob Woodard-whose com- ments were invaluable in the weeks just before our deadline. Christopher Metcalf Marc Sugiyama II II viii II - -II -II ~~~-------- Are you interested in graphics? Have you seen three­ dimensional graphics displays, like the ones used in TRON or The Last Starfighter, and wondered, "Why can't my personal computer do something like that?" Yesterday's micros just didn't have enough horsepower. Today, things are different. Smaller personal computers, such as the Commodore Amiga and the Atari ST, have more power than the minis of ten years ago. This book will show you the techniques you need to mas­ ter sophisticated computer graphics. You'll learn about two­ and three-dimensional graphics programming, line drawing and polygon filling, and much more. All of the example pro­ grams are implemented in the popular C programming lan­ guage. And since Learning C: Programming Graphics on the Amiga and Atari ST is written for the first-time C programmer, you'll learn C as well as how to program graphics. Equipment and Software Learning C: Programming Graphics on the Amiga and Atari ST has sample source code for the Commodore Amiga and the Atari ST. To use the sample programs, you'll need a Commo­ dore Amiga with at least 512K. You can also use an Atari 520 or 1040 ST with either a color or monochrome monitor. The programs have been thoroughly tested and work with the Lat­ tice and Aztec C compilers on the Amiga and the Lattice, Alcyon, and Megamax C compilers for the ST. Some Definitions Before beginning with C and graphics, we'll need to review some basic definitions. The compiler is the program which translates your C program into machine language, the only language the computer can understand directly.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    436 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us