views:

140

answers:

1

This code:

public void onSurfaceChanged(GL10 gl, int w, int h) {

gl.glViewport(0, 0, w, h);// 
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();    
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// set camera zoom
GLU.gluPerspective(gl, 45.0f,(float) w / h, 0.1f, 100.0f);
// point camera
GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
public void onDrawFrame(GL10 gl) {
// clear last frame
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// set model and projection matrices to identity
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);    
square.draw(gl);
}

works as expected on the emulator (on Android 2.1 virtual device) but on phone (HTC Desire Android 2.1) it just clears the screen, can't see anything drawn. If I comment out the

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

it works on the phone, but not if I have the gluLookAt call in onDrawFrame.

What's the problem with gluLookAt in onDrawFrame?

A: 

You shouldn't need to call

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();

every frame. If you're using GLU.gluLookAt() it should handle all of that. Granted, clearing the projection matrix shouldn't cause any problems, but maybe there is some sort of optimization going on that causes it to break. What happens if you comment out those two lines?

CaseyB
Thanks CaseyB, but commenting out those lines makes no difference - still works on emulator and nothing drawn on phone
Mercia Labs
But if I call gluPerspective again in onSurfaceDraw it works correctly on both emulator and phone. IOW: public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); GLU.gluPerspective(gl, 45.0f, ratio, 0.1f, 100.0f); GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); gl.glColor4f(0, 1, 0, 1); square.draw(gl); }Is good. This implies phone looses viewing frustum setting between calls to onSurfaceDraw, but emulator not. How can that be?
Mercia Labs