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 閱讀(598) 評論(0)  編輯  收藏 所屬分類: Android3D
          主站蜘蛛池模板: 临汾市| 舞钢市| 红安县| 息烽县| 武乡县| 江陵县| 澳门| 嘉兴市| 星座| 泗阳县| 玉溪市| 天峻县| 隆尧县| 嘉祥县| 隆安县| 普定县| 曲麻莱县| 包头市| 黑山县| 上虞市| 芮城县| 遵化市| 体育| 工布江达县| 遂平县| 涡阳县| 澳门| 清徐县| 阳泉市| 资源县| 长海县| 府谷县| 乐平市| 新竹市| 宁安市| 永丰县| 广元市| 区。| 溧阳市| 武穴市| 云安县|