【问题标题】:glDrawElements is not drawing the entire modelglDrawElements 没有绘制整个模型
【发布时间】:2012-11-27 04:53:02
【问题描述】:

我不明白发生了什么!我有这个网格加载器,当我用它加载小网格时,我的程序可以正常工作,绘制整个网格。但是现在我用一个大网格(超过 100,000 个顶点)测试了这个程序,它只画了它的一小部分!是不是我的显卡有什么问题,比如一些疯狂的小限制或其他什么?

使用 LWJGL,我从他们的教程中挑选了一些代码:

private ByteBuffer indexBuffer;

...

// Create a new VBO for the indices and select it (bind)
indxBufId = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indxBufId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);

...

// Bind to the index VBO that has all the information about the order of the vertices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indxBufId);

// Draw the vertices
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_BYTE, 0);

// Put everything back to default (deselect)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

【问题讨论】:

    标签: java opengl lwjgl vbo


    【解决方案1】:

    不,过去的自己,不是你的显卡什么的问题,而是你的索引的问题。您正在使用 BYTES 作为索引缓冲区,这非常好,而且在处理像 12 三角形立方体这样的小型模型时,由于它的体积小,可能更可取。

    但是如果你的模型在你的顶点数组中运行超过 256 个索引,你就会得到环绕。很多。尤其是当您要求索引 125,647 时。

    如果您的模型大于 256 verts,则需要使用 short 或 int。请记住更改以下内容:

    private ByteBuffer indexBuffer; //change to
    private ShortBuffer indexBuffer; //or
    private IntBuffer indexBuffer;
    

    还有这些:

    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_BYTE, 0); //fine for up to 256
    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, 0); //up to 65535
    glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); //up to 4294967295
    

    请记住,尽管 Java 的 ints、shorts 和 bytes 都是有符号的,但 OpenGL 使用的是无符号的,并且会相应地转换缓冲区中的内容。

    (虽然这些示例是 LWJGL,但这也适用于 C 语言中的普通 OpenGL。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多