现在的位置: 首页 > 综合 > 正文

Android 3D 旋转的三角形(一)

2013年04月09日 ⁄ 综合 ⁄ 共 4410字 ⁄ 字号 评论关闭

package com.sunny;

import android.app.Activity;
import android.os.Bundle;

public class mainActivity extends Activity {
    private static final String LOG_TAG=mainActivity.class.getSimpleName();
    private VortexView _vortexView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _vortexView=new VortexView(this);
        setContentView(_vortexView);
    }
}

VortexView

package com.sunny;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

public class VortexView extends GLSurfaceView {//继承了GLSurfaceView是因为它会帮助我们画3D图像
    private static final String LOG_TAG=VortexView.class.getSimpleName();
    private VortexRenderer _renderer;//一个Renderer包含画一帧所必需的所有东西
    public VortexView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        _renderer=new VortexRenderer();
        setRenderer(_renderer);
    }
    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        // TODO Auto-generated method stub
        queueEvent(new Runnable(){

            @Override
            public void run() {//调用renderer中的setColor方法
                // TODO Auto-generated method stub
                _renderer.setColor(event.getX()/getWidth(), event.getY()/getHeight(), 1.0f);
                _renderer.setAngle(event.getX()/10);
            }
           
        });
        return true;
    }
   

}

VortexRenderer

package com.sunny;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;
public class VortexRenderer implements GLSurfaceView.Renderer{
    private static final String LOG_TAG=VortexRenderer.class.getSimpleName();
    private float _red=0.9f;//用浮点数来定义RGB颜色系统中的每一个颜色
    private float _green=0.2f;
    private float _blue=0.2f;
   
    private ShortBuffer _indexBuffer;//保存索引
    private FloatBuffer _vertexBuffer;//为三角形保存坐标
    private short[] _indicesArray={0,1,2};
    private int _nrOfVertices=3;//定义需要多少个顶点.对于一个三角形来说,一共需要三个顶点
   
    private float _angle;
    public void setAngle(float angle){
        _angle=angle;
    }
    public void setColor(float r, float g, float b) {
        _red = r;
        _green = g;
        _blue = b;
    }
    private void initTriangle(){
        //为这里两个buffer分配必须的内存
        // float has 4 bytes
        ByteBuffer vbb=ByteBuffer.allocateDirect(_nrOfVertices*3*4);
        vbb.order(ByteOrder.nativeOrder());
        _vertexBuffer=vbb.asFloatBuffer();
        // short has 2 bytes
        ByteBuffer ibb=ByteBuffer.allocateDirect(_nrOfVertices*2);
        ibb.order(ByteOrder.nativeOrder());
        _indexBuffer=ibb.asShortBuffer();
       
        float[] coords={
                -0.5f,-0.5f,0f, // (x1, y1, z1)
                0.5f,-0.5f,0f,// (x2, y2, z2)
                0f,0.5f,0f// (x3, y3, z3)
        };
        _vertexBuffer.put(coords);
        _indexBuffer.put(_indicesArray);
       
        _vertexBuffer.position(0);
        _indexBuffer.position(0);
    }
   
   
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {//surface创建以后调用
        // TODO Auto-generated method stub
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//设置OpenGL使用vertex数组来画
        initTriangle();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int w, int h) {//surface发生改变以后调用,例如从竖屏切换到横屏的时候
        // TODO Auto-generated method stub
        gl.glViewport(0,0,w,h);
    }

    @Override
    public void onDrawFrame(GL10 gl) {//当任何时候调用一个画图方法的时候
        // TODO Auto-generated method stub
        // set rotation
        gl.glRotatef(_angle, 0f, 1f, 0f);//绕y轴旋,参数中的值表示一个向量,标志三角形绕着旋转的坐标轴
       
        // define the color we want to be displayed as the "clipping wall"
        gl.glClearColor(_red, _green, _blue, 1.0f);
        // clear the color buffer to show the ClearColor we called above...
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        // set the color of our element 设置三角形为暗红色
        gl.glColor4f(0.5f, 0f, 0f, 0.5f);
        // define the vertices we want to draw
        /*使用glVertexPointer()初始化Vertex Pointer.
         *  第一个参数是大小,也是顶点的维数。我们使用的是x,y,z三维坐标。
         *  第二个参数,GL_FLOAT定义buffer中使用的数据类型。
         *  第三个变量是0,是因为我们的坐标是在数组中紧凑的排列的,没有使用offset。
         *  最后第四个参数顶点缓冲。
         */
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
        // finally draw the vertices
        /*glDrawElements()将所有这些元素画出来。
         * 第一个参数定义了什么样的图元将被画出来。
         * 第二个参数定义有多少个元素,
         * 第三个是indices使用的数据类型。
         * 最后一个是绘制顶点使用的索引缓冲。
         */
        gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
       
    }

}

 

wps_clip_image-7498

抱歉!评论已关闭.