package org.android.demo;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Bitmap;
import android.opengl.GLUtils;
/**
* @see 绘制用到的通用方法
* @author (虾米吃鱼肉干)
*
*/
public class MyUtil {
private final static float PI = (float) Math.PI;
public static float DEPTH = -3.0f;// 深度
public static int NUMBER = 40;// 网格数量Num*Num
int[] mTextures;// 纹理标识数组
float[] mBase = new float[3];// 原点偏移量
private static float dx;// 单位x坐标
private static float dy;// 单位y坐标
private static float tx;// 单位纹理坐标
private static float angle;// 单位角度
private float mVertexsArray[][][] = new float[NUMBER][NUMBER][3];// 顶点坐标数组
private float mTexCoordsArray[][][] = new float[NUMBER][NUMBER][2];// 纹理坐标数组
private float mNormalsArray[][][] = new float[NUMBER][NUMBER][3];// 法向量坐标数组
private short mOrderArray[] = new short[(NUMBER - 1) * (NUMBER - 1) * 4];// 绘制顺序数组
FloatBuffer mVertexBuffer;// 顶点
FloatBuffer mTexCoordsBuffer;// 纹理
FloatBuffer mNormalsBuffer;// 法向量
ShortBuffer mOrderBuffer;// 绘制顺序
int rot = 0;// 旋转角度
int k = 0;//
float d;// 深度中间缓存
/**
* @see 初始化顶点信息、纹理信息、顶点法向量信息、顶点顺序信息
*/
public MyUtil() {
super();
initInfo();
generationPoints();
getPointBuffer();
getTexCoordsBuffer();
getNormals();
getNormalsBuffer();
getOrder();
}
/**
* @see 设置网格密度 n*n
* @param n
*/
public void setNum(int n) {
NUMBER = n;
}
/**
* @see 设置坐标深度值
* @param d
*/
public void setDepth(float d) {
DEPTH = d;
}
/**
* @see 初始化单位值
*/
private void initInfo() {
// 5 3.35
dx = 3.0f / (NUMBER - 1);
dy = 2.0f / (NUMBER - 1);
tx = 1.0f / (NUMBER - 1);
angle = ((2 * PI) / (NUMBER - 1));
mBase[0] = 0;
mBase[1] = 0;
mBase[2] = DEPTH;
}
/**
* @see 生成顶点坐标与纹理坐标
*/
public void generationPoints() {
for (int i = 0; i < NUMBER; i++) {
d = getDepth(i);
for (int j = 0; j < NUMBER; j++) {
mVertexsArray[i][j][0] = i * dx - NUMBER * dx / 2;
mVertexsArray[i][j][1] = NUMBER * dy / 2 - j * dy;
mVertexsArray[i][j][2] = d;
mTexCoordsArray[i][j][0] = i * tx;
mTexCoordsArray[i][j][1] = (NUMBER - 1 - j) * tx;
}
}
}
/**
* @see 计算深度值根据正弦曲线规律
* @param i
* @return
*/
public float getDepth(int i) {
return (DEPTH + (float) Math.sin(2 * i * angle)) * 0.05f
* Math.abs(DEPTH);
}
/**
* @see 得到绘制顶点顺序buffer
*/
private void getOrder() {
for (int i = 0; i < NUMBER - 1; i++) {
for (int j = 0; j < NUMBER - 1; j++) {
mOrderArray[(i * (NUMBER - 1) + j) * 4 + 0] = (short) (i
* NUMBER + j);
mOrderArray[(i * (NUMBER - 1) + j) * 4 + 1] = (short) ((i + 1)
* NUMBER + j);
mOrderArray[(i * (NUMBER - 1) + j) * 4 + 2] = (short) (i
* NUMBER + j + 1);
mOrderArray[(i * (NUMBER - 1) + j) * 4 + 3] = (short) ((i + 1)
* NUMBER + j + 1);
}
}
mOrderBuffer = ShortBuffer.wrap(mOrderArray);
}
/**
* @see 得到顶点buffer
*/
private void getPointBuffer() {
float vertex[] = new float[NUMBER * NUMBER * 3];
for (int i = 0; i < NUMBER; i++) {
for (int j = 0; j < NUMBER; j++) {
for (int k = 0; k < 3; k++) {
vertex[(i * NUMBER + j) * 3 + k] = mVertexsArray[i][j][k];
}
}
}
mVertexBuffer = FloatBuffer.wrap(vertex);
}
/**
* @see 得到纹理buffer
*/
private void getTexCoordsBuffer() {
float texCoord[] = new float[NUMBER * NUMBER * 2];
for (int i = 0; i < NUMBER; i++) {
for (int j = 0; j < NUMBER; j++) {
for (int k = 0; k < 2; k++) {
texCoord[(i * NUMBER + (NUMBER - 1 - j)) * 2 + k] = mTexCoordsArray[i][j][k];
}
}
}
mTexCoordsBuffer = FloatBuffer.wrap(texCoord);
}
/**
* @see 1得到法向量buffer
*/
private void getNormalsBuffer() {
float normal[] = new float[NUMBER * NUMBER * 3];
for (int i = 0; i < NUMBER; i++) {
for (int j = 0; j < NUMBER; j++) {
for (int k = 0; k < 3; k++) {
normal[(i * NUMBER + j) * 3 + k] = mNormalsArray[i][j][k];
}
}
}
mNormalsBuffer = FloatBuffer.wrap(normal);
}
/**
* @see 2计算出顶点法向量坐标
*/
private void getNormals() {
float[] nor = new float[3];// 三个顶点
for (int i = 0; i < NUMBER; i++) {
nor = getVertexsNormal(i, 1);
for (int j = 0; j < NUMBER; j++) {
mNormalsArray[i][j][0] = nor[0];
mNormalsArray[i][j][1] = nor[1];
mNormalsArray[i][j][2] = nor[2];
}
}
}
/**
* @see 3计算顶点法向量
* @param i
* @param j
* @return
*/
private float[] getVertexsNormal(int i, int j) {
float[] nor1 = new float[3];
float[] nor2 = new float[3];
float[] nor3 = new float[3];
float[] nor4 = new float[3];
float[] temp = new float[] { 0, 0, -1.0f, };
if (i - 1 < 0 || j - 1 < 0) {
nor1 = temp;
} else {
nor1 = getNormalFrom3Vertexs(new float[] {
mVertexsArray[i][j - 1][0], mVertexsArray[i][j - 1][1],
mVertexsArray[i][j - 1][2] }, new float[] {
mVertexsArray[i - 1][j][0], mVertexsArray[i - 1][j][1],
mVertexsArray[i - 1][j][2] }, new float[] {
mVertexsArray[i][j][0], mVertexsArray[i][j][1],
mVertexsArray[i][j][2] });
}
if (j - 1 < 0 || i + 1 == NUMBER) {
nor2 = temp;
} else {
nor2 = getNormalFrom3Vertexs(new float[] {
mVertexsArray[i + 1][j][0], mVertexsArray[i + 1][j][1],
mVertexsArray[i + 1][j][2] }, new float[] {
mVertexsArray[i][j - 1][0], mVertexsArray[i][j - 1][1],
mVertexsArray[i][j - 1][2] }, new float[] {
mVertexsArray[i][j][0], mVertexsArray[i][j][1],
mVertexsArray[i][j][2] });
}
if (i - 1 < 0 || j + 1 == NUMBER) {
nor3 = temp;
} else {
nor3 = getNormalFrom3Vertexs(new float[] { mVertexsArray[i][j][0],
mVertexsArray[i][j][1], mVertexsArray[i][j][2] },
new float[] { mVertexsArray[i - 1][j][0],
mVertexsArray[i - 1][j][1],
mVertexsArray[i - 1][j][2] }, new float[] {
mVertexsArray[i][j + 1][0],
mVertexsArray[i][j + 1][1],
mVertexsArray[i][j + 1][2] });
}
if (i + 1 == NUMBER || j + 1 == NUMBER) {
nor4 = temp;
} else {
nor4 = getNormalFrom3Vertexs(new float[] {
mVertexsArray[i + 1][j][0], mVertexsArray[i + 1][j][1],
mVertexsArray[i + 1][j][2] }, new float[] {
mVertexsArray[i][j][0], mVertexsArray[i][j][1],
mVertexsArray[i][j][2] }, new float[] {
mVertexsArray[i][j + 1][0], mVertexsArray[i][j + 1][1],
mVertexsArray[i][j + 1][2] });
}
return getNormalFrom4Normals(nor1, nor2, nor3, nor4);
}
/**
* @see 4根据每个顶点相邻的4个三角形计算出该顶点的法向量
* @param nor1
* @param nor2
* @param nor3
* @param nor4
* @return
*/
private float[] getNormalFrom4Normals(float[] nor1, float[] nor2,
float[] nor3, float[] nor4) {
float nor[] = new float[3];
nor[0] = nor1[0] + nor2[0] + nor3[0] + nor4[0];
nor[1] = nor1[1] + nor2[1] + nor3[1] + nor4[1];
nor[2] = nor1[2] + nor2[2] + nor3[2] + nor4[2];
return unitNormal(nor);
}
/**
* @see 5通过三个顶点计算该三角形的平面法向量
* @param v1
* @param v2
* @param v3
*/
private float[] getNormalFrom3Vertexs(float[] v1, float[] v2, float[] v3) {
float a0 = 0, a1 = 0, a2 = 0, b0 = 0, b1 = 0, b2 = 0;
float nor[] = new float[3];
a0 = v2[0] - v1[0];
a1 = v2[1] - v1[1];
a2 = v2[2] - v1[2];
b0 = v3[0] - v2[0];
b1 = v3[1] - v2[1];
b2 = v3[2] - v2[2];
nor[0] = a1 * b
HappyGirl快乐女孩
- 粉丝: 1w+
- 资源: 4152
最新资源
- 《铭仕基本法》(2006年定稿版)-17页.doc
- 海尔企业文化手册.doc
- xx有限公司企业文化手册.doc
- 【案例分析】企业文化案例精选.doc
- 陕西省地方电力集团公司企业文化手册道德礼仪规范.doc
- 微信小程序 实现计算器代码
- 01-【目标体系构建】-企业文化建设规划与实施细则方案撰写指导说明.doc
- 03-【目标体系构建】-企业文化建设工作任务分解与工作推进表.docx
- 02-【目标体系构建】-企业文化建设三年实施规划甘特图.docx
- 04-【识别体系构建】-员工行为规范制定指导书.doc
- 07-【保障体系构建】-职务说明书——企业文化专员.doc
- 06-【保障体系构建】-职务说明书——企业文化建设职能部门(负责人).doc
- 08-【保障体系构建】-企业文化建设经费预算表(年度).doc.docx
- 12-【保障体系构建】-企业文化培训成本费用预算表.doc.docx
- 10-【保障体系构建】-企业文化培训效果综合评估表.doc.docx
- 11-【保障体系构建】-企业文化培训计划表.doc.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