Statistics
| Revision:

root / PyOpenGL-Demo / NeHe / lesson3.py @ 1

History | View | Annotate | Download (6.6 kB)

1
#!
2

    
3
# This is statement is required by the build system to query build info
4
if __name__ == '__build__':
5
        raise Exception
6

    
7
import string
8
__version__ = string.split('$Revision: 1.1.1.1 $')[1]
9
__date__ = string.join(string.split('$Date: 2007/02/15 19:25:21 $')[1:3], ' ')
10
__author__ = 'Tarn Weisner Burton <twburton@users.sourceforge.net>'
11

    
12
#
13
# Ported to PyOpenGL 2.0 by Tarn Weisner Burton 10May2001
14
#
15
# This code was created by Richard Campbell '99 (ported to Python/PyOpenGL by John Ferguson 2000)
16
#
17
# The port was based on the PyOpenGL tutorial module: dots.py  
18
#
19
# If you've found this code useful, please let me know (email John Ferguson at hakuin@voicenet.com).
20
#
21
# See original source and C based tutorial at http://nehe.gamedev.net
22
#
23
# Note:
24
# -----
25
# This code is not a good example of Python and using OO techniques.  It is a simple and direct
26
# exposition of how to use the Open GL API in Python via the PyOpenGL package.  It also uses GLUT,
27
# which in my opinion is a high quality library in that it makes my work simpler.  Due to using
28
# these APIs, this code is more like a C program using function based programming (which Python
29
# is in fact based upon, note the use of closures and lambda) than a "good" OO program.
30
#
31
# To run this code get and install OpenGL, GLUT, PyOpenGL (see http://www.python.org), and PyNumeric.
32
# Installing PyNumeric means having a C compiler that is configured properly, or so I found.  For 
33
# Win32 this assumes VC++, I poked through the setup.py for Numeric, and chased through disutils code
34
# and noticed what seemed to be hard coded preferences for VC++ in the case of a Win32 OS.  However,
35
# I am new to Python and know little about disutils, so I may just be not using it right.
36
#
37
# BTW, since this is Python make sure you use tabs or spaces to indent, I had numerous problems since I 
38
# was using editors that were not sensitive to Python.
39
#
40
from OpenGL.GL import *
41
from OpenGL.GLUT import *
42
from OpenGL.GLU import *
43
import sys
44

    
45
# Some api in the chain is translating the keystrokes to this octal string
46
# so instead of saying: ESCAPE = 27, we use the following.
47
ESCAPE = '\033'
48

    
49
# Number of the glut window.
50
window = 0
51

    
52
# A general OpenGL initialization function.  Sets all of the initial parameters. 
53
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
54
    glClearColor(0.0, 0.0, 0.0, 0.0)        # This Will Clear The Background Color To Black
55
    glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
56
    glDepthFunc(GL_LESS)                                # The Type Of Depth Test To Do
57
    glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
58
    glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
59
        
60
    glMatrixMode(GL_PROJECTION)
61
    glLoadIdentity()                                        # Reset The Projection Matrix
62
                                                                                # Calculate The Aspect Ratio Of The Window
63
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
64

    
65
    glMatrixMode(GL_MODELVIEW)
66

    
67
# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
68
def ReSizeGLScene(Width, Height):
69
    if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small 
70
            Height = 1
71

    
72
    glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
73
    glMatrixMode(GL_PROJECTION)
74
    glLoadIdentity()
75
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
76
    glMatrixMode(GL_MODELVIEW)
77

    
78
# The main drawing function. 
79
def DrawGLScene():
80
        # Clear The Screen And The Depth Buffer
81
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
82
        glLoadIdentity()                                        # Reset The View 
83

    
84
        # Move Left 1.5 units and into the screen 6.0 units.
85
        glTranslatef(-1.5, 0.0, -6.0)
86

    
87
        # Since we have smooth color mode on, this will be great for the Phish Heads :-).
88
        # Draw a triangle
89
        glBegin(GL_POLYGON)                 # Start drawing a polygon
90
        glColor3f(1.0, 0.0, 0.0)            # Red
91
        glVertex3f(0.0, 1.0, 0.0)           # Top
92
        glColor3f(0.0, 1.0, 0.0)            # Green
93
        glVertex3f(1.0, -1.0, 0.0)          # Bottom Right
94
        glColor3f(0.0, 0.0, 1.0)            # Blue
95
        glVertex3f(-1.0, -1.0, 0.0)         # Bottom Left
96
        glEnd()                             # We are done with the polygon
97

    
98

    
99
        # Move Right 3.0 units.
100
        glTranslatef(3.0, 0.0, 0.0)
101

    
102
        # Draw a square (quadrilateral)
103
        glColor3f(0.3, 0.5, 1.0)            # Bluish shade
104
        glBegin(GL_QUADS)                   # Start drawing a 4 sided polygon
105
        glVertex3f(-1.0, 1.0, 0.0)          # Top Left
106
        glVertex3f(1.0, 1.0, 0.0)           # Top Right
107
        glVertex3f(1.0, -1.0, 0.0)          # Bottom Right
108
        glVertex3f(-1.0, -1.0, 0.0)         # Bottom Left
109
        glEnd()                             # We are done with the polygon
110

    
111
        #  since this is double buffered, swap the buffers to display what just got drawn. 
112
        glutSwapBuffers()
113

    
114
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
115
def keyPressed(*args):
116
        # If escape is pressed, kill everything.
117
    if args[0] == ESCAPE:
118
            sys.exit()
119

    
120
def main():
121
        global window
122
        # For now we just pass glutInit one empty argument. I wasn't sure what should or could be passed in (tuple, list, ...)
123
        # Once I find out the right stuff based on reading the PyOpenGL source, I'll address this.
124
        glutInit(sys.argv)
125

    
126
        # Select type of Display mode:   
127
        #  Double buffer 
128
        #  RGBA color
129
        # Alpha components supported 
130
        # Depth buffer
131
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
132
        
133
        # get a 640 x 480 window 
134
        glutInitWindowSize(640, 480)
135
        
136
        # the window starts at the upper left corner of the screen 
137
        glutInitWindowPosition(0, 0)
138
        
139
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
140
        # to Python (like myself), remember this assignment would make the variable local and not global
141
        # if it weren't for the global declaration at the start of main.
142
        window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
143

    
144
           # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
145
        # set the function pointer and invoke a function to actually register the callback, otherwise it
146
        # would be very much like the C version of the code.        
147
        glutDisplayFunc(DrawGLScene)
148
        
149
        # Uncomment this line to get full screen.
150
        #glutFullScreen()
151

    
152
        # When we are doing nothing, redraw the scene.
153
        glutIdleFunc(DrawGLScene)
154
        
155
        # Register the function called when our window is resized.
156
        glutReshapeFunc(ReSizeGLScene)
157
        
158
        # Register the function called when the keyboard is pressed.  
159
        glutKeyboardFunc(keyPressed)
160

    
161
        # Initialize our window. 
162
        InitGL(640, 480)
163

    
164
        # Start Event Processing Engine        
165
        glutMainLoop()
166

    
167
# Print message to console, and kick off the main to get it rolling.
168
print "Hit ESC key to quit."
169
main()
170