Statistics
| Revision:

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

History | View | Annotate | Download (5.8 kB)

1 1 equemene
# /***********************************************
2 1 equemene
# *                                              *
3 1 equemene
# *    Jeff Molofee's Revised OpenGL Basecode    *
4 1 equemene
# *  Huge Thanks To Maxwell Sayles & Peter Puck  *
5 1 equemene
# *            http://nehe.gamedev.net           *
6 1 equemene
# *                     2001                     *
7 1 equemene
# *            Adapted to PyOpenGL GLUT          *
8 1 equemene
# *                                              *
9 1 equemene
# ***********************************************/
10 1 equemene
#
11 1 equemene
# NeHe Tutorial Lesson: 48 - ArcBall Rotation
12 1 equemene
#
13 1 equemene
# C version of tutorial by Terence J. Grant
14 1 equemene
# This code was created by Jeff Molofee 2000
15 1 equemene
# Ported to PyOpenGL 2.0 by Brian Leair 2004
16 1 equemene
#
17 1 equemene
# The port was based on the PyOpenGL tutorials and from
18 1 equemene
# PyOpenGLContext (tests/glprint.py)
19 1 equemene
#
20 1 equemene
# If you've found this code useful, feel free to let me know
21 1 equemene
# at (Brian Leair telcom_sage@yahoo.com).
22 1 equemene
#
23 1 equemene
# See original source and C based tutorial at http://nehe.gamedev.net
24 1 equemene
#
25 1 equemene
# Note:
26 1 equemene
# -----
27 1 equemene
# This code is not an ideal example of Pythonic coding or use of OO
28 1 equemene
# techniques. It is a simple and direct exposition of how to use the
29 1 equemene
# Open GL API in Python via the PyOpenGL package. It also uses GLUT,
30 1 equemene
# a high quality platform independent library. Due to using these APIs,
31 1 equemene
# this code is more like a C program using procedural programming.
32 1 equemene
#
33 1 equemene
# To run this example you will need:
34 1 equemene
# Python         - www.python.org (v 2.3 as of 1/2004)
35 1 equemene
# PyOpenGL         - pyopengl.sourceforge.net (v 2.0.1.07 as of 1/2004)
36 1 equemene
# Numeric Python        - (v.22 of "numpy" as of 1/2004) numpy.sourceforge.net
37 1 equemene
#
38 1 equemene
#
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
from Lesson48 import *                # Draw (), Initialize () and all the real OpenGL work.
46 1 equemene
from ArcBall import *                # // *NEW* ArcBall header
47 1 equemene
48 1 equemene
49 1 equemene
# *********************** Globals ***********************
50 1 equemene
# Python 2.2 defines these directly
51 1 equemene
try:
52 1 equemene
        True
53 1 equemene
except NameError:
54 1 equemene
        True = 1==1
55 1 equemene
        False = 1==0
56 1 equemene
57 1 equemene
58 1 equemene
# Some api in the chain is translating the keystrokes to this octal string
59 1 equemene
# so instead of saying: ESCAPE = 27, we use the following.
60 1 equemene
ESCAPE = '\033'
61 1 equemene
62 1 equemene
# Number of the glut window.
63 1 equemene
window = 0
64 1 equemene
65 1 equemene
66 1 equemene
67 1 equemene
68 1 equemene
69 1 equemene
70 1 equemene
71 1 equemene
# A general OpenGL initialization function.  Sets all of the initial parameters.
72 1 equemene
def InitGL(Width, Height):                                # We call this right after our OpenGL window is created.
73 1 equemene
74 1 equemene
        glShadeModel(GL_SMOOTH)                                # Enables Smooth Color Shading
75 1 equemene
        glClearColor(0.0, 0.0, 0.0, 0.5)        # This Will Clear The Background Color To Black
76 1 equemene
        glClearDepth(1.0)                                        # Enables Clearing Of The Depth Buffer
77 1 equemene
        glEnable(GL_DEPTH_TEST)                                # Enables Depth Testing
78 1 equemene
        glDepthFunc(GL_LEQUAL)                                # The Type Of Depth Test To Do
79 1 equemene
        glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Really Nice Perspective Calculations
80 1 equemene
81 1 equemene
82 1 equemene
83 1 equemene
        return True                                                                        # // Initialization Went OK
84 1 equemene
85 1 equemene
86 1 equemene
# Reshape The Window When It's Moved Or Resized
87 1 equemene
def ReSizeGLScene(Width, Height):
88 1 equemene
        if Height == 0:                                                # Prevent A Divide By Zero If The Window Is Too Small
89 1 equemene
                Height = 1
90 1 equemene
91 1 equemene
        glViewport(0, 0, Width, Height)                # Reset The Current Viewport And Perspective Transformation
92 1 equemene
        glMatrixMode(GL_PROJECTION)                        # // Select The Projection Matrix
93 1 equemene
        glLoadIdentity()                                        # // Reset The Projection Matrix
94 1 equemene
        # // field of view, aspect ratio, near and far
95 1 equemene
        # This will squash and stretch our objects as the window is resized.
96 1 equemene
        # Note that the near clip plane is 1 (hither) and the far plane is 1000 (yon)
97 1 equemene
        gluPerspective(45.0, float(Width)/float(Height), 1, 100.0)
98 1 equemene
99 1 equemene
        glMatrixMode (GL_MODELVIEW);                # // Select The Modelview Matrix
100 1 equemene
        glLoadIdentity ();                                        # // Reset The Modelview Matrix
101 1 equemene
        g_ArcBall.setBounds (Width, Height)        # //*NEW* Update mouse bounds for arcball
102 1 equemene
        return
103 1 equemene
104 1 equemene
105 1 equemene
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
106 1 equemene
def keyPressed(*args):
107 1 equemene
        global g_quadratic
108 1 equemene
        # If escape is pressed, kill everything.
109 1 equemene
        key = args [0]
110 1 equemene
        if key == ESCAPE:
111 1 equemene
                gluDeleteQuadric (g_quadratic)
112 1 equemene
                sys.exit ()
113 1 equemene
114 1 equemene
115 1 equemene
116 1 equemene
def main():
117 1 equemene
        global window
118 1 equemene
        # pass arguments to init
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_ALPHA | 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, 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("Lesson 48: NeHe ArcBall Rotation Tutorial")
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(Draw)
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(Draw)
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
157 1 equemene
        # GLUT When mouse buttons are clicked in window
158 1 equemene
        glutMouseFunc (Upon_Click)
159 1 equemene
160 1 equemene
        # GLUT When the mouse mvoes
161 1 equemene
        glutMotionFunc (Upon_Drag)
162 1 equemene
163 1 equemene
164 1 equemene
        # We've told Glut the type of window we want, and we've told glut about
165 1 equemene
        # various functions that we want invoked (idle, resizing, keyboard events).
166 1 equemene
        # Glut has done the hard work of building up thw windows DC context and
167 1 equemene
        # tying in a rendering context, so we are ready to start making immediate mode
168 1 equemene
        # GL calls.
169 1 equemene
        # Call to perform inital GL setup (the clear colors, enabling modes
170 1 equemene
        Initialize (640, 480)
171 1 equemene
172 1 equemene
        # Start Event Processing Engine
173 1 equemene
        glutMainLoop()
174 1 equemene
175 1 equemene
# Print message to console, and kick off the main to get it rolling.
176 1 equemene
if __name__ == "__main__":
177 1 equemene
        print "Hit ESC key to quit."
178 1 equemene
        main()