Python Programming In OpenGL

Transcription

Python Programming in OpenGLA Graphical Approach to ProgrammingStan Blank, Ph.D.Wayne City High SchoolWayne City, Illinois62895October 6, 2009Copyright 2009

2Table of ContentsChapter 1Introduction.6Chapter 2Needs, Expectations, and Justifications .8Section 2.1What preparation do you need? .8Section 2.2What hardware and software do you need? .8Section 2.3My Expectations .9Section 2.4Your Expectations .9Section 2.5Justifications.10Section 2.6Python Installation .11Exercises .12Chapter 3Your First Python Program .13Section 3.1Super-3 Numbers .13Section 3.2Conclusion .21Exercises .21Chapter 4Your First OpenGL Program.23Section 4.1The Interactive Python Interpreter .23Section 4.2Introducing Python OpenGL.24Section 4.3Odds, Ends, and Terminology .29Section 4.4Conclusion .31Exercises .31Chapter 52 Dimensional Graphics .33Section 5.1Plotting Points .33Exercises .37Section 5.2Plotting 2D Functions .41Exercises .44Sections 5.3 Parametric Equations .50Exercises .53Section 5.4An Example from Physics .65Exercises .74Section 5.5Polar Coordinates.80Section 5.6Conclusion .89Exercises .90Figures for Exercises 2-15 .95Chapter 6Patterns and Chaos in 2 Dimensions .99Section 6.1PySkel .99Section 6.2Some Interesting Patterns.101Exercises .106Figures for exercises 7, 8, 9, and 10.111Section 6.3The Chaos Game .112Exercises .124Section 6.4The Barnsley Fern.129Exercises .133Section 6.5Chaos and the Logistic Map .136Exercises .143Section 6.6Predator-prey Relationships .148Exercises .152

3Chapter 7Strange Attractors and Beautiful Fractals .154Section 7.1Lorenz and the Weather.154Exercises .162Section 7.2Phase Portraits and Paint Swirls .167Exercises .170Section 7.3Mira (Look?) .172Exercises .174Section 7.4The 3-Body Problem .175Exercises .178Section 7.5Newton’s Method and the Complex Plane .183Exercises .193Addendum: .203Addendum II: .203Section 7.6The Julia Set .205Exercises .211Section 7.7Explorations with the Mandelbrot Set .222Exercises .240Chapter 82D Animation.246Section 8.1Follow the Bouncing Ball .246Exercises .255Section 8.2A Little Gravity! .261Exercises .264Section 8.3A Little MORE Gravity. a 2-Body Simulation .265Exercises .279Section 8.4The REAL 3 Body Problem .281Exercises .291Section 8.5From 3Body to NBody Using Arrays.294Exercises .307Section 8.6Navigating the Stars .309Exercises .320Chapter 93D and 3D Animation .322Section 9.1Rotating Objects in Space.322Exercises .330Section 9.2Real Time Interactive Computer Animator (RTICA) .336The German Bell.345Exercises .350illiTorus .353Exercises .359Chapter 10 Animation and Display Lists .370Electron Orbitals .370The Quaternion Julia Set .375Alternate Quaternion Julia Set and Mandelbrot Set.381Chapter 11 Miscellaneous Programs .388The Random Walk .388The 3D Sierpinski Sponge .391Rendering Teapots .393

4A Midpoint Conjecture .397Fog .405PyLorenz .409Nate Robins and Multiview .415Chapter 12VPython.421The Sphere .421The Bouncing Ball.421Bouncing Ball 2.423VPython Lorenz .424VPython Mandlebrot .426Index .428

5My heartfelt thanks to Professor George K. FrancisWithout his inspiration, interest, and mentoring, NONE of this would havebeen possible

