Andriod Java OpenGL ES基本框架

Andriod Java OpenGL ES基本框架(sdk1.5)

考察下Android平台,也许可以在上面做些Game之类的

Android的OpenGL ES,一般使用类android.opengl.GLSurfaceView;
下面使用5个class构造一个基本的框架

Main为入口Activity
MyOpenGLView为窗口,可取得消息做处理。
MyOpenGLRender为渲染器,这里可获得gl句柄,实现gl初始化,窗口大小改变,帧绘制。
MyDraw为OpenGL ES的实际操作,当然,可以合并到上面渲染器中,为了明晰,独立出来。
  其中包含函数init(gl),change(gl,w,h),drawframe(gl);这样修改起来是不是更便捷?
MyCube是一个彩色盒子的类,定义了顶点,色彩,面索引,draw(gl)即可绘制一个旋转的盒子。

1.入口 Main
package myopengl.openglframe;

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

//入口程序,在此构造MyOpenGLView
public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setContentView(new MyOpenGLView(this));
    }
}

2.GLView,可在此取得面板等消息
package myopengl.openglframe;

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

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

//OpenGL视口,可处理输入消息
public class MyOpenGLView extends GLSurfaceView {
    private MyOpenGLRender _renderer;
 
    public MyOpenGLView(Context context) {
        super(context);
        _renderer = new MyOpenGLRender(context);
        setRenderer(_renderer);//设置当前使用的渲染器
    }
   
//面板消息处理
    public boolean onTouchEvent(final MotionEvent event) {
        queueEvent(new Runnable() {
            public void run() {
//                _renderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);
            }
        });
        return true;
    }
}

3.渲染器Renderer
package myopengl.openglframe;

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

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;

//OpenGL ES Surface渲染器

public class MyOpenGLRender implements GLSurfaceView.Renderer {
    private Context mContext;
    MyDraw mydraw;
   
    public MyOpenGLRender(Context context)
    {
        mContext = context;
    }
   
//    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     //这里初始化OpenGL ES
     mydraw=new MyDraw(gl);
    }

    // @Override
    public void onSurfaceChanged(GL10 gl, int w, int h) {
     //屏幕改变
     mydraw.change(gl,w,h);
    }
 
 //   @Override
    public void onDrawFrame(GL10 gl) {
        //这里绘制帧
     mydraw.drawframe(gl);
        }
}

4.MyDraw OpenGL ES功能程序
package myopengl.openglframe;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

public class MyDraw {
 
 MyCube mycube;
 
 public MyDraw(GL10 gl){
     init(gl); 
      mycube=new MyCube(); //建立一个盒子
 }

       public void init(GL10 gl){
          //初始化OpenGL ES

     gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

         gl.glClearColor(0.0f, 0.0f, 1.0f, 1);
      gl.glShadeModel(GL10.GL_SMOOTH);
//      gl.glEnable(GL10.GL_TEXTURE_2D);
      gl.glDisable(GL10.GL_TEXTURE_2D);
      gl.glDisable(GL10.GL_DEPTH_TEST);
      gl.glDepthFunc(GL10.GL_LEQUAL);
      gl.glDisable(GL10.GL_DITHER);
      gl.glDisable(GL10.GL_LIGHTING);
// nbsp;    gl.glEnable(GL10.GL_LIGHT0);
     
 }

 //OpenGL ES窗口变化处理,设定透视模式
 public void change(GL10 gl,int w,int h){
     gl.glViewport(0, 0, w, h);

     float ratio = (float) w / h;
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);

        gl.glDisable(gl.GL_DITHER);

        gl.glClearColor(1, 1, 1, 1);
        gl.glEnable(gl.GL_SCISSOR_TEST);
        gl.glScissor(0, 0, w, h);
        gl.glClear(gl.GL_COLOR_BUFFER_BIT);

        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -3.0f);
        gl.glScalef(0.5f, 0.5f, 0.5f);

        gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);

        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
        gl.glEnableClientState(gl.GL_COLOR_ARRAY);
