总共三个类
[java]
package wyf.lgz;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;
public class DrawCylinder
{
private FloatBuffer myVertexBuffer;//顶点坐标缓冲
private FloatBuffer myNormalBuffer;//法向量缓冲
private FloatBuffer myTexture;//纹理缓冲
int textureId;
int vCount;//顶点数量
float length;//圆柱长度
float circle_radius;//圆截环半径
float degreespan; //圆截环每一份的度数大小
public float mAngleX;
public float mAngleY;
public float mAngleZ;
public DrawCylinder(float length,float circle_radius,float degreespan,int textureId)
{
this.circle_radius=circle_radius;
this.length=length;
this.degreespan=degreespan;
this.textureId=textureId;
float collength=(float)length;//圆柱每块所占的长度
int spannum=(int)(360.0f/degreespan);
ArrayList<Float> val=new ArrayList<Float>();//顶点存放列表
ArrayList<Float> ial=new ArrayList<Float>();//法向量存放列表
for(float circle_degree=180.0f;circle_degree>0.0f;circle_degree-=degreespan)//循环行
{
float x1 =(float)(-length/2);
float y1=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree)));
float z1=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree)));
float a1=0;
float b1=y1;
float c1=z1;
float l1=getVectorLength(a1, b1, c1);//模长
a1=a1/l1;//法向量规格化
b1=b1/l1;
c1=c1/l1;
float x2 =(float)(-length/2);
float y2=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree-degreespan)));
float z2=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree-degreespan)));
float a2=0;
float b2=y2;
float c2=z2;
float l2=getVectorLength(a2, b2, c2);//模长
a2=a2/l2;//法向量规格化
b2=b2/l2;
c2=c2/l2;
float x3 =(float)(length/2);
float y3=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree-degreespan)));
float z3=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree-degreespan)));
float a3=0;
float b3=y3;
float c3=z3;
float l3=getVectorLength(a3, b3, c3);//模长
a3=a3/l3;//法向量规格化
b3=b3/l3;
c3=c3/l3;
float x4 =(float)(length/2);
float y4=(float) (circle_radius*Math.sin(Math.toRadians(circle_degree)));
float z4=(float) (circle_radius*Math.cos(Math.toRadians(circle_degree)));
float a4=0;
float b4=y4;
float c4=z4;
float l4=getVectorLength(a4, b4, c4);//模长
a4=a4/l4;//法向量规格化
b4=b4/l4;
c4=c4/l4;
val.add(x1);val.add(y1);val.add(z1);//两个三角形,共6个顶点的坐标
val.add(x2);val.add(y2);val.add(z2);
val.add(x4);val.add(y4);val.add(z4);
val.add(x2);val.add(y2);val.add(z2);