6Python Programming in OpenGL/GLUTChapter 1IntroductionBefore we begin our journey with Python and OpenGL, we first need to go backin time. History serves many purposes, but one of its more important functions is toprovide us with a reference point so that we may see how far we’ve traveled. We’ll goback to about 1980 and the first computer programming class in our high school. Wewere the proud “owners” of a single new Commodore VIC-20 and an old black and whiteTV that served as a monitor (almost). There were about 5 or 6 students in the class andwe began to learn to program in BASIC.1 There were no graphics worth mentioning andthe only thing I remember is that we made such a fuss about getting the VIC to find theprime numbers from 2 to 997. If memory serves, it took about 30 minutes for the VIC torun this “sophisticated”2 prime finding program. We had no disk storage and the memoryin the computer was 4K.3 I think the processor speed was about 1 Mhz and might havebeen much lower4, but we didn’t care because we were computing!The next step occurred the following year when we purchased 10 TI 99/4acomputers for 50 each.5 They were not much better than the VIC-20, but we at leastwere able to store programs using cassette tape recorders. Cassette storage wasn’tmuch fun, extremely slow, and unreliable. I remember some slow, crude rudimentarygraphics, but nothing that stands out in my mind. Finally, in 1982, things began to getexciting. We were able to purchase several Apple II computers with disk drives. Wethought we were in heaven! The Apples were neat looking, nearly indestructible6, andmuch faster than anything we had used previously. Plus, they could actually produceusable GRAPHICS. Not just crude blocky stuff (which you could choose if you wanted but why?), but nice points and lines on the screen! These Apples had 64K of memory(all you could ever use or so we thought) and the disk storage was amazing. Wecould store 140K of programs on one floppy disk!7 Our prime number generator tookonly 53 seconds on the Apple, which was over 30 times faster than the VIC- 20. Had Ibeen acquainted with my friend George Francis at that time, we would have been able todo even more with these dependable machines.8Our final conversion was to the PC platform in 1987-88. We now had a lab of 12true-blue IBM PC’s with color monitors and hard drives running Windows 3.0 (or was it1BASIC is a computer language Beginner’s All-Purpose Symbolic Instruction Code. It hasbeen much maligned over the years; unjustly in my opinion.2Here, “sophisticated” means ‘brute strength and ignorance”. But the program worked and wewere thrilled!3This is 4 thousand bytes of memory. Compare this to my current laptop which has 2 BILLION(gigabytes) of memory.4Again, my current laptop has a processor that runs at 2 Ghz, over 2000x faster!5These were truly awful computers. Texas Instruments introduced them at a price of over 1000and ended up selling them at Wal-Mart for 49.95. I’m not certain they were worth that much.6I personally saw one dropped on a concrete sidewalk. It bounced once or twice and worked finefor several years afterward. No, I wasn't the one who dropped it.7Again, my trusty laptop has a 60 gigabyte hard drive. That’s 60 billion bytes. I also have aportable USB "diskless" drive that holds nearly 2000x the capacity of that Apple disk!8UIUC Math Prof. George K. Francis had a lab of Apples then that did some amazing graphicswith a 1983 Forth compiler written by one of his colleagues. It would have been nice to have that!

73.1?). By today’s standards, they were painfully slow, but at the time we thought that wewere cutting edge. Memory was now in the megabyte range and processor speed wasover 10 Mhz. I remember plotting Mandelbrot sets in less than 10 minutes, which wasrelatively fast for that era. We have steadily improved to our present lab setup of PCmachines running nearly at 1 Ghz (or faster) with at least 128 mb of RAM (or more) anddedicated video cards for graphics.9 The computers in our labs are supercomputerscompared to where we started! In fact, if we were to take the computer in front of youback to 1980, it would have been one of the fastest on the planet!So this was a brief history of our high school computer lab. The programmingclass curriculum followed the lab, as you might guess. We would spend 3 quarterslearning to program and then the 4th quarter was reserved for student projects.Invariably, once graphic capabilities were available, all 4th quarter projects would involvegraphics. The first graphics on the Apple were slow and rather crude by presentstandards. They were barely interactive, if at all. Even on our first PC’s it would takeseveral minutes to display minimal fractal images. Not so today. With the computers wehave in our lab we can create sophisticated graphics that can be manipulated in realtime something we didn’t even dream of back in 1980! It only took me 20 years torealize that my students were trying to tell me something! For the past 5 years we haveconcentrated on learning computer programming through computer graphics and that iswhat you will be doing this year. Learning how to program is hard work, but at the sametime, it is very rewarding and can be great fun!So, if a picture is worth a thousand words, how much more valuable is achangeable, interactive, creative graphics scene? You can graph mathematicalfunctions in 2D, 3D, and even 4D. You can create true stereo images! You can designprograms to simulate real-world events and you can manipulate the laws of physics andcreate your own worlds. If you are an artist, the computer becomes your easel. If youlike games, you can program your own according to your own specifications. Thecomputer can become a window into your mind and your limitations are governed byyour imagination. What you can envision, you can create! Now how cool is that?Oh, I forgot to say that you can make a fantastic living doing this stuff just askthe folks at PIXAR.109Previous computers used the cpu and onboard memory for graphics. This made them slow. Adedicated graphics board handles most of the work and has its own much speedier memory.This allows us to create some rather fancy graphics. By comparison, my laptop has 256 mb ofvideo memory alone more than the system memory of many computers.10You know, the people who made “The Incredibles” and other such movies.

