#android
#Android
Вопрос:
*я хочу коснуться экрана, и тогда cube изменит положение, я не знаю, что не так с моим кодом *
public class GLCamTest extends Activity {
private CamLayer mPreview;
private GLLayer glView,myRender;
static int counter=0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView glView = new GLSurfaceView(this);
myRender = new GLLayer(this);
glView.setRenderer(myRender);
counter ;
if (counter==2) {
MediaPlayer mp=MediaPlayer.create(this, R.raw.track);
mp.start();
}
}
/** Called when the activity is first created. */
@Override
public void onResume() {
super.onResume();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
final Window win = getWindow();
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
GLSurfaceView glView = new GLSurfaceView(this);
myRender = new GLLayer(this);
glView.setRenderer(myRender);
glView=new GLLayer(this);
mPreview = new CamLayer(this, (PreviewCallback) glView);//有改過
/*x1=new GLLayer(this);
glView.setRenderer(x1);*/
setContentView(glView);
addContentView(mPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
@Override
public boolean onTouchEvent(MotionEvent event)
{ super.onResume();
if(event.getAction() == MotionEvent.ACTION_DOWN)
myRender.light = !myRender.light;
return super.onTouchEvent(event);
}
protected void onPause() {
super.onPause();
if (counter>=2) {
System.exit(0);
}
}}`
package de.offis.magic.core;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.ViewGroup.LayoutParams;
import android.os.Bundle;
/**
* This class uses OpenGL ES to render the camera's viewfinder image on the
* screen. Unfortunately I don't know much about OpenGL (ES). The code is mostly
* copied from some examples. The only interesting stuff happens in the main
* loop (the run method) and the onPreviewFrame method.
*/
public class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback,
Camera.PreviewCallback, Renderer {
int onDrawFrameCounter=1;
int[] cameraTexture;
byte[] glCameraFrame=new byte[1024*1024]; //size of a texture must be a power of 2
FloatBuffer cubeBuff,cubeBuff2;
FloatBuffer texBuff,texBuff2;
public boolean light = true;
public GLLayer(Context c) {
super(c);
this.setEGLConfigChooser(5, 6, 5, 8, 16, 0); // red, green, blue, alpha depth
this.setRenderer(this); //渲染
this.getHolder().setFormat(PixelFormat.TRANSLUCENT); //半透明
}
public void onDrawFrame(GL10 gl) {
onDrawFrameCounter ;
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //清除螢幕和深度緩存
//bindCameraTexture(gl); //補camera捕捉到的變成紋理
gl.glLoadIdentity(); //重置矩陣
GLU.gluLookAt(gl, 0, 0, 4.2f, 0, 0, 0, 0, 1, 0);//http://pyopengl.sourceforge.net/documentation/ref/glu/lookat.html
gl.glTranslatef(-4,1,-3);
gl.glRotatef(onDrawFrameCounter,1,10,0); //Rotate the camera image 角度 x y z
gl.glTranslatef(-5,2,-3);
gl.glRotatef((float)Math.tan(onDrawFrameCounter/20.0f)*10,0,1,0); //Rotate the camera image
gl.glTranslatef(-1,0,0);
gl.glRotatef((float)Math.sin(onDrawFrameCounter/20.0f)*100,1,0,0); //Rotate the camera image
gl.glNormal3f(0,0,1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,12, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,16, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,20, 4);
gl.glRotatef((float)Math.sin(onDrawFrameCounter/20.0f)*100,1,0,0); //Rotate the camera image
//bindCameraTexture(gl); //補camera捕捉到的變成紋理
gl.glLoadIdentity();
gl.glTranslatef(5,2,-6);
gl.glRotatef(onDrawFrameCounter,0,1,0); //Rotate the camera image 角度 x y z
gl.glNormal3f(0,0,1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,12, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,16, 4);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,20, 4);
if (!light)
{
gl.glTranslatef(-4,2,-6);
}
else
{
gl.glTranslatef(-4,2,-6);
}
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height); // 設定圖形要顯示的區域
float ratio = (float)2* width / height;
gl.glMatrixMode(GL10.GL_PROJECTION); //設投影矩陣
gl.glLoadIdentity(); //重置
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); //選擇模型觀察矩陣
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 4.2f, 0, 0, 0, 0, 1, 0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0, 0, 0, 0);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
cubeBuff = makeFloatBuffer(camObjCoord);
texBuff = makeFloatBuffer(camTexCoords);
cubeBuff2 = makeFloatBuffer(camObjCoord);
texBuff2 = makeFloatBuffer(camTexCoords);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff); //設頂點座標
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff2);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //開紋理
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff2);//將紋理連繫到物體
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
/**
* Generates a texture from the black and white array filled by the onPreviewFrame
* method.
*/
void bindCameraTexture(GL10 gl) {
synchronized(this) {
if (cameraTexture==null)
cameraTexture=new int[1];
else
gl.glDeleteTextures(1, cameraTexture, 0);
gl.glGenTextures(1, cameraTexture, 0); //建紋理
int tex = cameraTexture[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, 256, 256, 0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE, ByteBuffer.wrap(glCameraFrame));
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
}
}
/**
* This method is called if a new image from the camera arrived. The camera
* delivers images in a yuv color format. It is converted to a black and white
* image with a size of 256x256 pixels (only a fraction of the resulting image
* is used). Afterwards Rendering the frame (in the main loop thread) is started by
* setting the newFrameLock to true.
*/
public void onPreviewFrame(byte[] yuvs, Camera camera) {
int bwCounter=0;
int yuvsCounter=0;
for (int y=0;y<160;y ) {
System.arraycopy(yuvs, yuvsCounter, glCameraFrame, bwCounter, 240);
yuvsCounter=yuvsCounter 240;
bwCounter=bwCounter 256;
}
}
FloatBuffer makeFloatBuffer(float[] arr) {
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(arr);
fb.position(0);
return fb;
}
final static float camObjCoord[] = new float[] {
// FRONT
-2.0f, -1.5f, 2.0f,
2.0f, -1.5f, 2.0f,
-2.0f, 1.5f, 2.0f,
2.0f, 1.5f, 2.0f,
// BACK
-2.0f, -1.5f, -2.0f,
-2.0f, 1.5f, -2.0f,
2.0f, -1.5f, -2.0f,
2.0f, 1.5f, -2.0f,
// LEFT
-2.0f, -1.5f, 2.0f,
-2.0f, 1.5f, 2.0f,
-2.0f, -1.5f, -2.0f,
-2.0f, 1.5f, -2.0f,
// RIGHT
2.0f, -1.5f, -2.0f,
2.0f, 1.5f, -2.0f,
2.0f, -1.5f, 2.0f,
2.0f, 1.5f, 2.0f,
// TOP
-2.0f, 1.5f, 2.0f,
2.0f, 1.5f, 2.0f,
-2.0f, 1.5f, -2.0f,
2.0f, 1.5f, -2.0f,
// BOTTOM
-2.0f, -1.5f, 2.0f,
-2.0f, -1.5f, -2.0f,
2.0f, -1.5f, 2.0f,
2.0f, -1.5f, -2.0f,
};
final static float camTexCoords[] = new float[] {
// Camera preview
0.0f, 0.0f,
0.9375f, 0.0f,
0.0f, 0.625f,
0.9375f, 0.625f,
// BACK
0.9375f, 0.0f,
0.9375f, 0.625f,
0.0f, 0.0f,
0.0f, 0.625f,
// LEFT
0.9375f, 0.0f,
0.9375f, 0.625f,
0.0f, 0.0f,
0.0f, 0.625f,
// RIGHT
0.9375f, 0.0f,
0.9375f, 0.625f,
0.0f, 0.0f,
0.0f, 0.625f,
// TOP
0.0f, 0.0f,
0.9375f, 0.0f,
0.0f, 0.625f,
0.9375f, 0.625f,
// BOTTOM
0.9375f, 0.0f,
0.9375f, 0.625f,
0.0f, 0.0f,
0.0f, 0.625f
};
}
*я хочу коснуться экрана, и тогда cube изменит положение, я не знаю, что не так с моим кодом *
`
Ответ №1:
Вам нужно реализовать onTouchEvent(…) в вашем классе view, а не в activity.
Смотрите пример TouchRotateActivity в Android SDK
Ответ №2:
glview.setonTouchListener(new View.onTouchListener(){
public boolean onTouch(View v,MotionEvent event){
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN)
{
//action to be performed
return true;
}
return false;
}
});