openGL es2.0 创建纹理球
一、Java代码:
package com.gzdxid.utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import android.opengl.GLES20;
public class DrawBallTexture {
int mProgram;
int muMVPMatrixHandle;
int maPositionHandle;
int maTexCoorHandle;
FloatBuffer mVertexBuffer;
FloatBuffer mTexCoorBuffer;
int vCount = 0;
final float UNIT_SIZE = 1f;
final float angleSpan = 10f;
float R = 0;
public float roateX;
public float roateY;
public DrawBallTexture(float r, int mProgram) {
// TODO Auto-generated constructor stub
initVertex(r);
initShader(mProgram);
}
private void initVertex(float r) {
// TODO Auto-generated method stub
R = r;
ArrayList<Float> alVertix = new ArrayList<Float>();
for (float vAngle = 90; vAngle > -90; vAngle -= angleSpan) {
for (float hAngle = 360; hAngle > 0; hAngle -= angleSpan) {
float x1 = getCoor(0, vAngle, hAngle);
float y1 = getCoor(1, vAngle, hAngle);
float z1 = getCoor(2, vAngle, hAngle);
float x2 = getCoor(0, vAngle - angleSpan, hAngle);
float y2 = getCoor(1, vAngle - angleSpan, hAngle);
float z2 = getCoor(2, vAngle - angleSpan, hAngle);
float x3 = getCoor(0, vAngle - angleSpan, hAngle - angleSpan);
float y3 = getCoor(1, vAngle - angleSpan, hAngle - angleSpan);
float z3 = getCoor(2, vAngle - angleSpan, hAngle - angleSpan);
float x4 = getCoor(0, vAngle, hAngle - angleSpan);
float y4 = getCoor(1, vAngle, hAngle - angleSpan);
float z4 = getCoor(2, vAngle, hAngle - angleSpan);
alVertix.add(x1);
alVertix.add(y1);
alVertix.add(z1);
alVertix.add(x2);
alVertix.add(y2);
alVertix.add(z2);
alVertix.add(x4);
alVertix.add(y4);
alVertix.add(z4);
// 构建第二三角形
alVertix.add(x4);
alVertix.add(y4);
alVertix.add(z4);
alVertix.add(x2);
alVertix.add(y2);
alVertix.add(z2);
alVertix.add(x3);
alVertix.add(y3);
alVertix.add(z3);
}
}
vCount = alVertix.size() / 3;
float vertices[] = new float[vCount * 3];
for (int i = 0; i < alVertix.size(); i++) {
vertices[i] = alVertix.get(i);
}
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
float[] texCoor = generateTexCoor(// 获取切分整图的纹理数组
(int) (360 / angleSpan), // 纹理图切分的列数
(int) (180 / angleSpan) // 纹理图切分的行数
);
ByteBuffer llbb = ByteBuffer.allocateDirect(texCoor.length * 4);
llbb.order(ByteOrder.nativeOrder());// 设置字节顺序
mTexCoorBuffer = llbb.asFloatBuffer();
mTexCoorBuffer.put(texCoor);
mTexCoorBuffer.position(0);
}
private void initShader(int mProgram) {
// TODO Auto-generated method stub
this.mProgram = mProgram;
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
}
public void drawSelf(int texId) {
MatrixState.rotate(roateX, 1, 0, 0);
MatrixState.rotate(roateY, 0, 1, 0);
GLES20.glUseProgram(mProgram);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);
GLES20.glEnableVertexAttribArray(maPositionHandle);
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
}
private float getCoor(int which, float vAngle, float hAngle) {
switch (which) {
case 0:// x
return (float) (R * UNIT_SIZE * Math.cos(Math.toRadians(vAngle)) * Math.cos(Math.toRadians(hAngle)));
case 1:// y
return (float) (R * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)));
case 2:// z
return (float) (R * UNIT_SIZE * Math.cos(Math.toRadians(vAngle)) * Math.sin(Math.toRadians(hAngle)));
}
return 0;
}
// 自动切分纹理产生纹理数组的方法
public float[] generateTexCoor(int bw, int bh) {
float[] result = new float[bw * bh * 6 * 2];
float sizew = 1.0f / bw;// 列数
float sizeh = 1.0f / bh;// 行数
int c = 0;
for (int i = 0; i < bh; i++) {
for (int j = 0; j < bw; j++) {
// 每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标
float s = j * sizew;
float t = i * sizeh;
result[c++] = s;
result[c++] = t;
result[c++] = s;
result[c++] = t + sizeh;
result[c++] = s + sizew;
result[c++] = t;
result[c++] = s + sizew;
result[c++] = t;
result[c++] = s;
result[c++] = t + sizeh;
result[c++] = s + sizew;
result[c++] = t + sizeh;
}
}
return result;
}
}
二、顶点着色器:
uniform mat4 uMVPMatrix;
attribute vec3 aPosition;
attribute vec2 aTexCoor;
varying vec2 vTextureCoord;
void main(){
gl_Position=uMVPMatrix*vec4(aPosition,1);
vTextureCoord=aTexCoor;
}
三、片源着色器:
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D sTexture;
void main(){
gl_FragColor=texture2D(sTexture,vTextureCoord);
}
还没有评论,来说两句吧...