我想了解如何在 OpenGL ES 2.0
中正确缩放。我已经成功绘制了一个模型,但它太小了,我无法缩放到这个模型中。我想要的是通过这个模型进行缩放。
该模型是一个带有不同楼层的建筑物-我想缩放到每个楼层的每个房间。但是要么由于视锥体而使对象消失,要么我无法很接近这个对象。
我正在使用缩放触摸手势,并获得值“scale”-现在我应该如何处理这个值?
我迄今为止尝试过的:
改变近平面和远平面距离以及在 Matrix.setLookAtM(...) 中更改 eyeZ 值,但我只能实现缩小... 它在一段时间后消失在缩放中... 因此,我无法缩放到一些特殊部分(“那么远…”)
如何才能实现这个?
最大的问题在于接近平面和通过eyeZ值进行缩放的结合。它根本不起作用。如果我放大,由于接近平面,对象会消失。但我没有看到这背后的逻辑。
目前我正在使用:
/*
* Set the camera position (View matrix)
*/
Matrix.setLookAtM(mViewMatrix, offset, eyeX, eyeY, eyeZ / mZoomLevel,
centerX, centerY, centerZ, upX, upY, upZ);
mZoomLevel是我通过onTouch缩放获得的因子。
这里展示了我的整个矩阵操作:
@Override
public void onDrawFrame(GL10 unused) {
LoggerHelper.calculateFPS();
/*
* Draw background color
*/
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
/*
* scale model down to smaller values
*/
Matrix.setIdentityM(mModelMatrix, 0);
Matrix.scaleM(mModelMatrix, 0, model3d.getRatio() * scaleFactor,
model3d.getRatio() * scaleFactor, model3d.getRatio()
* scaleFactor);
/*
* rotate and translate model in dependence to the user input
*/
Matrix.translateM(mModelMatrix, 0, translateX, translateY, translateZ);
Helper.rotateModel(mModelMatrix, rotationX, rotationY, rotationZ, true,
model3d.getWidth(), model3d.getLength(), model3d.getHeight());
/*
* Set the camera position (View matrix)
*/
Matrix.setLookAtM(mViewMatrix, offset, eyeX, eyeY, eyeZ / mZoomLevel,
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);
}
一些重要的变量:
private float nearPlaneDistance = 1f;
private float farPlaneDistance = 200f;
private float eyeZ = -1;
我在Github上上传了一个虚拟项目,仅包含OpenGL部分 - 如果你想更好地查看源代码,可以查看它
我有:
我需要的是: