2013-11-25 44 views
0

她是我的代碼幫助我如何實現touchlistner並用手指旋轉立方體。我創建了立方體繪製的立方體的單獨類。MainActivity其中SurfaceView也是declare和Renderer班級從MainActivity調用。在Android中用手指旋轉3D對象

public class GLCubeRenderer implements Renderer { 

//private float oldX; //valor anterior de X, para rotación 
//private float oldY; //valor anterior de Y, para rotación 
private GLCube cube; 
static float ratio; 
private final float[] mAccumulatedRotation = new float[16]; 
private final float[] mCurrentRotation = new float[16]; 
float[] mModelMatrix; 

public GLCubeRenderer(){ 

    cube = new GLCube(); 
} 

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig eglconfig) { 
    gl.glDisable(GL10.GL_DITHER); 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
    gl.glClearColor(.8f, 0, .7f, 1); 
    gl.glClearDepthf(1f); 
    Matrix.setIdentityM(mAccumulatedRotation, 0); 

} 

@Override 
public void onDrawFrame(GL10 gl) { 
    gl.glDisable(GL10.GL_DITHER); 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); 
    GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0); 
    long time =SystemClock.uptimeMillis() %4000L ; 
    float angle = .090f *((int) time); 
    gl.glRotatef(angle, 1, 0, 2); 
    gl.glRotatef(angle, 0, 0, 1); 
    cube.Draw(gl); 
} 

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 

    gl.glViewport(0, 0, width, height); 
    ratio = (float) width/height ; 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25); 
} 

回答