【问题标题】:FloatBuffer draws textured quad, but ShortBuffer doesn'tFloatBuffer 绘制纹理四边形,但 ShortBuffer 不
【发布时间】:2019-02-21 19:02:54
【问题描述】:

我正在尝试绘制一个带纹理的四边形。由于它是四边形,我想使用glDrawElements 和 VEO,但这需要ShortBuffer 而不是FloatBuffer。我尝试更改我的代码,但现在没有任何效果。 旧代码: 上传和绘图:

 public void flush() {
    if (numVertices > 0) {
        vertices.flip();

        if (vao != null) {
            vao.bind();
        } else {
            vbo.bind(GL_ARRAY_BUFFER);
            specifyVertexAttributes();
        }
        program.use();

        /* Upload the new vertex data */
        vbo.bind(GL_ARRAY_BUFFER);
        vbo.uploadSubData(GL_ARRAY_BUFFER, 0, vertices);

        /* Draw batch */
        glDrawArrays(GL_TRIANGLES, 0, numVertices);

        /* Clear vertex data for next batch */
        vertices.clear();
        numVertices = 0;
    }
}

将纹理添加到缓冲区:

if (vertices.remaining() < 7 * 6) {
        /* We need more space in the buffer, so flush it */
        flush();
    }

    float r = c.getRed();
    float g = c.getGreen();
    float b = c.getBlue();
    float a = c.getAlpha();

    vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
    vertices.put(x1).put(y2).put(r).put(g).put(b).put(a).put(s1).put(t2);
    vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);

    vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
    vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);
    vertices.put(x2).put(y1).put(r).put(g).put(b).put(a).put(s2).put(t1);

    numVertices += 6;

更新代码: 上传和绘图:

public void flush() {
    if (numVertices > 0) {
        vertices.flip();

        if (vao != null) {
            vao.bind();
        } else {
            vbo.bind(GL_ARRAY_BUFFER);
            specifyVertexAttributes();
        }
        program.use();

        /* Upload the new vertex data */
        vbo.bind(GL_ARRAY_BUFFER);
        vbo.uploadSubData(GL_ARRAY_BUFFER, 0, vertices);

        /* Draw batch */
        glDrawArrays(GL_TRIANGLES, 0, numVertices);

        /* Clear vertex data for next batch */
        vertices.clear();
        numVertices = 0;
    }
}

将纹理添加到缓冲区:

if (vertices.remaining() < 7 * 6) {
        /* We need more space in the buffer, so flush it */
        flush();
    }

    short r = (short) c.getRed();
    short g = (short) c.getGreen();
    short b = (short) c.getBlue();
    short a = (short) c.getAlpha();

    short sx1 = (short) Math.round(x1), sx2 = (short) Math.round(x2), sy1 = (short) Math.round(y1), sy2 = (short) Math.round(y2), ss1 = (short) Math.round(s1), ss2 = (short) Math.round(s2), st1 = (short) Math.round(t1), st2 = (short) Math.round(t2);
    vertices.put(sx1).put(sy1).put(r).put(g).put(b).put(a).put(ss1).put(st1);
    vertices.put(sx1).put(sy2).put(r).put(g).put(b).put(a).put(ss1).put(st2);
    vertices.put(sx2).put(sy2).put(r).put(g).put(b).put(a).put(ss2).put(st2);

    vertices.put(sx1).put(sy1).put(r).put(g).put(b).put(a).put(ss1).put(st1);
    vertices.put(sx2).put(sy2).put(r).put(g).put(b).put(a).put(ss2).put(st2);
    vertices.put(sx2).put(sy1).put(r).put(g).put(b).put(a).put(ss2).put(st1);

    numVertices += 6;

除了在我的uploadSubData 方法中将FloatBuffer 替换为ShortBuffer 之外,代码没有其他更改。 VBO 类只是 OpenGL 方法的包装,所以 uploadSubDataglUploadSubData,等等... 我错过了什么?为什么glDrawArrays 不画ShortBuffer? 如果我遗漏了什么,请告诉我,我没有太多时间来写这篇文章。

