Created
May 14, 2015 04:43
-
-
Save bitristan/5708d514e8c6c5ac30a9 to your computer and use it in GitHub Desktop.
使用OpenGlES2.0绘制球体
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.opengl.GLES20; | |
import java.nio.ByteBuffer; | |
import java.nio.ByteOrder; | |
import java.nio.FloatBuffer; | |
import java.util.ArrayList; | |
import xyz.sunting.opengles.light.model.TSApplication; | |
import xyz.sunting.opengles.light.model.util.TSMatrixState; | |
import xyz.sunting.opengles.light.model.util.TSShaderUtil; | |
public class Ball { | |
static final float UNIT_SIZE = 1.0f; | |
static final float DEFAULT_RADIUS = 0.5f; | |
int mProgram; | |
int muMVPMatrixHandle; | |
int maPositionHandle; | |
FloatBuffer mVertexBuffer; | |
int vCount = 0; | |
float mRadius; | |
TSMatrixState mState; | |
public Ball(TSMatrixState state) { | |
this(state, DEFAULT_RADIUS); | |
} | |
public Ball(TSMatrixState state, float radius) { | |
mState = state; | |
mRadius = radius; | |
initVertexData(); | |
initShader(); | |
} | |
private void initVertexData() { | |
ArrayList<Float> alVertix = new ArrayList<Float>(); | |
final int angleSpan = 5; | |
for (int vAngle = -90; vAngle < 90; vAngle = vAngle + angleSpan) { | |
for (int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan) { | |
float x0 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle)) * Math.cos(Math | |
.toRadians(hAngle))); | |
float y0 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle)) * Math.sin(Math | |
.toRadians(hAngle))); | |
float z0 = (float) (mRadius * UNIT_SIZE * Math.sin(Math | |
.toRadians(vAngle))); | |
float x1 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle)) * Math.cos(Math | |
.toRadians(hAngle + angleSpan))); | |
float y1 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle)) * Math.sin(Math | |
.toRadians(hAngle + angleSpan))); | |
float z1 = (float) (mRadius * UNIT_SIZE * Math.sin(Math | |
.toRadians(vAngle))); | |
float x2 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math | |
.cos(Math.toRadians(hAngle + angleSpan))); | |
float y2 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math | |
.sin(Math.toRadians(hAngle + angleSpan))); | |
float z2 = (float) (mRadius * UNIT_SIZE * Math.sin(Math | |
.toRadians(vAngle + angleSpan))); | |
float x3 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math | |
.cos(Math.toRadians(hAngle))); | |
float y3 = (float) (mRadius * UNIT_SIZE | |
* Math.cos(Math.toRadians(vAngle + angleSpan)) * Math | |
.sin(Math.toRadians(hAngle))); | |
float z3 = (float) (mRadius * UNIT_SIZE * Math.sin(Math | |
.toRadians(vAngle + angleSpan))); | |
// 将计算出来的XYZ坐标加入存放顶点坐标的ArrayList | |
alVertix.add(x0); | |
alVertix.add(y0); | |
alVertix.add(z0); | |
alVertix.add(x3); | |
alVertix.add(y3); | |
alVertix.add(z3); | |
alVertix.add(x1); | |
alVertix.add(y1); | |
alVertix.add(z1); | |
alVertix.add(x1); | |
alVertix.add(y1); | |
alVertix.add(z1); | |
alVertix.add(x3); | |
alVertix.add(y3); | |
alVertix.add(z3); | |
alVertix.add(x2); | |
alVertix.add(y2); | |
alVertix.add(z2); | |
} | |
} | |
vCount = alVertix.size() / 3;// 顶点的数量为坐标值数量的1/3,因为一个顶点有3个坐标 | |
// 将alVertix中的坐标值转存到一个float数组中 | |
float vertices[] = new float[vCount * 3]; | |
for (int i = 0; i < alVertix.size(); i++) { | |
vertices[i] = alVertix.get(i); | |
} | |
// 创建顶点坐标数据缓冲 | |
// vertices.length*4是因为一个整数四个字节 | |
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); | |
vbb.order(ByteOrder.nativeOrder()); | |
mVertexBuffer = vbb.asFloatBuffer(); | |
mVertexBuffer.put(vertices); | |
mVertexBuffer.position(0); | |
} | |
private void initShader() { | |
String vertexShader = TSShaderUtil.loadFromAssetsFile("normal_vertex.glsl", TSApplication.getInstance().getResources()); | |
String fragmentShader = TSShaderUtil.loadFromAssetsFile("normal_frag.glsl", TSApplication.getInstance().getResources()); | |
mProgram = TSShaderUtil.createProgram(vertexShader, fragmentShader); | |
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); | |
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); | |
} | |
public void drawSelf() { | |
GLES20.glUseProgram(mProgram); | |
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mState.getMVPMatrix(), 0); | |
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); | |
GLES20.glEnableVertexAttribArray(maPositionHandle); | |
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment