没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Python Programming in OpenGL
A Graphical Approach to Programming
Stan Blank, Ph.D.
Wayne City High School
Wayne City, Illinois
62895
April 8, 2008
Copyright 2008
2
Table of Contents
Chapter 1 Introduction......................................................................................4
Chapter 2 Needs, Expectations, and Justifications ..........................................6
Section 2.1 What preparation do you need? ...............................................6
Section 2.2 What hardware and software do you need?.............................6
Section 2.3 My Expectations.......................................................................7
Section 2.4 Your Expectations ....................................................................7
Section 2.5 Justifications.............................................................................7
Section 2.6 Python Installation....................................................................8
Exercises .......................................................................................................9
Chapter 3 Your First Python Program............................................................11
Section 3.1 Super-3 Numbers...................................................................11
Section 3.2 Conclusion .............................................................................19
Exercises .....................................................................................................19
Chapter 4 Your First OpenGL Program..........................................................21
Section 4.1 The Interactive Python Interpreter..........................................21
Section 4.2 Introducing Python OpenGL...................................................22
Section 4.3 Odds, Ends, and Terminology................................................27
Section 4.4 Conclusion .............................................................................29
Exercises .....................................................................................................29
Chapter 5 2 Dimensional Graphics ................................................................31
Section 5.1 Plotting Points ........................................................................31
Exercises .....................................................................................................35
Section 5.2 Plotting 2D Functions .............................................................39
Exercises .....................................................................................................42
Sections 5.3 Parametric Equations.............................................................48
Exercises .....................................................................................................51
Section 5.4 An Example from Physics ......................................................63
Exercises .....................................................................................................72
Section 5.5 Polar Coordinates...................................................................78
Section 5.6 Conclusion .............................................................................87
Exercises .....................................................................................................88
Figures for Exercises 2-15...........................................................................93
Chapter 6 Patterns and Chaos in 2 Dimensions ............................................97
Section 6.1 PySkel ....................................................................................97
Section 6.2 Some Interesting Patterns......................................................99
Exercises ...................................................................................................104
Figures for exercises 7, 8, 9, and 10..........................................................109
Section 6.3 The Chaos Game.................................................................110
Exercises ...................................................................................................122
Exercises ...................................................................................................131
Exercises ...................................................................................................141
Section 6.6 Predator-prey Relationships.................................................146
Exercises ...................................................................................................150
Chapter 7 Strange Attractors and Beautiful Fractals....................................152
Section 7.1 Lorenz and the Weather.......................................................152
3
Exercises ...................................................................................................160
Section 7.2 Phase Portraits and Paint Swirls ..........................................165
Exercises ...................................................................................................168
Section 7.3 Mira (Look?).........................................................................170
Exercises ...................................................................................................172
Section 7.4 The 3-Body Problem ............................................................173
Exercises ...................................................................................................176
Section 7.5 Newton’s Method and the Complex Plane ...........................181
Exercises ...................................................................................................191
Addendum: ................................................................................................201
Addendum II: .............................................................................................201
Exercises ...................................................................................................208
Section 7.7 Explorations with the Mandelbrot Set...................................220
Exercises ...................................................................................................238
Chapter 8 2D Animation...............................................................................244
Section 8.1 Follow the Bouncing Ball......................................................244
Exercises ...................................................................................................253
Section 8.2 A Little Gravity!.....................................................................259
Exercises ...................................................................................................262
Section 8.3 A Little MORE Gravity... a 2-Body Simulation ......................263
Exercises ...................................................................................................277
Section 8.4 The REAL 3 Body Problem ..................................................279
Exercises ...................................................................................................289
Section 8.5 From 3Body to NBody Using Arrays.....................................292
Exercises ...................................................................................................305
Section 8.6 Navigating the Stars............................................................307
Exercises ...................................................................................................318
Chapter 9 3D and 3D Animation .................................................................320
Section 9.1 Rotating Objects in Space...................................................320
4
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 point so that we may see how far we’ve traveled. We’ll go
back to about 1980 and the first computer programming class in our high school. We
were the proud “owners” of a single new Commodore VIC-20 and an old black and white
TV that served as a monitor (almost). There were about 5 or 6 students in the class and
we began to learn to program in BASIC.
1
There were no graphics worth mentioning and
the only thing I remember is that we made such a fuss about getting the VIC to find the
prime numbers from 2 to 997. If memory serves, it took about 30 minutes for the VIC to
run this “sophisticated”
2
prime finding program. We had no disk storage and the memory
in the computer was 4K.
3
I think the processor speed was about 1 Mhz and might have
been much lower
4
, but we didn’t care because we were computing!
The next step occurred the following year when we purchased 10 TI 99/4a
computers for $50 each.
5
They were not much better than the VIC-20, but we at least
were able to store programs using cassette tape recorders. Cassette storage wasn’t
much fun, extremely slow, and unreliable. I remember some slow, crude rudimentary
graphics, but nothing that stands out in my mind. Finally, in 1982, things began to get
exciting. We were able to purchase several Apple II+ computers with disk drives. We
thought we were in heaven! The Apples were neat looking, nearly indestructible
6
, and
much faster than anything we had used previously. Plus, they could actually produce
usable 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. We
could store 140K of programs on one floppy disk!
7
Our prime number generator took
only 53 seconds on the Apple, which was over 30 times faster than the VIC- 20. Had I
been acquainted with my friend George Francis at that time, we would have been able to
do even more with these dependable machines.
8
Our final conversion was to the PC platform in 1987-88. We now had a lab of 12
true-blue IBM PC’s with color monitors and hard drives running Windows 3.0 (or was it
1
BASIC is a computer language… Beginner’s All-Purpose Symbolic Instruction Code. It has
been much maligned over the years; unjustly in my opinion.
2
Here, “sophisticated” means ‘brute strength and ignorance”. But the program worked and we
were thrilled!
3
This is 4 thousand bytes of memory. Compare this to my current laptop which has 2 BILLION
(gigabytes) of memory.
4
Again, my current laptop has a processor that runs at 2 Ghz, over 2000x faster!
5
These were truly awful computers. Texas Instruments introduced them at a price of over $1000
and ended up selling them at Wal-Mart for $49.95. I’m not certain they were worth that much.
6
I personally saw one dropped on a concrete sidewalk. It bounced once or twice and worked fine
for several years afterward. No, I wasn't the one who dropped it.
7
Again, my trusty laptop has a 60 gigabyte hard drive. That’s 60 billion bytes. I also have a
portable USB "diskless" drive that holds nearly 2000x the capacity of that Apple disk!
8
UIUC Math Prof. George K. Francis had a lab of Apples then that did some amazing graphics
with a 1983 Forth compiler written by one of his colleagues. It would have been nice to have that!
5
3.1?). By today’s standards, they were painfully slow, but at the time we thought that we
were cutting edge. Memory was now in the megabyte range and processor speed was
over 10 Mhz. I remember plotting Mandelbrot sets in less than 10 minutes, which was
relatively fast for that era. We have steadily improved to our present lab setup of PC
machines running nearly at 1 Ghz (or faster) with at least 128 mb of RAM (or more) and
dedicated video cards for graphics.
9
The computers in our labs are supercomputers
compared to where we started! In fact, if we were to take the computer in front of you
back 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 programming
class curriculum followed the lab, as you might guess. We would spend 3 quarters
learning to program and then the 4
th
quarter was reserved for student projects.
Invariably, once graphic capabilities were available, all 4
th
quarter projects would involve
graphics. The first graphics on the Apple were slow and rather crude by present
standards. They were barely interactive, if at all. Even on our first PC’s it would take
several minutes to display minimal fractal images. Not so today. With the computers we
have in our lab we can create sophisticated graphics that can be manipulated in real-
time… something we didn’t even dream of back in 1980! It only took me 20 years to
realize that my students were trying to tell me something! For the past 5 years we have
concentrated on learning computer programming through computer graphics and that is
what you will be doing this year. Learning how to program is hard work, but at the same
time, it is very rewarding and can be great fun!
So, if a picture is worth a thousand words, how much more valuable is a
changeable, interactive, creative graphics scene? You can graph mathematical
functions in 2D, 3D, and even 4D. You can create true stereo images! You can design
programs to simulate real-world events and you can manipulate the laws of physics and
create your own worlds. If you are an artist, the computer becomes your easel. If you
like games, you can program your own according to your own specifications. The
computer can become a window into your mind and your limitations are governed by
your 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 ask
the folks at PIXAR.
10
9
Previous computers used the cpu and onboard memory for graphics. This made them slow. A
dedicated 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 of
video memory alone… more than the system memory of many computers.
10
You know, the people who made “The Incredibles” and other such movies.
剩余323页未读,继续阅读
资源评论
- starjjn2014-08-02关于python和OpenGL的书,英文文字版,帮助很大
KarlLok
- 粉丝: 4
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功