GluLookAt not working despite using Push / PopMatrix for transformations

I am trying to use gluLookAt in an Android application to move the “camera”, but I do not see any results. I looked around and realized that any calls to glLoadIndentity () would reset the matrix, overriding the gluLookAt effect.

The problem is that no matter what I injected gluLookAt into my call, nothing moves.

So, here is 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:

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 is 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.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}
+3
source share
1 answer

Make 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();
    }
}
+4
source

Source: https://habr.com/ru/post/1758816/


All Articles