Android OpenGL ES开发学习教程


1)首先你要定义自己的Polygon类,当然你一可以直接在Renderer的子类中直接绘制,为了更加符合面向对象,还是自定义一个Polygon类更好,这样代码更加清晰,Polygon类中主要有Polygon的顶点坐标,和绘制Polygon对象的draw方法,例如下面的代码:

public class Polygon{
 
 /** The buffer holding the Polygon‘s vertices

保存Polygon对象顶点坐标的FloatBuffer

 */
 private FloatBuffer vertexBuffer;
 
 /** The initial vertex definition

保存Polygon对象顶点坐标的的float数组

*/
 private float vertices[] = {
        0.0f, 1.0f, 0.0f,  //Top
        -1.0f, -1.0f, 0.0f, //Bottom Left
        1.0f, -1.0f, 0.0f  //Bottom Right
            };
 
 /**
  * The Triangle constructor.
  *
  * Initiate the buffers.
  */
 public Triangle() {
  //this is the common method to initiate the FloatBuffer

 //下面是一种常用的初始化FloatBuffer的方法,本人还见到过一种方法,如下:

 //vertexBuffer = FloatBuffer.wrap(vertices)

 //但是如果用这种方法在运行的时候会报错,指出你的FloatBuffer没有序列化,

//不明白原因,如有明白的高手帮解释一下,不胜感激
  ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
  byteBuf.order(ByteOrder.nativeOrder());
  vertexBuffer = byteBuf.asFloatBuffer();
  vertexBuffer.put(vertices);
  vertexBuffer.position(0);
 }

 /**
  * The object own drawing function.
  * Called from the renderer to redraw this instance
  * with possible changes in values.
  *
  * @param gl - The GL context
  */
 public void draw(GL10 gl) {

//这就是Polygon被绘制的方法,下面都是一些在draw方法中经常用到的简单的设置,

//在此不一一解释了
  //Set the face rotation
  gl.glFrontFace(GL10.GL_CW);
  
  //Point to our vertex buffer
  gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
  
  //Enable vertex buffer
  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  
  //Draw the vertices as triangle strip
  gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
  
  //Disable the client state before leaving
  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
 }
}

2)下面就是Renderer子类,它就是一个OpenGL渲染器,就是真正绘制3D图形的地方,主要是重写三个方法(三个方法将在下面一一标出),设置一些属性,来完成我们想要达到的效果,代码如下:

public class Myrenderer implements Renderer {
 
  public Myrenderer () {
  triangle = new Triangle();
  square = new Square();
 }

 /**
  * The Surface is created/init()

这个方法是当surface创建时调用的方法,主要是设置一些属性
  */
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {  
  gl.glShadeModel(GL10.GL_SMOOTH);    //Enable Smooth Shading
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);  //Black Background
  gl.glClearDepthf(1.0f);      //Depth Buffer Setup
  gl.glEnable(GL10.GL_DEPTH_TEST);    //Enables Depth Testing
  gl.glDepthFunc(GL10.GL_LEQUAL);    //The Type Of Depth Testing To Do
  
  //Really Nice Perspective Calculations
  gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
 }

 /**
  * Here we do our drawing
  */
 public void onDrawFrame(GL10 gl) {

//这个方法就是真正绘制3D图形的方法,系统会根据机器的性能在固定的时间间隔自动调用这个方法

//这里通过设置gl的属性,和我们定义的Polygon类的顶点坐标来绘制我们想要达到的效果

  //Clear Screen And Depth Buffer
  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
  gl.glLoadIdentity();     //Reset The Current Modelview Matrix
  
  gl.glTranslatef(0.0f, -1.2f, -6.0f); //Move down 1.2 Unit And Into The Screen 6.0
  square.draw(gl);      //Draw the square
  
  gl.glTranslatef(0.0f, 2.5f, 0.0f);  //Move up 2.5 Units
  triangle.draw(gl);      //Draw the triangle  
 }

 /**
  * If the surface changes, reset the view
  */
 public void onSurfaceChanged(GL10 gl, int width, int height) {

 

//这个方法是当surface改变时调用的方法,也是设置一些gl的属性,

//大体的设置没有太大变化,所以这基本上是一个通用的写法
  if(height == 0) {       //Prevent A Divide By Zero By
   height = 1;       //Making Height Equal One
  }

  gl.glViewport(0, 0, width, height);  //Reset The Current Viewport
  gl.glMatrixMode(GL10.GL_PROJECTION);  //Select The Projection Matrix
  gl.glLoadIdentity();      //Reset The Projection Matrix

  //Calculate The Aspect Ratio Of The Window
  GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);

  gl.glMatrixMode(GL10.GL_MODELVIEW);  //Select The Modelview Matrix
  gl.glLoadIdentity();      //Reset The Modelview Matrix
 }
}

 

 

3)最后就是把我们的GLSurfaceView通过activity的setContentView()方法加载到屏幕上,代码如下:

public class Run extends Activity {

 /** The OpenGL View */
 private GLSurfaceView glSurface;

 /**
  * Initiate the OpenGL View and set our own
  * Renderer   */
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  //Create an Instance with this Activity
  glSurface = new GLSurfaceView(this);
  //Set our own Renderer
  glSurface.setRenderer(new Lesson02());
  //Set the GLSurface as View to this Activity
  setContentView(glSurface);
 }

 /**
  * Remember to resume the glSurface
  */
 @Override
 protected void onResume() {
  super.onResume();
  glSurface.onResume();
 }

 /**
  * Also pause the glSurface
  */
 @Override
 protected void onPause() {
  super.onPause();
  glSurface.onPause();
 }

希望给刚刚入门的同学一些帮助,也希望和高手们交流一下经验。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页
【内容导航】
第1页:绘制简单的Polygon 第2页:为Polygon添加颜色
第3页:绘制一个可以翻转的Polygon 第4页:绘制一个六棱柱
第5页:纹理映射

相关内容