大龙的博客

常用链接

统计

最新评论

一个简单的Android OpenES Demo --转

这个例子是根据APIDemo 中的例子更改而来,主要是加入了我对透视投影和坐标系的理解。

1. Activity

  1. @Override  
  2.    public void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.      
  5.        mGLView = new GLSurfaceView(this);  
  6.        mGLView.setEGLConfigChooser(false);  
  7.        mGLView.setRenderer(new StaticTriangleRenderer(this));        
  8.        setContentView(mGLView);  
  9.    }  

2. RenderView, GLSurface

  1. public class StaticTriangleRenderer extends GLSurfaceView implements GLSurfaceView.Renderer{  
  2.   
  3.     private Context mContext;  
  4.     private Triangle mTriangle;  
  5.     private int mTextureID;  
  6.     private GL11 mGL;   
  7.       
  8.     public StaticTriangleRenderer(Context context) {  
  9.         super(context);  
  10.           
  11.         mContext = context;  
  12.         mTriangle = new Triangle();  
  13.         // We want an 8888 pixel format because that's required for  
  14.         // a translucent window.  
  15.         // And we want a depth buffer.  
  16.         setBackgroundDrawable(null);  
  17.         setFocusable(true);  
  18.         setEGLConfigChooser(8888160);  
  19.         setRenderer(this);  
  20.         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);  
  21.         // Use a surface format with an Alpha channel:  
  22.         getHolder().setFormat(PixelFormat.TRANSLUCENT);  
  23.     }  
  24.   
  25.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  26.         /* 
  27.          * By default, OpenGL enables features that improve quality 
  28.          * but reduce performance. One might want to tweak that 
  29.          * especially on software renderer. 
  30.          */  
  31.         //gl.glDisable(gl.GL_DITHER);  
  32.   
  33.         /* 
  34.          * Some one-time OpenGL initialization can be made here 
  35.          * probably based on features of this particular context 
  36.          */  
  37.         gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT,  
  38.                 gl.GL_FASTEST);  
  39.   
  40.         gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
  41.         gl.glShadeModel(gl.GL_SMOOTH);  
  42.         gl.glEnable(gl.GL_DEPTH_TEST);  
  43.         gl.glEnable(GL_TEXTURE_2D);  
  44.   
  45.         /* 
  46.          * Create our texture. This has to be done each time the 
  47.          * surface is created. 
  48.          */  
  49.   
  50.         int[] textures = new int[1];  
  51.         gl.glGenTextures(1, textures, 0);  
  52.   
  53.         mTextureID = textures[0];  
  54.         gl.glBindTexture(GL_TEXTURE_2D, mTextureID);  
  55.   
  56.         gl.glTexParameterf(GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,  
  57.                 gl.GL_NEAREST);  
  58.         gl.glTexParameterf(GL_TEXTURE_2D,  
  59.                 gl.GL_TEXTURE_MAG_FILTER,  
  60.                 gl.GL_LINEAR);  
  61.   
  62.         gl.glTexParameterf(GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S,  
  63.                 gl.GL_CLAMP_TO_EDGE);  
  64.         gl.glTexParameterf(GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T,  
  65.                 gl.GL_CLAMP_TO_EDGE);  
  66.   
  67.         gl.glTexEnvx(gl.GL_TEXTURE_ENV, gl.GL_TEXTURE_ENV_MODE,  
  68.                 GL11.GL_REPLACE);    
  69.   
  70.         Bitmap bmp =   
  71.             BitmapFactory.decodeResource(mContext.getResources(), R.drawable.myvideo);  
  72.           
  73.         int width = bmp.getWidth();  
  74.         int height = bmp.getHeight();  
  75.   
  76.         Matrix matrix = new Matrix();  
  77.   
  78.         float scaleWidth = ((float128) / width;  
  79.         float scaleHeight = ((float128) / height;  
  80.   
  81.         matrix.postScale(scaleWidth, scaleHeight);  
  82.         Bitmap newbmp = Bitmap.createBitmap(bmp, 00, width, height,  
  83.                 matrix, true);  
  84.             GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, newbmp, 0);        
  85.     }  
  86.   
  87.     public void onDrawFrame(GL10 gl) {  
  88.         /* 
  89.          * By default, OpenGL enables features that improve quality 
  90.          * but reduce performance. One might want to tweak that 
  91.          * especially on software renderer. 
  92.          */  
  93.         gl.glDisable(gl.GL_DITHER);  
  94.   
  95.           
  96.         /* 
  97.          * Usually, the first thing one might want to do is to clear 
  98.          * the screen. The most efficient way of doing this is to use 
  99.          * glClear(). 
  100.          */  
  101.   
  102.         gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);  
  103.   
  104.         /* 
  105.          * Now we're ready to draw some 3D objects 
  106.          */  
  107.   
  108.         gl.glMatrixMode(gl.GL_MODELVIEW);  
  109.         gl.glLoadIdentity();  
  110.   
  111.         //portalDrawFrame(gl);  
  112.         GLU.gluLookAt(gl,   004,        // The position of the eye  
  113.                             0f, 0f, 0f,     // Eye to see the pointer  
  114.                             0f, 1.0f, 0.0f);// The direction of the see  
  115.   
  116.         gl.glEnableClientState(gl.GL_VERTEX_ARRAY);  
  117.         gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);  
  118.          
  119.           
  120.         long time = SystemClock.uptimeMillis() % 4000L;  
  121.         float angle = 0.090f * ((int) time);  
  122.         gl.glRotatef(angle, 001.0f);  
  123.           
  124.         gl.glEnable(GL_TEXTURE_2D);  
  125.         gl.glBindTexture(GL11.GL_TEXTURE_2D, mTextureID);  
  126.   
  127.         mTriangle.draw(gl);  
  128.     }  
  129.   
  130.     public void onSurfaceChanged(GL10 gl, int w, int h) {  
  131.         gl.glViewport(00, w, h);  
  132.   
  133.         /* 
  134.         * Set our projection matrix. This doesn't have to be done 
  135.         * each time we draw, but usually a new projection needs to 
  136.         * be set when the viewport is resized. 
  137.         */  
  138.   
  139.         float ratio = (float) w / h;  
  140.         gl.glMatrixMode(gl.GL_PROJECTION);  
  141.         gl.glLoadIdentity();  
  142.         GLU.gluPerspective(gl, 90, ratio, 4100);  
  143.     }  
  144.       
  145.    
  146.     static class Triangle {  
  147.         public Triangle() {  
  148.   
  149.             // Buffers to be passed to gl*Pointer() functions  
  150.             // must be direct, i.e., they must be placed on the  
  151.             // native heap where the garbage collector cannot  
  152.             // move them.  
  153.             //  
  154.             // Buffers with multi-byte datatypes (e.g., short, int, float)  
  155.             // must have their byte order set to native order  
  156.   
  157.             ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);  
  158.             vbb.order(ByteOrder.nativeOrder());  
  159.             mFVertexBuffer = vbb.asFloatBuffer();  
  160.   
  161.             ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);  
  162.             tbb.order(ByteOrder.nativeOrder());  
  163.             mTexBuffer = tbb.asFloatBuffer();  
  164.   
  165.             ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);  
  166.             ibb.order(ByteOrder.nativeOrder());  
  167.             mIndexBuffer = ibb.asShortBuffer();  
  168.   
  169.             // A unit-sided equilateral triangle centered on the origin.  
  170.             float[] coords = {  
  171.                     // X,  Y, Z  
  172.                     -2.0f,  -3.0f,  -4.0f,  
  173.                      2.0f,  -3.0f,  -4.0f,  
  174.                      2.0f,  3.0f,   -4.0f,  
  175.                      -2.0f, 3.0f,   -4.0f};  
  176.   
  177.             float[] texture = {  
  178.                 0.0f, 0.0f,   
  179.                 0.0f, 1.0f,  
  180.                 1.0f, 1.0f,  
  181.                 1.0f, 0.0f};  
  182.               
  183.             mFVertexBuffer.put(coords);  
  184.             mTexBuffer.put(texture);  
  185.               
  186.             for(int i = 0; i < VERTS; i++) {  
  187.                 mIndexBuffer.put((short) i);  
  188.             }  
  189.   
  190.             mFVertexBuffer.position(0);  
  191.             mTexBuffer.position(0);  
  192.             mIndexBuffer.position(0);  
  193.         }  
  194.   
  195.         public void draw(GL10 gl) {  
  196.             gl.glVertexPointer(3, GL_FLOAT, 0, mFVertexBuffer);  
  197.             glTexCoordPointer(2, GL_FLOAT, 0, mTexBuffer);  
  198.             gl.glDrawElements(GL11.GL_TRIANGLE_FAN, VERTS, GL_UNSIGNED_SHORT, mIndexBuffer);  
  199.             //gl.glDrawArrays(GL11.GL_TRIANGLE_FAN, 0, VERTS);  
  200.         }  
  201.   
  202.         private final static int VERTS = 4;  
  203.         private FloatBuffer mFVertexBuffer;  
  204.         private FloatBuffer mTexBuffer;  
  205.         private ShortBuffer mIndexBuffer;  
  206.     }  
  207. }  

posted on 2011-01-04 17:25 大龙 阅读(826) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理