A maior rede de estudos do Brasil

Grátis
433 pág.
Apostila PyOpenGL

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

do NOT have to use DrPython as a programming editor. There are others, such as Scite 
that are free and also work well. Python comes with its own editor called Idle. You can use that. 
You can even write Python code in a text processor and run the code from the command prompt 
by typing: C:>\python prog.py where prog.py is the name of your saved program. 
4 I once worked for nearly 6 hours on a program and the power went off. I lost everything. I 
learned to save my work early and often after that. 
 25
can see whether or not you need to save your work by looking at the caption bar at the 
top of the DrPython window. If the words “untitled” or “[Modified]” appear, then save 
your work! 
 
 Let’s add more lines to our program: 
 
from OpenGL.GL import * 
from OpenGL.GLU import * 
from OpenGL.GLUT import * 
import sys 
 
 Remember that Python comes with a basic set of commands that you can 
immediately use. The import statements add extra functions to this basic set of 
commands. With the exception of import sys, these are import statements with a 
somewhat different format than the one used in the first program (import string). In 
the first program, we had to preface the “find” command with the word “string” in 
order for it to use the string module commands (string.find). In this new 
from... import * format, we can use OpenGL module commands directly without 
having to use the OpenGL.GL, OpenGL.GLU, or OpenGL.GLUT preface in a similar 
fashion as string in the Super-3 program. If we simply used import OpenGL.GL, 
then every time we used an OpenGL.GL command, we would need to write (for 
example): OpenGL.GL.glVertex2f(x, y). With the from... import * format, 
we can simply write: glVertex2f(x, y). Much simpler, don't you think?5 The final 
import sys statement provides some housekeeping tools we will need to create the 
graphics display. We will usually only need a couple of commands from this module, so 
we do not need to use the from... import * method here. Likewise, in the Super-3 
program we only needed one string function (string.find). In contrast, we would like 
ALL the OpenGL GL, GLU, and GLUT commands available to us when we program. 
Having all the OpenGL commands available will save time! 
 
 To summarize, each of the from OpenGL statements adds new commands to 
Python. There are OpenGL commands (.GL), GLU commands (.GLU), and GLUT 
commands (.GLUT). You will know when you are using commands from each specific 
OpenGL module by looking at the first few letters of the OpenGL command. For 
example, glClear() is a command from the main OpenGL (GL) module. 
gluPerspective() would be from GLU and glutWireTeapot() is from GLUT. You 
will see and use MANY examples of these commands (usually prefaced by gl, glu, or 
GLUT) throughout this text. Let's continue: 
 
def draw(): 
 glutWireTeapot(0.5) 
 glFlush() 
 
 This is new. The def keyword marks the beginning of a function block of code in 
Python. Functions are segments of code that act as a group or family of commands.6 
 
5 You may have guessed by now that the * in the from... import statement imports ALL 
commands available to that particular module. Good guess! 
6 See the "Odds, Ends, and Terminology" section in this chapter. 
 26
The name of this function is "draw" and we can call this function from within our Python 
program by using this name. Note the two indented lines after def draw():. The 
indents mark these lines as belonging to this particular function.7 The def draw() 
function does two things. First, it uses a GLUT command to draw a wire teapot with a 
size of 0.5. Second, the glFlush() statement "flushes"8 the drawing to the screen so 
we can see it. Save your work. 
 
 Finally add these lines, making certain to line them up even with the left-hand 
margin.9 
 
glutInit(sys.argv) 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) 
glutCreateWindow("My First OGL Program") 
glutDisplayFunc(draw) 
glutMainLoop() 
 
 Save your work! The first line, glutInit(sys.argv) tells Python we are 
going to be displaying GLUT style graphics. GLUT stands for "GL Utilities Toolkit" and 
provides cross-platform (linux-windows-mac) background calculations making the job of 
doing graphics MUCH easier than if we had to write this code ourselves. In other words, 
a LOT is going on "behind the scenes" here and GLUT takes care of the gory details. 
The (sys.argv)10 argument allows us to enter a command line instruction if we wish 
(we don't in this program). The next line, glutInitDisplayMode(GLUT_SINGLE | 
GLUT_RGB), tells the graphics "wizard" that we are going to be using a single display 
screen (buffer) and that we'll be using the RGB (red, green, blue) color scheme. The 
vertical line between GLUT_SINGLE and GLUT_RGB means "and" and is typed using the 
"shift+\" key (above "Enter" on most keyboards). 
 
 The glutCreateWindow("My First OGL Program") 
command uses GLUT to create a graphics window and places "My First OGL 
Program" in the new window's caption bar. The glutDisplayFunc(draw)statement 
tells GLUT where to find the function that creates the graphics scene. In this program, 
the display function is def draw().11 Finally, the glutMainLoop() statement starts 
the program running. This is another example of "gluttery" and keeps our program in an 
eternal loop, allowing GLUT to check continually for things like mouse and keyboard 
input/control. 
 
 
7 A common error in Python is to forget to add a ":" after the def statement. Ditto for if 
statements and for loops. 
8 Yes, I know... like a toilet. Later we will learn how to avoid glFlush() by using something 
called buffering. 
9 Improper indentation can cause major headaches in Python. If you indent these commands, 
Python will think they belong in the def draw(): function and the program will not work 
properly. 
10 Remember import sys? 
11 We could have named it anything as long as we are consistent. We could have created a def 
picasso(): function that draws the teapot and as long as we used 
glutDisplayFunc(picasso), everything would be OK. 
 27
 If you haven't already, run the program! You should see something that looks 
similar to Figure 4.2 depending on what is in the background on your desktop: 
 
 Figure 4.2 
 
 Well, at least you can see something that looks like a teapot. What happened? 
When we created the graphics window, we did not specify anything about the 
background, so GLUT simply copied everything from the screen location into the window 
and then drew the teapot on top of the whole mess. Move the window around with your 
mouse (left-click on the caption bar, hold the mouse button down, and move the 
window). You'll see that the background moves with the window! 
 
 We can easily fix this by adding one line of code. Place the statement 
glClear(GL_COLOR_BUFFER_BIT)12 after the def draw(): line. Remember to 
indent at the same level as the other two lines in this function! Save the program and 
run it again. You should now see something like Figure 4.3 on the next page. Move this 
window around with the mouse and everything works as expected. Finally, we are going 
to add two more lines to our first program. Click on the line that contains the 
glutInitDisplayMode command. Press the "End" key on the keyboard or click at 
the end of this line. Press "Enter" twice to make room for the following two lines of code. 
 
glutInitWindowSize(250,250) 
glutInitWindowPosition(100,100) 
 
 Save your work. These lines allow us to specify the size of the graphics window 
(250x250 pixels on a side) and the initial location of the window (100, 100), which is 100 
 
12 This statement obviously clears the screen. The default

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