# Android多媒体之GLES2战记第六集--九层之台

#### 第九副本：`擎天之柱`：

##### 1.第一关卡：`GL_TRIANGLES画圆`

###### 1.1：顶点的计算

```    /**
* 初始化顶点坐标数据的方法
*
* @param r          半径
* @param splitCount 切分的份数
*/
public void initVertex(float r, int splitCount) {
float dθ = 360.0f / splitCount;//顶角的度数
vertexCount = 3 * splitCount;//顶点个数，共有n个三角形，每个三角形都有三个顶点
float[] vertices = new float[vertexCount * 3];//坐标数据

for (int v = 0, t = 0; v < vertexCount; v += 3, t += 3) {
int n = v / 3;
vertices[3 * v] = 0;//顶点坐标:p0
vertices[3 * v + 1] = 0;
vertices[3 * v + 2] = 0;
vertices[3 * v + 3] = r * cos(n * dθ);//顶点坐标:p1
vertices[3 * v + 4] = r * sin(n * dθ);
vertices[3 * v + 5] = 0;
vertices[3 * v + 6] = r * cos((n + 1) * dθ);//顶点坐标:p2
vertices[3 * v + 7] = r * sin((n + 1) * dθ);
vertices[3 * v + 8] = 0;
}
}```
###### 1.2：贴图坐标的计算

```for (int v = 0, t = 0; v < vertexCount; v += 3, t += 3) {
int n = v / 3;
//顶点坐标计算同上, 略 ....
textures[2 * t] = 0.5f;//贴图：p0
textures[2 * t + 1] = 0.5f;
textures[2 * t + 2] = 0.5f + 0.5f * r * cos(n * dθ);//贴图：p1
textures[2 * t + 3] = 0.5f - 0.5f * r * sin(n * dθ);
textures[2 * t + 4] = 0.5f + 0.5f * r * cos((n + 1) * dθ);//贴图：p2
textures[2 * t + 5] = 0.5f - 0.5f * r * sin((n + 1) * dθ);
}```
##### 2.第二关卡：圆柱侧面

``` /**
* 圆柱侧面
* @param r          半径
* @param h          高度
* @param splitCount 切分的份数
*/
public void initVertex(float r, float h, int splitCount) {
float dθ = 360.0f / splitCount;
vertexCount = splitCount * 4 * 3;//顶点个数，共有3*splitCount*4个三角形，每个三角形都有三个顶点
//坐标数据初始化
float[] vertices = new float[vertexCount * 3];
float[] textures = new float[vertexCount * 2];//顶点纹理S、T坐标值数组

for (int v = 0, t = 0; v < vertexCount; v += 6, t += 6) {
int n = v / 6;
float x = r * cos(n * dθ);
float xNext = r * cos(n * dθ + dθ);
float z = -r * sin(n * dθ);
float zNext = -r * sin(n * dθ + dθ);

vertices[3 * v + 0] = x;//底部p0
vertices[3 * v + 1] = 0;
vertices[3 * v + 2] = z;
vertices[3 * v + 3] = xNext;//顶部p2
vertices[3 * v + 4] = h;
vertices[3 * v + 5] = zNext;
vertices[3 * v + 6] = x;//顶部p1
vertices[3 * v + 7] = h;//y
vertices[3 * v + 8] = z;//z

vertices[3 * v + 9] = x;//底部p0
vertices[3 * v + 10] = 0;
vertices[3 * v + 11] = z;
vertices[3 * v + 12] = xNext;//底部p3
vertices[3 * v + 13] = 0;//y
vertices[3 * v + 14] = zNext;//z
vertices[3 * v + 15] = xNext;//顶部p2
vertices[3 * v + 16] = h;//y
vertices[3 * v + 17] = zNext;//z

float s = n * dθ / 360.f;
float sNext = (n + 1) * dθ / 360.f;
textures[2 * t + 0] = s;//贴图：p0
textures[2 * t + 1] = 1;
textures[2 * t + 2] = sNext;//贴图：p2
textures[2 * t + 3] = 0;
textures[2 * t + 4] = s;//贴图：p1
textures[2 * t + 5] = 0;

textures[2 * t + 6] = s;//贴图：p0
textures[2 * t + 7] = 1;
textures[2 * t + 8] = sNext;//贴图：p3
textures[2 * t + 9] = 1;
textures[2 * t + 10] = sNext;//贴图：p2
textures[2 * t + 11] = 0;
}

//法向量数据初始化
float[] normals = new float[vertices.length];
for (int i = 0; i < vertices.length; i++) {
if (i % 3 == 1) {
normals[i] = 0;
} else {
normals[i] = vertices[i];
}
}
vertexBuffer = GLUtil.getFloatBuffer(vertices);
mNormalBuffer = GLUtil.getFloatBuffer(normals);
mTexCoorBuffer = GLUtil.getFloatBuffer(textures);
}```

