Ios 在iphone上的opengl中找到当前视图前面的空间点
我有一些代码在opengl中创建一个3d视图,然后使用设备运动在其中四处查看。我知道这是可行的,因为我可以在我周围的空间中放置3d立方体,并看到它们位于正确的位置。(我只是用x/y/z坐标创建它们) 该代码使用设备的旋转矩阵,然后将其应用于各个块Ios 在iphone上的opengl中找到当前视图前面的空间点,ios,opengl-es,Ios,Opengl Es,我有一些代码在opengl中创建一个3d视图,然后使用设备运动在其中四处查看。我知道这是可行的,因为我可以在我周围的空间中放置3d立方体,并看到它们位于正确的位置。(我只是用x/y/z坐标创建它们) 该代码使用设备的旋转矩阵,然后将其应用于各个块 CMRotationMatrix r = dm.attitude.rotationMatrix; GLKMatrix4 baseModelViewMatrix = GLKMatrix4Make(r.m11, r.m21, r.m31, 0.0f,
CMRotationMatrix r = dm.attitude.rotationMatrix;
GLKMatrix4 baseModelViewMatrix = GLKMatrix4Make(r.m11, r.m21, r.m31, 0.0f,
r.m12, r.m22, r.m32, 0.0f,
r.m13, r.m23, r.m33, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, kNearZ, kFarZ);
block.effect.transform.projectionMatrix = projectionMatrix;
我想做的是找出我在看的地方,然后在我面前创建一个方块
通过在一个方向上创建一个向量,对其应用旋转矩阵,然后读取它的新值,我取得了一些有限的成功。但它只在某些方向上起作用——当我旋转太远的时候,它会搞砸
GLKVector4 vect = GLKVector4Make(0.0f,0.0f,10.0f,1.0f);
GLKVector4 newVec = GLKMatrix4MultiplyVector4(baseModelViewMatrix,vect);
然后我读出newVec.x、newVec.y、newVec.z,并用它们来放置立方体
有人能告诉我这条路走对了吗?有没有更简单的方法来实现这一点
这一切的数学计算是相当令人畏惧的
更新:
我在使用中取得了部分成功
GLKVector3 newVec1 = GLKVector3Make(-r.m22, -r.m33, r.m21);
这仅适用于一个lanscape方向,也仅适用于我当前位置周围的圆柱体。上/下轴不太正确
旋转矩阵的这些部分足以在我周围得到一个点吗
更新2:
我想发布更多的代码来让它变得更加清晰可能会有所帮助
这就是展示一切的方式
//1- get device position
CMDeviceMotion *dm = motionManager.deviceMotion;
CMRotationMatrix r = dm.attitude.rotationMatrix;
GLKMatrix4 baseModelViewMatrix = GLKMatrix4Make(r.m11, r.m21, r.m31, 0.0f,
r.m12, r.m22, r.m32, 0.0f,
r.m13, r.m23, r.m33, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(45.0f), aspect, kNearZ, kFarZ);
//2- work out position ahead of current view
GLKVector3 newVector = GLKVector3Make(-r.m22, -r.m33, r.m21);
//place cube in 3d space- this works fine when just positioning with x,y,z co-ordinates
cube.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 modelViewMatrix = GLKMatrix4Identity;
modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, newVector.x*100, newVector.y*100, newVector.z*100);
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
cube.effect.transform.modelviewMatrix = modelViewMatrix;
你所看到的向量应该乘以反矩阵。你没有写下你得到了什么结果,但这里有一些可能的解决方案:
newVect2=(-r.m13,-r.m23,r.m33)
特别指(0,0,1)是前向向量的情况。其思想是旋转矩阵由3个基本向量x、y、z组成,其中每一行分别表示其中一个(它总是用于创建旋转矩阵,但可能不可逆)。如果第三行表示z基向量,则向量(0,0,1)将转换为(r.m13、r.m23、r.m33),并且您似乎面对的是通过其法线(0,0,1)镜像的向量。其运算为R'=normal*(2*dot(normal,R))-R,对于normal为(0,0,1)且R为(R.m13,R.m23,R.m33)的情况,结果R'为(-R.m13,-R.m23,R.m33)。正如这里要注意的,它可能会被反转,z基向量是(r.m31,r.m32,r.m33)
在任何情况下,如果第三个选项不起作用,第二个选项也不会起作用,问题可能是(0,0,1)不是恒等式的前向向量。Hi。谢谢你的回答。我怎么用这个?如果按原样添加代码,则会出现代码错误。我需要使用GLKVector3Make吗?无论哪种向量类型更适合您,GLKVector3Make或GLKVector4Make(…,1)我已经更新了问题,添加了更多解释。谢谢你的帮助!你是怎么得到的(-r.m22,-r.m33,r.m21)?还有,你是否使用了“注视”或者在透视图上做了一些旋转?不,我没有使用注视。我只是使用设备的转换矩阵来旋转所有东西。根据你的回答,我得到了-r.m22,-r.m33,r.m21,然后或多或少地通过反复试验,一次找出一个。这3个变量中似乎没有足够的信息来描述完整的转换?请查看我发布的编辑