A maior rede de estudos do Brasil

Grátis
433 pág.
Apostila PyOpenGL

Pré-visualização | Página 9 de 50

color is black. 
 28
pixels to the right and 100 pixels down from the screen origin.13 If you run the program, 
you should see a graphics window in slightly different location. 
 
 Figure 4.3 
 
Here is the complete listing of our modified first program. 
 
# First Python OpenGL program 
# ogl1.py 
 
from OpenGL.GLUT import * 
from OpenGL.GL import * 
from OpenGL.GLU import * 
 
def draw(): 
 glClear(GL_COLOR_BUFFER_BIT) 
 glutWireTeapot(0.5) 
 glFlush() 
 
glutInit(sys.argv) 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) 
glutInitWindowSize(250, 250) 
glutInitWindowPosition(100, 100) 
glutCreateWindow("My Second OGL Program") 
glutDisplayFunc(draw) 
glutMainLoop() 
 
# End of program 
 
13 In computer graphics, the origin is by default in the upper left corner of the display screen and 
uses positive numbers down the y-axis and across the x-axis. You can change this to Cartesian 
coordinates easily in OpenGL with the origin in the center of the screen. We'll do that a bit later. 
 29
Section 4.3 Odds, Ends, and Terminology 
 
 Programming can be very frustrating. Computers (at least the ones we use now) 
are not intelligent. They will do exactly what we tell them to do, but not necessarily what 
we want them to do… and that's assuming that we have not made any errors in 
programming syntax or grammar. 
 
There are two major sources of errors in programming. The first concerns visible 
errors in the spelling of key words, punctuation, and/or grammar. These errors are 
placed in the category of "syntax" errors. The program simply won't run until all syntax 
errors are corrected. Fortunately, we are often given information by our programming 
editor or the Python interpreter about the nature of the syntax error and we can usually 
fix these errors rather easily. If the syntax error is not found in the line of code specified 
by the interpreter, it will usually be found in the line immediately preceding the "flagged" 
line. Often several lines will be listed as having errors. Such a complex listing is called 
a "traceback" and will include a detailed history of how, where, and in which modules the 
error occurred. In such instances, look at the very last line in the list of errors. This line 
will usually contain the offending code and fixing this line may repair the damage. 
 
The second source of errors is more insidious and far more frustrating. Your 
program seems to run and will not generate error messages, but you don't get the 
results you expect. This may mean that the program IS running correctly, but you need 
to rethink your expectations. You should remember that computers will not always do 
what you want them to do, but they WILL always do what you tell them to do. Such 
errors are found in the logic of your program and can be very difficult to trace. If you are 
typing a program from a code listing that is known to work,14 then you have forgotten to 
type something correctly. When you find and correct the error, the program will run 
properly and it may be something as simple as forgetting a line of code or forgetting to 
use a closing brace in a function. If you are creating your own program from "scratch" or 
from the PySkel.py template we'll be using later, then you may have to look more 
closely. Common problems in Python include improper usage of if conditional 
statements, incorrect structure in loops, incorrect indentations in loops, functions, and if 
statements, and improper usage of OpenGL commands. Fixing these errors may involve 
rethinking the problem. Often, using a print statement to display the values of 
variables at appropriate times will help immensely. The best weapons against 
programming errors of any kind are patience, clear thinking, and experience!15 
 
Terminology is important in any field, not just in computer science. Sometimes 
fields (such as education!) are unnecessarily burdened with terminology, but for the most 
part, a properly defined vocabulary unique to a subject area is the most efficient way to 
teach, learn, and communicate with others in that discipline. Much of the terminology in 
computer science has become mainstream since the proliferation of computers into 
nearly every home. There are some vocabulary terms that we need to define, though, to 
avoid confusion in this course. Teachers tend to repeat themselves (more often as we 
age…), so I may define some of these terms again (and again) throughout the text. If 
so, simply nod your head and remember the word(s) and meaning(s)! 
 
 
14 Hopefully ALL programs in this text fall under this category! 
15 These are outstanding attributes to have in ANY walk of life. 
 30
First, we need to discuss the distinction between functions in computer science 
and functions in mathematics. In computer science, sections of code that are "set aside" 
to perform a specific task are called functions (or archaically, subroutines). A function is 
a code segment that is designed to calculate a value and "return" that value to the main 
program. Buttons such as sin() or tan() on a calculator are an example of a function. In 
Python, we might have something like this: 
 
def sqr(x): 
 n = x*x 
 return n 
 
print sqr(5) 
 
# End Program Listing 
 
 This small piece of code will actually run!16 We define a function sqr using def 
sqr(x):. This function takes an argument (a number we send it through code) and 
stores it in the variable x, which is then used in the function by the line n = x*x. The 
return n command does exactly that: it returns the value of n to the program. In this 
example, we have defined a squaring function and all we have to do to use this new 
function is to issue a sqr(j) command, where j is a number we want to square. So, a 
function in Python returns a value. A function can also be a code segment that is 
designed to perform a task, but it does not necessarily perform a calculation (again, 
archaically called a subroutine). The def draw(): function in the program in this 
chapter is such an example. In modern terminology, all blocks of code that are set aside 
to perform a task are called functions. 
 
 Functions in mathematics are analogous to functions in programming. A function 
in mathematics is an operation, action, or process that converts one or more numbers 
into another (probably different) number.17 The key to a mathematical function is that 
when we supply a number or numbers as input, we will get only one unique output.18 
You might think that this definition is restrictive and that we will be unable to produce 
plots of 2D curves such as circles (which fail the vertical line test) and 3D objects such 
as spheres (ditto). The short answer is that we won’t use a single function for these 
objects. We will use combinations of equations in parametric or polar form to produce 
any plot we choose. 
 
Another important term is the concept of iteration. Iteration is the process of 
repeating the same calculation(s) a specified or perhaps indeterminate number of times 
using a loop structure. Usually the repeated calculations are performed on a 
mathematical function or set of functions. One key to iteration is that the result of the 
previous computation is used as the input for the next computation. This is analogous to 
starting with a value and repeatedly pressing the "sqrt()" or "sin()" button on a calculator. 
 
 
16 Use "File|Save As" and name the program something like "sqr.py". 
17 Devaney, Robert L. (1990). "Chaos, Fractals, and Dynamics: Computer Experiments in 
Mathematics." Addison-Wesley Publishing Co. 
18 Hence the vertical line test used on graphs in algebra.

Crie agora seu perfil grátis para visualizar sem restrições.