'Google'에 해당되는 글 22건
- 2009.09.02 [Android] ARU (Augmented Reality for yoU) 1
- 2009.07.26 [Android] Camera와 OpenGL 동시에 표시하기 14
- 2009.07.25 [Android] OpenGL 로 Sensor 이용하기
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Preview mPreview = new Preview(this);
mRenderer = new CubeRenderer(true);
mGLSurfaceView = new GLSurfaceView(this);
//translucent
mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
mGLSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);
mGLSurfaceView.setRenderer(mRenderer);
//OpenGL 먼저 표시하기
setContentView(mGLSurfaceView);
//그 다음 카메라 표시하기
addContentView(mPreview, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
private GLSurfaceView mGLSurfaceView;
private CubeRenderer mRenderer;
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
}
//////////////////////////////
// camer preview
////////////////////////////
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
boolean mPreviewRunning = false;
Preview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
mCamera.startPreview();
}
};
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try{
mCamera.setPreviewDisplay(holder);
if (false) throw new IOException();
}catch (IOException e){
//do something
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mPreviewRunning = false;//// added
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mPreviewRunning) { ///added
mCamera.stopPreview();
}
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(w, h);
mCamera.setParameters(parameters);
mCamera.startPreview();
//
try{
mCamera.setPreviewDisplay(holder);
if (false) throw new IOException();
}catch (IOException e){
//do something
}
mCamera.startPreview();
mPreviewRunning = true; ///added
}
}
@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;
}