Android OpenGL ES(十)绘制三角形Triangle .

三角形为OpenGL ES支持的面,同样创建一个DrawTriangle Activity,定义6个顶点使用三种不同模式来绘制三角形:

 

float vertexArray[] = {
 -0.8f, -0.4f * 1.732f, 0.0f,
 0.0f, -0.4f * 1.732f, 0.0f,
 -0.4f, 0.4f * 1.732f, 0.0f,
 
 0.0f, -0.0f * 1.732f, 0.0f,
 0.8f, -0.0f * 1.732f, 0.0f,
 0.4f, 0.4f * 1.732f, 0.0f,
};

 

本例绘制

 

public void DrawScene(GL10 gl) {
 super.DrawScene(gl);
 
 ByteBuffer vbb
 = ByteBuffer.allocateDirect(vertexArray.length*4);
 vbb.order(ByteOrder.nativeOrder());
 FloatBuffer vertex = vbb.asFloatBuffer();
 vertex.put(vertexArray);
 vertex.position(0);
 
 gl.glLoadIdentity();
 gl.glTranslatef(0, 0, -4);
 
 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
 
 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
 index++;
 index%=10;
 switch(index){
 case 0:
 case 1:
 case 2:
 gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
 gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
 break;
 case 3:
 case 4:
 case 5:
 gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6);
 break;
 case 6:
 case 7:
 case 8:
 case 9:
 gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
 gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);
 break;
 }
 
 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
 
}

 

这里index 的目的是为了延迟一下显示(更好的做法是使用固定时间间隔)。前面说过GLSurfaceView 的渲染模式有两种,一种是连续不断的更新屏幕,另一种为on-demand ,只有在调用requestRender() 在更新屏幕。 缺省为RENDERMODE_CONTINUOUSLY 持续刷新屏幕。

OpenGLDemos 使用的是缺省的RENDERMODE_CONTINUOUSLY持续刷新屏幕 ,因此Activity的drawScene 会不断的执行。本例中屏幕上顺序以红,绿,蓝色显示TRIANGLES, TRIANGLE_STRIP,TRIANGLE_FAN。

 完整代码:

MainActivity.java

package com.example.gltest;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity
       implements IOpenGLDemo{
    float vertexArray[] = {
             -0.8f, -0.4f * 1.732f, 0.0f,
             -0.4f, 0.4f * 1.732f, 0.0f,
             0.0f, -0.4f * 1.732f, 0.0f,
             0.4f, 0.4f * 1.732f, 0.0f,
            };

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow()
             .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
     
            mGLSurfaceView = new GLSurfaceView(this);
            mGLSurfaceView.setRenderer(new OpenGLRenderer(this));
            setContentView(mGLSurfaceView);
        }
     
//        public void DrawScene(GL10 gl) {
//            gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
//            // Clears the screen and depth buffer.
//            gl.glClear(GL10.GL_COLOR_BUFFER_BIT
//              | GL10.GL_DEPTH_BUFFER_BIT);
//     
//        }
        public void DrawScene(GL10 gl) {
             //super.DrawScene(gl);
             
             ByteBuffer vbb
             = ByteBuffer.allocateDirect(vertexArray.length*4);
             vbb.order(ByteOrder.nativeOrder());
             FloatBuffer vertex = vbb.asFloatBuffer();
             vertex.put(vertexArray);
             vertex.position(0);
             
             gl.glLoadIdentity();
             gl.glTranslatef(0, 0, -4);
             
             gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
             
             gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
             int index = 0;
            index ++;
             index%=10;
             switch(index){
             case 0:
             case 1:
             case 2:
             gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
             gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
             break;
             case 3:
             case 4:
             case 5:
             gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
             gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6);
             break;
             case 6:
             case 7:
             case 8:
             case 9:
             gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
             gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);
             break;
             }
             
             gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
             
            }
        @Override
        protected void onResume() {
            // Ideally a game should implement
            // onResume() and onPause()
            // to take appropriate action when the
            //activity looses focus
            super.onResume();
            mGLSurfaceView.onResume();
        }
     
        @Override
        protected void onPause() {
            // Ideally a game should implement onResume()
            //and onPause()
            // to take appropriate action when the
            //activity looses focus
            super.onPause();
            mGLSurfaceView.onPause();
        }
     
        private GLSurfaceView mGLSurfaceView;
     
    }

后面的 OpenGLRenderer.java和  IOpenGLDemo.java一样。

posted @ 2015-04-23 14:51  Anita-ff  阅读(741)  评论(0编辑  收藏  举报