##### 3.第三关卡：圆柱的拼接

###### 3.1:移动和旋转的辅助方法`MatrixStack`

```/**
* 设置沿xyz轴移动 注意:本方法和restore联合使用
*
* @param x 移动的 x 分量
* @param y 移动的 y 分量
* @param z 移动的 z 分量
*/
public static void reTranslate(float[] target, float x, float y, float z) {
save();
reset();
Matrix.translateM(MatrixStack.getOpMatrix(), 0, target, 0,
x, y, z);
}
/**
* 设置沿(x,y,z)点旋转 注意:本方法和restore联合使用
*
* @param deg 角度
* @param x   旋转点的 x 分量
* @param y   旋转点的 y 分量
* @param z   旋转点的 z 分量
*/
public static void reRotate(float[] target, float deg, float x, float y, float z) {
save();
reset();
Matrix.rotateM(MatrixStack.getOpMatrix(), 0, target, 0,
deg, x, y, z);
}```
###### 3.2:三块拼型：`Cylinder.java`

```/**
* 作者：张风捷特烈<br/>
* 时间：2019/1/16/016:19:22<br/>
* 邮箱：1981462002@qq.com<br/>
* 说明：圆柱类
*/
public class Cylinder extends RendererAble {
private final Circle mBottomCircle;//底圆
private final Circle mTopCircle;//顶圆
private final CylinderSide mCylinderSide;
private float mH;
/**
* @param context     上下文
* @param h           高
* @param r           底面半径
* @param splitCount  切割数
* @param textureIdX3 贴图id 上、下、周围贴图
*/
public Cylinder(Context context, float r, float h, int splitCount, int[] textureIdX3) {

super(context);
if (textureIdX3.length != 3) {
throw new IllegalArgumentException("the length of textureIdX3 must be 3");
}
mH = h;
mBottomCircle = new Circle(context, r, splitCount, textureIdX3[0]);
mTopCircle = new Circle(context, r, splitCount, textureIdX3[1]);
mCylinderSide = new CylinderSide(mContext, r, h, splitCount, textureIdX3[2]);
}

@Override
public void draw(float[] mvpMatrix) {
MatrixStack.reTranslate(mvpMatrix, 0, 0, mH);
mTopCircle.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();

MatrixStack.reRotate(mvpMatrix, 90, 1, 0, 0);
mCylinderSide.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();

mBottomCircle.draw(mvpMatrix);
}
}```

#### 第十副本：`钻天之锥`：

##### 1.第一关卡：`GL_TRIANGLE_FAN绘制 三角形，拼合圆形`

image

```/**
* 初始化顶点坐标数据的方法
*
* @param r          半径
* @param splitCount 切分的份数
*/
public void initVertex(float r, int splitCount) {
float dθ = 360.0f / splitCount;//顶角的度数
int vertexCount = splitCount + 2;//顶点个数，共有n个三角形，每个三角形都有三个顶点
float[] vertices = new float[vertexCount * 3];//坐标数据
float[] textures = new float[vertexCount * 2];//顶点纹理S、T坐标值数组
vertices[0] = 0;
vertices[1] = 0;
vertices[2] = 0;
textures[0] = 0.5f;
textures[1] = 0.5f;
for (int n = 1; n < vertexCount; n++) {
//顶点坐标
vertices[n * 3 + 0] = r * cos((n - 1) * dθ);//x
vertices[n * 3 + 1] = r * sin((n - 1) * dθ);//y
vertices[n * 3 + 2] = 0;//z
//纹理坐标
textures[2 * n] = 0.5f + 0.5f * cos((n - 1) * dθ);
textures[2 * n + 1] = 0.5f - 0.5f * sin((n - 1) * dθ);
}
}```
##### 2.第二关卡：`圆锥侧面方式一：GL_TRIANGLES`

image

