球体绘制类
package test.com.opengles6_1;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.opengl.GLES20;
import android.os.Build;
/**
*
*/
@SuppressLint("NewApi")
public class Ball {
int mProgram;// 自定义渲染管线着色器程序id
int muMVPMatrixHandle;// 总变换矩阵引用
int maPositionHandle; // 顶点位置属性引用
int muRHandle;// 球的半径属性引用
String mVertexShader;// 顶点着色器
String mFragmentShader;// 片元着色器
FloatBuffer mVertexBuffer;// 顶点坐标数据缓冲
int vCount = 0;
float yAngle = 0;// 绕y轴旋转的角度
float xAngle = 0;// 绕x轴旋转的角度
float zAngle = 0;// 绕z轴旋转的角度
float r = 0.8f;
public Ball(MySurfaceView mv) {
// 初始化顶点坐标与着色数据
initVertexData();
// 初始化shader
initShader(mv);
}
// 初始化顶点坐标数据的方法
public void initVertexData() {
// 顶点坐标数据的初始化================begin============================
/**
* 原理见 http://www.tuicool.com/articles/Qjm6bmy
<span style="white-space:pre"> </span>
<span style="white-space:pre"> http://blog.csdn.net/majing19921103/article/details/45017547</span>
*/
ArrayList<Float> alVertix = new ArrayList<Float>();// 存放顶点坐标的ArrayList
final int angleSpan = 2;// 将球进行单位切分的角度
for (int vAngle = -90; vAngle < 90; vAngle = vAngle + angleSpan)// 垂直方向angleSpan度一份
{
for (int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan)// 水平方向angleSpan度一份
{// 纵向横向各到一个角度后计算对应的此点在球面上的坐标
float x0 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle)) * Math.cos(Math
.toRadians(hAngle)));
float y0 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle)) * Math.sin(Math
.toRadians(hAngle)));
float z0 = (float) (r * Constant.UNIT_SIZE * Math.sin(Math
.toRadians(vAngle)));
float x1 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle)) * Math.cos(Math
.toRadians(hAngle + angleSpan)));
float y1 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle)) * Math.sin(Math
.toRadians(hAngle + angleSpan)));
float z1 = (float) (r * Constant.UNIT_SIZE * Math.sin(Math
.toRadians(vAngle)));
float x2 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math
.cos(Math.toRadians(hAngle + angleSpan)));
float y2 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math
.sin(Math.toRadians(hAngle + angleSpan)));
float z2 = (float) (r * Constant.UNIT_SIZE * Math.sin(Math
.toRadians(vAngle + angleSpan)));
float x3 = (float) (r * Constant.UNIT_SIZE
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math
.cos(Math.toRadians(hAngle)));
float y3 &