简体   繁体   English

沿手指android旋转3d立方体

[英]rotate a 3d cube along finger android

i want to rotate my 3d cube usning my finger touch but i am unable to do that how to do it my code is for rotating cube it is rotatting quite nisely but i want it should rotate on finger touch for example if finger toches of right side it shuold rotate at right direction ans so on 我想用手指触摸来旋转我的3d立方体,但是我无法做到这一点。我的代码是旋转立方体,它正好在旋转,但是我希望它在手指触摸时也可以旋转,例如,如果手指的右手边shuold沿正确的方向旋转ans,依此类推

  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);
}

i used this code and its works 我用了这段代码及其作品

@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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM