-2

一天多来,我一直在尝试解决我的立方体纹理顶点不正确的问题,但运气不佳。纹理显示错误,而且每一面都不一样。

这是我目前的代码。我认为对于熟悉 OpenGL 的人来说这相对简单,但如果您有任何问题,请提出。

几天前我上次发布此消息时,我被否决了,没有得到答复;如果我没有给你足够的信息或做错了什么,请告诉我。

   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;   }
   }

输出看起来像这样

如果您需要更多信息,请告诉我,我一直在努力解决这个问题。我知道 GL_QUADS 已被弃用,但我真的很想让它运行。

4

1 回答 1

0

您还可以提供您使用/定位的 OpenGL 吗?在 GL 1.1 中,一切都是可选的(如 VBO),但在较旧的(即更新的)版本中,许多事情发生了变化,因此您可以将 glVertexPointer / glTexCoordPointer 与 glVertexAttribPointer 交换。让我们假设您使用旧的 OpenGL,然后删除 VBO 并使用直接指针检查它。

也尝试自己指定索引,即使它们是直截了当的,但最好创建一个列表并使用 glDrawElements 手动指定它。

Java 并不真正喜欢原生的东西,所以你需要为 OpenGL 使用正确的缓冲区格式,否则它可能会弄乱看起来像你得到的结果的数据。

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

除此之外,您还需要将其分解为更小的部分,例如绘制一个 Quad 并查看结果,因为这可能是您如何布置顶点/UV 数据的问题。

于 2013-10-14T19:10:10.983 回答