-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLReels.py
293 lines (193 loc) · 7.26 KB
/
GLReels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
GL algo
Drawing
1. Get the size of the client window.
2. Determine size of cylinder such that the appropriate amount of symbols to be displayed, are displayed. Also determine the size necessary to fit all the reels.
3. Create a texture out of the symbols that corresponds with the appropriate sizing.
4. Wrap Reel in textures.
Animating
1. Randomly determine the order in which each reel will stop.
2. Spin the suckas...
"""
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys
import wx
import os
import cfg
import math
def fitScreen():
#get viewport origin and extent
global xpos, inset, radius, quad_width, theta
viewport = glGetIntegerv(GL_VIEWPORT)
#get modelview & projection matrix information
modelview = glGetDoublev(GL_MODELVIEW_MATRIX)
projection = glGetDoublev(GL_PROJECTION_MATRIX)
reels = len(allstops)
quad_width = windowSize[0] / reels
winY = windowSize[1] / 2
count = 0
#coords = []
xpos = []
#quad_width = 20
for r in range(reels):
xpos.append([quad_width * (count + 1), quad_width * count, quad_width * count, quad_width * (count + 1)])
count+=1
faces = len(allstops[0])
theta = (2 * math.pi) / faces
radius = quad_width / math.sin(theta/2) / 2
inset = 0
def LoadTextures():
global textures
global allstops
global images
count = 0
stops = []
for s in allstops:
stops = stops + s
images = list(set(stops))
textures = glGenTextures(len(images))
for i in images:
imgpath = os.path.join(os.getcwd(), "images", i)
image = wx.Image(imgpath)
ix = image.GetSize()[0]
iy = image.GetSize()[1]
image = image.GetData()
# Create Texture
glBindTexture(GL_TEXTURE_2D, int(textures[count])) # 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGB, GL_UNSIGNED_BYTE, image)
count += 1
# A general OpenGL initialization function. Sets all of the initial parameters.
def InitGL(Width, Height): # We call this right after our OpenGL window is created.
LoadTextures()
glEnable(GL_TEXTURE_2D)
glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
glViewport(0, 0, Width, Height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity() # Reset The Projection Matrix
# Set up orthographic project here
#glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
glOrtho(0, Width, 0, Height, -1000, 0)
#gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLightfv(GL_LIGHT0, GL_AMBIENT, (0.5, 0.5, 0.5, 1.0)) # Setup The Ambient Light
glLightfv(GL_LIGHT0, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0)) # Setup The Diffuse Light
glLightfv(GL_LIGHT0, GL_POSITION, (0.0, 0.0, 2.0, 1.0)) # Position The Light
glEnable(GL_LIGHT0) # Enable Light One
fitScreen()
def drawCylinder(reelStops = [], xpos=[], xrot=0, stopAt=0):
#global images, textures
faces = len(reelStops)
#quad_width = (2 * radius * math.sin(theta/2)) / 2
stopAngle = theta * stopAt - (theta/2)
a = 0
b = 0
lastz = a + radius
lasty = b
#glTranslate(0.0, windowSize[0]/2, 0.0)
glRotatef(xrot,1.0,0.0,0.0)
for f in range(1, faces+1):
face = reelStops[f-1]
texture_num = images.index(face)
angle = theta * f
z = a + (radius * math.cos(angle))
y = b + (radius * math.sin(angle))
#print texture_num, textures[texture_num]
glBindTexture(GL_TEXTURE_2D, int(textures[texture_num]))
glBegin(GL_QUADS)
glTexCoord2f(1.0, 1.0); glVertex3f(xpos[0], lasty, lastz)
glTexCoord2f(0.0, 1.0); glVertex3f(xpos[1], lasty, lastz)
glTexCoord2f(0.0, 0.0); glVertex3f(xpos[2], y, z)
glTexCoord2f(1.0, 0.0); glVertex3f(xpos[3], y, z)
lastz = z
lasty = y
glEnd() #done drawing the reel
glRotatef(-xrot,1.0,0.0,0.0)
deg = math.degrees(stopAngle)
if deg < 0:
return 360 - abs(deg)
else:
return deg
# The main drawing function.
def DrawGLScene():
global xrot, textures, texture_num, settle, radius, inset, inc, settle
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear The Screen And The Depth Buffer
#inset = -5.0
glLoadIdentity() # Reset The View
glTranslatef(0.0,windowSize[1]/2 - quad_width/2,0.0) # Move Into The Screen
#glBindTexture(GL_TEXTURE_2D, int(textures[texture_num]))
#glEnable(GL_LIGHTING)
stopAngles = []
for s, p, r, stop in zip(allstops, xpos, xrot, stopAt):
sa = drawCylinder(s, p, r, stop)
stopAngles.append(sa)
#drawCylinder(allstops[1], 1.5, 0, xrot)
#drawCylinder(allstops[2], 1.5, .65, xrot)
#glBindTexture(GL_TEXTURE_2D, textures[0])
if xrot[0] >= 1440:
settle = True
xrot = map(lambda x: x % 360, xrot)
if settle:
inc = inc - 1
if inc < 1:
inc = 1
count = 0
for xr, sa in zip(xrot, stopAngles):
if int(xr % 360) == int(sa):
print xr, sa
xrot[count] = xr
else:
print xr, sa
xrot[count] = xr + inc
count += 1
else:
xrot = map(lambda x: x + inc, xrot)
#inc = inc - 0.05
#print inc
# since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers()
def keyPressed(key, x, y):
global settle, inc
# If escape is pressed, kill everything.
if key == 's':
settle = False
#xrot = map(lambda x: x % 360, xrot)
#SPIN!
inc = 30
def main():
global window
global allstops, stopAt, settle, windowSize, xrot, inc
inc = 0
glutInit(sys.argv)
allstops = [
[cfg.IM_CHERRIES, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_BELL],
[cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_CLOVER],
[cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_BAR],
[cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_CLOVER, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_CHERRIES, cfg.IM_BELL, cfg.IM_BAR, cfg.IM_BAR]]
stopAt = [1, 5, 10, 15]
xrot = [0.0] * len(allstops)
settle = False
windowSize = (200, 200)
#Set Display Mode
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(windowSize[0], windowSize[1])
# the window starts at the upper left corner of the screen
glutInitWindowPosition(0, 0)
window = glutCreateWindow("GLReels.py")
# Register the drawing function with glut
glutDisplayFunc(DrawGLScene)
# When we are doing nothing, redraw the scene.
glutIdleFunc(DrawGLScene)
glutKeyboardFunc(keyPressed)
# Initialize our window.
InitGL(windowSize[0], windowSize[1])
# Start Event Processing Engine
glutMainLoop()
main()