Statistiques
| Révision :

root / PyOpenGL-Demo / NeHe / lesson2.py @ 2

Historique | Voir | Annoter | Télécharger (6,3 ko)

1 1 equemene
#!
2 1 equemene
3 1 equemene
# This is statement is required by the build system to query build info
4 1 equemene
if __name__ == '__build__':
5 1 equemene
        raise Exception
6 1 equemene
7 1 equemene
import string
8 1 equemene
__version__ = string.split('$Revision: 1.1.1.1 $')[1]
9 1 equemene
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:20 $')[1:3], ' ')
10 1 equemene
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
11 1 equemene
12 1 equemene
#
13 1 equemene
# Ported to PyOpenGL 2.0 by Tarn Weisner Burton 10May2001
14 1 equemene
#
15 1 equemene
# This code was created by Richard Campbell '99 (ported to Python/PyOpenGL by John Ferguson 2000)
16 1 equemene
#
17 1 equemene
# The port was based on the PyOpenGL tutorial module: dots.py
18 1 equemene
#
19 1 equemene
# If you've found this code useful, please let me know (email John Ferguson at hakuin@voicenet.com).
20 1 equemene
#
21 1 equemene
# See original source and C based tutorial at http://nehe.gamedev.net
22 1 equemene
#
23 1 equemene
# Note:
24 1 equemene
# -----
25 1 equemene
# This code is not a good example of Python and using OO techniques.  It is a simple and direct
26 1 equemene
# exposition of how to use the Open GL API in Python via the PyOpenGL package.  It also uses GLUT,
27 1 equemene
# which in my opinion is a high quality library in that it makes my work simpler.  Due to using
28 1 equemene
# these APIs, this code is more like a C program using function based programming (which Python
29 1 equemene
# is in fact based upon, note the use of closures and lambda) than a "good" OO program.
30 1 equemene
#
31 1 equemene
# To run this code get and install OpenGL, GLUT, PyOpenGL (see http://www.python.org), and PyNumeric.
32 1 equemene
# Installing PyNumeric means having a C compiler that is configured properly, or so I found.  For
33 1 equemene
# Win32 this assumes VC++, I poked through the setup.py for Numeric, and chased through disutils code
34 1 equemene
# and noticed what seemed to be hard coded preferences for VC++ in the case of a Win32 OS.  However,
35 1 equemene
# I am new to Python and know little about disutils, so I may just be not using it right.
36 1 equemene
#
37 1 equemene
# BTW, since this is Python make sure you use tabs or spaces to indent, I had numerous problems since I
38 1 equemene
# was using editors that were not sensitive to Python.
39 1 equemene
#
40 1 equemene
from OpenGL.GL import *
41 1 equemene
from OpenGL.GLUT import *
42 1 equemene
from OpenGL.GLU import *
43 1 equemene
import sys
44 1 equemene
45 1 equemene
# Some api in the chain is translating the keystrokes to this octal string
46 1 equemene
# so instead of saying: ESCAPE = 27, we use the following.
47 1 equemene
ESCAPE = '\033'
48 1 equemene
49 1 equemene
# Number of the glut window.
50 1 equemene
window = 0
51 1 equemene
52 1 equemene
# A general OpenGL initialization function.  Sets all of the initial parameters.
53 1 equemene
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
54 1 equemene
    glClearColor(0.0, 0.0, 0.0, 0.0)        # This Will Clear The Background Color To Black
55 1 equemene
    glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
56 1 equemene
    glDepthFunc(GL_LESS)                                # The Type Of Depth Test To Do
57 1 equemene
    glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
58 1 equemene
    glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
59 1 equemene
60 1 equemene
    glMatrixMode(GL_PROJECTION)
61 1 equemene
    glLoadIdentity()                                        # Reset The Projection Matrix
62 1 equemene
                                                                                # Calculate The Aspect Ratio Of The Window
63 1 equemene
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
64 1 equemene
65 1 equemene
    glMatrixMode(GL_MODELVIEW)
66 1 equemene
67 1 equemene
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
68 1 equemene
def ReSizeGLScene(Width, Height):
69 1 equemene
    if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small
70 1 equemene
            Height = 1
71 1 equemene
72 1 equemene
    glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
73 1 equemene
    glMatrixMode(GL_PROJECTION)
74 1 equemene
    glLoadIdentity()
75 1 equemene
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
76 1 equemene
    glMatrixMode(GL_MODELVIEW)
