基于Android的OpenGL—使用GLSurfaceView

Android提供了两个基本的类让我们使用OpenGL ES API来创建和操纵图形:GLSurfaceView和 GLSurfaceView.Renderer。因此我们首先需要了解这两个类。

1.      GLSurfaceView:

这是一个视图类,你可以调用OpenGL API在上面绘制图形和操纵物体,功能和SurfaceView相似。我们可以创建一个GLSurfaceView类的实例,并添加自己的渲染器。如果我们要自己实现一些触摸屏的操作,我们必须扩展这个类来实现触摸监听器。

 

2.      GLSurfaceView.Renderer

这个接口定义了在一个OpenGL的GLSurfaceView中绘制图形所需要的方法。我们必须在一个单独的类中为这些接口提供实现,并使用GLSurfaceView.setRenderer()方法将它依附到GLSurfaceView实例对象上。

我们需要实现GLSurfaceView.Renderer的以下方法:

a)      onSurfaceCreated():系统在创建GLSurfaceView时调用它一次。我们可以使用它来设置OpenGL的环境变量,或是初始化OpenGL的图形物体。

b)      onDrawFrame():系统在每次重绘GLSurfaceView时调用这个方法。这个方法主要完成绘制图形的操作。

c)      onSurfaceChanged():系统在GLSurfaceView的几何属性发生改变时调用该方法,包括大小或是设备屏幕的方向发生变化。例如,系统在屏幕从直立变为水平使调用它。这个方法主要用来对GLSurfaceView容器的变化进行响应。

 

实验步骤

1.      添加新项目

2.      添加新类myGLRenderer,实现GLSurfaceView.Renderer接口

代码如下:

[java] <span style="font-size:16px;">public class myGLRenderer implements Renderer { 
 
    @Override 
    public void onDrawFrame(GL10 gl) { 
        // TODO Auto-generated method stub  
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//清空缓存  
    } 
 
    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) { 
        // TODO Auto-generated method stub  
        gl.glViewport(0, 0, width, height);//设置视口  
    } 
 
    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
        // TODO Auto-generated method stub  
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); //设置清除色  
    } 
 

</span> 
<span style="font-size:16px;">public class myGLRenderer implements Renderer {

 @Override
 public void onDrawFrame(GL10 gl) {
  // TODO Auto-generated method stub
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//清空缓存
 }

 @Override
 public void onSurfaceChanged(GL10 gl, int width, int height) {
  // TODO Auto-generated method stub
        gl.glViewport(0, 0, width, height);//设置视口
 }

 @Override
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  // TODO Auto-generated method stub
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); //设置清除色
 }

}
</span>

 


3.      添加新的类myGLSurfaceView,父类为GLSurfaceView

代码如下:

 

[java] <span style="font-size:16px;">public class myGLSurfaceView extends GLSurfaceView { 
 
    public myGLSurfaceView(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub  
        mrender = new myGLRenderer(); 
        setRenderer(mrender); 
    } 
    private myGLRenderer mrender; 

</span> 
<span style="font-size:16px;">public class myGLSurfaceView extends GLSurfaceView {

 public myGLSurfaceView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  mrender = new myGLRenderer();
  setRenderer(mrender);
 }
 private myGLRenderer mrender;
}
</span>

 


4.      主程序代码如下:

 

[java] <span style="font-size:16px;">public class HelloOpenGLActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        mGLSurfaceView = new myGLSurfaceView(this); 
        setContentView(mGLSurfaceView);//这里我们用mGLSurfaceView来替换以前常用的R.layout.main  
    }    
    private myGLSurfaceView mGLSurfaceView; 

</span> 
<span style="font-size:16px;">public class HelloOpenGLActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLSurfaceView = new myGLSurfaceView(this);
        setContentView(mGLSurfaceView);//这里我们用mGLSurfaceView来替换以前常用的R.layout.main
    }  
    private myGLSurfaceView mGLSurfaceView;
}
</span>

 


这样,我们便完成了使用OpenGL绘制灰色背景的应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值