Statistics
| Revision:

root / PyOpenGL-Demo / NeHe / lesson48 / NeHeGL.py @ 1

History | View | Annotate | Download (5.8 kB)

1
# /***********************************************
2
# *                                              *
3
# *    Jeff Molofee's Revised OpenGL Basecode    *
4
# *  Huge Thanks To Maxwell Sayles & Peter Puck  *
5
# *            http://nehe.gamedev.net           *
6
# *                     2001                     *
7
# *            Adapted to PyOpenGL GLUT          *
8
# *                                              *
9
# ***********************************************/
10
#
11
# NeHe Tutorial Lesson: 48 - ArcBall Rotation
12
#
13
# C version of tutorial by Terence J. Grant
14
# This code was created by Jeff Molofee 2000
15
# Ported to PyOpenGL 2.0 by Brian Leair 2004
16
#
17
# The port was based on the PyOpenGL tutorials and from 
18
# PyOpenGLContext (tests/glprint.py)
19
#
20
# If you've found this code useful, feel free to let me know 
21
# at (Brian Leair telcom_sage@yahoo.com).
22
#
23
# See original source and C based tutorial at http://nehe.gamedev.net
24
#
25
# Note:
26
# -----
27
# This code is not an ideal example of Pythonic coding or use of OO 
28
# techniques. It is a simple and direct exposition of how to use the 
29
# Open GL API in Python via the PyOpenGL package. It also uses GLUT, 
30
# a high quality platform independent library. Due to using these APIs, 
31
# this code is more like a C program using procedural programming.
32
#
33
# To run this example you will need:
34
# Python         - www.python.org (v 2.3 as of 1/2004)
35
# PyOpenGL         - pyopengl.sourceforge.net (v 2.0.1.07 as of 1/2004)
36
# Numeric Python        - (v.22 of "numpy" as of 1/2004) numpy.sourceforge.net
37
#
38
#
39

    
40
from OpenGL.GL import *
41
from OpenGL.GLUT import *
42
from OpenGL.GLU import *
43
import sys
44

    
45
from Lesson48 import *                # Draw (), Initialize () and all the real OpenGL work.
46
from ArcBall import *                # // *NEW* ArcBall header
47

    
48

    
49
# *********************** Globals *********************** 
50
# Python 2.2 defines these directly
51
try:
52
        True
53
except NameError:
54
        True = 1==1
55
        False = 1==0
56

    
57

    
58
# Some api in the chain is translating the keystrokes to this octal string
59
# so instead of saying: ESCAPE = 27, we use the following.
60
ESCAPE = '\033'
61

    
62
# Number of the glut window.
63
window = 0
64

    
65

    
66

    
67

    
68

    
69

    
70

    
71
# A general OpenGL initialization function.  Sets all of the initial parameters. 
72
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
73

    
74
        glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
75
        glClearColor(0.0, 0.0, 0.0, 0.5)        # This Will Clear The Background Color To Black
76
        glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
77
        glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
78
        glDepthFunc(GL_LEQUAL)                                # The Type Of Depth Test To Do
79
        glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations
80

    
81

    
82

    
83
        return True                                                                        # // Initialization Went OK
84

    
85

    
86
# Reshape The Window When It's Moved Or Resized
87
def ReSizeGLScene(Width, Height):
88
        if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small 
89
                Height = 1
90

    
91
        glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
92
        glMatrixMode(GL_PROJECTION)                        # // Select The Projection Matrix
93
        glLoadIdentity()                                        # // Reset The Projection Matrix
94
        # // field of view, aspect ratio, near and far
95
        # This will squash and stretch our objects as the window is resized.
96
        # Note that the near clip plane is 1 (hither) and the far plane is 1000 (yon)
97
        gluPerspective(45.0, float(Width)/float(Height), 1, 100.0)
98

    
99
        glMatrixMode (GL_MODELVIEW);                # // Select The Modelview Matrix
100
        glLoadIdentity ();                                        # // Reset The Modelview Matrix
101
        g_ArcBall.setBounds (Width, Height)        # //*NEW* Update mouse bounds for arcball
102
        return
103

    
104

    
105
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
106
def keyPressed(*args):
107
        global g_quadratic
108
        # If escape is pressed, kill everything.
109
        key = args [0]
110
        if key == ESCAPE:
111
                gluDeleteQuadric (g_quadratic)
112
                sys.exit ()
113

    
114

    
115

    
116
def main():
117
        global window
118
        # pass arguments to init
119
        glutInit(sys.argv)
120

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

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

    
147
        # When we are doing nothing, redraw the scene.
148
        glutIdleFunc(Draw)
149
        
150
        # Register the function called when our window is resized.
151
        glutReshapeFunc(ReSizeGLScene)
152
        
153
        # Register the function called when the keyboard is pressed.  
154
        glutKeyboardFunc(keyPressed)
155

    
156

    
157
        # GLUT When mouse buttons are clicked in window
158
        glutMouseFunc (Upon_Click)
159

    
160
        # GLUT When the mouse mvoes
161
        glutMotionFunc (Upon_Drag)
162

    
163

    
164
        # We've told Glut the type of window we want, and we've told glut about
165
        # various functions that we want invoked (idle, resizing, keyboard events).
166
        # Glut has done the hard work of building up thw windows DC context and 
167
        # tying in a rendering context, so we are ready to start making immediate mode
168
        # GL calls.
169
        # Call to perform inital GL setup (the clear colors, enabling modes
170
        Initialize (640, 480)
171

    
172
        # Start Event Processing Engine        
173
        glutMainLoop()
174

    
175
# Print message to console, and kick off the main to get it rolling.
176
if __name__ == "__main__":
177
        print "Hit ESC key to quit."
178
        main()
179