2013-11-26 96 views
0

我想旋轉我的手指觸摸我的3D立方體,但我無法做到這一點如何做到這一點我的代碼是用於旋轉立方體 它旋轉得相當不錯,但我希望它應該旋轉手指觸摸例如,如果右側是shuold右方向旋轉的手指toches ANS等沿手指旋轉3D立方體android

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    GLSurfaceView view = new GLSurfaceView(this); 
    view.setRenderer(new OpenGLRenderer()); 
    setContentView(view); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    super.onTouchEvent(event); 
    switch (event.getAction()) { 

     case MotionEvent.ACTION_DOWN : 

      break; 
     case MotionEvent.ACTION_MOVE : 
      // handle in between rotation 

      break; 

     case MotionEvent.ACTION_UP : 


      break; 
    } 
return false; 
} 
} 

class OpenGLRenderer extends Activity implements Renderer { 

    private Cube mCube = new Cube(); 
    private float mCubeRotation; 
    @Override 
    public void onDrawFrame(GL10 gl) { 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);   
     gl.glLoadIdentity(); 

     gl.glTranslatef(0.0f, 0.0f, -10.0f); 
     gl.glRotatef(mCubeRotation, 1.0f, 1.0f, 1.0f); 

     mCube.draw(gl); 

     gl.glLoadIdentity();          

     mCubeRotation -= 0.15f; 
    } 
    @Override 
    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, 45.0f, (float)width/(float)height, 0.1f, 100.0f); 
     gl.glViewport(0, 0, width, height); 

     gl.glMatrixMode(GL10.GL_MODELVIEW); 
     gl.glLoadIdentity(); 
    } 
    @Override 
    public void onSurfaceCreated(GL10 gl, 
      javax.microedition.khronos.egl.EGLConfig config) { 
      gl.glClearColor(0.0f, 0.0f, 0.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); 

    } 



} 

class Cube { 

private FloatBuffer mVertexBuffer; 
private FloatBuffer mColorBuffer; 
private ByteBuffer mIndexBuffer; 

private float vertices[] = { 
          -1.0f, -1.0f, -1.0f, 
          1.0f, -1.0f, -1.0f, 
          1.0f, 1.0f, -1.0f, 
          -1.0f, 1.0f, -1.0f, 
          -1.0f, -1.0f, 1.0f, 
          1.0f, -1.0f, 1.0f, 
          1.0f, 1.0f, 1.0f, 
          -1.0f, 1.0f, 1.0f 
          }; 
private float colors[] = { 
          0.0f, 1.0f, 0.0f, 1.0f, 
          0.0f, 1.0f, 0.0f, 1.0f, 
          1.0f, 0.5f, 0.0f, 1.0f, 
          1.0f, 0.5f, 0.0f, 1.0f, 
          1.0f, 0.0f, 0.0f, 1.0f, 
          1.0f, 0.0f, 0.0f, 1.0f, 
          0.0f, 0.0f, 1.0f, 1.0f, 
          1.0f, 0.0f, 1.0f, 1.0f 
         }; 

private byte indices[] = { 
          0, 4, 5, 0, 5, 1, 
          1, 5, 6, 1, 6, 2, 
          2, 6, 7, 2, 7, 3, 
          3, 7, 4, 3, 4, 0, 
          4, 7, 6, 4, 6, 5, 
          3, 0, 1, 3, 1, 2 
          }; 

public Cube() { 
     ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); 
     byteBuf.order(ByteOrder.nativeOrder()); 
     mVertexBuffer = byteBuf.asFloatBuffer(); 
     mVertexBuffer.put(vertices); 
     mVertexBuffer.position(0); 

     byteBuf = ByteBuffer.allocateDirect(colors.length * 4); 
     byteBuf.order(ByteOrder.nativeOrder()); 
     mColorBuffer = byteBuf.asFloatBuffer(); 
     mColorBuffer.put(colors); 
     mColorBuffer.position(0); 

     mIndexBuffer = ByteBuffer.allocateDirect(indices.length); 
     mIndexBuffer.put(indices); 
     mIndexBuffer.position(0); 
} 

public void draw(GL10 gl) {    
     gl.glFrontFace(GL10.GL_CW); 

     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer); 
     gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer); 

     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 

     gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, 
         mIndexBuffer); 

     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glDisableClientState(GL10.GL_COLOR_ARRAY); 
} 

回答

0

我用這個代碼和它的作品

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // 
    float x = event.getX(); 
    float y = event.getY(); 

    //If a touch is moved on the screen 
    if(event.getAction() == MotionEvent.ACTION_MOVE) { 
     //Calculate the change 
     float dx = x - oldX; 
     float dy = y - oldY; 
     //Define an upper area of 10% on the screen 
     int upperArea = this.getHeight()/10; 

     //Zoom in/out if the touch move has been made in the upper 
     if(y < upperArea) { 
      z -= dx * TOUCH_SCALE/2; 

     //Rotate around the axis otherwise 
     } else {     
      xrot += dy * TOUCH_SCALE; 
      yrot += dx * TOUCH_SCALE; 
     }   

    //A press on the screen 
    } else if(event.getAction() == MotionEvent.ACTION_UP) { 
     //Define an upper area of 10% to define a lower area 
     int upperArea = this.getHeight()/10; 
     int lowerArea = this.getHeight() - upperArea; 

     //Change the light setting if the lower area has been pressed 
     if(y > lowerArea) { 
      if(light) { 
       light = false; 
      } else { 
       light = true; 
      } 
     } 
    } 

    //Remember the values 
    oldX = x; 
    oldY = y; 

    //We handled the event 
    return true; 
} 
+0

你好,像你寫我發現的代碼,你可以發佈完整的代碼嗎?謝謝 :) – kemdo