3.Core Location、Core Motion
今回は、iPhoneを向けた方向の仮想空間の風景をiPhoneの液晶画面に映す所までやる。
前回は、SkyBoxで仮想空間の風景を眺める所まで作った。しかし表示は自動的にパンするだけで、iPhoneの向いている方向の風景を表示するようにはなっていない。
これをするには、iOSのフレームワークのひとつCore Motionを使ってiPhoneが向いている方向(=姿勢)を取得する。今回欲しいのは姿勢だけだが、密接に関係しているCore Locationも入れてしまおう。
Core Locationは、GPSで現在位置の取得、またコンパスで磁方位の取得をするためのiOSのフレームワークである。Core Motionは、ジャイロコンパスと加速度センサーでiPhoneの姿勢や動きを取得するための、同じくフレームワークである。
取得した姿勢でSkyBoxに表示させる向き(視点の方向)を指定するには、フレームワークを追加
(*1)した後に、下のようにソースコードを修正する。
ViewController.h
| #import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreMotion/CoreMotion.h>
@interface ViewController : GLKViewController
<CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@end |
ViewController.m
| (前略)
@property (strong, nonatomic) EAGLContext *context;
@property (strong, nonatomic) GLKBaseEffect *effect;
@property (strong, nonatomic) CMMotionManager *motionManager;
- (void)setupGL;
- (void)tearDownGL;
(後略) |
| - (void)viewDidLoad
{
(中略)
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
if (!locationManager) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
}
if ([CLLocationManager headingAvailable] == YES){
[locationManager startUpdatingHeading];
}
if(!self.motionManager) {
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 1.0/60.0;
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical];
}
[self setupGL];
}
|
| - (void)dealloc
{
[self.motionManager stopDeviceMotionUpdates];
[self tearDownGL];
(後略) |
| }
}
- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if (newHeading.headingAccuracy <0)
return;
_rotation = (float)(newHeading.magneticHeading) / 180 * 3.14159;
}
#pragma mark - GLKView and GLKViewController delegate methods |
- (void)update
{
(中略)
self.effect.transform.projectionMatrix = projectionMatrix;
CMDeviceMotion *motion = self.motionManager.deviceMotion;
GLKQuaternion glkq;
if(motion) {
CMAttitude *attitude = motion.attitude;
glkq.x = -attitude.quaternion.x;
glkq.y = -attitude.quaternion.y;
glkq.z = -attitude.quaternion.z;
glkq.w = attitude.quaternion.w;
}else {
glkq.x = 0.0;
glkq.y = 1.0;
glkq.z = 0.0;
glkq.w = 0.0;
//glkq.w = 3.14159;
}
//GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
//baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeWithQuaternion(glkq);
baseModelViewMatrix = GLKMatrix4RotateX(baseModelViewMatrix, 3.14159/2.0f);
baseModelViewMatrix = GLKMatrix4Translate(baseModelViewMatrix, 0.0f, 0.0f, -4.0f);
// Compute the model view matrix for the object rendered with GLKit
(中略)
_modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix); //_rotation += self.timeSinceLastUpdate * 0.5f; // 1フレーム描画ごとに呼び出される処理。(前回追加したコード) // SkyBox用の投影用行列を設定。立方体の物を流用。 skyboxEffect.transform.projectionMatrix = projectionMatrix; skyboxEffect.transform.modelviewMatrix = baseModelViewMatrix;
} |
ぐすぐずしている間にiOSは6になり、Xcodeは容赦なくアップデートしたので、前に言っていたのとはコードがちょっと違っている。1年もしてiOSがアップデートしたらまた違うだろうから、そこら辺は書いてあることを真に受けずに、臨機応変に読み替えること。
今回は特に、iPhoneの向き(ポートレイト/ランドスケープ)によって表示を変える(変えない)ための設定をコードから取り去った。代わりに、iOS6ではXcodeのGUI操作で設定をする。以下のように、Portraitのアイコンだけを暗転させておく。
ところでiOSで扱っている「姿勢」には数学的にいくつかの表現方法があり、Core MotionのCMAttitudeクラスでは3通りの方法(四元数(Quaternion)、回転行列、オイラー角)で取得できる。今回は四元数を使ったが、これらは本質的に同じものを表現しているのでどれを使っても良い。(ただし数学的な扱いが違うので、その理解は要る。)
さて、Core Location、Core Motionを組み込んで起動すると、iPhoneの向いている方向の(仮想世界の)風景が液晶画面に表示されるだろう。サンプルのデータを使っているなら、上に向ければ北極が、下に向ければ南極が見えるはずである。かなり仮想現実っぽくなってきた。
なお当然であるが、動作確認には実機が要る。XcodeのシミュレータではCoreMotionで姿勢情報は得られず、表示する向きを変えられない。
また、実機で動作させるにはアップルへの開発者登録(¥8,400/年)が要る。
さて次回は、HMDなどに表示できるように外部ディスプレイへの出力機能をつける。