77 1 equemene
78 1 equemene
# The main drawing function.
79 1 equemene
def DrawGLScene():
80 1 equemene
        # Clear The Screen And The Depth Buffer
81 1 equemene
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
82 1 equemene
        glLoadIdentity()                                        # Reset The View
83 1 equemene
84 1 equemene
        # Move Left 1.5 units and into the screen 6.0 units.
85 1 equemene
        glTranslatef(-1.5, 0.0, -6.0)
86 1 equemene
87 1 equemene
        # Draw a triangle
88 1 equemene
        glBegin(GL_POLYGON)                 # Start drawing a polygon
89 1 equemene
        glVertex3f(0.0, 1.0, 0.0)           # Top
90 1 equemene
        glVertex3f(1.0, -1.0, 0.0)          # Bottom Right
91 1 equemene
        glVertex3f(-1.0, -1.0, 0.0)         # Bottom Left
92 1 equemene
        glEnd()                             # We are done with the polygon
93 1 equemene
94 1 equemene
95 1 equemene
        # Move Right 3.0 units.
96 1 equemene
        glTranslatef(3.0, 0.0, 0.0)
97 1 equemene
98 1 equemene
        # Draw a square (quadrilateral)
99 1 equemene
        glBegin(GL_QUADS)                   # Start drawing a 4 sided polygon
100 1 equemene
        glVertex3f(-1.0, 1.0, 0.0)          # Top Left
101 1 equemene
        glVertex3f(1.0, 1.0, 0.0)           # Top Right
102 1 equemene
        glVertex3f(1.0, -1.0, 0.0)          # Bottom Right
103 1 equemene
        glVertex3f(-1.0, -1.0, 0.0)         # Bottom Left
104 1 equemene
        glEnd()                             # We are done with the polygon
105 1 equemene
106 1 equemene
        #  since this is double buffered, swap the buffers to display what just got drawn.
107 1 equemene
        glutSwapBuffers()
108 1 equemene
109 1 equemene
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
110 1 equemene
def keyPressed(*args):
111 1 equemene
        # If escape is pressed, kill everything.
112 1 equemene
    if args[0] == ESCAPE:
113 1 equemene
            sys.exit()
114 1 equemene
115 1 equemene
def main():
116 1 equemene
        global window
117 1 equemene
        # For now we just pass glutInit one empty argument. I wasn't sure what should or could be passed in (tuple, list, ...)
118 1 equemene
        # Once I find out the right stuff based on reading the PyOpenGL source, I'll address this.
119 1 equemene
        glutInit(sys.argv)
120 1 equemene
121 1 equemene
        # Select type of Display mode:
122 1 equemene
        #  Double buffer
123 1 equemene
        #  RGBA color
124 1 equemene
        # Alpha components supported
125 1 equemene
        # Depth buffer
126 1 equemene
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
127 1 equemene
128 1 equemene
        # get a 640 x 480 window
129 1 equemene
        glutInitWindowSize(640, 480)
130 1 equemene
131 1 equemene
        # the window starts at the upper left corner of the screen
132 1 equemene
        glutInitWindowPosition(0, 0)
133 1 equemene
134 1 equemene
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
135 1 equemene
        # to Python (like myself), remember this assignment would make the variable local and not global
136 1 equemene
        # if it weren't for the global declaration at the start of main.
137 1 equemene
        window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
138 1 equemene
139 1 equemene
           # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
140 1 equemene
        # set the function pointer and invoke a function to actually register the callback, otherwise it
141 1 equemene
        # would be very much like the C version of the code.
142 1 equemene
        glutDisplayFunc(DrawGLScene)
143 1 equemene
144 1 equemene
        # Uncomment this line to get full screen.
145 1 equemene
        #glutFullScreen()
146 1 equemene
147 1 equemene
        # When we are doing nothing, redraw the scene.
148 1 equemene
        glutIdleFunc(DrawGLScene)
149 1 equemene
150 1 equemene
        # Register the function called when our window is resized.
151 1 equemene
        glutReshapeFunc(ReSizeGLScene)
152 1 equemene
153 1 equemene
        # Register the function called when the keyboard is pressed.
154 1 equemene
        glutKeyboardFunc(keyPressed)
155 1 equemene
156 1 equemene
        # Initialize our window.
157 1 equemene
        InitGL(640, 480)
158 1 equemene
159 1 equemene
        # Start Event Processing Engine
160 1 equemene
        glutMainLoop()
161 1 equemene
162 1 equemene
# Print message to console, and kick off the main to get it rolling.
163 1 equemene
print "Hit ESC key to quit."
164 1 equemene
main()