```/**
* 初始化顶点
* @param r          半径
* @param h          高度
* @param splitCount 切分的份数
*/
public void initVertexData(float r, float h, int splitCount) {
float dθ = 360.0f / splitCount;
int vCount = splitCount * 3;//顶点个数，共有3*splitCount*4个三角形，每个三角形都有三个顶点
//坐标数据初始化
float[] vertices = new float[vCount * 3];
float[] textures = new float[vCount * 2];//顶点纹理S、T坐标值数组
float[] normals = new float[vertices.length];//法向量数组
for (int v = 0, t = 0; v < vCount; v += 3, t += 3) {
int n = v / 3;
float x = r * cos(n * dθ);
float xNext = r * cos(n * dθ + dθ);
float z = r * sin(n * dθ);
float zNext = r * sin(n * dθ + dθ);
//顶点坐标
vertices[3 * v + 0] = 0;//p0
vertices[3 * v + 1] = h;
vertices[3 * v + 2] = 0;
vertices[3 * v + 3] = x;//p1
vertices[3 * v + 4] = 0;
vertices[3 * v + 5] = z;
vertices[3 * v + 6] = xNext;//p2
vertices[3 * v + 7] = 0;
vertices[3 * v + 8] = zNext;
//纹理坐标
float s = n * dθ / 360.f;
float sNext = (n + 1) * dθ / 360.f;
textures[2 * t + 0] = 0.5f;//p0
textures[2 * t + 1] = 0f;
textures[2 * t + 2] = s;//p1
textures[2 * t + 3] = 1f;
textures[2 * t + 4] = sNext;//p2
textures[2 * t + 5] = 1f;
}
}```
##### 3.第三关卡：`圆锥侧面方式二：GL_TRIANGLE_FAN`

```/**
* 初始化顶点
*
* @param r          半径
* @param h          高度
* @param splitCount 切分的份数
*/
public void initVertexData(float r, float h, int splitCount) {
float dθ = 360.0f / splitCount;
int vCount = splitCount + 2;//顶点个数，共有3*splitCount*4个三角形，每个三角形都有三个顶点
//坐标数据初始化
float[] vertices = new float[vCount * 3];
float[] textures = new float[vCount * 2];//顶点纹理S、T坐标值数组
float[] normals = new float[vertices.length];//法向量数组
//顶点坐标
vertices[0] = 0;//p0
vertices[1] = h;
vertices[2] = 0;
textures[0] = 0.5f;//p0
textures[1] = 0f;
for (int n = 1; n < vCount; n++) {
float x = r * cos(n * dθ);
float z = r * sin(n * dθ);
//顶点坐标
vertices[3 * n + 0] = x;//p1
vertices[3 * n + 1] = 0;
vertices[3 * n + 2] = z;
//纹理坐标
float s = n * dθ / 360.f;
textures[2 * n + 0] = s;//p1
textures[2 * n + 1] = 1f;
}
}```
##### 4.第三关卡：`拼接圆锥`

```/**
* 作者：张风捷特烈<br/>
* 时间：2019/1/16/016:19:22<br/>
* 邮箱：1981462002@qq.com<br/>
* 说明：圆锥类
*/
public class Cone extends RenderAble {
private  CircleFanEvn mBottomCircleTris;//底圆
private  ConeSideFanEvn mConeSide;//侧面
private float mH;

/**
* @param context     上下文
* @param h           高
* @param r           底面半径
* @param splitCount  切割数
* @param textureIdX2 贴图id 下、周围贴图
*/
public Cone(Context context, float r, float h, int splitCount, int[] textureIdX2) {
super(context);
if (textureIdX2.length != 2) {
throw new IllegalArgumentException("the length of textureIdX3 must be 2");
}
mH = h;
mBottomCircleTris = new CircleFanEvn(context, textureIdX2[0], r, splitCount);
mConeSide = new ConeSideFanEvn(context, textureIdX2[1], r, h,splitCount);
}

@Override
public void draw(float[] mvpMatrix) {
MatrixStack.reRotate(mvpMatrix, 90, 1, 0, 0);
mConeSide.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();
mBottomCircleTris.draw(mvpMatrix);
}
}```

#### 第十一副本： `立方之魔`

##### 1.第一关卡：面封装

image

