@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurfaceView = new SensorSurfaceView(this);
setContentView(mGLSurfaceView);
mGLSurfaceView.requestFocus();
mGLSurfaceView.setFocusableInTouchMode(true);
//
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
//sensor
mSensorManager.registerListener(mListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}
// Sensor Stop 추가
@Override
protected void onStop()
{
mSensorManager.unregisterListener(mListener);
super.onStop();
}
private SensorSurfaceView mGLSurfaceView;
//Acc sensor
//private static final String TAG = "Compass";
private SensorManager mSensorManager;
private final SensorEventListener mListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
float x = sensorEvent.values[2]; //0 - head , 1 - Pitch, 2 - roll
float y = sensorEvent.values[1];
//set scale
x = - x ;
y = - y ;
mGLSurfaceView.onSensor(x,y);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
}
/**
* Implement a simple rotation control by sensor.
*
*/
class SensorSurfaceView extends GLSurfaceView {
public SensorSurfaceView(Context context) {
super(context);
mRenderer = new CubeRenderer();
setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public void onSensor(float x, float y){
mRenderer.mAngleX = x;
mRenderer.mAngleY = y;
requestRender();
}
/**
* Render a cube. -> Cube.java 참조할 것
*/
private class CubeRenderer implements GLSurfaceView.Renderer {
public CubeRenderer() {
mCube = new Cube();
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -3.0f);
gl.glRotatef(mAngleX, 0, 1, 0);
gl.glRotatef(mAngleY, 1, 0, 0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
mCube.draw(gl);
}
public int[] getConfigSpec() {
int[] configSpec = {
EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_NONE
};
return configSpec;
}
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, 10);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
gl.glClearColor(1,1,1,1);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
}
private Cube mCube;
public float mAngleX;
public float mAngleY;
}
private CubeRenderer mRenderer;
}