Chapter 2Needs, Expectations, and JustificationsSection 2.1 What preparation do you need?In order to successfully complete this class, you will need to have someknowledge of computer operations as a prerequisite. This class is NOT an introductionto computers or a computer concepts class. This is a programming class and I expectthat you already know something about computers. You should already be able to use acomputer to do tasks such as word processing, gaming, and internet searches. Youshould know how to install programs and you should know how to follow directions. Youdon’t need to know how to program, although it’s certainly OK if you have someprogramming experience.In terms of knowledge, you should have some familiarity with algebra andgeometry. You don’t have to be an “A” student, but you should be comfortable with theconcept of variables, equations, points, lines, angles, and coordinates in the x, y, and zaxes. Also, you should have some knowledge of physics and physical science,particularly the equations and concepts for location, distance, velocity, and acceleration.If you are uncomfortable at this point, take a deep breath, relax, and simply be preparedto work a bit harder and I think you’ll be fine.Section 2.2 What hardware and software do you need?If you are physically in my computer science class, then your computer andsoftware needs are already met by our lab computers. If you are reading this on yourown, then I should take some time to tell you what minimum hardware and software youwill need to complete the examples in the text. At a minimum, you should have acomputer with at least 128 mb of RAM and a dedicated video card with at least 32 mb ofvideo RAM. Most current minimum computer systems should suffice.1 If your computerdoesn’t quite meet the minimum requirements, then give the programs a try anyway.They’ll probably run fine, albeit more slowly.As far as software is concerned, we will be using a programming language calledPython with the PyOpenGL module.2 We will also use an open source programmingeditor called DrPython, although other fine open source editors such as Scite areavailable. Again, if you are in my class, these programs are already on the labcomputers. If you are studying on your own, the programs may be downloaded from theinternet at no cost.3 I use both Windows and Linux without a real preference, although itis probably easier to use the Windows platform in terms of the necessary software1More is better!You will need to have OpenGL and GLUT installed on your computer. Windows machinesalready have OpenGL installed. You may need to search online for GLUT and install theglut32.dll file in your system directory.3Use your search engine. DrPython needs another program called wxWindows, also freelyavailable. If you need help with the installations, seek advice from your teacher or friendlyneighborhood computer geek. These programs are “Open Source”, meaning that they are free.2

