augmented-reality - 横向模式下的旋转矩阵改变了吗? iPhone 上的增强现实

标签 augmented-reality

我正在构建 au 增强现实应用程序,该应用程序在某些兴趣点给出的地方显示建筑物。当你用你的 iPhone 指向那个点时(在纵向模式下),一个 OpenGL 3D 模型会出现在纬度和经度指示的位置(它使用来自 CMDeviceMotion 的 rotationMatrix 从该 GPS 坐标转换为屏幕坐标并获取值x 和 y)。

我的问题是我也希望它在横向模式下工作。我已经完成了旋转 View 的所有工作,根据当前设备方向调整标题……但是当您以横向模式指向建筑物时,模型并未固定到该坐标,而是在屏幕上移动。我认为问题是当你改变设备的方向时,rotationMatrix 必须改变,但我找不到解决这个问题的方法。有什么帮助吗?

- (void)onDisplayLink:(id)sender {
    //Obtenemos los datos de movimiento del dispositivo
    CMDeviceMotion *d = motionManager.deviceMotion;

    //Si no son nulos (la localizacion esta activada y el magnetometro calibrado)
    if (d != nil) {
        //Obtenemos la matriz de rotacion
        CMRotationMatrix r = d.attitude.rotationMatrix;
        transformFromCMRotationMatrix(cameraTransform, &r);

        //Indicamos a la vista que necesita ser renderizada de nuevo
        [self setNeedsDisplay];
    }
}


- (void)drawRect:(CGRect)rect {

    //vistaGPS.transform = CGAffineTransformMakeRotation([gestorOrientacion >devolverAnguloSegunOrientacion]);

    //Si aun no tenemos puntos de interes, salimos
    if (placesOfInterestCoordinates == nil) {
        return;
    }

    //Multiplica la matriz de proyeccion y la de rotacion ¿¿¿para obtener la rotacion en >coordenadas openGL
    mat4f_t projectionCameraTransform;
    multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, >cameraTransform);

    //Inicializamos el contador
    int i = 0;

    CLHeading *heading = locationManager.heading;
    CLLocationDirection gradosDiferenciaNorteSinCorregir = heading.trueHeading;

    //NSLog(@"%f , %f", gradosDiferenciaNorteSinCorregir, >gradosDiferenciaNorteSinCorregir+90);
    CLLocationDirection gradosDiferenciaNorte = [gestorOrientacion >devolverHeadingCorregidoSegunOrientacion:gradosDiferenciaNorteSinCorregir];
    ////float bearing = [self getHeadingForDirectionFromCoordinate:newLocation.coordinate >toCoordinate: poi.coordenadas.coordinate];

    labelHeading.text = [NSString stringWithFormat:@"Heading: %f", gradosDiferenciaNorte];        


    NSArray *subvistas = [self subviews];

    for (PuntoInteres *poi in [puntosDeInteres objectEnumerator]) {
        if ([subvistas containsObject:poi.vistaGL]) {
            vec4f_t v;

            //Multiplicamos la matriz por el vector de coordenadas del punto de interes
            multiplyMatrixAndVector(v, projectionCameraTransform, >placesOfInterestCoordinates[i]);

            float x = (v[0] / v[3] + 1.0f) * 0.5f;
            float y = (v[1] / v[3] + 1.0f) * 0.5f;

            [vistaGPS cambiarTextoLabelLatitud:x LabelLongitud:y LabelPrecision:88];


            //Si la coordenada Z del vector es negativa, el modelo debe verse
            if (v[2] < 0.0f) {
                //Centramos la vista en el punto adecuado
                poi.vistaGL.center = CGPointMake(x*self.bounds.size.width, >self.bounds.size.height-y*self.bounds.size.height);

                //Indicamos que deje de estar oculta
                poi.vistaGL.hidden = NO;

                [poi.vistaGL.modeloAPintar establecerRotacionEnX:0.0 Y:gradosDiferenciaNorte >Z:0.0];

                //Le decimos que comience a dibujarse
                [poi.vistaGL startAnimation];
            } else {
                //Indicamos que este oculta
                poi.vistaGL.hidden = YES;

                //Le decimos que deje de dibujarse
                [poi.vistaGL stopAnimation];
            }
        }
        i++;
    }
}