```/**
* 作者：张风捷特烈<br/>
* 时间：2019/1/17/017:11:28<br/>
* 邮箱：1981462002@qq.com<br/>
* 说明：与Y轴组成的面
*/
public class RectangleEvn extends EvnRender {
public RectangleEvn(Context context, int tId, float x, float y, float z) {
super(context, tId, GLES20.GL_TRIANGLE_STRIP);
initVertex(x, y, z);
}

private void initVertex(float x, float y, float z) {
int vertexCount = 4;//顶点个数，共有n个三角形，每个三角形都有三个顶点
float[] vertices = new float[vertexCount * 3];//坐标数据
float[] textures = new float[vertexCount * 2];//顶点纹理S、T坐标值数组
float[] normals = new float[vertices.length];

//顶点坐标
vertices[0] = 0;//p0
vertices[1] = 0;
vertices[2] = 0;

vertices[3] = 0;//p1
vertices[4] = y;
vertices[5] = 0;

vertices[6] = x;//p3
vertices[7] = 0;
vertices[8] = z;

vertices[9] = x;//p2
vertices[10] = y;
vertices[11] = z;

//贴图坐标
textures[0] = 0;//p0
textures[1] = 1;

textures[2] = 0;//p1
textures[3] = 0;

textures[4] = 1;//p3
textures[5] = 1;

textures[6] = 1;//p2
textures[7] = 0;
init(vertices, textures, normals);
}
}```
##### 2.第二关卡：封装立方

```/**
* 作者：张风捷特烈<br/>
* 时间：2019/1/9 0009:20:09<br/>
* 邮箱：1981462002@qq.com<br/>
* 说明：贴图立方
*/
public class Cube3d extends RenderAble {
private final RectangleEvn mRectA;
private final RectangleEvn mRectB;
private final RectangleEvn mRectD;
private final RectangleEvn mRectC;
private final RectangleEvn mRectE;
private final RectangleEvn mRectF;

private float rate;

private float mX;
private float mY;
private float mZ;

public Cube3d(Context context, float x, float y, float z, int[] textureIdX6) {
super(context);
if (textureIdX6.length != 6) {
throw new IllegalArgumentException("the length of textureIdX3 must be 6");
}
mX = x;
mY = y;
mZ = z;

mRectA = new RectangleEvn(mContext, textureIdX6[0], 0, y, z);
mRectB = new RectangleEvn(mContext, textureIdX6[1], 0, y, z);
mRectC = new RectangleEvn(mContext, textureIdX6[2], 0, y, z);
mRectD = new RectangleEvn(mContext, textureIdX6[3], 0, y, z);
mRectE = new RectangleEvn(mContext, textureIdX6[4], 0, y, z);
mRectF = new RectangleEvn(mContext, textureIdX6[5], 0, y, z);
}

@Override
public void draw(float[] mvpMatrix) {

mRectA.draw(mvpMatrix);

MatrixStack.reTranslate(mvpMatrix, 0, 0, mZ);
MatrixStack.rotate(90, 0, 1, 0);
mRectB.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();

MatrixStack.reTranslate(mvpMatrix, mX, 0, 0);
MatrixStack.rotate(90, 0, -1, 0);
mRectD.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();

MatrixStack.reTranslate(mvpMatrix, 0, 0, 0);
MatrixStack.rotate(-90, 0, 0, 1);

MatrixStack.translate(0, 0, mZ);
MatrixStack.rotate(180, 0, 1, 0);
mRectF.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();

MatrixStack.reTranslate(mvpMatrix, 0, mY, 0);
MatrixStack.rotate(-90, 0, 0, 1);
mRectE.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();

MatrixStack.reTranslate(mvpMatrix, mX, 0, mZ);
MatrixStack.rotate(-180, 0, 1, 0);
mRectC.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();
}

public void setRate(float rate) {
this.rate = rate;
}
}```
##### 3.第三关卡：拼合魔方

```---->[WorldShape#draw]----------
//立方的偏移数组
mTrans = new float[]{
0, 0, 0,
0, 0, 0.5f,
0, 0, -0.5f,

0, 0.5f, 0,
0, 0.5f, 0.5f,
0, 0.5f, -0.5f,

0.5f, 0.5f, 0,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,

0.5f, 0f, 0,
0.5f, 0f, 0.5f,
0.5f, 0f, -0.5f,

0.5f, -0.5f, 0,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,

0f, -0.5f, 0,
0f, -0.5f, 0.5f,
0f, -0.5f, -0.5f,

-0.5f, -0.5f, 0,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,

-0.5f, 0f, 0,
-0.5f, 0f, 0.5f,
-0.5f, 0f, -0.5f,

-0.5f, 0.5f, 0,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};

---->[WorldShape#draw]----------

for (int i = 0; i < mTrans.length / 3; i++) {
MatrixStack.reTranslate(mvpMatrix, mTrans[3 * i], mTrans[3 * i + 1], mTrans[3 * i + 2]);
mCube3d.draw(MatrixStack.getOpMatrix());
MatrixStack.restore();
}```

243 篇文章30 人订阅

0 条评论