尽管使用Push / PopMatrix进行转换,gluLook仍无法正常工作

时间:2010-08-09 22:03:18

标签: android opengl-es

我正在尝试在Android应用中使用gluLookAt移动“相机”,但我没有看到任何结果。我环顾四周,我认为任何glLoadIndentity()调用都会重置矩阵,取消gluLookAt的效果。

问题是,无论我在gluLookAt调用中放入什么,都没有动作。

所以这是表面设置的相关部分:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glDisable(GL10.GL_DITHER);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glClearColor(0f, 0f, 0f, 0.5f);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluPerspective(gl, 45f, (float) width / (float) height, 0.01f, 200f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
}

然后我的框架绘制方法是:

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    GLU.gluLookAt(gl, 0, 0, 100, 0, 0, 0, 0, 1, 0);

    for (int i = 0; i < obj.size; i++) { // obj is an array of positions
        obj = mObj[i];

        gl.glPushMatrix();
        gl.glLoadIdentity();
        gl.glTranslatef(obj.position.x(), obj.position.y(), obj.position.z());
        gl.glRotatef(obj.rotation.x(), 1f, 0f, 0f);
        gl.glRotatef(obj.rotation.y(), 0f, 1f, 0f);
        gl.glRotatef(obj.rotation.z(), 0f, 0f, 1f);
        obj.draw(gl); // the objects 'draw' themselves
        gl.glPopMatrix();

    }

}

这是我的对象的draw()函数的一个例子,它实际上绘制了一堆元素:

class SomeObject {

    public void draw(GL10 gl) {
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glFrontFace(GL10.GL_CCW);
        gl.glCullFace(GL10.GL_BACK);            

        gl.glColor4f(1f, 0f, 0f, 1f);
        gl.glDrawElements(GL10.GL_TRIANGLES, SomeObject.fCount, GL10.GL_UNSIGNED_BYTE, faceBuffer);
        gl.glColor4f(1, 1, 1, 1);

        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}

1 个答案:

答案 0 :(得分:4)

给这个版本一个镜头:

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, 100, 0, 0, 0, 0, 1, 0);

    for (int i = 0; i < obj.size; i++) { // obj is an array of positions
        obj = mObj[i];

        gl.glPushMatrix();
        gl.glTranslatef(obj.position.x(), obj.position.y(), obj.position.z());
        gl.glRotatef(obj.rotation.x(), 1f, 0f, 0f);
        gl.glRotatef(obj.rotation.y(), 0f, 1f, 0f);
        gl.glRotatef(obj.rotation.z(), 0f, 0f, 1f);
        obj.draw(gl); // the objects 'draw' themselves
        gl.glPopMatrix();
    }
}