-
Notifications
You must be signed in to change notification settings - Fork 1
/
boatRender.py
408 lines (330 loc) · 9.68 KB
/
boatRender.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''
3D Rotating Monkey Head
========================
This example demonstrates using OpenGL to display a rotating monkey head. This
includes loading a Blender OBJ file, shaders written in OpenGL's Shading
Language (GLSL), and using scheduled callbacks.
The monkey.obj file is an OBJ file output from the Blender free 3D creation
software. The file is text, listing vertices and faces and is loaded
using a class in the file objloader.py. The file simple.glsl is
a simple vertex and fragment shader written in GLSL.
'''
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.core.image import Image
from kivy.graphics import BindTexture
from kivy.uix.widget import Widget
from kivy.resources import resource_find
from kivy.graphics.transformation import Matrix
from kivy.graphics.opengl import *
from kivy.graphics import *
from objloader import ObjFile
from modelsLoader import modelsLoader
import math as m
from kivy.properties import ListProperty, ObjectProperty, NumericProperty
class Renderer(Widget):
mesh_texture = ObjectProperty(None)
r = 0.0
aniSteps = 0.86
aniApli = 1.0
sailG = 0.0
meshs = {
'heel': [None, 0.0 ],
'pitch': [None, 0.0 ]
}
cam = [0.8965517241379324, -0.8965517241379324, -0.6206896551724128]
boatRot = [0.0,0.0,0.0]
camPon = None
objs = {}
objRot = {}
def __init__(self, **kwargs):
self.canvas = RenderContext(compute_normal_mat=True)
self.canvas.shader.source = resource_find('simple.glsl')
ml = modelsLoader("boat2")
self.o = ml.getObjects()
scenRos = ObjFile(resource_find("./3dModels/3d_roseta.obj"))
self.o['roseta'] = scenRos.objects[ list(scenRos.objects.keys())[0] ]
#print("roseta[%s]"%self.o['roseta'])
super(Renderer, self).__init__(**kwargs)
with self.canvas:
self.cb = Callback(self.setup_gl_context)
PushMatrix()
self.setup_scene()
PopMatrix()
self.cb = Callback(self.reset_gl_context)
def on_displayNow(self):
self.scheduler = Clock.schedule_interval(self.update_glsl, 1 / 60.)
self.openScreenAnimation()
def on_noMoreDisplayd(self):
try:
Clock.unschedule(self.scheduler)
except:
print("no running scheduler for boatRender")
def setGui(self,gui):
self.gui = gui
def openScreenAnimation(self):
print("openScreenAnimation")
self.cam = [9.586206896551722, -14.0, 0.0]
aniStepsOld = self.aniSteps
self.aniSteps = 0.00
self.update_glsl(forceUpdate=True)
self.update_glsl(forceUpdate=True)
self.update_glsl(forceUpdate=True)
#self.cam = [0.8965517241379324, -0.8965517241379324, -0.6206896551724128]
self.cam = [0.6028708133971286, -6.765550239234449, -14.0]
self.aniSteps = aniStepsOld
def setCamera(self,axis,angle):
self.cam[axis] = angle
print(self.cam)
def setBoatRotation(self,axis, val):
self.boatRot[axis] = val
def getMainSailObj(self):
return self.o['sailMain_sailup_onPower']
def setSail(self, angle):
#self.getMainSail().mrot[1].angle = -angle
self.o['boom'].mrot[1].angle = -angle
self.o['sailGenoa'].sailAngle.angle = angle*1.1
if angle > 0:
self.sailGenoa_scale.x = 1
self.sailMain_scale.x = 1
else:
self.sailGenoa_scale.x = -1
self.sailMain_scale.x = -1
def setHeel(self, angle):
self.setBoatRotation(2, angle )
def setPitch(self,angle):
self.setBoatRotation(0, angle )
def setRoseta(self, angle):
self.o['roseta'].mrot[1].angle = self.AnimateValue(self.o['roseta'].mrot[1].angle, -angle)
def update(self, fromWho, vals):
#print("boatRender.update fromWho",fromWho," - ",vals)
if self.gui.rl.current == "Model Screen" and fromWho == 'comCal':
self.setRoseta(vals[0])
def setArrowsAccel(self, axis=[]):
#print("\n\nsetArrowsAccel",axis)
for i,a in enumerate("XYZ"):
axis_ = axis[i]
#print(" -- > ",i,a)
dif = axis_['last']-axis_['avg']
fdif = m.fabs(dif)*10.0
#print(i,a,fdif)
if a == "X":
self.arrowAxisN_scale = self.arrowAxisX_scale
self.arrowAxisNm_scale = self.arrowAxisXm_scale
elif a == "Y":
self.arrowAxisN_scale = self.arrowAxisY_scale
self.arrowAxisNm_scale = self.arrowAxisYm_scale
elif a == "Z":
self.arrowAxisN_scale = self.arrowAxisZ_scale
self.arrowAxisNm_scale = self.arrowAxisZm_scale
if dif > 0.0:
self.arrowAxisN_scale.x = 0.0
self.arrowAxisN_scale.y = 0.0
self.arrowAxisN_scale.z = 0.0
self.arrowAxisNm_scale.x = fdif
self.arrowAxisNm_scale.y = fdif
self.arrowAxisNm_scale.z = 1.0
else:
self.arrowAxisN_scale.x = -fdif
self.arrowAxisN_scale.y = -fdif
self.arrowAxisN_scale.z = -fdif
self.arrowAxisNm_scale.x = 0.0
self.arrowAxisNm_scale.y = 0.0
self.arrowAxisNm_scale.z = 0.0
def setup_gl_context(self, *args):
glEnable(GL_DEPTH_TEST)
def reset_gl_context(self, *args):
glDisable(GL_DEPTH_TEST)
def AnimateValue(self, sv, ev):
return sv*self.aniSteps+ev*(1.00-self.aniSteps)
def update_glsl(self, *largs, forceUpdate=False):
if forceUpdate == False:
if self.gui:
if self.gui.rl.current != "Model Screen":
return None
#print("Model Screen")
asp = 200.00 / float(200.00)
proj = Matrix().view_clip(
-asp,asp,-1, 1, 1, 100, 1)
self.canvas['projection_mat'] = proj
self.canvas['diffuse_light'] = (1.0, 1.0, 0.8)
self.canvas['ambient_light'] = (0.5, 0.5, 0.5)
self.rot.angle += 0.2
"""
for i in self.meshs.keys():
m = self.meshs[i]
m[0].angle = m[0].angle*self.aniSteps+m[1]*(1.00-self.aniSteps)
"""
boat = self.o["boat"]
if self.gui.animation:
self.camPos.x = self.AnimateValue(self.camPos.x,self.cam[0])
self.camPos.y = self.AnimateValue(self.camPos.y,self.cam[1])
self.camPos.z = self.AnimateValue(self.camPos.z,self.cam[2])
for i in range(0,3,1):
boat.mrot[i].angle = self.AnimateValue(boat.mrot[i].angle, self.boatRot[i])
else:
self.camPos.x = self.cam[0]
self.camPos.y = self.cam[1]
self.camPos.z = self.cam[2]
for i in range(0,3,1):
boat.mrot[i].angle = self.boatRot[i]
#print(self.cam)
sa = self.o['boom'].mrot[1].angle
if sa <= 0.0:
self.getMainSailObj().scale = (1,-1,1,1)
else:
self.getMainSailObj().scale = (1,1,1,1)
self.setSail( -sa - self.sailG )
if 70.00 > sa > -70.0:
self.sailG-= self.boatRot[2]*0.01
else:
self.sailG = 0.0
if sa > 0:
self.setSail(-69.99)
else:
self.setSail(69.99)
#print(".")
def setup_scene(self):
def _draw_element(m):
m.mrot = [Rotate(0,1,0,0),Rotate(0,0,1,0),Rotate(0,0,0,1)]
m.mrot = [Rotate(0,-1,0,0),Rotate(0,0,-1,0),Rotate(0,0,0,-1)]
m.mrot[0].angle = 0.0
m.mrot[1].angle = 0.0
m.mrot[2].angle = 0.0
texture = None
if 0:#m.textureFile != -1:
print("- mesh with texture ",m.textureFile)
print(" 1",self.mesh_texture)
self.mesh_texture = Image.load('/tmp/g.jpg').texture
self.mesh_texture.wrap = 'repeat'
print(" 2",self.mesh_texture)
mm = Mesh(
vertices=m.vertices,
indices=m.indices,
fmt=m.vertex_format,
mode='triangles',
texture = self.mesh_texture
)
else:
print("- mesh without texture")
Mesh(
vertices=m.vertices,
indices=m.indices,
fmt=m.vertex_format,
mode='triangles',
)
Color(1, 1, 1,1)
self.camPos = Translate(1,1,1)
Rotate(90,90,0)
PushMatrix()
Translate(0, -3, -12)
self.rot = Rotate(1, 0, 1, 0)
UpdateNormalMatrix()
o = self.o
# what to display
if True:
PushMatrix()
_draw_element(o['boat'])
PushMatrix()
_draw_element(o['boom'])
self.sailMain_scale = Scale(1,1,1)
_draw_element(o['sailMain_sailup_onPower'])
PopMatrix()
PushMatrix()
UpdateNormalMatrix()
o['sailGenoa'] = o['sailGenoa_onPower']
#Rotate(24,-1,0,0)
Translate(0,0,-7)
o['sailGenoa'].sailAngle = Rotate(1,0,1,0.39)
Translate(0,0,7)
self.sailGenoa_scale = Scale(1,1,1)
_draw_element(o['sailGenoa'])
PopMatrix()
PushMatrix()
_draw_element(o['ruder'])
_draw_element(o['tiler'])
PopMatrix()
PopMatrix()
"""
keysAxis = "XYZ"
keysSufix = " m"
for axis in keysAxis:
print("c",axis)
for sufix in keysSufix:
if sufix == " ":
sufix = ""
o["aAcc%s%s"%(axis,sufix)] = o['arrowAccel']
PushMatrix()
Translate(10,0,0)
PushMatrix()
self.arrowAxisX_scale = Scale(1,1,1)
_draw_element(o['aAccX'])
PopMatrix()
Translate(-20,0,0)
Rotate(180,0,1,1)
PushMatrix()
self.arrowAxisXm_scale = Scale(1,1,1)
_draw_element(o['aAccXm'])
PopMatrix()
PopMatrix()
PushMatrix()
Translate(0,0,-10)
Rotate(90,0,1,0)
PushMatrix()
self.arrowAxisY_scale = Scale(1,1,1)
_draw_element(o['aAccY'])
PopMatrix()
Translate(-20,0,0)
Rotate(180,0,1,1)
PushMatrix()
self.arrowAxisYm_scale = Scale(1,1,1)
_draw_element(o['aAccYm'])
PopMatrix()
PopMatrix()
PushMatrix()
Translate(0,10,0)
Rotate(90,0,0,1)
PushMatrix()
self.arrowAxisZ_scale = Scale(1,1,1)
_draw_element(o['aAccZ'])
PopMatrix()
Translate(-10,0,0)
Rotate(180,0,1,1)
PushMatrix()
self.arrowAxisZm_scale = Scale(1,1,1)
_draw_element(o['aAccZm'])
PopMatrix()
PopMatrix()
"""
PushMatrix()
Translate(0,-0.5,0)
Scale(10.5,1,5,1.5)
_draw_element(o['roseta'])
o['roseta'].z = -50.0
PopMatrix()
else:
PushMatrix()
_draw_element(o['Cube'])
PopMatrix()
"""
for ok in self.scene.objects:
PushMatrix()
print(" draw obj [%s]"%ok)
o = self.scene.objects[ok]
_draw_element( o )
PopMatrix()
"""
"""
boat = self.scene.objects['boat']
_draw_element(boat)
sail = self.scene.objects['sailsMain_Plane']
_draw_element(sail)
"""
PopMatrix()
#self.setArrowsAccel()
class RendererApp(App):
def build(self):
return Renderer()
if __name__ == "__main__":
RendererApp().run()