/
PygameWrapperPlane.py
executable file
·386 lines (327 loc) · 15 KB
/
PygameWrapperPlane.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 British Broadcasting Corporation and Kamaelia Contributors(1)
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
# http://www.kamaelia.org/AUTHORS - please extend this file,
# not this notice.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------
"""\
=====================
General 3D Object
=====================
TODO
"""
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame, pygame.image
from pygame.locals import *
from Display3D import Display3D
from Util3D import *
from Intersect3D import *
import Axon
textures = [0,0]
class PygameWrapperPlane(Axon.Component.component):
Inboxes = {
"inbox": "used to handle events",
"control": "ignored",
# 3D control
# "translation" : "receive 3D movement Vectors here"
# "rotation": "receive 3D rotation Vectors here",
# "scaling": "receive 3D scaling Vectors here",
# "rel_translation" : "receive 3D movement Vectors here"
# "rel_rotation": "receive 3D rotation Vectors here",
# "rel_scaling": "receive 3D scaling Vectors here",
"control3d": "receive Control3D commands here",
"wrapcallback": "receive wrap data after WRAPREQUEST",
"eventrequests": "receive event requests from wrapped component here",
}
Outboxes = {
"outbox": "not used",
"display_signal" : "Outbox used for communicating to the display surface",
# 3D status
"position" : "send position status when updated",
"rotation": "send rotation status when updated",
"scaling": "send scaling status when updated",
"wrapped_eventsfeedback": "Used to send events to wrapped pygame comp",
}
def __init__(self, **argd):
super(PygameWrapperPlane, self).__init__()
self.size = argd.get("size", Vector(2,2,2))
self.pos = argd.get("pos",Vector(0,0,-15))
self.rot = Vector(0.0,0.0,0.0)
self.scaling = argd.get("scaling",Vector(1,1,1))
self.transform = Transform()
self.oldrot = Vector()
self.oldpos = Vector()
self.oldscaling = Vector()
self.name = argd.get("name", "nameless")
self.tex = argd.get("tex", None)
self.grabbed = 0
self.texname = 0
self.tex_w = 0
self.tex_h = 0
self.width = 0
self.height= 0
self.wrapped_comp = argd.get("wrap")
self.events_wanted = {}
# vertices for intersection test
self.vertices = []
self.transformedVertices = []
# similar to Pygame component registration
self.disprequest = { "3DDISPLAYREQUEST" : True,
# "callback" : (self,"callback"),
"events" : (self, "inbox"),
# "size": self.size,
# "pos": self.pos,
"object": self }
# Ray intersection test
# returns the distance of the origin o to the point of intersection
# if no intersection occurs, 0 is returned
# Algorithm from "Realtime Rendering"
def intersectRay(self, o, d):
self.transformedVertices = [self.transform.transformVector(v) for v in self.vertices]
t = Intersect3D.ray_Polygon(o, d, self.transformedVertices)
return t
def applyTransforms(self):
# generate new transformation matrix if needed
if self.oldscaling != self.scaling or self.oldrot != self.rot or self.oldpos != self.pos:
self.transform.reset()
self.transform.applyScaling(self.scaling)
self.transform.applyRotation(self.rot)
self.transform.applyTranslation(self.pos)
if self.oldscaling != self.scaling:
self.send(self.scaling, "scaling")
self.oldscaling = self.scaling.copy()
if self.oldrot != self.rot:
self.send(self.rot, "rotation")
self.oldrot = self.rot.copy()
if self.oldpos != self.pos:
self.send(self.pos, "position")
self.oldpos = self.pos.copy()
def draw(self):
glMatrixMode(GL_MODELVIEW)
# set texure
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texname)
# set generated matrix
glPushMatrix()
glLoadMatrixf(self.transform.getMatrix())
w = self.width/200.0
h = self.height/200.0
# print "size", self.width, self.height
# print "draw", w,h
# draw faces
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
glBegin(GL_QUADS)
glColor3f(0,1,0)
glTexCoord2f(0.0, 1.0-self.tex_h); glVertex3f(-w, -h, 0.0)
glTexCoord2f(self.tex_w, 1.0-self.tex_h); glVertex3f( w, -h, 0.0)
glTexCoord2f(self.tex_w, 1.0); glVertex3f( w, h, 0.0)
glTexCoord2f(0.0, 1.0); glVertex3f(-w, h, 0.0)
glEnd()
glPopMatrix()
glDisable(GL_TEXTURE_2D)
def handleEventRequests(self):
while self.dataReady("eventrequests"):
message = self.recv("eventrequests")
if message.get("ADDLISTENEVENT", None) is not None:
self.events_wanted[message["ADDLISTENEVENT"]] = True
elif message.get("REMOVELISTENEVENT", None) is not None:
self.events_wanted[message["REMOVELISTENEVENT"]] = False
def handleEvents(self):
while self.dataReady("inbox"):
for event in self.recv("inbox"):
# If movementMode is True, translate input to movement commands
if event.type == pygame.MOUSEBUTTONUP:
if event.button in [1,3]:
self.grabbed = 0
if event.movementMode:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button in [1,3] and self.intersectRay(Vector(0,0,0), event.dir) > 0:
self.grabbed = event.button
if event.button == 4 and self.intersectRay(Vector(0,0,0), event.dir) > 0:
self.pos.z -= 1
if event.button == 5 and self.intersectRay(Vector(0,0,0), event.dir) > 0:
self.pos.z += 1
if event.type == pygame.MOUSEMOTION:
if self.grabbed == 1:
self.rot.y += float(event.rel[0])
self.rot.x += float(event.rel[1])
self.rot %= 360
if self.grabbed == 3:
self.pos.x += float(event.rel[0])/10.0
self.pos.y -= float(event.rel[1])/10.0
# If movementMode is False, forward events to wrapped component
else:
wanted = False
#test if event is wanted by wrapped component
try: wanted = self.events_wanted[event.type]
except KeyError: pass
if wanted:
# print "Event forwarded to ", self.wrapped_comp.name
# test if ray intersects plane
t = self.intersectRay(Vector(0,0,0), event.dir);
# if an intersection was detected, map position on plane
if t != 0:
p = event.dir*t
Ap = p-self.transformedVertices[0]
AB = self.transformedVertices[1]-self.transformedVertices[0]
AD = self.transformedVertices[3]-self.transformedVertices[0]
x = Ap.dot(AB)/(AB.length()**2)
y = Ap.dot(AD)/(AD.length()**2)
event.pos = (x*self.width,y*self.height)
self.send([event], "wrapped_eventsfeedback")
# self.send("2D: (%2.2f, %2.2f); " % (x*self.width, y*self.height), "outbox")
def handleMovementCommands(self):
while self.dataReady("control3d"):
cmd = self.recv("control3d")
if cmd.type == Control3D.POSITION:
self.pos = cmd.amount
if cmd.type == Control3D.REL_POSITION:
self.pos += cmd.amount
if cmd.type == Control3D.ROTATION:
self.rot = cmd.amount
if cmd.type == Control3D.REL_ROTATION:
self.rot = (self.rot+cmd.amount)%360
if cmd.type == Control3D.SCALING:
self.scaling = cmd.amount
if cmd.type == Control3D.REL_SCALING:
self.scaling += cmd.amount
def waitBox(self,boxname):
"""Generator. yields 1 until data ready on the named inbox."""
waiting = True
while waiting:
if self.dataReady(boxname): return
else: yield 1
def main(self):
displayservice = Display3D.getDisplayService()
self.link((self,"display_signal"), displayservice)
self.send(self.disprequest, "display_signal");
while 1:
try:
self.wraprequest = { "WRAPPERREQUEST" : True,
"wrapcallback" : (self, "wrapcallback"),
"eventrequests" : (self, "eventrequests"),
"surface": self.wrapped_comp.display }
self.send( self.wraprequest, "display_signal")
break
except AttributeError:
yield 1
for _ in self.waitBox("wrapcallback"): yield 1
b = self.recv("wrapcallback")
self.texname = b.texname
self.tex_w = b.tex_w
self.tex_h = b.tex_h
self.width = float(b.width)
self.height = float(b.height)
#prepare vertices for intersection test
x = self.width/200.0
y = self.height/200.0
self.vertices = [ Vector(-x, y, 0.0), Vector(x, y, 0.0), Vector(x, -y, 0.0), Vector(-x, -y, 0.0) ]
# setup event communications
if b.eventservice is not None:
self.link((self, "wrapped_eventsfeedback"), b.eventservice)
while 1:
yield 1
self.handleEventRequests()
self.handleEvents()
self.handleMovementCommands()
self.applyTransforms()
self.draw()
# Later it might be a good idea to provide a set of drawing functions
# so the component developer does not need to know about opengl
# This way opengl could later easily be replaced by an other mechanism
# for drawing
# e.g. TOGRA
if __name__=='__main__':
class Bunch: pass
class CubeRotator(Axon.Component.component):
def main(self):
while 1:
yield 1
self.send( Control3D(Control3D.REL_ROTATION, Vector(0.1, 0.1, 0.1)), "outbox")
class CubeMover(Axon.Component.component):
def main(self):
x,y,z = 3.0, 3.0, -20.0
dx = -0.03
dy = -0.03
dz = -0.03
while 1:
yield 1
self.send( Control3D(Control3D.POSITION, Vector(x, y, z)), "outbox")
x +=dx
y +=dy
z +=dz
if abs(x)>5: dx = -dx
if abs(y)>5: dy = -dy
if abs(z+20)>10: dz = -dz
# print x, y, abs(x), abs(y)
import random
class CubeBuzzer(Axon.Component.component):
def main(self):
r = 1.00
f = 0.01
while 1:
yield 1
if r>1.0: f -= 0.001
else: f += 0.001
r += f
self.send( Control3D(Control3D.SCALING, Vector(r, r, r)), "outbox")
text = """\
All objects in this scene can be moved when the CTRL
key is pressed. Otherwise all interaction gets translated
to pygame events. Try it on the button.
The size of these 2 Ticker components is (350,250).
The wrapped button is now fully functional (assigned to SPACE).
Bottom left there is a Magna Doodle (tm) component. You can draw
green lines on it by using your left mouse button. Use the right mouse
button to erase your artwork.
"""
class datasource(Axon.Component.component):
def main(self):
for x in text.split():
self.send(x,"outbox")
yield 1
from Kamaelia.Util.ConsoleEcho import consoleEchoer
from Kamaelia.Util.Graphline import Graphline
from Kamaelia.UI.Pygame.Ticker import Ticker
from Kamaelia.UI.Pygame.Button import Button
from SimpleCube import *
import sys;
sys.path.append("../pygame/")
from MagnaDoodle import *
sys.path.append("../../MPS/Systems/Paint/")
from Paint import *
Display3D.getDisplayService()[0].overridePygameDisplay()
TEXT = datasource().activate()
TICKER1 = Ticker(position = (400, 300), render_left = 0, render_right=350, render_top=0, render_bottom=250).activate()
TICKER1WRAPPER = PygameWrapperPlane(wrap=TICKER1, pos=Vector(-2, 1,-10), name="1st Wrapper Plane").activate()
TICKER2 = Ticker(position = (400, 300), render_left = 0, render_right=350, render_top=0, render_bottom=250).activate()
TICKER2WRAPPER = PygameWrapperPlane(wrap=TICKER2, pos=Vector(2, 1,-10), name="2nd Wrapper Plane").activate()
BUTTON = Button(caption="This button...",msg="...can be moved AND activated!", key=pygame.K_SPACE).activate()
BUTTONWRAPPER = PygameWrapperPlane(wrap=BUTTON, pos=Vector(0, 1.5,-5), name="2nd Wrapper Plane").activate()
MAGNADOODLE = MagnaDoodle(size=(255,255)).activate()
MAGNADOODLEWRAPPER = PygameWrapperPlane(wrap=MAGNADOODLE, pos=Vector(-2, -2,-10), name="Magna Doodle Wrapper Plane").activate()
ECHO = consoleEchoer().activate()
CUBE = SimpleCube(pos = Vector(2,-2,-10)).activate()
CUBEROTATOR = CubeRotator().activate()
TICKER1WRAPPER.link((TICKER1WRAPPER, "outbox"), (TICKER2, "inbox"))
TICKER2WRAPPER.link((TICKER2WRAPPER, "outbox"), (TICKER2, "inbox"))
BUTTON.link((BUTTON, "outbox"), (TICKER2, "inbox"))
TEXT.link((TEXT, "outbox"), (TICKER1, "inbox"))
CUBEROTATOR.link((CUBEROTATOR,"outbox"), (CUBE, "control3d"))
Axon.Scheduler.scheduler.run.runThreads()