9installation. All the program examples in this text will work on either platform as long asthe proper software is installed.4Section 2.3 My ExpectationsMy expectations are simple. I expect you to make an honest effort to learn thematerial. If you feel that you are deficient in an area, then take some time andstrengthen your knowledge. I expect you to try the programs in this text, to modify theprograms, and to create some programs of your own. The exercises at the end of eachsection or chapter will help achieve fluency in programming and should be attemptedwith honest effort.5 If you have “bugs”6 in your program, try to find them yourself ratherthan immediately asking for help. Even though I enjoy helping my students, it gets a bittiresome when a student insists that “the program you gave us doesn’t work” after about10 seconds of effort. At that point, the student will sit back and expect me to fix theproblem. You’ll learn much more if you spend the time to find and repair your ownmistakes.I will start out by making a valiant effort to explain as much as possible in eachchapter, section, and individual program. But as we progress through the text, I plan toexplain less and less (always difficult for a teacher I should explain less? Ask me thetime and I’ll tell you how to make a watch!) At some point you, the student, shouldbecome self-sufficient and research for yourself various topics of interest or conceptsthat you do not understand. When you reach the point where you are an independentlearner, then you have attained an elusive, but important goal. Every individual shouldstrive to become an independent learner and it should be the goal of every teacher to tryto help students attain this coveted plateau. Whether or not I am successful in this goalwill depend on whether or not YOU are successful in becoming a self-sufficientprogrammer.Section 2.4 Your ExpectationsYou should have some realistic expectations for the class. You will most likelyNOT be recruited by Electronic Arts7 following the completion of this course. You will beable to create some interesting interactive computer generated graphics, however. Youwill also have a fairly solid background in basic programming and a platform for furtherlearning, either independently or formally. Since this course was started almost 30 yearsago, over 40 former students have gone on to rewarding careers in the computerscience field. So if this type of work interests you, then you may have found the start toa great career opportunity! I am not trying to toss out clichés, but you WILL get out ofthis course a level of expertise that will correlate highly with the effort you put into tryingto understand and utilize the concepts.4For Mac enthusiasts, as long as you have Python, PyOpenGL, and a programming editor youshould also be able to run the example programs.5In other words, as much as possible do your own work! If you get help from someone orsomewhere, and we all do, acknowledge the help. Claiming original work in programs or problemsolutions that are actually not your own is dishonest at best6You should look up the history of how programming errors came to be called “bugs”.7A company specializing in interactive sports games.

10Section 2.5 JustificationsBack in the early days of our computer science curriculum, graphicsprogramming was synonymous with gaming and we didn't buy those computers so thatstudents could play games.8 Gradually as time passed and the personal computer worldbecame more graphically oriented thanks to the Mac and Windows operating systems,computer graphics became more mainstream. We began to realize that being able toproduce computer graphics meant that not only did you have to understand somethingabout programming, you also had to understand something about mathematics andscience if your computer graphics were to look and behave realistically. I am stubborn,though, and it indeed took me two decades to realize that I could teach computerprogramming utilizing computer graphics. My students seem to enjoy the experienceand so do I. I don't know and don't care whether or not this is acceptable practice inother schools it works for us.The choice of Python from dozens of other languages is a personal preference.Python is a relatively simple language, but it is easily expanded through externalmodules such as the PyOpenGL module. Python is object-oriented9 and is a verypopular language. Plus, Python is freely available! The downside is that programexecution is somewhat slow10, but I believe the advantages outweigh the loss of speed.Finally, once you learn Python, it is relatively easy to go on to other languages if youdesire (and you may not. Python is a very powerful language!). See section 2.6 belowfor instructions on how to obtain a copy of Python for your computer.The choice of which graphics environment to implement was a bit easier. Thereare two major graphic API’s11 available today, DirectX and OpenGL. DirectX is awindows specific graphics API and OpenGL12 is available on all platforms. Both are freefor programmers to download and use. I personally like OpenGL13 and think it’s a bitbetter at creating and displaying graphics than DirectX, but I could be wrong.DrPython is my current Python programming editor. It is freely available andallows interactive access to the Python console. The only minor drawback is thatDrPython needs the wxWindows library to function properly, but that is also a freedownload. DrPython is actually written in Python! However, Scite, or any otherprogramming IDE that supports Python (and most of them do) will suffice. If you useanother editor, the only difference you will see is that the images in the initial chapters8I remember that one school board member didn't think that we were ready for computers in1980. I translated that to mean that HE wasn't ready for them. When we purchased thecomputers, we were expected to compute whatever that meant, and not "play".9Object-oriented programming is a mainstay of modern computer languages.10Python is an interpreted language (for the most part) rather than optimized and compiled like C.This isn't all bad, because it allows us to use Python interactively from a command prompt. Lookup interpreted languages for further information.11Computer jargon for a set of commands specific to a particular task, such as graphics.12Originally created by Jim Clark and available on Silicon Graphics (SGI) workstations as GL(graphics language). It used to be a commercial package, but was eventually made available asOpen Source software, hence OpenGL.13Prof. Francis was strong influence on my preference. He uses OpenGL/GLUT in his illiMathclasses.

