2012-06-01 136 views
1

我使用的頂點索引來呈現我mesh.I具有通過頂點指數和頂點位置沒有問題。OpenGL ES2:頂點索引和紋理座標?

但我在紋理座標的情況下不知道,我應該讓它們使用他們太指數?

編輯:我想通過和渲染OBJ文件和困惑如何處理紋理「FX/Y/Z」座標描述

例如,在多維數據集的情況下,我通過我的頂點座標:

* 8頂點COORDS

* 36個頂點索引:

當傳遞紋理(或甚至法線)我應該將它們作爲:

* (8個紋理COORDS + 36紋理座標索引)。 如果是這樣,在渲染過程中如何分配它們?

* (36紋理座標)

這是我的抽象類網,

public class AbstractMesh3D { 
    //private int buffers[]={0,0}; 
    private int vbVertices[]=new int[1]; 
    private int vbIndices[]=new int[1]; 
    private int vbNormals[]=new int[1]; 
    private int vbColors[]=new int[1]; 
    private int vbTextureCords[]=new int[1]; 
    private boolean bVertices=false; 
    private boolean bIndices=false; 
    private boolean bNormals=false; 
    private int numOfVertices; 
    private int numOfIndices; 
    private int numOfNormals; 
    private World3D _world; 
    private String textureId; 

    public void setTextureId(String textureId) { 
     this.textureId = textureId; 
    } 




    public AbstractMesh3D(){ 


    } 
    public void setWorld(World3D world){ 
     this._world=world; 
    } 
    public void setVertexBuffer(float vertexData[]){ 
     GLES20.glGenBuffers(1, vbVertices, 0); 
     //Short 
     FloatBuffer vertexFloatBuffer=ByteBuffer.allocateDirect(vertexData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 
     vertexFloatBuffer.put(vertexData); 
     vertexFloatBuffer.position(0); 

     //GLES20.glGenBuffers(1, vbVertices, 0); 
     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbVertices[0]); 
     GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length*4, vertexFloatBuffer, GLES20.GL_STATIC_DRAW); 
     numOfVertices=vertexData.length/3; 
     bVertices=true; 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 
     GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 


     //vertexFloatBuffer. 
    } 

    public void setNormalBuffer(float normalData[]){ 
     GLES20.glGenBuffers(1, vbNormals, 0); 
     //Short 
     FloatBuffer vertexFloatBuffer=ByteBuffer.allocateDirect(normalData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 
     vertexFloatBuffer.put(normalData); 
     vertexFloatBuffer.position(0); 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbNormals[0]); 
     GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, normalData.length*4, vertexFloatBuffer, GLES20.GL_STATIC_DRAW); 
     bNormals=true; 
     numOfNormals=normalData.length/3; 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 
     GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 

    } 

    public void setIndices(short indices[]){ 
     GLES20.glGenBuffers(1, vbIndices, 0); 
     ShortBuffer vertexFloatBuffer=ByteBuffer.allocateDirect(indices.length*2).order(ByteOrder.nativeOrder()).asShortBuffer(); 
     vertexFloatBuffer.put(indices); 
     vertexFloatBuffer.position(0); 

     //GLES20.glGenBuffers(1, buffers[1], 0); 
     GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, vbIndices[0]); 
     GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indices.length*2, vertexFloatBuffer, GLES20.GL_STATIC_DRAW); 
     numOfIndices=indices.length; 
     bIndices=true; 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 
     GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 

    } 

    public void render(){ 
     int posHandle=_world.getOgl_aPositionHandle(); 
     int normalHandle=_world.getOgl_aNormalHandle(); 

     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbVertices[0]); 
     GLES20.glEnableVertexAttribArray(posHandle); 
     GLES20.glVertexAttribPointer(posHandle, 3, GLES20.GL_FLOAT, false, 12, 0); 

     if(bNormals){ 
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbNormals[0]); 
      GLES20.glEnableVertexAttribArray(normalHandle); 
      GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT, false, 12, 0); 
     } 

     if(bIndices){ 
      //System.out.println("render indices"); 
      GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, vbIndices[0]); 

      GLES20.glDrawElements(GLES20.GL_TRIANGLES, numOfIndices, GLES20.GL_UNSIGNED_SHORT, 0);   

     }else{ 
      GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, numOfVertices); 
     } 
     GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); 
     GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0); 

     GLES20.glDisableVertexAttribArray(posHandle); 
     GLES20.glDisableVertexAttribArray(normalHandle); 
     //System.out.println("rendered"); 





    } 

} 

回答

3

您必須重新排列texcoords和頂點使得它們共享相同的索引。您不能爲不同的屬性使用單獨的索引。

始終頂點的數量必須等於texcoords的數量必須等於法線等

當解析OBJ這將涉及到重複一些頂點/ texcoords這樣就可以達到這個要求。