【问题讨论】:

  • 我之前在尝试使用 IntBuffers 而不是 FloatBuffers 时遇到过类似的问题。我所做的一切似乎都不起作用,所以最后我只是继续使用 FloatBuffers。性能影响应该不会太大,所以我认为你也可以使用 FloatBuffers。
  • 由于它是四边形,我想使用 glDrawElements 和 VEO,但这需要 ShortBuffer 而不是 FloatBuffer。 不,使用 glDrawElements 不需要您的 顶点数据 是一个 ShortBuffer。它要求 index_/_element 数据是整数类型(例如短),但您的顶点数据当然仍然可以保持浮点类型。除此之外,您似乎仍在使用 glDrawArrays 而不是 glDrawElements。
  • " glDrawElements 和一个 VEO,但这需要一个 ShortBuffer 而不是 FloatBuffer"。这不是真的。 GL_ARRAY_BUFFER 保持浮动缓冲区(或您之前使用的任何缓冲区。但您需要一个额外的 GL_ELEMENT_ARRAY_BUFFER 来存储索引信息(int、short 等)。
  • 这不是真的,每当我将 GL_FLOAT 传递给 glDrawElements 时,它都会引发错误。它只允许短裤、字节或整数。我使用 glDrawArrays 而不是 glDrawElements 因为它接受短裤,这是一个很好的第一步,如果我能得到 glDrawArrays,那么我会将其更改为 glDrawElements。

标签: java opengl lwjgl


【解决方案1】:

您混淆了索引和顶点坐标。坐标是GL_ARRAY_BUFFERGL_FLOAT 类型的元组。但索引是GL_ELEMENT_ARRAY_BUFFER 中的整数索引列表(例如,键入GL_SHORT),它指的是顶点坐标。

一个四边形可以由两个三角形绘制。可以定义6个顶点坐标和属性,使用glDrawArrays

在下面的verticesFloatBuffer 类型:

vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
vertices.put(x1).put(y2).put(r).put(g).put(b).put(a).put(s1).put(t2);
vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);

vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);
vertices.put(x2).put(y1).put(r).put(g).put(b).put(a).put(s2).put(t1);

numVertices += 6;
vbo.bind(GL_ARRAY_BUFFER);
vbo.uploadSubData(GL_ARRAY_BUFFER, 0, vertices);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

或者你可以分别定义4个顶点坐标属性和6个索引并使用glDrawElements

在下面的vertices 仍然是FloatBuffer 类型,但indicesShortBuffer 类型:

vertices.put(x1).put(y1).put(r).put(g).put(b).put(a).put(s1).put(t1);
vertices.put(x1).put(y2).put(r).put(g).put(b).put(a).put(s1).put(t2);
vertices.put(x2).put(y2).put(r).put(g).put(b).put(a).put(s2).put(t2);
vertices.put(x2).put(y1).put(r).put(g).put(b).put(a).put(s2).put(t1);

numVertices += 4;
indices.put(0).put(1).put(2);
indices.put(0).put(2).put(3);

numIndices += 4;
vbo.bind(GL_ARRAY_BUFFER);
vbo.uploadSubData(GL_ARRAY_BUFFER, 0, vertices);
ibo.bind(GL_ELEMENT_ARRAY_BUFFER);
ibo.uploadSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices);

glDrawElements(GL_TRIANGLES, GL_SHORT, numIndices, null);

所以关键是您需要 2 个uploadSubData 方法。前者需要处理FloatBuffer,后者需要处理ShortBuffer
请注意,通常顶点属性是浮点值。颜色通常是 [0, 1] 范围内的浮点值。纹理坐标在 [0, 1] 范围内。当然可以将其编码为整数数据类型,但至少对于顶点坐标,这会导致准确性损失。

【讨论】:

  • 谢谢!我对这个方法的用法很困惑,你的澄清很有帮助。
猜你喜欢
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多