void transformFromCMRotationMatrix(vec4f_t mout, const CMRotationMatrix *m) {
    mout[0] = (float)m->m11;
    mout[1] = (float)m->m21;
    mout[2] = (float)m->m31;
    mout[3] = 0.0f;

    mout[4] = (float)m->m12;
    mout[5] = (float)m->m22;
    mout[6] = (float)m->m32;
    mout[7] = 0.0f;

    mout[8] = (float)m->m13;
    mout[9] = (float)m->m23;
    mout[10] = (float)m->m33;
    mout[11] = 0.0f;

    mout[12] = 0.0f;
    mout[13] = 0.0f;
    mout[14] = 0.0f;
    mout[15] = 1.0f;
}

最佳答案

这是我回答的副本 On stackoverflow

当屏幕方向改变时,旋转矩阵不会围绕 Z 轴旋转。角度属性对此是正确的,但我宁愿只使用旋转矩阵。这是我的代码:

GLKMatrix4 deviceMotionAttitudeMatrix;
if (_cmMotionmanager.deviceMotionActive) {
    CMDeviceMotion *deviceMotion = _cmMotionmanager.deviceMotion;

    // Correct for the rotation matrix not including the screen orientation:
    // TODO: Let the device notify me when the orientation changes instead of querying on each update.
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    float deviceOrientationRadians = 0.0f;
    if (orientation == UIDeviceOrientationLandscapeLeft) {
        deviceOrientationRadians = M_PI_2;
    }
    if (orientation == UIDeviceOrientationLandscapeRight) {
        deviceOrientationRadians = -M_PI_2;
    }
    if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        deviceOrientationRadians = M_PI;
    }
    GLKMatrix4 baseRotation = GLKMatrix4MakeRotation(deviceOrientationRadians, 0.0f, 0.0f, 1.0f);

    CMRotationMatrix a = deviceMotion.attitude.rotationMatrix;
    deviceMotionAttitudeMatrix
        = GLKMatrix4Make(a.m11, a.m21, a.m31, 0.0f,
                         a.m12, a.m22, a.m32, 0.0f,
                         a.m13, a.m23, a.m33, 0.0f,
                         0.0f, 0.0f, 0.0f, 1.0f);
    deviceMotionAttitudeMatrix = GLKMatrix4Multiply(baseRotation, deviceMotionAttitudeMatrix);
}
else
{
    // Look straight forward (we're probably in the simulator, or a device without a gyro)
    deviceMotionAttitudeMatrix = GLKMatrix4MakeRotation(-M_PI_2, 1.0f, 0.0f, 0.0f);
}

关于augmented-reality - 横向模式下的旋转矩阵改变了吗? iPhone 上的增强现实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8316426/

相关文章:

Android camara 参数 getZoomRatio() 值不符合预期

iphone - 哪些 AR 库对于开发用于显示某些对象信息的 iOS 应用程序很有用?

javascript - 我可以加载多个模型以使用 AFrame 的 AR 标记来激活吗?

android - 用增强现实概念测量液体

swift - 将 ARKit 面部跟踪 3D 网格投影到 2D 图像坐标

swift - Apple ARKit 人脸识别项目 (ARKitFaceExample) 的运行时问题

java - 使用 LookAR 的增强现实示例

unity-game-engine - 如何使用Unity在ARcore增强图像场景中显示不同图像的不同预制件?

android - Appcelerator/Phonegap 中具有运行时覆盖的相机 View ?可能的?

android - 是否有任何适用于 android 的开源增强现实 sdk?