posts - 189,comments - 115,trackbacks - 0

自己的一个画点线面的代码,有注释。

public class dotClass extends GLSurfaceView {
 private trangleFan tFan2d;
 //0x10000似乎是一个像素的距离
 int one = 0x10000;
    int vertex[] = {
             one*200, one*200, 
             one*300, one*500, 
     };
    
    //为各个点提供颜色,不知道出了rgb以外的颜色怎么提供
     int color[] = {
       one,   one ,   one,  one,
             one,    one,    0,  one,
     };
     
     int triangleColor[] = {
    one, 0, 0, one,
    0, one, 0, one,
    0, 0, one, one,
  };
  int triangleVertex[] = {
   0, 0,
   100*one, 0,
   50*one, 100*one,
 };
 
 
     
 public dotClass( Context ctx){
  super( ctx );
  Init();  
 }
 public dotClass( Context ctx, AttributeSet attrs){
  super( ctx, attrs );
  Init();  
 }

 //
 private void Init(){
  tFan2d=new trangleFan( triangleVertex, triangleColor );
  this.setRenderer( renderer );
  
 
 }
 
 
 
 
 GLSurfaceView.Renderer renderer=new Renderer(){

  @Override
  public void onDrawFrame(GL10 gl) {
   // TODO Auto-generated method stub
   
   gl.glClear( GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT ); 
     
   drawLine(gl, vertex, color, 5);
   drawPoint(gl, vertex, color, 20);
//   tFan.draw( gl );
   tFan2d.draw(gl);
//   tStrip.draw(gl);
  }

  @Override
  public void onSurfaceChanged(GL10 gl, int width, int height) {
   // TODO Auto-generated method stub
   //首先是opengl可用的空间 :
      gl.glViewport( 0, 0, width/2, height/2 ); 
      //这个matrix是如何把三维坐标转换为二维坐标并把它放在显示器上。
      gl.glMatrixMode( GL10.GL_PROJECTION );
      //告诉opengl初始化这个matrix。
      gl.glLoadIdentity(); 
      GLU.gluOrtho2D( gl, 0.0f, width, 0.0f, height );
  }

  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
   // TODO Auto-generated method stub
   //设置北京颜色
   gl.glClearColor( 0.0f, 1.0f, 1.0f, 0.0f ); //

      gl.glShadeModel( GL10.GL_FLAT ); //
      
      gl.glHint( GL10.GL_POINT_SMOOTH_HINT, GL10.GL_FASTEST ); //
      //使gl可以接受位置数组
      gl.glEnableClientState( GL10.GL_VERTEX_ARRAY ); //set for  array as vertex
      //使gl可以接受颜色数组
      gl.glEnableClientState( GL10.GL_COLOR_ARRAY ); //set for  array as color
      gl.glDisable( GL10.GL_TEXTURE_2D ); //no 2
      
  }
  
 };
  
 
 
    //将int数组转化成inbuff类型,为glVertexPointer,glColorPointer方法用
    private IntBuffer getIntBuffer( int[] table ) {
        ByteBuffer bb = ByteBuffer.allocateDirect( table.length * 4 ); 
        bb.order( ByteOrder.nativeOrder() ); 
        IntBuffer ib = bb.asIntBuffer(); 
        ib.put( table ); 
        ib.position( 0 ); 
        
        return ib;
    }
    
    private void drawPoint( GL10 gl, int[] vertex, int[] color, int size ){
     //gl的大小,也就是下边的点的尺寸
  gl.glPointSize(size );
  //vertex数组里存的坐标,这里的2代表2维,就是数组里面的数字2个位一族表示一个点的位置。需要装成buff
  gl.glVertexPointer( 2, GL10.GL_FIXED, 0, getIntBuffer( vertex ) );
  //color数组里面存储的点的颜色,这里的4代表RGBA类型颜色。也需要妆化成buff
  gl.glColorPointer( 4, GL10.GL_FIXED, 0, getIntBuffer( color ) );
  //告诉gl画什么图形,这里的2代表画多少个点,及数组里面包含多少个点
     gl.glDrawArrays( GL10.GL_POINTS, 0, 2 );
    }
    private void drawLine( GL10 gl, int[] vertex, int[] color, int size ){
  gl.glLineWidth( size ); //set line width
  gl.glVertexPointer( 2, GL10.GL_FIXED, 0, getIntBuffer( vertex ) ); //set vertex points
  gl.glColorPointer( 4, GL10.GL_FIXED, 0, getIntBuffer( color ) ); //set color points
     gl.glDrawArrays( GL10.GL_LINES, 0, 1*2 );  //mode is GL_LINES
    }
    class trangleFan{
     private IntBuffer vertexBuff = null;
     private IntBuffer colorBuff = null;
     private int pointNum;
     
     
     public trangleFan( int vertex[], int color[] ){
      if( vertex != null ){
       vertexBuff = getIntBuffer( vertex ); 
       pointNum = vertex.length / 2;
      }
      if( color != null ){
       colorBuff = getIntBuffer( color ); 
      }
     }

     public void draw( GL10 gl ){
      if( vertexBuff != null ){
       gl.glVertexPointer( 2, GL10.GL_FIXED, 0, vertexBuff );
       gl.glColorPointer( 4, GL10.GL_FIXED, 0, colorBuff );
       gl.glDrawArrays( GL10.GL_TRIANGLES, 0, pointNum );
      }
     }
    }
   
    
}


http://code.google.com/p/linuxgraphicsweb/source/browse/trunk/Pages/android/gallery3d_glsurfaceview.muse?spec=svn176&r=176

posted on 2010-08-26 20:17 MEYE 阅读(587) 评论(0)  编辑  收藏 所属分类: Android3D

只有注册用户登录后才能发表评论。


网站导航: