-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
229 lines (188 loc) · 7.13 KB
/
main.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
# coding: utf-8
"""
This is purely an example file that is also used to test the library
Attributes:
VERTEX_SHADER_SOURCE (str): Constant variable to store the
Vertex Shader source code
from a file
FRAGMENT_SHADER_SOURCE (str): Constant Variable to store the
Fragment Shader source code
from a file
glviewv (OpenGLES.GLKit.view.GLKView): The object to store an
OpenGLES rendering context
and present it to screen
"""
import ui
import sys
import time
import math
import copy
import time
import random
from objc_util import *
import OpenGLES.Util
import OpenGLES.Util.Model
import OpenGLES.Util.Shader
import OpenGLES.GLES as GLES
import OpenGLES.EAGL as EAGL
import OpenGLES.Util as Util
import OpenGLES.GLKit as GLKit
from OpenGLES.GLES.gles1 import *
from OpenGLES.GLES.gles2 import *
from OpenGLES.Util import Physics
import OpenGLES.Util.LightsCameras as LightsCameras
from OpenGLES.Util.LightsCameras import PhysicsCamera
import OpenGLES.GLKit.texture as texture
from OpenGLES.GLKit.glkmath import matrix4 as m4
from OpenGLES.GLKit.glkmath import vector3 as v3
reload(GLKit.view)
OpenGLES.Util.Physics.resetPhysicsWorld()
PhysicsWorld = OpenGLES.Util.Physics.getPhysicsWorld()
LightsCameras.setPhysicsWorld(PhysicsWorld)
Util.Model.setPhysicsWorld(PhysicsWorld)
MAX_DIST = 100
with open("shader.vs", "rb") as f:
VERTEX_SHADER_SOURCE = f.read()
with open("shader.fs", "rb") as f:
FRAGMENT_SHADER_SOURCE = f.read()
glviewv = GLKit.GLKView(frame=(0, 0, 800, 600))
at_front = False
def physics_info(sender):
global at_front
global PhysicsWorld
if at_front:
PhysicsWorld.js.send_to_back()
at_front = False
else:
PhysicsWorld.js.bring_to_front()
at_front = True
btn = ui.ButtonItem()
btn.title = "Physics Info"
btn.action = physics_info
def change(sender):
glviewv.tc.accelerometer_speed = 1 + sender.value * 5
setting_front = False
setting_view = ui.View()
setting_view.background_color = '#FFFFFF'
setting_view.width = 800
setting_view.height = 300
accelerometer_speed = ui.Slider()
accelerometer_speed.action = change
accelerometer_speed.continuous = True
setting_view.add_subview(accelerometer_speed)
def settings(sender):
global setting_front
if setting_front:
setting_front = False
setting_view.send_to_back()
else:
setting_front = True
setting_view.bring_to_front()
sbtn = ui.ButtonItem()
sbtn.image = ui.Image.named('iob:ios7_gear_outline_32')
sbtn.action = settings
glviewv.left_button_items = [btn, sbtn]
class Renderer(Util.RenderCycle):
def __init__(self):
Util.RenderCycle.__init__(self)
self.objects = []
for x in range(-10, 10, 4):
for y in range(10, 14, 4):
for z in range(-10, 10, 4):
o1 = Util.Model.PhysicsObject("test_model.xml", v3.GLKVector3Make(x, y, z))
self.objects.append(o1)
for _ in range(0, 25):
o1 = Util.Model.PhysicsObject("test_model.xml", v3.GLKVector3Make(0, 0, 0))
self.objects.append(o1)
o1 = Util.Model.XMLModel("plane.xml", v3.GLKVector3Make(-20, 0, -20))
o1.model = m4.GLKMatrix4Scale(o1.model, 100, 100, 100)
self.objects.append(o1)
self.v = Util.Shader.ShaderSource(VERTEX_SHADER_SOURCE, GL_VERTEX_SHADER)
self.f = Util.Shader.ShaderSource(FRAGMENT_SHADER_SOURCE, GL_FRAGMENT_SHADER)
self.sp = Util.Shader.ShaderProgram(self.v, self.f)
self.eye = PhysicsCamera(v3.GLKVector3Make(-20, 10, -20), yaw=0, pitch=0)
self.projection = m4.GLKMatrix4MakePerspective(45.0, 800.0/600.0, 0.1, 1000.0)
self.view = self.eye.view
self.model = m4.GLKMatrix4Identity()
self.rt = 0
self.ut = 0
self.texture = None
glviewv.add_subview(setting_view)
setting_view.send_to_back()
def setup(self, context):
if EAGL.setCurrentContext(context):
self.sp.build()
self.sp.bind()
# self.texture = texture.loadTexture('test.png', 0)
print len(self.objects), " object/s in the world"
for rObj in self.objects:
rObj.setup_object()
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK)
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glViewport(0, 0, int(glviewv.width*2), int(glviewv.height*2))
glClearColor(0.1, 0.12, 0.45, 1.0)
PhysicsWorld.js.eval_js('startUpdates();')
glviewv.add_subview(PhysicsWorld.js)
PhysicsWorld.js.send_to_back()
else:
print "Could not Setup OpenGLES"
self.last = time.clock()
def teardown(self):
self.sp.teardown()
EAGL.setCurrentContext(None)
PhysicsWorld.js.eval_js('done();')
def update(self, dt):
start = time.clock()
end = time.clock()
# glviewv.name = "FPS: %i. Frames: %s" % (self.fps, self.framesDisplayed)
glviewv.name = "Render Time: %.3f\tUpdate Time: %.3f\tFrames: %i\tFPS: %i" % (self.rt, self.ut, self.framesDisplayed, self.fps)
su = time.clock()
for rObj in self.objects:
so = time.clock()
if rObj.distance_from_point(self.eye.position) < MAX_DIST:
rObj.update(dt)
rObj.renderable = True
else:
rObj.renderable = False
eo = time.clock()
eu = time.clock()
self.eye.update(dt)
self.view = self.eye.view
end = time.clock()
self.ut = end - start
def move_f(self, mdir):
mdir = [mdir[1], mdir[0]]
self.eye.move(*mdir)
def look_f(self, ldir):
self.eye.look(*ldir)
def render(self, context):
start = time.clock()
if EAGL.setCurrentContext(context):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, int(glviewv.width*2), int(glviewv.height*2))
self.sp.bind()
self.sp.uniform4x4("V", list(self.view.s1.m))
self.sp.uniform4x4("P", list(self.projection.s1.m))
for rObj in self.objects:
rObj.render(self.sp)
self.eye.debug_draw(self.sp)
else:
raise RuntimeError('Render Failed as context could not be set.')
end = time.clock()
# print 'render', (end - self.last)
self.rt = end - start
self.last = end
@on_main_thread
def main():
from ui import Image
i = Image.named('test:Lenna')
with open('test.png', 'wb') as f:
f.write(i.to_png())
contextc = EAGL.EAGLContext(EAGL.RenderingAPI.OpenGLES2)
GLKit.setRenderEngine(Renderer())
glviewv.setDelegate(GLKit.GLKViewDelegate())
glviewv.setContext(contextc)
glviewv.present("sheet")
main()