我使用OpenGL做的第一个Android 3D效果图,留着纪念。(每一步都有详细注释,欢迎参考)

来源:互联网 发布:嘉华plc编程软件 编辑:程序博客网 时间:2024/05/11 21:59

package cn.itcast.xx;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

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

import android.opengl.GLSurfaceView.Renderer;

public class myRender implements Renderer{
 private int one = 0x10000;
 private float rotateTri; //三角形旋转的角度
    private IntBuffer triggerBuffer = IntBuffer.wrap(//三角形的三个顶点
      new int[]{0,one,0,    //上顶点
        -one,-one,0,  //左下点
        one,-one,0});//右下点
    private IntBuffer colorBuffer = IntBuffer.wrap(//三角形顶点的颜色值
      new int[]{one,0,0,one,   //红色
          0,one,0,one,   //绿色
          0,0,one,one, });// 蓝色
 @Override
 public void onDrawFrame(GL10 gl) {
  // TODO Auto-generated method stub
  // 所有的绘图操作都在此方法中进行
  
  
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  gl.glLoadIdentity(); //在绘图之前,需要将屏幕清除成前面所指定的颜色,
                       //清除深度缓存并重置场景,然后就可以进行绘图了。
  
  // 左移1.5个单位,并移入屏幕6.0个单位
  gl.glTranslatef(-1.5f, 0.0f, -6.0f);
  
  //开启设置顶点功能
  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  //开启颜色渲染功能
  gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  //设置三角形,size用来描述顶点的尺寸,本例中使用xyz坐标系,所以是3
  //type描述了顶点的类型,本例中数据是固定的,所以是FIXED
  //stride描述了步长
  //pointer则是顶点缓存,即我们创建的顶点数组
  gl.glVertexPointer(3, GL10.GL_FIXED, 0, triggerBuffer);
  
  
        //为图形着色
  
  
  //为图形定义设置好的颜色
  gl.glColorPointer(4, GL10.GL_FIXED, 0, colorBuffer);
  
  //旋转三角形,可通过控制xyz轴的数值改变图形旋转的轴
  gl.glRotatef(rotateTri,0.0f,0.0f,1.0f);
  rotateTri += 0.5f;
  
  //绘制三角形。参数mode描述了绘制的模式,我们使用TRIANGLES表示绘制三角形
  //first跟count分别表示开始位置和要绘制的顶点计数
  gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
  //如果还需要再画另外的图形,重复23-37行代码,如果不画三角形,则需重新定制顶点数组
  
  
  //关闭颜色渲染功能
  gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
  // 切记:画完之后,需要关闭顶点设置功能
  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  
  
  
  
 }

 @Override
 public void onSurfaceChanged(GL10 gl, int width, int height) {
  
  // TODO Auto-generated method stub
  
  float ratio = (float) width / height;
  //设置OpenGL 显示场景的大小为所在窗口的大小
  gl.glViewport(0, 0, width, height);
  //设置投影矩阵
  gl.glMatrixMode(GL10.GL_PROJECTION);
  //重置投影矩阵
  gl.glLoadIdentity();
  //设置视口的大小
  gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  gl.glMatrixMode(GL10.GL_MODELVIEW);
  gl.glLoadIdentity();
 }

 @Override
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  // TODO Auto-generated method stub
  // 启用阴影平滑
  gl.glShadeModel(GL10.GL_SMOOTH);
  
  // 黑色背景
  gl.glClearColor(0,0,one,one);
  
  // 设置深度缓存
  gl.glClearDepthf(1.0f);       
  // 启用深度测试
  gl.glEnable(GL10.GL_DEPTH_TEST);      
  // 所作深度测试的类型
  gl.glDepthFunc(GL10.GL_LEQUAL);       
  
  // 告诉系统对透视进行修正
  gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
 }
   
 
}

 

package cn.itcast.xx;

import java.util.List;

 

 

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class myActivity extends Activity {
    /** Called when the activity is first created. */
 
  Renderer render = new myRender();
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GLSurfaceView mView = new GLSurfaceView(this);
        mView.setRenderer(render); 
     setContentView(mView);
       
 
 }
}

原创粉丝点击