forked from pi3d/pi3d_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_environmentsphere.py
109 lines (90 loc) · 2.67 KB
/
camera_environmentsphere.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
#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals
""" Example showing what can be left out. ESC to quit"""
import demo
import pi3d
import numpy as np
import picamera
import picamera.array
import threading
import time
from math import cos, sin, radians
npa = None
new_pic = False
def get_pics():
global npa, new_pic
with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as output:
camera.resolution = (320, 240)
while True:
output.truncate(0)
camera.capture(output, 'rgb')
if npa is None:
npa = np.zeros(output.array.shape[:2] + (4,), dtype=np.uint8)
npa[:,:,3] = 255
npa[:,:,0:3] = output.array
new_pic = True
time.sleep(0.05)
t = threading.Thread(target=get_pics)
t.daemon = True
t.start()
while not new_pic:
time.sleep(0.1)
# Setup display and initialise pi3d
DISPLAY = pi3d.Display.create(x=100, y=100,
background=(0.2, 0.4, 0.6, 1))
shader = pi3d.Shader("uv_reflect")
flatsh = pi3d.Shader('uv_flat')
#========================================
# this is a bit of a one off because the texture has transparent parts
# comment out and google to see why it's included here.
from pi3d import opengles, GL_CULL_FACE
opengles.glDisable(GL_CULL_FACE)
#========================================
# load bump and reflection textures
bumptex = pi3d.Texture("textures/floor_nm.jpg")
shinetex = pi3d.Texture(npa)
# load model_loadmodel
mymodel = pi3d.Model(file_string='models/teapot.obj', name='teapot')
mymodel.set_shader(shader)
mymodel.set_normal_shine(bumptex, 0.0, shinetex, 0.7)
mysphere = pi3d.Sphere(radius=400.0, rx=180, ry=180, invert=True)
mysphere.set_draw_details(flatsh, [shinetex], vmult=3.0, umult=3.0)
# Fetch key presses
mykeys = pi3d.Keyboard()
mymouse = pi3d.Mouse(restrict=False)
mymouse.start()
omx, omy = mymouse.position()
CAMERA = pi3d.Camera.instance()
dist = 4.0
rot = 0.0
tilt = 0.0
while DISPLAY.loop_running():
k = mykeys.read()
if k >-1:
if k==ord('w'):
dist += 0.02
if k==ord('s'):
dist -= 0.02
elif k==27:
mykeys.close()
DISPLAY.destroy()
break
if new_pic:
shinetex.update_ndarray(npa)
new_pic = False
mx, my = mymouse.position()
rot -= (mx - omx)*0.4
tilt += (my - omy)*0.4
omx = mx
omy = my
CAMERA.reset()
CAMERA.rotate(-tilt, rot, 0)
CAMERA.position((dist * sin(radians(rot)) * cos(radians(tilt)),
dist * sin(radians(tilt)),
-dist * cos(radians(rot)) * cos(radians(tilt))))
mymodel.draw()
mysphere.draw()
mymodel.rotateIncY(0.41)
mymodel.rotateIncZ(0.12)
mymodel.rotateIncX(0.23)