我在 android 中使用 opengGl Es 2 来渲染 3D 对象。但我面临的问题是对象不渲染意味着屏幕上没有绘制任何内容。
public void draw(GL10 gl)
{
gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
for(int i=0; i<parts.size(); i++)
{
TDModelPart t=parts.get(i);
gl.glNormalPointer(GL10.GL_FLOAT, 0, t.getNormalBuffer());
gl.glDrawElements(GL10.GL_TRIANGLES,t.getFacesCount(),GL10.GL_FLOAT,t.getFaceBuffer());
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}
public class TDModelPart
{
Vector<Float> faces;
Vector<Float> vtPointer;
Vector<Float> vnPointer;
Material material;
private FloatBuffer normalBuffer;
FloatBuffer faceBuffer;
public TDModelPart(Vector<Float> faces, Vector<Float> vtPointer, Vector<Float> vnPointer, Material material, Vector<Float> vn)
{
super();
this.faces = faces;
this.vtPointer = vtPointer;
this.vnPointer = vnPointer;
this.material = material;
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vnPointer.size() * 4*3);
byteBuf.order(ByteOrder.nativeOrder());
normalBuffer = byteBuf.asFloatBuffer();
for(int i=0; i<vnPointer.size(); i++){
float x=vn.get((int) (vnPointer.get(i)*3));
float y=vn.get((int) (vnPointer.get(i)*3+1));
float z=vn.get((int) (vnPointer.get(i)*3+2));
normalBuffer.put(x);
normalBuffer.put(y);
normalBuffer.put(z);
}
normalBuffer.position(0);
ByteBuffer fBuf = ByteBuffer.allocateDirect(faces.size() * 4);
fBuf.order(ByteOrder.nativeOrder());
faceBuffer = fBuf.asFloatBuffer();
faceBuffer.put(toPrimitiveArrayS(faces));
faceBuffer.position(0);
}
public String toString()
{
String str=new String();
if(material!=null)
str+="Material name:"+material.getName();
else
str+="Material not defined!";
str+="\nNumber of faces:"+faces.size();
str+="\nNumber of vnPointers:"+vnPointer.size();
str+="\nNumber of vtPointers:"+vtPointer.size();
return str;
}
public FloatBuffer getFaceBuffer()
{
return faceBuffer;
}
public FloatBuffer getNormalBuffer()
{
return normalBuffer;
}
private static float[] toPrimitiveArrayS(Vector<Float> vector)
{
float[] s;
s=new float[vector.size()];
for (int i=0; i<vector.size(); i++){
s[i]=vector.get(i);
}
return s;
}
public int getFacesCount()
{
return faces.size();
}
public Material getMaterial()
{
return material;
}
}