开发者

gluLookAt not working despite using Push/PopMatrix for transforms

I'm trying to use gluLookAt in an Android app to move the "camera" around, but I'm not seeing any results. I've looked around and I gather that any glLoadIndentity() calls will reset the matrix, cancelling the effect of the gluLookAt.

Problem is, no matter what I put in my gluLookAt call, nothing moves.

So here's the relevant part of my surface setup:

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);
}

And then my frame-drawing method is:

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();

    }

}

And here's an example of the draw() function of my objects, which essentially draw a bunch of elements:

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.glDisableClie开发者_运维知识库ntState(GL10.GL_NORMAL_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}


Give this version a shot:

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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