-2

我在 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;
    }
}
4

1 回答 1

0

首先,您为 设置数据glNormalPointer,但从未设置过glEnableClientState(GL_NORMAL_ARRAY)。然后在 for 循环块的末尾禁用顶点数组 ( glDisableCLientState(GL_VERTEX_ARRAY)),这意味着在绘制第一部分之后,所有后续部分都不会绘制任何内容,因为它们没有启用顶点数组。

同样,不适合您的法线数据是按零件指定的,而您的顶点数据只为所有零件指定一次。也许这是你想要的,但它看起来很奇怪,因为顶点(或者更确切地说是位置)和法线(以及所有其他属性)必须匹配,即从这些数组绘制的绘制命令使用相同的索引来访问所有启用的属性数组对于单个顶点。

于 2013-07-15T11:25:30.077 回答