-
Notifications
You must be signed in to change notification settings - Fork 1
/
obj_octagon.lua
218 lines (179 loc) · 4.48 KB
/
obj_octagon.lua
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
-- obj_octagon.
--
-- @eigen
--
--
-- K1 held is SHIFT
--
-- E1: zoom in/out
-- E2: camera x axis
-- E3: camera y axis
--
-- SHIFT+K2: toggle multi-axis
-- SHIFT+E1: rotate x axis
-- SHIFT+E2: rotate y axis
-- SHIFT+E3: rotate z axis
-- K2: toggle auto-rotate
-- K3: emmergency stop
-- ------------------------------------------------------------------------
-- requires
include('lib/3d/utils/core')
local Polyhedron = include('lib/3d/polyhedron')
-- local Wireframe = include('lib/3d/wireframe')
local draw_mode = include('lib/3d/enums/draw_mode')
-- ------------------------------------------------------------------------
-- conf
local fps = 30
local model_filepath = "/home/we/dust/code/3d/model/octa-color.obj"
-- ------------------------------------------------------------------------
-- init
function init()
screen.aa(1)
-- screen.aa(0)
screen.line_width(1)
end
redraw_clock = clock.run(
function()
local step_s = 1 / fps
while true do
clock.sleep(step_s)
redraw()
end
end)
function cleanup()
clock.cancel(redraw_clock)
end
-- ------------------------------------------------------------------------
-- state
model = Polyhedron.new_from_obj(model_filepath)
-- model = Wireframe.new_from_obj(model_filepath)
-- init
cam = {0,0,-4} -- Initilise the camera position
mult = 64 -- View multiplier
a = flr(rnd(3))+1 -- Angle for random rotation
t = flr(rnd(50))+25 -- Time until next angle change
rot_speed = 0
rot_speed_a = {0,0,0} -- Independant angle rotation
independant_rot_a = False
prev_a = nil
random_angle = false
is_shift = false
-- ------------------------------------------------------------------------
-- USER INPUT
function key(id,state)
if id == 1 then
if state == 0 then
is_shift = false
else
is_shift = true
end
elseif id == 2 then
if state == 0 then
if is_shift then
independant_rot_a = not independant_rot_a
if not independant_rot_a then
print("independant angle rotation off")
rot_speed = rot_speed_a[1] + rot_speed_a[2] + rot_speed_a[3]
else
print("independant angle rotation on")
rot_speed_a = {0,0,0}
rot_speed_a[a] = rot_speed
end
else
random_angle = not random_angle
if not random_angle then
print("random rotation off")
rot_speed = 0
else
print("random rotation on")
independant_rot_a = false
rot_speed = 0.01
end
end
end
elseif id == 3 then
if state == 0 then
print("emergency stop!")
random_angle = False
rot_speed = 0
rot_speed_a = {0,0,0}
end
end
end
function enc(id,delta)
if id == 1 then
if is_shift then
a = 3
else
cam[3] = cam[3] + delta / 5
end
elseif id == 2 then
if is_shift then
a = 2
else
cam[1] = cam[1] - delta / 10
end
elseif id == 3 then
if is_shift then
a = 1
else
cam[2] = cam[2] - delta / 10
end
end
if is_shift then
local sign = 1
if delta < 0 then
sign = -1
end
if id == 2 then
sign = -sign
end
if not independant_rot_a then
if prev_a == a then
rot_speed = util.clamp(rot_speed + 0.01 * sign, -0.05, 0.05)
else
rot_speed = 0.01 * sign
end
prev_a = a
else
rot_speed_a[a] = util.clamp(rot_speed_a[a] + 0.005 * sign, -0.03, 0.03)
end
end
end
-- ------------------------------------------------------------------------
-- MAIN LOOP
function draw_v_as_circle(x, y, col)
if l then
screen.level(l)
end
local radius = 2
screen.move(x + radius, y)
screen.circle(x, y, radius)
screen.fill()
end
function redraw()
if random_angle then
t = t - 1 -- Decrease time until next angle change
if t <= 0 then -- If t is 0 then change the random angle and restart the timer
t = flr(rnd(50))+25 -- Restart timer
a = flr(rnd(3))+1 -- Update angle
end
end
local nClock = os.clock()
if not independant_rot_a then
model:rotate(a, rot_speed)
else
model:rotate(1, rot_speed_a[1])
model:rotate(2, rot_speed_a[2])
model:rotate(3, rot_speed_a[3])
end
-- print("rotation took "..os.clock()-nClock)
screen.clear()
nClock = os.clock()
model:draw(nil, draw_mode.FACES | draw_mode.WIREFRAME | draw_mode.POINTS, mult, cam,
{point_draw_fn = draw_v_as_circle,
point_level = 15,
line_level = 4})
-- print("drawing took "..os.clock()-nClock)
screen.update()
end