我在 OpenGL 中为我的 android 应用程序构建了一个非常好的魔方。不幸的是,当我将它靠近相机时,它看起来有点拉长。它看起来像这样:
我想让我的立方体尽可能大地出现在我的屏幕上,但没有那种拉伸。
这是一段代码:
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
float z = 3;
float x = 0;
float y = 3;
GLU.gluLookAt(gl, x, y, z, 0, 0, 0, 0, 1, 0);
我尝试使用 GLU.gluPerspective() 但它不起作用。
编辑:这是我的渲染器类的更多代码:
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glClearColor(0, 0, 0, 0);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glDisable(GL10.GL_DITHER);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
float z = 3;
float x = 0;
float y = 3;
GLU.gluLookAt(gl, x, y, z, 0, 0, 0, 0, 1, 0);
gl.glRotatef(angleX, 1, 0, 0); //This is the rotation if surface is touched
gl.glRotatef(angleY, 0, 1, 0);
f.draw(gl); //drawing the pieces of the cube
r.draw(gl);
l.draw(gl);
u.draw(gl);
d.draw(gl);
b.draw(gl);
for (int i = 0; i < corners.length; i++) {
corners[i].draw(gl);
}
for (int i = 0; i < edges.length; i++) {
edges[i].draw(gl);
}
display(); //this has nothing to do with opengl
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
}