Skip to content

Commit 4da2c8d

Browse files
committed
all twist animations are now the right shape and direction, just colour to do
1 parent cbf8781 commit 4da2c8d

File tree

6 files changed

+121
-19
lines changed

6 files changed

+121
-19
lines changed

controller/src/cli.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ fn main() {
194194
return;
195195
}
196196
let gui_release = move||{let _ignored = sync_sender.send(());};
197+
enum TwistMode{
198+
Client(),
199+
Server(),
200+
}
201+
let mut twist_mode = TwistMode::Client();
197202
for ev in receiver.iter() {
198203
let result: Result<bool, SendError<client::FromGUI>> = (||{
199204
match ev {
@@ -236,6 +241,8 @@ fn main() {
236241
mprintln!(p, "\tsolved - move to the solved state");
237242
mprintln!(p, "\tstart - start the game (applies a scramble too)");
238243
mprintln!(p, "\ttwist <TWIST> - execute a twist on the cube. eg: \"twist U'\"");
244+
mprintln!(p, "\ttwistmode client - twists happen in the client, and the resulting raw state sent to the server");
245+
mprintln!(p, "\ttwistmode server - twists happend on the server, and the resulting raw state is pulled from the server");
239246
}
240247
,"show" => {
241248
let data = state.lock().unwrap();
@@ -299,12 +306,37 @@ fn main() {
299306
mprintln!(p, "twist requires one parameter");
300307
}
301308
let mut data = state.lock().unwrap();
302-
match data.cube.twists(args_str){
303-
Err(msg) => {mprintln!(p, "Error: {}", msg);}
304-
,Ok(_) => {
305-
sender.send(SyncState())?;
306-
draw(&gfx, &data, p);
307-
}
309+
match twist_mode{
310+
TwistMode::Client() => {
311+
match data.cube.twists(args_str){
312+
Err(msg) => {mprintln!(p, "Error: {}", msg);}
313+
,Ok(_) => {
314+
sender.send(SyncState())?;
315+
draw(&gfx, &data, p);
316+
}
317+
}
318+
},
319+
TwistMode::Server() => {
320+
if let Ok(t) = cube_model::Twist::seq_from_string(args_str){
321+
for t in t{
322+
sender.send(DoTwist(t));
323+
}
324+
sender.send(GetState());
325+
}
326+
else{
327+
mprintln!(p, "Invalid twist sequence.");
328+
}
329+
},
330+
}
331+
}
332+
"twistmode" => {
333+
if args.len() != 1{
334+
mprintln!(p, "twistmode requires one parameter");
335+
}
336+
match args[0]{
337+
"client" => {twist_mode = TwistMode::Client();},
338+
"server" => {twist_mode = TwistMode::Server();},
339+
a => {mprintln!(p, "unknown twist mode '{}'", a)},
308340
}
309341
}
310342
,"map" => {

controller/src/client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ pub enum FromGUI {
259259
EnableCalibrationView(),
260260
DisableCalibrationView(),
261261
RotateSubface(usize,usize),
262+
DoTwist(Twist),
262263
}
263264

264265
impl FromGUI{
@@ -684,6 +685,9 @@ pub fn start_client() -> (Arc<Mutex<ClientState>>, Sender<FromGUI>, Receiver<ToG
684685
RotateSubface(f,sf) => {
685686
command_queue.push_back(("rotate_subface".to_string(), vec![f.to_string(),sf.to_string()]));
686687
},
688+
DoTwist(t) => {
689+
command_queue.push_back(("do_twist".to_string(), vec![t.to_string()]));
690+
}
687691
}
688692
}
689693
}

service/src/cube.frag.glsl

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ uniform uint u_map_facenum[45];
1010
uniform uint u_map_subfacenum[45];
1111
uniform uint u_inverse_facemap[45];
1212
uniform uint u_adjacent[45];
13+
uniform uint u_twist_dirs[6*9*9];
1314
uniform vec3 u_base_cols[6];
1415
uniform uint u_twist_face;
1516
uniform float u_twist_dir;
@@ -134,23 +135,23 @@ void main() {
134135

135136
vec2 fl_coord = vec2(px_pos.x - (ix*u_facelet_px), px_pos.y - (iy*u_facelet_px)) / u_facelet_px;
136137
fl_coord -= vec2(0.5,0.5);
138+
139+
FragColor = vec4(0.0,0.0,0.0,1.0);
137140
if (this_face && u_anim_pos < 1.0){
138141
// Animations for subfaces on the twisted face
139142
float anim = u_anim_pos;
140143
//float anim = 0.5;
141144
float a = (PI/2) * anim * -u_twist_dir;
142145
float a2 = (PI/2) * (1.0-anim) * u_twist_dir;
143-
FragColor = vec4(0.0,0.0,0.0,1.0);
144146
if (is_centre){
145147
mat3 rot = rotate(a);
146148
vec2 fl_coord1 = (vec3(fl_coord,1.0) * rot).xy;
147149
FragColor += render_tile(fl_coord1, base, sf_rot, is_centre, is_edge, is_corner);
148150
vec3 r = vec3(1.0,0.0,1.0) * rotate(sf_rot * PI/2);
149-
// TODO calculate actual edge base colours
150-
vec3 edge_base1 = u_base_cols[u_prev_colours[get_index(f, 1u)]]; // TODO determine centre colour to render here
151-
vec3 edge_base2 = u_base_cols[u_prev_colours[get_index(f, 3u)]]; // TODO determine centre colour to render here
152-
vec3 edge_base3 = u_base_cols[u_prev_colours[get_index(f, 7u)]]; // TODO determine centre colour to render here
153-
vec3 edge_base4 = u_base_cols[u_prev_colours[get_index(f, 5u)]]; // TODO determine centre colour to render here
151+
vec3 edge_base1 = u_base_cols[u_prev_colours[get_index(f, 1u)]];
152+
vec3 edge_base2 = u_base_cols[u_prev_colours[get_index(f, 3u)]];
153+
vec3 edge_base3 = u_base_cols[u_prev_colours[get_index(f, 7u)]];
154+
vec3 edge_base4 = u_base_cols[u_prev_colours[get_index(f, 5u)]];
154155
vec3 edge_coord1 = vec3(fl_coord,1.0) * rotate(a) * translate(-r.x,r.y);
155156
vec3 edge_coord2 = vec3(fl_coord,1.0) * rotate(a + (1*PI/2)) * translate(-r.x,r.y);
156157
vec3 edge_coord3 = vec3(fl_coord,1.0) * rotate(a + (2*PI/2)) * translate(-r.x,r.y);
@@ -225,7 +226,26 @@ void main() {
225226
FragColor += render_tile(fl_coord, base, sf_rot, is_centre, is_edge, is_corner);
226227
}
227228
else {
228-
// TODO animate these faces
229+
float anim = u_anim_pos * 3;
230+
uint angle = sf_rot+1u;
231+
uint dir_index = (u_twist_face * 54u) + (f * 9u) + sf;
232+
uint d = u_twist_dirs[dir_index];
233+
angle += d;
234+
if (u_twist_dir < 0){ angle += 2u; }
235+
mat3 rot = rotate(angle * (PI/2));
236+
vec2 fl_coord_a = vec3(fl_coord + (vec3( anim ,0.0,1.0)*rot).xy,1.0).xy;
237+
vec2 fl_coord_a2 = vec3(fl_coord + (vec3(-(3.0-anim),0.0,1.0)*rot).xy,1.0).xy;
238+
vec2 fl_coord_a3 = vec3(fl_coord + (vec3(-(1.0-anim),0.0,1.0)*rot).xy,1.0).xy;
239+
vec2 fl_coord_a4 = vec3(fl_coord + (vec3(-(2.0-anim),0.0,1.0)*rot).xy,1.0).xy;
240+
// previous colour, slides out
241+
FragColor += render_tile(fl_coord_a, prev_base, sf_rot, is_centre, is_edge, is_corner);
242+
243+
// two intermediate colours slide through
244+
FragColor += render_tile(fl_coord_a3, u_base_cols[0], sf_rot, is_centre, is_edge, is_corner);
245+
FragColor += render_tile(fl_coord_a4, u_base_cols[0], sf_rot, is_centre, is_edge, is_corner);
246+
247+
// next colour, slides in
248+
FragColor += render_tile(fl_coord_a2, base, sf_rot, is_centre, is_edge, is_corner);
229249
}
230250
}
231251
if (is_centre && (u_debug_arrow > 0u)){

service/src/dir_map.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ enum ClientEvent{
9090
EnableCalibrationView(),
9191
DisableCalibrationView(),
9292
RotateSubface(usize,usize),
93-
ApplyTwist(String),
93+
DoTwist(cube_model::Twist),
9494
}
9595

9696
enum Event{
@@ -232,6 +232,19 @@ fn handle_stream<R: 'static + Read + Send + Sync, W: 'static + Write + Send + Sy
232232
}
233233
}
234234
},
235+
"do_twist" => {
236+
if args.len() != 1{
237+
let msg = auth.construct_reply("wrong_arguments", &vec![&command]);
238+
write_stream.write(msg.as_bytes())?;
239+
}
240+
if let Ok(t) = Twist::from_string(&args[0]){
241+
sender.send(Event::Client(ClientEvent::DoTwist(t)))?;
242+
}
243+
else{
244+
let msg = auth.construct_reply("bad_argument", &vec![&command]);
245+
write_stream.write(msg.as_bytes())?;
246+
}
247+
}
235248
_=>{
236249
let msg = auth.construct_reply("unknown_command", &vec![&command]);
237250
write_stream.write(msg.as_bytes())?;
@@ -637,6 +650,7 @@ shader_struct!{
637650
u_map_subfacenum: Uniform1UIV,
638651
u_inverse_facemap: Uniform1UIV,
639652
u_adjacent: Uniform1UIV,
653+
u_twist_dirs: Uniform1UIV,
640654
u_base_cols: Uniform3FV,
641655
u_twist_face: Uniform1UI,
642656
u_twist_dir: Uniform1F,
@@ -918,7 +932,8 @@ fn jumbotron_thread_main(
918932
shader.u_map_subfacenum.set(&lm.subfacemap);
919933
shader.u_inverse_facemap.set(&lm.inverse());
920934
shader.u_rotation_map.set(&lm.rotationmap);
921-
shader.u_adjacent.set(&[12, 136, 24, 68, 192, 80, 6, 130, 18, 5, 129, 17, 260, 384, 272, 36, 160, 48, 9, 65, 3, 264, 320, 258, 40, 96, 34, 17, 129, 5, 272, 384, 260, 48, 160, 36, 3, 65, 9, 258, 320, 264, 34, 96, 40, 24, 136, 12, 80, 192, 68, 18, 130, 6])
935+
shader.u_adjacent.set(&[12, 136, 24, 68, 192, 80, 6, 130, 18, 5, 129, 17, 260, 384, 272, 36, 160, 48, 9, 65, 3, 264, 320, 258, 40, 96, 34, 17, 129, 5, 272, 384, 260, 48, 160, 36, 3, 65, 9, 258, 320, 264, 34, 96, 40, 24, 136, 12, 80, 192, 68, 18, 130, 6]);
936+
shader.u_twist_dirs.set(&include!("dir_map.txt"));
922937
}
923938
shader.u_prev_colours.set(prev_cols.as_slice());
924939
shader.u_cur_colours.set(cols.as_slice());
@@ -1001,8 +1016,8 @@ fn jumbotron_thread_main(
10011016
let sf = 0.3;
10021017
let base_trans = &Transform::scale(height as f32/width as f32,1.0,1.0) * &Transform::scale(sf,sf,sf);
10031018
let base_trans = &base_trans * &Transform::rotate_xyz(-0.25,0.0,0.0);
1004-
//let base_trans = &base_trans * &Transform::rotate_xyz(0.00,(Instant::now()-start_time).as_millis() as f32 / 4000.0,0.0);
1005-
let base_trans = &base_trans * &Transform::rotate_xyz(0.00,1.0,0.0);
1019+
let base_trans = &base_trans * &Transform::rotate_xyz(0.00,(Instant::now()-start_time).as_millis() as f32 / 4000.0,0.0);
1020+
//let base_trans = &base_trans * &Transform::rotate_xyz(0.00,1.0,0.0);
10061021
for (i, t1) in face_transforms.iter().enumerate(){
10071022
preview_cube.u_cur_face.set(i.try_into().unwrap());
10081023
for (j, t2) in facelet_translations.iter().enumerate(){
@@ -1266,8 +1281,12 @@ fn main() {
12661281
config.rotation_map = rotstr;
12671282
persist_config(&config, &args.config);
12681283
}
1269-
,ClientEvent::ApplyTwist(t) => {
1270-
todo!();
1284+
,ClientEvent::DoTwist(twist) => {
1285+
let mut c = cube.lock().unwrap();
1286+
prev_cube.lock().unwrap().deserialise(&c.serialise()).unwrap();
1287+
c.twist(twist);
1288+
last_twist.lock().unwrap().twist = Some(twist);
1289+
last_twist.lock().unwrap().time = Some(Instant::now());
12711290
}
12721291
,ClientEvent::StartDetectLED() => {
12731292
println!("Detect LEDs");

service/twist_dirs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
3+
dirs = {
4+
# V V
5+
0: [(1,0,1),(1,1,2),(1,2,2),(4,0,1),(4,1,2),(4,2,2),(3,0,1),(3,1,2),(3,2,2),(2,0,1),(2,1,2),(2,2,2),],##
6+
1: [(0,6,0),(0,7,0),(0,8,3),(2,2,3),(2,5,0),(2,8,0),(4,0,0),(4,3,0),(4,6,3),(5,6,0),(5,7,0),(5,8,0),],##
7+
2: [(0,0,2),(0,3,2),(0,6,1),(1,0,0),(1,3,0),(1,6,3),(3,2,3),(3,5,0),(3,8,0),(5,2,0),(5,5,0),(5,8,0),],##
8+
3: [(0,0,3),(0,1,0),(0,2,0),(2,0,0),(2,3,0),(2,6,3),(4,2,3),(4,5,0),(4,8,0),(5,0,0),(5,1,0),(5,2,0),],##
9+
4: [(0,2,1),(0,5,2),(0,8,2),(1,2,3),(1,5,0),(1,8,0),(3,0,0),(3,3,0),(3,6,3),(5,0,0),(5,3,0),(5,6,0),],##
10+
5: [(1,6,2),(1,7,2),(1,8,1),(2,6,2),(2,7,2),(2,8,1),(3,6,2),(3,7,2),(3,8,1),(4,6,2),(4,7,2),(4,8,1),],##
11+
6: [(0,3,1),(0,4,0),(0,5,3),(2,1,3),(2,4,1),(2,7,1),(4,1,1),(4,4,3),(4,7,3),(5,3,0),(5,4,0),(5,5,0),],##
12+
7: [(0,1,3),(0,4,1),(0,7,1),(1,1,1),(1,4,3),(1,7,3),(3,1,3),(3,4,1),(3,7,1),(5,1,0),(5,4,0),(5,7,0),],##
13+
14+
8: [(1,3,3),(1,4,2),(1,5,1),(2,3,3),(2,4,2),(2,5,1),(3,3,3),(3,4,2),(3,5,1),(4,3,3),(4,4,2),(4,5,1),],
15+
}
16+
17+
dir_map = [0]*6*9*9
18+
19+
for twist_face, adjacent in dirs.items():
20+
for a in adjacent:
21+
f,sf,dir_ = a
22+
index = (f * 9) + sf # 0 to 53
23+
index2 = (twist_face * 54) + index
24+
dir_map[index2] = dir_
25+
26+
print(dir_map)

0 commit comments

Comments
 (0)