posts - 189,comments - 115,trackbacks - 0

          自己的一個(gè)畫點(diǎn)線面的代碼,有注釋。

          public class dotClass extends GLSurfaceView {
           private trangleFan tFan2d;
           //0x10000似乎是一個(gè)像素的距離
           int one = 0x10000;
              int vertex[] = {
                       one*200, one*200, 
                       one*300, one*500, 
               };
              
              //為各個(gè)點(diǎn)提供顏色,不知道出了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 ); 
                //這個(gè)matrix是如何把三維坐標(biāo)轉(zhuǎn)換為二維坐標(biāo)并把它放在顯示器上。
                gl.glMatrixMode( GL10.GL_PROJECTION );
                //告訴opengl初始化這個(gè)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
             //設(shè)置北京顏色
             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可以接受位置數(shù)組
                gl.glEnableClientState( GL10.GL_VERTEX_ARRAY ); //set for  array as vertex
                //使gl可以接受顏色數(shù)組
                gl.glEnableClientState( GL10.GL_COLOR_ARRAY ); //set for  array as color
                gl.glDisable( GL10.GL_TEXTURE_2D ); //no 2
                
            }
            
           };
            
           
           
              //將int數(shù)組轉(zhuǎn)化成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的大小,也就是下邊的點(diǎn)的尺寸
            gl.glPointSize(size );
            //vertex數(shù)組里存的坐標(biāo),這里的2代表2維,就是數(shù)組里面的數(shù)字2個(gè)位一族表示一個(gè)點(diǎn)的位置。需要裝成buff
            gl.glVertexPointer( 2, GL10.GL_FIXED, 0, getIntBuffer( vertex ) );
            //color數(shù)組里面存儲(chǔ)的點(diǎn)的顏色,這里的4代表RGBA類型顏色。也需要妝化成buff
            gl.glColorPointer( 4, GL10.GL_FIXED, 0, getIntBuffer( color ) );
            //告訴gl畫什么圖形,這里的2代表畫多少個(gè)點(diǎn),及數(shù)組里面包含多少個(gè)點(diǎn)
               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 閱讀(593) 評(píng)論(0)  編輯  收藏 所屬分類: Android3D
          主站蜘蛛池模板: 阿瓦提县| 楚雄市| 丹棱县| 余庆县| 南靖县| 韶关市| 扶风县| 昌都县| 五华县| 始兴县| 射阳县| 清流县| 南投县| 泸州市| 西丰县| 荃湾区| 临沭县| 卓资县| 都匀市| 盐城市| 大厂| 章丘市| 阿瓦提县| 利辛县| 汶川县| 宽城| 肥乡县| 昌乐县| 麻栗坡县| 乌拉特前旗| 肇州县| 京山县| 怀化市| 岗巴县| 湟中县| 海口市| 辰溪县| 突泉县| 正宁县| 吴堡县| 汝城县|