Android (OpenGL ES)拉伸远离视图中心的对象
因此,我在OpenGLES中生成了一个球体(具体来说,OpenGLES2.0,Java,Android)。如果将此球体放置在与视图矩阵使用的中心相同的位置,则可以,但如果偏离中心,则球体会严重扭曲(请参见下文) 为什么会发生这种情况,我该如何阻止它 那是同一个球体。右上角的那个只是在x和y(而不是z)中平移 我的GLSurfaceView.renderer实现中的一些代码片段Android (OpenGL ES)拉伸远离视图中心的对象,android,opengl-es,Android,Opengl Es,因此,我在OpenGLES中生成了一个球体(具体来说,OpenGLES2.0,Java,Android)。如果将此球体放置在与视图矩阵使用的中心相同的位置,则可以,但如果偏离中心,则球体会严重扭曲(请参见下文) 为什么会发生这种情况,我该如何阻止它 那是同一个球体。右上角的那个只是在x和y(而不是z)中平移 我的GLSurfaceView.renderer实现中的一些代码片段 public void onSurfaceCreated(GL10 unused, EGLConfig config)
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
// Both centred on (0,0,0), of radius 1.0.
outerSphere = new Sphere();
centreSphere = new Sphere();
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
ratio = (float) width / height;
final float left = -ratio;
final float right = ratio;
final float bottom = -1.0f;
final float top = 1.0f;
final float near = 1.0f;
final float far = 100.0f;
Matrix.frustumM(projMatrix, 0, left, right, bottom, top, near, far);
}
public void onDrawFrame(GL10 unused) {
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 10.0f;
final float lookX = 0.0f;
final float lookY = 0.0f;
final float lookZ = 0.0f;
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ,
upX, upY, upZ);
// Set identity matrix as input for translations.
Matrix.setIdentityM(outerModelMatrix, 0);
// Translate outer sphere by 5 in x and y.
Matrix.translateM(outerModelMatrix, 0, 5.0f, 5.0f, 0.0f);
// MVP matrix = Projection * View * Model.
Matrix.multiplyMM(centreMVPMatrix, 0, viewMatrix, 0, centreModelMatrix, 0);
Matrix.multiplyMM(centreMVPMatrix, 0, projectionMatrix, 0, centreMVPMatrix, 0);
Matrix.multiplyMM(outerMVPMatrix, 0, viewMatrix, 0, outerModelMatrix, 0);
Matrix.multiplyMM(outerMVPMatrix, 0, projectionMatrix, 0, outerMVPMatrix, 0);
outerSphere.draw(outerMVPMatrix);
centreSphere.draw(outerMVPMatrix);
}
我的着色器很简单
private final static String vertexShaderCode =
"uniform mat4 u_MVPMatrix;" +
"attribute vec4 a_Position;" +
"uniform vec4 u_Color;" +
"void main() {" +
" gl_Position = u_MVPMatrix * a_Position;" +
"}";
private final static String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 u_Color;" +
"void main() {" +
" gl_FragColor = u_Color;" +
"}";
我已经省略了Sphere类中几乎所有的代码,以及其他我认为不必要的东西(?),但是如果需要,我会把它们放在上面。欢迎来到这个世界
再详细一点:你的视野太窄了,你得把平截头体弄大一点。Aha!现在我知道我在寻找什么了,当然信息是现成的(包括关于堆栈溢出的类似问题。对于遇到此问题的任何人,请参阅例如和)谢谢。知道谷歌做什么通常是最难的:)很高兴我能为你指出正确的方向。我还应该补充一点,我认为问题的视野太广了。它需要更窄一些(相机放在远离父亲的地方进行补偿)。