【问题标题】:Moving Camera to specific Point in Scene (while rotated/translated) - OpenGL ES 2将相机移动到场景中的特定点(旋转/平移时) - OpenGL ES 2
【发布时间】:2015-03-01 07:44:52
【问题描述】:

我想知道如何将相机移动到场景中的特定点,即使我已经在所有三个维度上进行了旋转、平移和缩放。对于点 P (x,y,z),我显然不能使用 translate(x,y,z) - 如果我这样做,我会以完全不同的点结束。

我有一栋有楼层和房间的建筑。我想要两个不同的相机视角。第一个是鸟眼,我可以从顶部看到一个特定的位置。它应该类似于:

第二个是 3 维的,我的相机具有特定值(但恒定,例如 30°),我正在寻找重点(我想选择我的“x”旋转这里)。如下图所示,第二个相机视图应该是这样的:

就像我说的那样,我不知道如何将相机移动到特定位置,因为旋转/平移后的值完全不同。除此之外,我不确定我是否在考虑正确的方法。

我的整个平移/旋转/缩放仅与模型有关。我没有更改查看方法中的任何内容。我正在使用以下方法来修改模型(通过触摸手势):

             // translate to world position
             Matrix.setIdentityM(tmpMatrix, 0);
             Matrix.translateM(tmpMatrix, 0, translateX, translateY, translateZ);
             Matrix.multiplyMM(resMatrix, 0, tmpMatrix, 0, mModelMatrix, 0);
             System.arraycopy(resMatrix, 0, mModelMatrix, 0, 16);

            // rotate around center
            Matrix.setIdentityM(tmpMatrix, 0);
            if (rotationX != 0)
                    Matrix.rotateM(tmpMatrix, 0, rotationX, 1.0f, 0.0f, 0.0f);
            if (rotationY != 0)
                    Matrix.rotateM(tmpMatrix, 0, rotationY, 0.0f, 1.0f, 0.0f);
            if (rotationZ != 0)
                    Matrix.rotateM(tmpMatrix, 0, rotationZ, 0.0f, 0.0f, 1.0f);

            Matrix.multiplyMM(resMatrix, 0, tmpMatrix, 0, mModelMatrix, 0);
            System.arraycopy(resMatrix, 0, mModelMatrix, 0, 16);
             
            // scale down
            Matrix.setIdentityM(tmpMatrix, 0);
            Matrix.scaleM(tmpMatrix, 0, r * s, r * s, r * s);
            Matrix.multiplyMM(resMatrix, 0, tmpMatrix, 0, mModelMatrix, 0);
            System.arraycopy(resMatrix, 0, mModelMatrix, 0, 16);


*
             * Set the camera position (View matrix)
             */
            Matrix.setLookAtM(mViewMatrix, offset, eyeX, eyeY, eyeZ,
                              centerX, centerY, centerZ, upX, upY, upZ);
            
            /*
             * combine the model with the view matrix
             */
            Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

            /*
             * this projection matrix is applied to object coordinates in the
             * onDrawFrame() method
             */
            Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, 1, -1,
                           nearPlaneDistance, farPlaneDistance);

            /*
             * Calculate the projection and view transformation
             */
            float[] mMVPMatrix = new float[16];
            Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0);

            /*
             * all the drawing stuff inside the model-object (otherwise
             * translation/rotation wouldn't affect every object)
             */
            model3d.draw(mMVPMatrix);

我还负责在模型旋转时平移 x、y、z(例如,模型旋转 90° - 当我向左滑动时,对象仍应向左移动)。这是由我的 onTouchEvent-Helper 管理的:

 position.add(touchManager.moveDelta(0).rotate(-angle));

我的整个源代码都是开源的,you could have a look into github.

问题

无论我当前的旋转/位置如何,如何将相机平移和旋转到特定点(具有特定距离/角度)?有没有好的文献?有什么好的开源项目我可以看看吗? GitHub 上的资源真的很少见……

我应该使用“lookAt”方法还是必须对模型进行某种平移/旋转?或者我应该改变我的眼睛和中心?

编辑:假设我不自己翻译我的模型(我将禁用用户翻译)。我想看一个特定的点 p (x,y,z) - 无论我如何旋转和缩放我的模型,我总是想看这个点 p! - 我该怎么做呢?我必须翻译我的模型吗?我必须改变我的眼睛或中心吗?

【问题讨论】:

  • 您是在制作过渡动画还是只是在不同的视点之间切换?
  • 如果只是切换...是的,glLookAt 就足够了!
  • 好吧,我想在视图之间制作动画,但如果它比切换复杂得多 - 切换就可以了。我基本上需要知道的是,我怎样才能“处理” glLookAt 以便它总是看着同一个点(这是我房间里的一个点)。在我的图片中,您可以看到红点。无论我如何旋转和缩放,它都应该始终关注这一点(假设我没有自己翻译我的模型!) - 我必须如何翻译模型或如何设置 glLookAt / eye/ center - 针对特定点 p)

标签: android opengl-es camera


【解决方案1】:

如果您希望相机的方向和到点的距离保持不变,如果您知道围绕 y 和俯仰轴的角度,您可以设置 3D 距离(x、y 和 z 中的距离轴)从标量距离d:

Vector3 dist   = Vector3 (    d * sin(yawAngle) * fabs(cos(pitchRollAngle)),
                              d * sin(pitchRollAngle),
                              d * cos(yawAngle) * fabs(cos(pitchRollAngle))    );

yawAngle 的值可以从 -π 到 π,而 pitchRollAngle 的值可以从 -π / 2.0f 到 π / 2.0f。因此,您可以将 eyePt 向量定义为:

Vector3 eyePt  = Vector3 (    pnt.x - dist.x,
                              pnt.y - dist.y,
                              pnt.z - dist.z    );

其中pnt向量是点的位置。然后,您以这种方式设置 lookAt 向量:

Vector3 lookAt = pnt;

最后,上向量为:

Vector3 up     = Vector3 (    0.0f,    1.0f,    0.0f    );

我希望这会对你有所帮助。对不起,我的英语很差。

【讨论】:

  • 叮叮叮叮!!!谢谢!这比我想象的要容易得多。现在我可以轻松地放大到点,甚至是动画;)我可以像我想要的那样翻译我的模型......我只需要将这个转换应用到我的点上。感谢战利品,享受 +200 的乐趣 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
相关资源
最近更新 更多