A maior rede de estudos do Brasil

Grátis
433 pág.
Apostila PyOpenGL

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

system with central origin 
 
from OpenGL.GL import * 
from OpenGL.GLU import * 
from OpenGL.GLUT import * 
import sys 
 
 As explained previously, the lines following the remark statements add OpenGL 
commands to Python for our use. Now let’s explore the program listing. 
 
def init(): 
 glClearColor(0.0, 0.0, 0.0, 1.0) 
 gluOrtho2D(-1.0, 1.0, -1.0, 1.0) 
 
 The first program section is an initialization function.5 We set the background 
color to black using the glClearColor command. This command does not DO 
anything other than inform OpenGL that when we clear the graphics screen, we are 
going to use “black” as our background color.6 The numbers in the parentheses 
following this command dictate the color and they are, respectively, (red, green, 
blue, alpha). Each color position accepts a value from 0.0 to 1.0 inclusive. You can 
 
4 Pun intended. 
5 It makes sense to choose variable and function names that describe their purpose. This is a 
function that sets some initial values, hence the name init(). 
6 glClearColor sets the GL_COLOR_BUFFER_BIT to the color of your choosing. 
 35
mix and match colors if you wish and use various intensities.7 The “alpha” setting deals 
with the opacity or transparency of the colors. It isn’t used as much as the RGB (red, 
green, and blue) color values and is generally set at 1.0. For example, a purple or 
magenta background color would be: glClearColor(1.0, 0.0, 1.0, 1.0). Note 
that the mixture of red (1.0) and blue (1.0) will result in a purple/magenta background 
color.8 The absence of color (all RGB settings excluding alpha at 0.0) results in black. 
Setting 1.0 for each color will provide a white background. 
 
 The gluOrtho2D command allows us to set the coordinate system ranges. The 
command is used this way: 
 
 gluOrtho2D(x-left, x-right, y-bottom, y-top) 
 
In this particular instance, we are setting a coordinate system that ranges from -1.0 to 
1.0 in both the x and y axes which places the origin (0,0) in the center of the screen. 
 
 Let’s look at the plotpoints() function: 
 
def plotpoints(): 
 glClear(GL_COLOR_BUFFER_BIT) 
 glColor3f(1.0, 0.0, 0.0) 
 
 The plotpoints() function contains the drawing procedure. Again, the exact 
function name, plotpoints(), is not crucial, but should be descriptive of the function’s 
behavior (if possible). The glClear(GL_COLOR_BUFFER_BIT) command does what 
you think it should do. It clears the screen (the color buffer bit) and sets the background 
color to the choice you made in the init() function. The next line, 
glColor3f(1.0, 0.0, 0.0), sets the plotting color to red. The “3f” in the 
glColor3f command reminds us that we need to use 3 floating point (decimal between 
0.0 and 1.0 inclusive) values for the RGB settings.9 Placing a 1.0 in the red position and 
0.0 in the green and blue positions insures that we will be drawing or plotting using pure 
red. The glColor3f command MUST be placed before the plotting or drawing 
command in order for it to work properly. Take special care that you do not confuse the 
two OpenGL color commands. glClearColor is designed specifically to set the 
background color of the graphics window after it has been cleared. glColor3f and 
glColor3ub are designed to set the color of the plotting pen. 
 
 The actual plotting of points occurs in the next program section. 
 
 glBegin(GL_POINTS) 
 glVertex2f(0.0, 0.0) 
 glEnd() 
 
 glFlush() 
 
 
7 Example: glClearColor(0.5, 0.8, 0.3, 1.0) for an interesting shade of green. 
8 Try it! If the color is too intense, try 0.7 for both red and blue values. 
9 There are other possibilities such as: glColor3ub(red, green, blue) where “ub” stands 
for “unsigned byte”. In this case, the color values range from 0-255. 
 36
 When we draw or plot in OpenGL, we must inform the graphics system of our 
intentions. The glBegin(GL_POINTS) command serves this purpose. It basically 
says that we are ready to begin drawing and that we are going to plot points.10 
glVertex2f(0.0, 0.0) locates the single point (red, remember?) on the screen 
using the coordinates (0.0, 0.0), which should represent the origin at the screen’s center. 
The 2f portion of the glVertex2f(0.0, 0.0) command indicates that we are going 
to use 2 floating point values with glVertex, the first to represent the x coordinate and 
the second to represent the y coordinate.11 When we are done plotting, we “pick up our 
pen” by issuing a glEnd() command. Finally, we “flush” our drawing to the screen with 
glFlush().12 
 
 In this and subsequent programs we will use a def main(): function to initiate 
OpenGL and call any setup routines (such as init()) needed by our program. 
Technically, a separate main() function is not required by Python, but the OpenGL 
tradition is based on the C programming language, which requires a main() function. 
We’ll keep with tradition. 
 
def main(): 
 glutInit(sys.argv) 
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB) 
 glutInitWindowSize(500,500) 
 glutInitWindowPosition(50,50) 
 glutCreateWindow(“Plot Points”) 
 glutDisplayFunc(plotpoints) 
 
 init() 
 glutMainLoop() 
 
main() 
 
 We have seen most of these lines in our first OpenGL program. Note that the 
graphics window size in glutInitWindowSize was set to provide a square drawing 
surface 600 pixels on a side. The plotpoints display function must be properly 
named and spelled in the glutDisplayFunc command.13 We “call” the init() 
function to set everything up and then put OpenGL/GLUT into an eternal loop to run the 
program. The final command, main(), calls the main() function. Without this final 
command, the program will not run. Try it! Put a # in front of main() and then run the 
program. 
 
 Now it's time to explore this program further in the exercises! 
 
10 GL_POINTS specifies point plotting. We can draw lines by using GL_LINES and triangles by 
GL_TRIANGLES. There are other possibilities for glBegin that we will research later. 
11 An ordered pair, just like in algebra! 
12 To this point, the drawing exists only in the computer’s memory. We must transfer the drawing 
from memory to the screen. I’m not certain where “flush” originated in this usage… but we “flush” 
the memory to the screen using glFlush(). 
13 Proper spelling includes making certain that you pay attention to upper and lower case letters! 
“plotpoints” is NOT the same as “plotpoints”. 
 37
Exercises 
 
 To avoid overwriting previous exercises, remember to save each new problem as 
a different program name (such as ch5ex1.py) 
 
1) Experiment with the background color settings in glClearColor. See if you 
can find both a pleasing and a putrid color. You might want to jot down the 
settings in your notes or journal for later reference. 
 
2) Change the color of the pixel(s) in the glColor3f statement. Try plotting a 
white pixel using (1.0, 1.0, 1.0). Remember to place this command above the 
glVertex2f command or it will not work properly! 
 
3) Add the line glPointSize(2.0) directly above the glBegin(GL_POINTS) 
using the same indentation level and see what happens.14 Experiment with this 
command by increasing and decreasing the number in parentheses. 
 
4) Plot several points in several sizes and colors. You may insert as many 
glVertex2f command as you need between the glBegin(GL_POINTS) and 
glEnd() commands. What happens if you plot a point that is beyond the x and 
y axis ranges set in the gluOrtho2D command? Try it! 
 
5) Change the gluOrtho2D command so that the x and y axis ranges are from -
10.0 to +10.0. Plot some points in those ranges. Can you predict where the 
points will appear before you run the program? 
 
6) Experiment: Comment

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