//      gl.glEnableClientState(gl.GL_NORMAL_ARRAY);
//      gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);

 
        gl.glEnable(gl.GL_CULL_FACE);
}
 
 //OpenGL ES帧绘制
 public void drawframe(GL10 gl){
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glClearColor(0,0,0,1.0f);

        mycube.draw(gl); //绘制盒子
 }
}

5.彩色盒子:建立与绘制
package myopengl.openglframe;

import javax.microedition.khronos.opengles.GL10;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

//一个盒子的建立,绘制

class MyCube {
 private float mAngleX=0.0f;
 private float mAngleY=0.0f;
 private IntBuffer mVertexBuffer; //顶点
    private IntBuffer mColorBuffer;  //颜色
    private ByteBuffer mIndexBuffer; //索引
//    private IntBuffer mTextureBuffer;//贴图坐标

    public MyCube() {
         int one = 0x10000;
/*        
后         3-2
     0-1

     7-6
     4-5
    
         */
         //顶点坐标数据
         int vertices[] = { -one, -one, -one,
                            one, -one, -one,
                            one, one, -one,
                            -one, one, -one,
                            -one, -one, one,
                            one, -one, one,
                            one, one, one,
                            -one, one, one, };

         //顶点颜色 
         int colors[] = { 0, 0, 0, one,
                          one, 0, 0, one,
                          one, one, 0, one,
                          0, one, 0, one,
                          0, 0, one, one,
                          one, 0, one, one,
                          one, one, one, one,
                          0, one, one, one, };
        
 /*        //顶点贴图坐标
         int texCoords[] ={
           0,0,
           0,0,
           0,0,
           0,0,
           0,0,
           one,0,
           one,one,
           0,one
         };*/

         //面的索引      
         byte indices[] = {
              0, 4, 5,
                                  0, 5, 1,
                                  1, 5, 6,
                                  1, 6, 2,
                                  2, 6, 7,
                           &nbs;      2, 7, 3,
                                  3, 7, 4,
                                  3, 4, 0,
                                  4, 7, 6,
                                  4, 6, 5,
                                  3, 0, 1,
                                  3, 1, 2 };

         //建立顶点缓存mVertexBuffer
         ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);  // * 4 becuase of int
         vbb.order(ByteOrder.nativeOrder());
         mVertexBuffer = vbb.asIntBuffer();
         mVertexBuffer.put(vertices);
         mVertexBuffer.position(0);

         //建立颜色缓存mColorBuffer
         ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); // * 4 becuase of int
         cbb.order(ByteOrder.nativeOrder());
         mColorBuffer = cbb.asIntBuffer();
         mColorBuffer.put(colors);
         mColorBuffer.position(0);

         //建立索引缓存mIndexBuffer 
         mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
         mIndexBuffer.put(indices);
         mIndexBuffer.position(0);
        
/*         //建立贴图坐标缓存mTextureBuffer
         ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
         tbb.order(ByteOrder.nativeOrder());
         mTextureBuffer = tbb.asIntBuffer();
         mTextureBuffer.put(texCoords);
         mTextureBuffer.position(0);
*/
    }

    public void draw(GL10 gl)
 {
     gl.glEnable(GL10.GL_DITHER);
        
        gl.glPushMatrix();

        gl.glMatrixMode(gl.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -3.0f);
        gl.glScalef(0.5f, 0.5f, 0.5f);
        gl.glRotatef(mAngleX,        1, 0, 0);
        gl.glRotatef(mAngleY,        0, 1, 0);

        gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
        gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
        gl.glEnableClientState(gl.GL_COLOR_ARRAY);
        gl.glEnable(gl.GL_CULL_FACE);

        
        gl.glFrontFace(gl.GL_CW);
        gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);
        gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);

//      gl.glNormalPointer(gl.GL_FIXED,mNormalBuffer);
//      gl.glTexCoordPointer(2, gl.GL_FIXED, 0, mTextureBuffer);
//      gl.glBindTexture(GL10.GL_TEXTURE_2D, blueButtonTexture);

        gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);
          
        gl.glPopMatrix();
       
        mAngleX+=2;
        mAngleY+=2;
    }

}

posted @ 2010-11-22 16:03  IT圈儿  阅读(1581)  评论(0)    收藏  举报