2013-09-26 61 views
-2

我一直試圖解決我的立方體紋理頂點不正確的問題,現在一天沒有多少運氣。紋理顯示錯誤,每個邊都不一樣。OpenGL紋理拉伸和錯誤。 VBO頂點不正確?

這是我目前的代碼。我認爲對熟悉OpenGL的人來說相對簡單,但是如果您有任何疑問,請提問。

上次我發佈這個前幾天我是downvoted,沒有得到答案;如果我沒有給你足夠的信息或做錯什麼,請告訴我。

private boolean hasBeenRendered = false; 
    private int amountOfVertices; 
    private int vertexSize; 
    private int textureSize; 
    private int vboVertexHandle; 
    private int vboTextureHandle; 
    private boolean canDraw = false; 

    public Block(BlockType type, Location loc) { 
     this.type = type; 
     this.loc = loc; 

     initRendering(); 
    } 

    private void initRendering() { 

     amountOfVertices = 24; 
     vertexSize = 3; 
     textureSize = 2; 

     FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize); 
     float[] vertices = { 
      // X  Y  Z   R  G  B 
      // face 0: 
      1.0f, 1.0f, 1.0f,  // vertex 0 
      - 1.0f, 1.0f, 1.0f,  // vertex 1 
      - 1.0f, - 1.0f, 1.0f,  // vertex 3 
      1.0f, - 1.0f, 1.0f,  // vertex 2 

      // face 1: 
      1.0f, 1.0f, 1.0f,  // vertex 0 
      1.0f, - 1.0f, 1.0f,  // vertex 1 
      1.0f, - 1.0f, - 1.0f,  // vertex 3 
      1.0f, 1.0f, - 1.0f,  // vertex 2 

      // face 2: 
      1.0f, 1.0f, 1.0f,  // vertex 0 
      1.0f, 1.0f, - 1.0f,  // vertex 1 
      - 1.0f, 1.0f, - 1.0f,  // vertex 3 
      - 1.0f, 1.0f, 1.0f,  // vertex 2 

      // face 3: 
      1.0f, 1.0f, - 1.0f,  // vertex 0 
      1.0f, - 1.0f, - 1.0f,  // vertex 1 
      - 1.0f, - 1.0f, - 1.0f,  // vertex 3 
      - 1.0f, 1.0f, - 1.0f,  // vertex 2 

      // face 4: 
      - 1.0f, 1.0f, 1.0f,  // vertex 0 
      - 1.0f, 1.0f, - 1.0f,  // vertex 1 
      - 1.0f, - 1.0f, - 1.0f,  // vertex 3 
      - 1.0f, - 1.0f, 1.0f, // vertex 2 

      // face 5: 
      1.0f, - 1.0f, 1.0f,  // vertex 0 
      - 1.0f, - 1.0f, 1.0f,  // vertex 1 
      - 1.0f, - 1.0f, - 1.0f,  // vertex 3 
      1.0f, - 1.0f, - 1.0f,  // vertex 2 
      // 6 faces with 4 vertices with 6 components (floats) 

     }; 
     vertexData.put(vertices); 

     vertexData.flip(); 

     FloatBuffer textureData = BufferUtils.createFloatBuffer(amountOfVertices * textureSize); 
     textureData.put(new float[] { 
      1, 1, 0, 1, 0, 0, 1, 0, 

      1, 1, 0, 1, 0, 0, 1, 0, 

      1, 1, 0, 1, 0, 0, 1, 0, 

      1, 1, 0, 1, 0, 0, 1, 0, 

      1, 1, 0, 1, 0, 0, 1, 0, 

      1, 1, 0, 1, 0, 0, 1, 0, 
     }); 
     textureData.flip(); 

     vboVertexHandle = glGenBuffers(); 
     glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); 
     glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW); 
     glBindBuffer(GL_ARRAY_BUFFER, 0); 

     vboTextureHandle = glGenBuffers(); 
     glBindBuffer(GL_ARRAY_BUFFER, vboTextureHandle); 
     glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW); 
     glBindBuffer(GL_ARRAY_BUFFER, 0); 
    } 

    @Override 
    public void render() { 
//  if(! hasBeenRendered) { 

     canDraw = true; 
     glPushMatrix(); 
     { 
      glTranslatef(loc.getX(), loc.getY(), loc.getZ()); 
      //glRotatef(x, 1, 1, 0); 
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

      //glBindTexture(GL_TEXTURE, type.getTexture().getTextureID()); 
      glBindTexture(GL_TEXTURE_2D, type.getTexture().getTextureID()); 

      glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); 
      glVertexPointer(vertexSize, GL_FLOAT, 0, 0L); 

      glBindTexture(GL_ARRAY_BUFFER, vboTextureHandle); 
      glTexCoordPointer(textureSize, GL_FLOAT, 0, 0L); 

      glEnableClientState(GL_VERTEX_ARRAY); 
      glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
      glDrawArrays(GL_QUADS, 0, amountOfVertices); 
      glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
      glDisableClientState(GL_VERTEX_ARRAY); 

     } 

     glPopMatrix(); 
     //hasBeenRendered = true; } 
    } 

The output looks something like this

如果您需要更多信息,請讓我知道,我一直在爲此付出很多努力。我知道GL_QUADS已被棄用,但我真的想讓這個運行。

+0

[OpenGL立方體頂點錯誤]的可能重複(http://stackoverflow.com/questions/18945859/opengl-cube-vertices-are-wrong) – genpfault

+0

它不是。同一代碼不同的問題。即便如此,也不像任何人會回頭找到問題並添加答案。 – user2804551

+0

你可以發佈你的渲染結果嗎? – hiroki

回答

0

你還可以提供你使用哪個OpenGL/target?在GL 1.1中,所有東西都是可選的(如VBO),但在較舊版本(即更新版本)中,許多事情都發生了變化,因此您可以使用glVertexAttribPointer交換glVertexPointer/glTexCoordPointer。 讓我們假設你使用舊的OpenGL,然後刪除VBO並用直接指針檢查它。

也嘗試自己指定索引,即使它們是直接前進的,但最好創建一個列表並使用glDrawElements手動指定它。

Java並不是真的很喜歡本地的東西,所以你需要爲OpenGL使用適當的緩衝格式,否則它可能會弄亂看起來像你得到的結果的數據。

vertexData.put(vertices); 
vertexData.order(ByteOrder.nativeOrder()); 
vertexData.flip(); 

除了這些,你需要把它分解成更小的部分,如只是畫一個Quad和看到的結果,因爲它可能與你如何佈置你的頂點/ UV數據畢竟是一個問題。