2013-02-17 59 views
0

到目前爲止,我使用了不推薦使用的立即模式渲染,並且我試圖切換到VAO然後使用VBO。 2D四邊形通常使用VAO渲染,但是當我嘗試附加紋理時,它仍然是黑色的。如果有人能看看我的代碼,我會很感激,並指出我錯在哪裏。紋理在使用VAO的JOGL中爲黑色

public class CSpriteVAO { 
/*VAO*/ 
private FloatBuffer vertices; 
private ShortBuffer indices; 
private FloatBuffer textures; 
private int VAOVertices; 
private int VAOIndices; 
private int VAOTextures; 

/*SPRITE*/ 
private String mTexture; 
private CPoint mPosition; 
private CPoint mDimension; 
private CPreferences mPreferences; 

public CSpriteVAO(GL2 gl, CPreferences preferences, String spriteID, CRectangle dimensions){ 
    mPreferences = preferences; 
    mTexture = spriteID; 
    mDimension = new CPoint(dimensions.width, dimensions.height); 
    mPosition = new CPoint(dimensions.x, dimensions.y); 

    CCreateVAO(gl); 
} 

public void onDraw(GL2 gl){ 
    gl.glLoadIdentity(); 
    CBindTexture(gl); 
    CDraw(gl); 
} 

private void CDraw(GL2 gl){ 
    //gl.glCullFace(GL2.GL_CW); 
    gl.glTranslatef(mPosition.x, mPosition.y, 0); 
    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY); 
     gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VAOVertices); 
     gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0); 

     gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VAOTextures); 
     gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, VAOTextures); 

     gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, VAOIndices); 
     gl.glDrawElements(GL2.GL_TRIANGLES, indices.capacity(), GL2.GL_UNSIGNED_SHORT, 0); 

    gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY); 
    gl.glDisableClientState(GL2.GL_VERTEX_ARRAY); 
} 

private void CCreateVAO(GL2 gl){ 
    //float[] textureArray = {0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f}; 
    float[] textureArray = {0f, 0f, 1f, 0f, 1f, 1f, 0f, 1f}; 
    textures = Buffers.newDirectFloatBuffer(textureArray.length); 
    textures.put(textureArray); 
    textures.flip(); 

    float[] vertexArray = {0,   mDimension.y, 0, 
          mDimension.x, mDimension.y, 0, 
          mDimension.x, 0,   0, 
          0,   0,   0}; 
    vertices = Buffers.newDirectFloatBuffer(vertexArray.length); 
    vertices.put(vertexArray); 
    vertices.flip(); 

    short[] indexArray = {0, 1, 2, 0, 2, 3}; 
    indices = Buffers.newDirectShortBuffer(indexArray.length); 
    indices.put(indexArray); 
    indices.flip(); 

    int[] temp = new int[3]; 
    gl.glGenBuffers(3, temp, 0); 

    VAOTextures = temp[0]; 
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, VAOTextures); 
    gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, textures.capacity() * Buffers.SIZEOF_FLOAT, textures, GL2.GL_STATIC_DRAW); 
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0); 

    VAOVertices = temp[1]; 
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, VAOVertices); 
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.capacity() * Buffers.SIZEOF_FLOAT, vertices, GL2.GL_STATIC_DRAW); 
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0); 

    VAOIndices = temp[2]; 
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, VAOIndices); 
    gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * Buffers.SIZEOF_SHORT, indices, GL2.GL_STATIC_DRAW); 
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0); 
} 

protected void CBindTexture(GL2 gl){ 
    if (mTexture != CUtils.CurrentTexture){ 
     if (mTexture != null){ 
      CAssets.CWGGetTexture(mTexture).enable(gl); 
      CAssets.CWGGetTexture(mTexture).bind(gl); 
     } 
     CUtils.CurrentTexture = mTexture; 
    } 
} 
} 

備案:我的vcard報告有(顯然)VAO支持OpenGl 4.3.0。立即渲染紋理工作正常。

我真的很感激任何形式的幫助。提前謝謝了。

+0

你的紋理有黑色區域嗎?嘗試與白色的紋理。如果它變成白色,那麼你的texcoords是錯誤的或別的東西。你也選擇了顏色嗎?像glColor()我的意思是...我不知道喬吉爾。 – Rookie 2013-02-17 18:46:29

+0

感謝您的答覆,結果證明我的texcoords不好。有紋理,現在是白色。如果您發佈您的答覆作爲答案,我可以批准它。 – LugaidVandroiy 2013-02-17 18:59:34

回答

1

檢查您的紋理座標。

如果這樣做不起作用,請檢查您是否已將頂點顏色設置爲白色(以便它會在紋理中顯示顏色)。