A maior rede de estudos do Brasil

433 pág.
Apostila PyOpenGL

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

You may encounter these and other terms again as we journey through this text. 
If it sounds as if I’m defining for the first time a previously defined term or phrase, simply 
bear with me. The more times you read a term and perform a task, the more likely you 
are to remember the term and learn the task. Also, I’m a teacher (and I’m old) and I tend 
to repeat things repeat things. 
Section 4.4 Conclusion 
 In this chapter we introduced both the Python interactive interpreter and our first 
OpenGL based program. We also briefly discussed programming errors. Always 
remember that you MUST be careful when programming. As I stated earlier, a Python 
program, or any program in any language, will not always do what you want it to do, but 
it WILL always do what you TELL it to do. Pay strict attention to both syntax errors and 
logical errors! Be prepared to exercise patience and thought when attempting to fix 
programs that are not running correctly or perhaps not running at all! 
 We also introduced some terminology that we'll be using throughout the text. 
This terminology should not be considered final or complete. We will probably add to 
your vocabulary as we progress in knowledge and no doubt many of the terms will be 
visited more than once! 
 The following exercises are designed to allow you to explore the code in our first 
OpenGL program. You probably want to keep your original program intact, so open it 
again (if it isn't open) and use "File" and "Save As" to save it under a new name (ending 
in ".py"). You can then experiment with the new program. Also, you'll notice that most 
programming editors such as DrPython can have several programs loaded at once. 
Under the menu labels or icons, you should see a tab or tabs which contain the names 
of the program or programs currently open. You can click on any of the tabs to make 
that particular program active and ready to edit/run. 
1) Change the caption to display your name instead of "My First OGL 
2) Change the initial window location to something other than (100, 100). Can you 
predict where the window will appear based on the ordered pair of numbers? 
3) Change the initial window size to (400, 400). Try various values and see what 
happens. The numbers do not have to be equal, although you should probably 
try to avoid negative numbers and/or zero. Try a tall narrow window or a short 
wide display. Which number controls the width and which the height? 
4) Change the size of the teapot from (0.5) to larger and smaller values. What 
5) Comment the glutWireTeapot command to disable it (How?). Create a blank 
line below it and add this line: glutWireSphere(0.5, 10, 10) 
a) Change the 0.5 to 0.75 and see what happens. What does this value do? 
b) Change the line to: glutWireSphere(0.75, 25, 25) 
c) What do the last two values do? 
d) Change the line to: glutWireCube(1.0) 
e) Change the line to: glutWireTetrahedron() 
f) Change the word Wire to Solid in each of the commands above. 
 There are additional GLUT geometric shapes that we’ll explore later. 
6) Research “iteration”. How is it used in computer science? Are there particular 
problems that can be more easily solved by iteration than by other means? Also 
research “recursion”. Are iteration and recursion the same? If not, how are they 
7) Research “function” and “subroutine”. Compare the definitions you find with the 
definitions given in this text. 
8) You’ll need a calculator for the next few exercises. Type in a number larger than 
1 (such as 5). Repeatedly press the sqrt() button. Do you eventually reach a 
value that doesn’t change? What is this value? Do you think this is correct? Try 
another value > 1 and see if you get the same result. 
9) Now type in a positive19 value less than 1. Repeatedly press the sqrt() button. 
Do you eventually reach a value that doesn’t change? What is this value? 
Again, do you think this is correct? Try another positive value less than 1 and 
see if you get the same result. 
10) Type in another value and repeatedly press the cos() button. Do you eventually 
reach a value that doesn’t change? What is this value? This value is the 
solution to what function? Try another number and see if you get the same 
11) Try the sin() and tan() buttons in the same manner. Do you get the same 
results? Why or why not? 
12) Try running the program from this chapter again, but this time, use (.5) instead of 
(0.5) for the size of the teapot. What happens? Do we need to include the 
leading zero to the left of the decimal? Some languages require a leading zero in 
front of decimal values between -1.0 and 1.0. 
19 Why a positive value? 
Chapter 5 2 Dimensional Graphics 
 When you stop and think about it, all computer graphics are 2 dimensional.1 We 
can create the illusion of 3D (and beyond) by the proper placement of pixels, objects, 
and the use of visual cues to “trick” your mind into seeing depth, but essentially we are 
still plotting points on a 2D screen. With this concept in mind, it makes sense to start 
with “true” 2D graphics and we’ll begin by plotting points. 
Section 5.1 Plotting Points 
 One of the basic functions of any graphics language is to manipulate individual 
points or pixels within the graphics window. We need to have complete control over 
where the points are plotted, the point size, and the point color. If we can control the 
plotting and characteristics2 of individual points, then there is nothing we can’t draw! 
 Our first program in this chapter will be a simple exercise in setting up the 
graphics window so that it behaves much like the coordinate system you learned in 
algebra class. As stated in an earlier chapter, the origin in a graphics window is usually 
in the upper left hand corner. We would like to move the origin to the center of the 
graphics display window. Once we have established the origin, it should be simple to 
plot points where we please. 
 Start DrPython or your Python editor and enter the following program. Make 
certain that you save the program in your directory with a “.py” suffix or ending. I’ll 
suggest a name for each program in the first remark3 statement, but feel free to choose 
your own name if you wish. Remember to pay strict attention to indenting! 
# PyPoints.py 
# Setting a coordinate system with central origin 
from OpenGL.GL import * 
from OpenGL.GLU import * 
from OpenGL.GLUT import * 
import sys 
def init(): 
 glClearColor(0.0, 0.0, 0.0, 1.0) 
 gluOrtho2D(-1.0, 1.0, -1.0, 1.0) 
def plotpoints(): 
 glColor3f(1.0, 0.0, 0.0) 
 glVertex2f(0.0, 0.0) 
1 Unless you have a holographic display? Do you? Guess what I want for Christmas? 
2 Sometimes called attributes 
3 Since I am explaining the first programs in great detail, I’ll keep remark statements out of the 
program listing at this point in the text in order to avoid clutter. 
def main(): 
 glutCreateWindow(“Plot Points”) 
# End of Program 
 When you run the program, assuming you have no errors, you should see a 
graphics window with a single tiny red dot in the center (at the origin!). Not very exciting 
(yet), but it’s an important “point”.4 
 Let’s look at the listing in detail. The first few lines we’ve seen: 
# PyPoints.py 
# Setting a coordinate

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