Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112465
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2017-03-28 12:00:19

参考,这是一位德国人Martin写的文章,年代比较久远了.但是入门还是比较好的
1. 工程的建立
eclipse --> 新建一项目  com.example.gldemo
SDK 都选API 19: android 4.4 (KitKat)


2. MainActivity.java  -->跟以前一样
  1. package com.example.gldemo;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;

  6. public class MainActivity extends Activity {
  7.     private VortexView _vortexView;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         _vortexView = new VortexView(this);
  12.         setContentView(_vortexView);
  13.         //setContentView(R.layout.activity_main);
  14.     }

  15.     @Override
  16.     public boolean onCreateOptionsMenu(Menu menu) {
  17.         // Inflate the menu; this adds items to the action bar if it is present.
  18.         getMenuInflater().inflate(R.menu.main, menu);
  19.         return true;
  20.     }

  21.     @Override
  22.     public boolean onOptionsItemSelected(MenuItem item) {
  23.         // Handle action bar item clicks here. The action bar will
  24.         // automatically handle clicks on the Home/Up button, so long
  25.         // as you specify a parent activity in AndroidManifest.xml.
  26.         int id = item.getItemId();
  27.         if (id == R.id.action_settings) {
  28.             return true;
  29.         }
  30.         return super.onOptionsItemSelected(item);
  31.     }
  32. }
VortexView.java-->跟以前一样
  1. package com.example.gldemo;

  2. import android.content.Context;
  3. import android.opengl.GLSurfaceView;
  4. import android.view.MotionEvent;

  5. public class VortexView extends GLSurfaceView{
  6.     private static final String LOG_TAG="VortexView";
  7.     private VortexRenderer _renderer;
  8.     public VortexView (Context context){
  9.         super(context);
  10.         _renderer = new VortexRenderer();
  11.         setRenderer(_renderer);
  12.     }

  13.     public boolean onTouchEvent(final MotionEvent event){
  14.         queueEvent(new Runnable(){
  15.             public void run(){
  16.                 _renderer.setColor(event.getX()/getWidth(),
  17.                                  event.getY()/getHeight(), 1.0f);    
  18.             }
  19.         });
  20.         return true;
  21.     }
  22. }
VortexRenderer.java
  1. package com.example.gldemo;

  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import java.nio.ShortBuffer;

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

  8. import android.opengl.GLSurfaceView;

  9. public class VortexRenderer implements GLSurfaceView.Renderer {
  10.     private static final String LOG_TAG="VortexRenderer";
  11.     private float _red = 0.9f;
  12.     private float _green = 0.9f;
  13.     private float _blue = 0.9f;
  14.     //use for initTriangle
  15.     private ShortBuffer _indexBuffer;          -->三角形的索引
  16.     private FloatBuffer _vertexBuffer;         -->三角形的坐标
  17.     private short[] _indicesArray = {0, 1, 2}
  18.     private int _nrOfVertices = 3;             -->三角形的顶点数为3个   
  19.     @Override
  20.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  21.         // TODO Auto-generated method stub    
  22.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);    -->设置opengl使用vertex数组来画
  23.         initTriangle();                                  -->初始化vertex数组
  24.     }
  25.     
  26.     @Override
  27.     public void onSurfaceChanged(GL10 gl, int w, int h){
  28.         gl.glViewport(0, 0, w, h);
  29.     }
  30.     
  31.     @Override
  32.     public void onDrawFrame(GL10 gl ){
  33.         gl.glClearColor(_red, _green, _blue, 1.0f);         -->用定义的rgb来设设置背景色
  34.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);               -->清空buffer
  35.         gl.glColor4f(0.5f, 0f,    0f, 0.5f);                -->设置绘制三角形的颜色
  36.         //设置顶点: 3代表三维,GL_FLOAT代表buffer里面是float,0代表没有offset,_vertexBuffer是存放顶点坐标的buffer
  37.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer)
  38.         //画:GL_TRIANGLES代表要画三角形,_nrOfVertices代表一个三角形三个顶点,GL_UNSIGNED_SHORT是type,_indexBuffer指向索引存贮位置的指针
  39.         gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
  40.     }
  41.     public void setColor(float r, float g, float b) {
  42.         _red = r;
  43.         _green = g;
  44.         _blue = b;
  45.     }
  46.     private void initTriangle(){
  47.         //ByteBuffer vbb = ByteBuffer.allocate(_nrOfVertices*3*4);
  48.         ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices*3*4);   -->这儿要用allocateDirect方法
  49.         vbb.order(ByteOrder.nativeOrder());
  50.         _vertexBuffer = vbb.asFloatBuffer();
  51.         
  52.         //ByteBuffer ibb = ByteBuffer.allocate(_nrOfVertices*3*4);
  53.         ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices*3*4);
  54.         ibb.order(ByteOrder.nativeOrder());
  55.         _indexBuffer = ibb.asShortBuffer();
  56.         
  57.         float [] coords = {
  58.                 -0.5f, -0.5f, 0f,
  59.                 0.5f, -0.5f, 0f,
  60.                 0.0f, 0.5f, 0f
  61.         };
  62.         
  63.         _vertexBuffer.put(coords);
  64.         _indexBuffer.put(_indicesArray);
  65.         _vertexBuffer.position(0);
  66.         _indexBuffer.position(0);        
  67.     }
  68. }
注意:原文中使用了ByteBuffer.allocate,但是这个方法现在不能使用了,会报以下错误
E/AndroidRuntime( 6951): java.lang.IllegalArgumentException: Must use a native order direct Buffer
替换为allocateDirect就可以了

3. 按屏幕后出现一个旋转的三角形
glDemo1.rar(下载后不用改名)
在VortexRenderer.java中的onDrawFrame
  1. public void onDrawFrame(GL10 gl ){
  2.         gl.glClearColor(_red, _green, _blue, 1.0f);
  3.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  4.         gl.glRotatef(_angle, 0f, 1f, 0f);
  5.         gl.glColor4f(0.5f, 0f,    0f, 0.5f);
  6.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
  7.         gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
  8.     }

阅读(1879) | 评论(0) | 转发(0) |
0

上一篇:opengl开发----1.opengles的helloworld

下一篇:没有了

给主人留下些什么吧!~~