ããããã·ãã¥ã¬ã¼ã¿èªä½ç©èª - Day 2: arrayfireã§ç·å½¢ä»£æ° & Rustã®çãã¤ã³ã¿
å¼ã£è¶ãã®æºåã«è¿½ããã¦ãã¾ãã³ã¼ããæ¸ãã¦ããæéãããã¾ããã
TL; DR
arrayfireã§Array
ã®ä¸èº«ãRustã®é
åã¨ãã¦åãåºãã®ãã¡ãã£ã¨ããã©ããã
ä»æ¥ã®ç®æ¨
ãã¼ãã¼ãå ¥åã«ãã£ã¦ãã£ã¼ããããããããåãããããã«ãã¾ãã
ææç©
gliumã§ãã¼å ¥å
ãã¼å
¥åã¯ãæç»ãããåãã¬ã¼ã ãã¨ã«glium::glutin::Event::KeyboardInput
ã¨ãã¦åå¾ã§ãã¾ããKeyboardInput
ã¯
pub enum Event { // ... KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>), // ... }
ã¨ãã¦å®ç¾©ããã¦ããã®ã§ãããããVirtualKeyCode
ãåãåºãã¦ããã¨ã©ã®ãã¼ãæ¼ããããå¤å®ã§ãã¾ãã
// application main loop loop { for ev in display.poll_events() { use glium::glutin::{ElementState, Event, VirtualKeyCode}; match ev { Event::Closed => return, // Windowãéãããããloopãæããâã¢ããªã±ã¼ã·ã§ã³çµäº Event::KeyboardInput(ElementState::Pressed, _, Some(code)) => { match code { VirtualKeyCode::S => model_z -= 1.0, // move backward VirtualKeyCode::W => model_z += 1.0, // move forward // other moves _ => (), // ä»ã®ãã¼ã«å¯¾ããfallbackãç¨æãã¦ãããªãã¨ã³ã³ãã¤ã«ãéããªã } }, _ => () } } }
arrayfireã§ã¢ãã«å¤æè¡åãè¨ç®
â»è注⻠ãã®è¨äºãæ¸ãã¦ããæä¸ã«æ°ã¥ãããã¨ã§ãããgliumã®ä¾åcrateã¨ãã¦nalgeblaã¨ããç·å½¢ä»£æ°ã©ã¤ãã©ãªãå ¥ã£ã¦ãããæ®éã¯ãã£ã¡ã使ãã®ã ã¨æãã¾ãããããnalgebraã«ã¯CGã§ä½¿ãåºæ¬çãªå¤æè¡åï¼translation, rotation, transformation, …ï¼ãä¸çºã§çæã§ããé¢æ°ãå ¥ã£ã¦ãããããªã®ã§æ¸ãæããäºå®ã§ãã
ååã®è¨äºã§ãç´¹ä»ãããã®è¨äºã«ãããããªå¤æè¡åãç¨æããVertex shaderã«æ¸¡ãã¦ããã¨ãã£ã¼ãããåããããåãããã§ãã¾ããä¸æ¬¡å
空éãæ±ãã®ã«ãªã4x4ã®è¡åãªã®ãï¼ã¨ããçåãããããããã¾ãããããã®4è¡ç®ã»4åç®ãå ãããã¨ã«ãã£ã¦è¤æ°ã®å¤æè¡åã«å¯¾ãã¦åç´ã«å
ç©ãæ±ããã ãã§ãã¹ã¦ã®å¤æãé©ç¨ã§ããã¨ãããã°ãããã¡ãªããã®ããã§ãï¼ä»¥ä¸ã®ã³ã¼ãä¸ã®position
ã¨pitch
ã«ã¤ãã¦ãç´ã¨éçã§å
ç©ãåã£ã¦ã¿ã¾ãããï¼ï¼
ç·å½¢ä»£æ°æ¼ç®ã«ã¯arrayfire-rustã使ç¨ãã¾ããããã¯Cargoã§å
¥ããåã«ããã¤ãdependencyãããã¾ãã®ã§ãREADMEãåç
§ãã¦æºåãã¦ãã ããã
注æç¹ã¨ãã¦ãOpenGLã«æ¸¡ãè¡åã¯ãã¹ã¦Column-major orderã§ããè¦ããã«ãè¡ã横ãã§ã¯ãªããåã横ãã§ã*1ããããã£ã¦ãposition
è¡åã§åº§æ¨ã¯4åç®ã§ã¯ãªã4è¡ç®ã«ãªããå転è¡åã§ã¯sin
ã®ç¬¦å·ãç´æã¨ã¯éã«ãªã£ã¦ãã¾ãã
extern crate arrayfire as af fn model_matrix(pos: &[f32; 3], rot: &[f32; 3]) -> [[f32; 4]; 4] { use af::{Array, Dim4}; // ãã£ã¼ãããã¢ãã«ã¯ -100 <= x, y, z <= 100 ã®åº§æ¨ç©ºéã§ä½ããã¦ããã®ã§ã1/100ã«ç¸®å° let base = Array::new(&[ 0.01, 0.0 , 0.0 , 0.0, 0.0 , 0.01, 0.0 , 0.0, 0.0 , 0.0 , 0.01, 0.0, 0.0 , 0.0 , 0.0 , 1.0f32, ], Dim4::new(&[4, 4, 1, 1])); // å¹³è¡ç§»å let position = Array::new(&[ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, pos[0], pos[1], pos[2], 1.0f32, ], Dim4::new(&[4, 4, 1, 1])); // x軸ã¾ããã®å転 let pitch = Array::new(&[ 1.0, 0.0, 0.0, 0.0, 0.0, rot[0].cos(), rot[0].sin(), 0.0, 0.0, -rot[0].sin(), rot[0].cos(), 0.0, 0.0, 0.0, 0.0, 1.0f32, ], Dim4::new(&[4, 4, 1, 1])); // y軸ã¾ããã®å転 let yaw = Array::new(&[ rot[1].cos(), 0.0, rot[1].sin(), 0.0, 0.0, 1.0, 0.0, 0.0, -rot[1].sin(), 0.0, rot[1].cos(), 0.0, 0.0, 0.0, 0.0, 1.0f32, ], Dim4::new(&[4, 4, 1, 1])); // z軸ã¾ããã®å転 let roll = Array::new(&[ rot[2].cos(), rot[2].sin(), 0.0, 0.0, -rot[2].sin(), rot[2].cos(), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0f32, ], Dim4::new(&[4, 4, 1, 1])); use af::matmul; use af::MatProp::NONE; let res = matmul(&matmul(&matmul(&matmul(&roll, &yaw, NONE, NONE), &pitch, NONE, NONE), &position, NONE, NONE), &base, NONE, NONE); // çãã¤ã³ã¿ãå©ç¨ãã¦Rusté åã®åãåºãã次ç¯ã«ã¦è§£èª¬ã unsafe { *(res.device_ptr() as *const [[f32; 4]; 4]) } }
â»è注⻠ãã®å®è£ ã§ã¯å種å¤æè¡åããã¹ã¦å ç©ãã²ã¨ã¤ã«ã¾ã¨ãã¦ããOpenGLã«æ¸¡ãã¦ãã¾ãããããããã®å¤æè¡åãOpenGLã«æ¸¡ãã¦Vertex shaderã§ãããããã¦ããã£ã¦ãããã§ããã¨ããããã£ã¡ã®ã»ããéããããªæ°ãããã®ã§ãã®ã³ã¼ãã¯æ¶æ» ããäºå®ã§ãã
Rustã®çãã¤ã³ã¿
arrayfireã¯Rustã®FFIï¼Foreign Fuction Interfaceï¼ãå©ç¨ãã¦ãã¾ããã¤ã¾ããarrayfire (C++ Ver.)ãbackendã¨ãã¦ãarrayfire-rustããã®wrapperã«ãªã£ã¦ãã¾ããmutmul
ã§è¡åã®å
ç©ãè¨ç®ããã¨ããã®çµæã¯C++ã確ä¿ããã¡ã¢ãªã®ä¸ã«ãããRustããã¯ç´æ¥å©ç¨ã§ãã¾ãããaf::Array::device_ptr()
ã¯ããã®è¨ç®çµæã«å¯¾ããC++ã®ãã¤ã³ã¿ï¼unsigned long long
ï¼ãè¿ãã¾ããRustãããã®å¤ãåå¾ããããã«ã¯ã
- ã¾ããã¤ã³ã¿ãæãã¦ããåãä½ãªã®ããæå®ã:
res.device_ptr() as *const [[f32; 4]; 4]
- dereferenceã:
*(...)
- ããã«ãå¤é¨ã¡ã¢ãªãåç
§ããè¡çºã¯éã®ãã¯ã¼ãå©ç¨ããæ¥ãªã®ã§ãã®ãã¨ãæ示ãã¾ã:
unsafe { ... }
Step1ã§åå[[f32; 4]; 4]
ã®åã«ã¤ãã¦ãã*const
ã¯çãã¤ã³ã¿ã®æãå¤ãå®æ°ã¨ãã¦å©ç¨ããã¨ãã«ä½¿ããéã«ãã®å¤ãæ¸ãæããå ´åã«ã¯*mut
ã¨ãã¦åç
§ãã¾ãããã®ä»è©³ãããã¨ã¯å
¬å¼docã¸ã
Day 3ã«åãã¦
ããããããªãã¾ã¾ã³ã¼ããæ¸ã足ãã¦ããã®ã§ãã¡ããã¡ãã§ãï¼ãã®è¨äºã«ã³ã¼ãã¹ããããããè¼ã£ã¦ãããgithubã¸ã®ãªã³ã¯ããªãã®ã¯ãã®ããã§ãï¼ã å¿ è¦ãªç¯å²ã§ãªãã¡ã¯ã¿ãªã³ã°ãã¦GitHubã«å ¬éãã¾ããããã¦ã¾ã Blenderãããããã£ã¦ãªãã®ã§ãã£ã¡ã®åå¼·ããã¦ããã¾ãã