-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.py
350 lines (290 loc) · 10.4 KB
/
Scene.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
from __future__ import division
#####################################################################
# -*- coding: iso-8859-1 -*- #
# #
# Frets on Fire #
# Copyright (C) 2006 Sami Kyöstilä #
# #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation; either version 2 #
# of the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, #
# MA 02110-1301, USA. #
#####################################################################
from builtins import object
from past.utils import old_div
from Player import Player
from View import BackgroundLayer
from Session import MessageHandler, Message
from Input import KeyListener
from Camera import Camera
import Network
import Player
import Config
from OpenGL.GL import *
from OpenGL.GLU import *
#from OpenGL.GLUT import *
import math
import colorsys
import pygame
from numpy import array, transpose, reshape, dot
Config.define("network", "updateinterval", int, 72)
#stump: raised by a scene constructor to abort scene processing
class SuppressScene(Exception):
pass
# Messages from client to server
class CreateActor(Message): pass
class ControlEvent(Message): pass
# Messages from server to client
class ActorCreated(Message): pass
class ActorDeleted(Message): pass
class ActorData(Message): pass
class ControlData(Message): pass
class Actor(object):
def __init__(self, scene):
self.scene = scene
self.body = ode.Body(scene.world)
self.geom = None
self.mass = ode.Mass()
self.svgDrawing = None
self.scale = 1.0
self.rotation = 0
def getState(self):
return (self.body.getPosition(),
self.body.getQuaternion(),
self.body.getLinearVel(),
self.body.getAngularVel())
def setSvgDrawing(self, svgDrawing):
svgDrawing.convertToTexture(256, 256)
self.svgDrawing = svgDrawing
def setState(self, pos, quat, linearVel, angularVel):
self.body.setPosition(pos)
self.body.setQuaternion(quat)
self.body.setLinearVel(linearVel)
self.body.setAngularVel(angularVel)
def render(self):
s = self.svgDrawing
if not s:
return
x, y, z = self.body.getPosition()
modelview = glGetDoublev(GL_MODELVIEW_MATRIX)
projection = glGetDoublev(GL_PROJECTION_MATRIX)
viewport = glGetIntegerv(GL_VIEWPORT)
# calculate the z coordinate
m = transpose(reshape(modelview, (4, 4)))
wz = -dot(m, reshape((x, y, z, 1), (4, 1)))[2][0]
# don't draw anything if we're behind the viewer
if wz < 0.1:
return
# calculate the screen-space x and y coordinates
x, y, z = gluProject(x, y, z, modelview, projection, viewport)
scale = old_div(self.scale, wz)
s.transform.reset()
s.transform.translate(x, y)
s.transform.scale(scale, -scale)
s.transform.rotate(self.rotation)
s.draw()
class BoxActor(Actor):
def __init__(self, scene, owner, size = [1.0, 1.0, 1.0], density = 1.0):
Actor.__init__(self, scene)
self.owner = owner
self.size = size
self.mass.setBox(density, size[0], size[1], size[2])
self.geom = ode.GeomBox(self.scene.space, lengths = size)
self.geom.setBody(self.body)
import random
self.body.setPosition((random.random(), random.random(), random.random() * 3))
def render(self):
Actor.render(self)
x, y, z = self.body.getPosition()
R = self.body.getRotation()
T = array((R[0], R[3], R[6], 0,
R[1], R[4], R[7], 0,
R[2], R[5], R[8], 0,
x, y, z, 1), float)
glPushMatrix()
glMultMatrixd(T.tolist())
sx, sy, sz = self.size
glScalef(sx, sy, sz)
glDisable(GL_LIGHTING)
glEnable(GL_BLEND)
glBlendFunc(GL_ONE, GL_ONE)
glFrontFace(GL_CW)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glColor3f(1, 1, 1)
#glutSolidCube(1)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glEnable(GL_LIGHTING)
glFrontFace(GL_CCW)
glColor4f(*[abs(x) + .3 for x in self.body.getLinearVel() + (.5,)])
glScalef(.97, .97, .97)
#glutSolidCube(1)
glDisable(GL_LIGHTING)
glPopMatrix()
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
#glutInit([]) # FIXME: do we need glut?
class Scene(MessageHandler, BackgroundLayer):
def __init__(self, engine, owner, **args):
self.objects = Network.ObjectCollection()
self.args = args
self.owner = owner
self.engine = engine
self.actors = []
self.camera = Camera()
self.world = None
self.space = None
self.time = 0.0
self.actors = []
self.players = []
self.createCommon(**args)
def initPhysics(self):
import ode
# Create a default world and a space
self.world = ode.World()
self.world.setGravity((0, -9.81 , 0))
self.world.setERP(0.8)
self.world.setCFM(1e-5)
self.space = ode.Space()
self.contactGroup = ode.JointGroup()
def addPlayer(self, player):
self.players.append(player)
def removePlayer(self, player):
self.players.remove(player)
def createCommon(self, **args):
pass
def runCommon(self, ticks, world):
pass
def run(self, ticks):
self.time += ticks / 50.0
if self.world:
self.contactGroup.empty()
self.space.collide(None, self.nearCallback)
self.world.step(ticks / 512.0)
def nearCallback(self, args, geom1, geom2):
contacts = ode.collide(geom1, geom2)
for c in contacts:
c.setBounce(0.2)
c.setMu(5000)
j = ode.ContactJoint(self.world, self.contactGroup, c)
j.attach(geom1.getBody(), geom2.getBody())
def handleActorCreated(self, sender, id, owner, name):
actor = globals()[name](self, owner)
self.objects[id] = actor
self.actors.append(actor)
def handleActorDeleted(self, sender, id):
actor = self.objects[id]
self.actors.remove(actor)
del self.objects[id]
class SceneClient(Scene, KeyListener):
def __init__(self, engine, owner, session, **args):
Scene.__init__(self, engine, owner, **args)
self.session = session
self.player = self.session.world.getLocalPlayer()
self.multiplayers = self.session.world.getMultiplayers()
self.controls = engine.input.controls
self.createClient(**args)
def createClient(self, **args):
pass
def createActor(self, name):
self.session.sendMessage(CreateActor(name = name))
def shown(self):
self.engine.input.addKeyListener(self)
def hidden(self):
self.engine.input.removeKeyListener(self)
def keyPressed(self, key, str):
c = self.controls.keyPressed(key)
if c:
self.session.sendMessage(ControlEvent(flags = self.controls.flags))
return True
return False
def keyReleased(self, key):
c = self.controls.keyReleased(key)
if c:
self.session.sendMessage(ControlEvent(flags = self.controls.flags))
return True
return False
def handleControlData(self, sender, owner, flags):
pass
# TODO: player mapping
# for player in self.session.world.players:
# if player.owner == owner:
# player.controls.flags = flags
# break
def handleActorData(self, sender, id, data):
actor = self.objects[id]
actor.setState(*data)
def run(self, ticks):
self.runCommon(ticks, self.session.world)
Scene.run(self, ticks)
def render3D(self):
for actor in self.actors:
actor.render()
def render(self, visibility, topMost):
font = self.engine.data.font
# render the scene
try:
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluPerspective(60, self.engine.view.aspectRatio, 0.1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glPushMatrix()
self.camera.apply()
self.render3D()
finally:
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
class SceneServer(Scene):
def __init__(self, engine, owner, server, **args):
Scene.__init__(self, engine, owner, **args)
self.server = server
self.updateInterval = self.engine.config.get("network", "updateinterval")
self.updateCounter = 0
self.changedControlData = {}
def handleControlEvent(self, sender, flags):
self.changedControlData[sender] = flags
def handleControlData(self, sender, owner, flags):
# TODO: player mapping
pass
# for player in self.server.world.players:
# if player.owner == owner:
# player.controls.flags = flags
# break
def handleCreateActor(self, sender, name):
id = self.objects.generateId()
self.server.broadcastMessage(ActorCreated(owner = sender, name = name, id = id))
def handleSessionClosed(self, session):
for actor in self.actors:
if actor.owner == session.id:
id = self.objects.id(actor)
self.server.broadcastMessage(ActorDeleted(id = id))
def handleSessionOpened(self, session):
for actor in self.actors:
id = self.objects.id(actor)
session.sendMessage(ActorCreated(owner = actor.owner, name = actor.__name__, id = id))
def broadcastState(self):
for actor in self.actors:
id = self.objects.id(actor)
self.server.broadcastMessage(ActorData(id = id, data = actor.getState()), meToo = False)
for sender, flags in list(self.changedControlData.items()):
self.server.broadcastMessage(ControlData(owner = sender, flags = flags))
self.changedControlData = {}
def run(self, ticks):
self.runCommon(ticks, self.server.world)
Scene.run(self, ticks)
self.updateCounter += ticks
if self.updateCounter > self.updateInterval:
self.updateCounter %= self.updateInterval
self.broadcastState()