FloatBuffer 绘制带纹理的四边形,但 ShortBuffer 不
FloatBuffer draws textured quad, but ShortBuffer doesn't
我正在尝试绘制带纹理的四边形。由于它是一个四边形,我想使用 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 class 只是 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] 范围内。当然可以将其编码为整数数据类型,但至少对于顶点坐标,这会导致精度损失。
我正在尝试绘制带纹理的四边形。由于它是一个四边形,我想使用 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 class 只是 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] 范围内。当然可以将其编码为整数数据类型,但至少对于顶点坐标,这会导致精度损失。