11display the DrPython editor rather than the one you chose. All Python compatibleeditors will have syntax highlighting. What this means is that all valid Python commandswill appear in color, as opposed to simple black on white text. This will allow you tocheck to see if you’ve spelled the command correctly! The program listings in this textare in black type, so don’t be surprised to see blue, green, or red text (depending on theeditor) in your editor window. As in the art and science of programming, the choice of aprogramming IDE or editor is yours.There may be other modules or programs that we will use as the courseprogresses. I will introduce these when the need arises.Section 2.6 Python InstallationIf you are working in the Microsoft Windows environment, I would recommendthat you visit http://www.python.org in order to download the latest stable version ofPython.14 You will also need to visit http://pyopengl.sourceforge.net/ to download thePyOpenGL module. In addition, if you wish to install DrPython (recommended), youshould go to http://drpython.sourceforge.net/ to download this program editor. There islink on this website to download wxWindows if needed.As another option for Windows environment, I would recommend that you take aclose look at the Enthought Python distribution (http://www.enthought.com). Enthoughtprovides a complete Python package with an enormous number of libraries available,including the Scite programming editor at no charge for individuals and academic users.The only drawback to the Enthought distribution is that the installation file over 200megabytes in size, so you would need a fast internet connection to download the entirepackage.If you are working on a linux platform, check the online package repositories forPython, DrPython and/or Scite, and the PyOpenGL library. Ubuntu linux and its aptrepositories allow for easy installation of everything you need. I would also recommendthe Python Psyco compiler for Windows and/or Linux. If you are unsure about how toproceed, do some research online. There are many pages devoted to installing andusing Python and its multitude of libraries.The examples I use in this text are based on a Windows environment, but I alsohave a linux system (Ubuntu Linux) on the same computer in order to make certain theprograms run properly in both environments. My current students work entirely inUbuntu Linux, so I make certain that the programming examples are compatible withboth operating systems. Linux has a few minor quirks (features?) in certain programs,but there are no major problems.That’s enough for the preliminary information. Let’s get ready to program!14I used Python 2.5 and PyOpenGL 3.0.0a under Windows for these program examples. I knowthat earlier versions of both packages also work properly under both Windows and Linux.

12Exercises1. Go to the www.python.org website and spend some time looking at the resourcesthat are available there. You'll find various tutorials and answers to your Pythonquestions. You should bookmark this site in your browser!2. Go the www.opengl.org website and see what OpenGL is all about.3. Visit Professor Francis' home page www.new.math.uiuc.edu/ gfrancis

Oct 06, 2009 · Python Programming in OpenGL/GLUT Chapter 1 Introduction Before we begin our journey with Python and OpenGL, we first need to go back in time. History serves many purposes, but one of its more important functions is to provide us with a reference