我正在尝试绘制一个纹理的四边形。由于它是四边形,所以我想使用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方法的包装器,因此uploadSubData
是glUploadSubData
等...我想念什么?glDrawArrays
为什么不绘制ShortBuffer
?如果我遗漏了任何东西,请告诉我,我没有太多时间写这篇文章。
您正在混淆索引和顶点坐标。坐标是GL_ARRAY_BUFFER
中GL_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]。当然,可以将其编码为积分数据类型,但是至少对于顶点坐标,这将导致准确性损失。