FloatBuffer 绘制带纹理的四边形,但 ShortBuffer 不绘制



我正在尝试绘制一个纹理的四边形。由于它是四边形,所以我想使用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替换CC_4。VBO类只是OpenGL方法的包装器,因此uploadSubDataglUploadSubData等...我想念什么?glDrawArrays为什么不绘制ShortBuffer?如果我遗漏了任何东西,请告诉我,我没有太多时间写这篇文章。

您正在混淆索引和顶点坐标。坐标是GL_ARRAY_BUFFERGL_FLOAT型的元素。但是这些索引是GL_ELEMENT_ARRAY_BUFFER中的积分索引列表(例如类型GL_SHORT),该索引参考了顶点坐标。

可以通过2个三角形绘制四核。您可以定义6个顶点坐标和属性,并使用glDrawArrays

在以下vertices中是类型FloatBuffer

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类型,但是indices是类型ShortBuffer

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]。当然,可以将其编码为积分数据类型,但是至少对于顶点坐标,这将导致准确性损失。

最新更新