在android中顺时针和逆时针旋转方块

时间:2013-03-05 06:10:10

标签: android opengl-es android-animation

我使用OPENGL概念在android中创建了一些简单的动画,我要做的是在时钟和逆时针方向上旋转一个正方形。我意味着在特定的时间内,正方形将分别以时钟方式和逆时针方向旋转。

    my main activity


 public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.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);
}

}

OpenGLRenderer.java

public class OpenGLRenderer implements Renderer {
private Square square;
private float angle = 0;

public OpenGLRenderer() {
    // Initialize our square. 
    square = new Square();
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Set the background color to black ( rgba ).
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    // Enable Smooth Shading, default not really needed.
    gl.glShadeModel(GL10.GL_SMOOTH);
    // Depth buffer setup.
    gl.glClearDepthf(1.0f);
    // Enables depth testing.
    gl.glEnable(GL10.GL_DEPTH_TEST);
    // The type of depth testing to do.
    gl.glDepthFunc(GL10.GL_LEQUAL);
    // Really nice perspective calculations.
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

public void onDrawFrame(GL10 gl) {
    // Clears the screen and depth buffer.
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    // Replace the current matrix with the identity matrix
    gl.glLoadIdentity();
    // Translates 10 units into the screen.
    gl.glTranslatef(0, 0, -10); 

    // SQUARE 
    // Save the current matrix.
    gl.glPushMatrix();
    // Rotate square counter-clockwise.
    gl.glRotatef(angle, 0, 0, 1);
    // Draw square .    
    square.draw(gl);
    // Restore the last matrix.
    gl.glPopMatrix();


    square.draw(gl);            

    // Restore to the matrix as it was before B.
    gl.glPopMatrix();

    // Increse the angle.
    angle++;
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    // Sets the current view port to the new size.
    gl.glViewport(0, 0, width, height);
    // Select the projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    // Reset the projection matrix
    gl.glLoadIdentity();
    // Calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
            100.0f);
    // Select the modelview matrix
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // Reset the modelview matrix
    gl.glLoadIdentity();
}

}

Squar.java

public class Square {
// Our vertices.
private float vertices[] = {
          -1.0f,  1.0f, 0.0f,  // 0, Top Left
          -1.0f, -1.0f, 0.0f,  // 1, Bottom Left
           1.0f, -1.0f, 0.0f,  // 2, Bottom Right
           1.0f,  1.0f, 0.0f,  // 3, Top Right
    };

// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };

// Our vertex buffer.
private FloatBuffer vertexBuffer;

// Our index buffer.
private ShortBuffer indexBuffer;

public Square() {
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);
}

/**
 * This function draws our square on screen.
 * @param gl
 */
public void draw(GL10 gl) {
    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW);
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE);
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK);

    // Enabled the vertices buffer for writing and to be used during 
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, 
            GL10.GL_UNSIGNED_SHORT, indexBuffer);

    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    // Disable face culling.
    gl.glDisable(GL10.GL_CULL_FACE);
}

}

这里我的代码在两侧都是旋转的..现在这个部分只出现问题

public class OpenGLRenderer implements Renderer {
private Square square;
private float angle = 0;

public OpenGLRenderer() {
    square = new Square();
}
public void onSurfaceCreated(final GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    gl.glShadeModel(GL10.GL_SMOOTH_LINE_WIDTH_RANGE);
    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);
}

public void onDrawFrame(final GL10 gl) {

     Thread t = new Thread(new Runnable() {     

            public void run(){  

                        try{

                            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                            gl.glLoadIdentity();
                            gl.glTranslatef(0.0f, 4.9f,-9.3f);
                            //gl.glTranslatef(2.0f, 0.0f,0.0f);
                            gl.glPushMatrix();
                            gl.glRotatef(-angle, 0, 0, 1);  

                            gl.glScalef(1.0f, 1.0f, 1.0f);
                            square.draw(gl);
                            gl.glPopMatrix();
                            angle++;
                              Thread.sleep(5000);

                        }
                        catch(InterruptedException e){
                        } 
                       // To do
                        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                        gl.glLoadIdentity();
                        gl.glTranslatef(0.0f, 4.9f,-9.3f);
                        //gl.glTranslatef(2.0f, 0.0f,0.0f);
                        gl.glPushMatrix();
                        gl.glRotatef(angle, 0, 0, 1);   

                        //gl.glScalef(1.0f, 1.0f, 1.0f);
                        square.draw(gl);    
                        gl.glPopMatrix();
                        angle++;
            }
       });
        t.start();

    }



public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    //glClearColor(5.0f, 6.0f, 0.0f, 5.5f); yellow
    gl.glLoadIdentity();
    //GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
    GLU.gluPerspective(gl, 80.0f, (float) width / (float) height, 8.0f, 10.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

}

1 个答案:

答案 0 :(得分:0)

public class Main extends Activity {
private Runnable runnable;  

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CONTEXT_MENU); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    final GLSurfaceView view = new GLSurfaceView(this);
    view.setRenderer(new OpenGLRenderer());
    setContentView(view);

    runnable =new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try 
            {  


                view.setRenderer(new OpenGLRenderer());
                Thread.sleep(10);
            finish();

            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        }  
    };  
    try  
    {  
        Thread t=new Thread(null,runnable);  
        t.start();  
        view.setRenderer(new OpenGLRenderer1());
       // Intent i=new Intent(this,OpenGLRenderer.class);  
         //   startActivity(i);  
    }  
    catch(Exception e)  
    {  

    }  

        }




}
你可以尝试一下。