Statistics
| Revision:

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

History | View | Annotate | Download (6.1 kB)

1
#! /usr/bin/env python
2
# -*- coding: utf8 -*-
3
"""Port of NeHe Lesson 12 by Ivan Izuver <izuver@users.sourceforge.net>"""
4
from OpenGL.GL import *
5
from OpenGL.GLU import *
6
from OpenGL.GLUT import *
7

    
8
import sys
9
import numpy
10

    
11
ESCAPE = '\033'
12

    
13
# Number of the glut window.
14
window = 0
15
xloop=yloop=0
16
xrot=yrot=0
17

    
18
boxcol=numpy.zeros((5, 3), 'f')
19
boxcol[0]=1.0,0.0,0.0
20
boxcol[1]=1.0,0.5,0.0
21
boxcol[2]=1.0,1.0,0.0
22
boxcol[3]=0.0,1.0,0.0
23
boxcol[4]=0.0,1.0,1.0
24

    
25
topcol=numpy.zeros((5, 3), 'f')
26
topcol[0]=0.5,0.0,0.0
27
topcol[1]=0.5,0.25,0.0
28
topcol[2]=0.5,0.5,0.0
29
topcol[3]=0.0,0.5,0.0
30
topcol[4]=0.0,0.5,0.5
31

    
32
# A general OpenGL initialization function.  Sets all of the initial parameters. 
33
def InitGL(Width, Height):                # We call this right after our OpenGL window is created.
34
        glClearColor(0.0, 0.0, 0.0, 0.0)    # This Will Clear The Background Color To Black
35
        glClearDepth(1.0)                    # Enables Clearing Of The Depth Buffer
36
        glDepthFunc(GL_LEQUAL)                # The Type Of Depth Test To Do
37
        glEnable(GL_DEPTH_TEST)                # Enables Depth Testing
38
        glShadeModel(GL_SMOOTH)                # Enables Smooth Color Shading
39
        
40
        glEnable(GL_LIGHT0)
41
        glEnable(GL_LIGHTING)
42
        glEnable(GL_COLOR_MATERIAL)
43
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
44
        
45
        
46
        glMatrixMode(GL_PROJECTION)
47
        glLoadIdentity()                    # Reset The Projection Matrix
48
                                                                                # Calculate The Aspect Ratio Of The Window
49
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
50

    
51
        glMatrixMode(GL_MODELVIEW)
52

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

    
58
        glViewport(0, 0, Width, Height)        # Reset The Current Viewport And Perspective Transformation
59
        glMatrixMode(GL_PROJECTION)
60
        glLoadIdentity()
61
        gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
62
        glMatrixMode(GL_MODELVIEW)
63

    
64
# The main drawing function. 
65
def DrawGLScene():
66
        # Clear The Screen And The Depth Buffer
67
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
68
        
69
        box=glGenLists(2)
70
        
71
        # create list maned box (with quad)
72
        glNewList(box,GL_COMPILE)
73
        
74
        glBegin(GL_QUADS);
75
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0)
76
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, -1.0)
77
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0)
78
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0)
79
        
80
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, 1.0)
81
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, 1.0)
82
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0)
83
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 1.0)
84
        
85
        glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, -1.0)        
86
        glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, -1.0)        
87
        glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, -1.0)        
88
        glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, -1.0)        
89
 
90
        glTexCoord2f(1.0 , 0.0 ); glVertex3f( 1.0 , -1.0 , -1.0 )        
91
        glTexCoord2f(1.0 , 1.0 ); glVertex3f( 1.0 , 1.0 , -1.0 )
92
        glTexCoord2f(0.0 , 1.0 ); glVertex3f( 1.0 , 1.0 , 1.0 )
93
        glTexCoord2f(0.0 , 0.0 ); glVertex3f( 1.0 , -1.0 , 1.0 )
94
                                                        
95
        glTexCoord2f(0.0 , 0.0 ); glVertex3f(-1.0 , -1.0 , -1.0 )        
96
        glTexCoord2f(1.0 , 0.0 ); glVertex3f(-1.0 , -1.0 , 1.0 )
97
        glTexCoord2f(1.0 , 1.0 ); glVertex3f(-1.0 , 1.0 , 1.0 )
98
        glTexCoord2f(0.0 , 1.0 ); glVertex3f(-1.0 , 1.0 , -1.0 )      
99
        glEnd();                        
100
        glEndList(); 
101
        
102
        top=box+1
103
        # create new list named top ( with head of quad)
104
        glNewList(top,GL_COMPILE)
105
        
106
        glBegin(GL_QUADS)
107
        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
108
        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0);
109
        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0);
110
        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0);
111
        glEnd()
112
        glEndList()   
113

    
114
        for yloop in range(6):
115
                for xloop in range(5):
116
                        glLoadIdentity()                         # Reset The View
117
                        glTranslatef(1.4+(float(xloop)*2.8)-(float(yloop)*1.4),((6.0-float(yloop))*2.4)-7.0,-20.0)
118
                        glRotatef(45.0-(2.0*yloop)+xrot,1.0,0.0,0.0);      
119
                        glRotatef(45.0+yrot,0.0,1.0,0.0);                  
120
                        
121
                        glColor3fv(boxcol[yloop-1]);             
122
                        glCallList(box);                        # call list named box
123
                        
124
                        glColor3fv(topcol[yloop-1]);            
125
                        glCallList(top);                        # call list named top
126

    
127
        #  since this is double buffered, swap the buffers to display what just got drawn. 
128
        glutSwapBuffers()
129

    
130
# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)  
131
def keyPressed(*args):
132
        global window
133
        # If escape is pressed, kill everything.
134
        if args[0] == ESCAPE:
135
                sys.exit()
136

    
137
def main():
138
        global window
139
        # pass arguments to init
140
        glutInit(sys.argv)
141

    
142
        # Select type of Display mode:   
143
        #  Double buffer 
144
        #  RGBA color
145
        # Alpha components supported 
146
        # Depth buffer
147
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
148
        
149
        # get a 640 x 480 window 
150
        glutInitWindowSize(640, 480)
151
        
152
        # the window starts at the upper left corner of the screen 
153
        glutInitWindowPosition(0, 0)
154
        
155
        # Okay, like the C version we retain the window id to use when closing, but for those of you new
156
        # to Python (like myself), remember this assignment would make the variable local and not global
157
        # if it weren't for the global declaration at the start of main.
158
        window = glutCreateWindow("glLists by RISC")
159

    
160
           # Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
161
        # set the function pointer and invoke a function to actually register the callback, otherwise it
162
        # would be very much like the C version of the code.    
163
        glutDisplayFunc(DrawGLScene)
164
        
165
        # Uncomment this line to get full screen.
166
        #glutFullScreen()
167

    
168
        # When we are doing nothing, redraw the scene.
169
        glutIdleFunc(DrawGLScene)
170
        
171
        # Register the function called when our window is resized.
172
        glutReshapeFunc(ReSizeGLScene)
173
        
174
        # Register the function called when the keyboard is pressed.  
175
        glutKeyboardFunc(keyPressed)
176

    
177
        # Initialize our window. 
178
        InitGL(640, 480)
179

    
180
        # Start Event Processing Engine    
181
        glutMainLoop()
182

    
183
# Print message to console, and kick off the main to get it rolling.
184
if __name__ == "__main__":
185
        print "Hit ESC key to quit."
186
        main()
187