【发布时间】:2015-07-30 20:03:20
【问题描述】:
所以我有一个顶点对象数组,如下所示:
Vertex: {[0.0, 0.0], [1.0, 1.0, 1.0, 1.0], [0.0, 0.0]}
Vertex: {[0.0, 512.0], [1.0, 1.0, 1.0, 1.0], [0.0, 1.0]}
Vertex: {[512.0, 0.0], [1.0, 1.0, 1.0, 1.0], [1.0, 0.0]}
Vertex: {[512.0, 512.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0]}
它的组织方式如下:
{[X, Y], [R, G, B, A], [U, V]}
我有一个着色器接受这些作为属性;
Sprite.vs:
#version 330 core
layout (location = 0) in vec2 vertex;
layout (location = 1) in vec4 color;
layout (location = 2) in vec2 texcoords;
out vec4 SpriteColor;
out vec2 TexCoords;
uniform mat4 gProjection;
void main()
{
SpriteColor = color;
TexCoords = texcoords;
gl_Position = gProjection * vec4(vertex, 0.0, 1.0);
}
Sprite.fs:
#version 330 core
in vec4 SpriteColor;
in vec2 TexCoords;
out vec4 color;
uniform sampler2D gSpriteTexture;
void main()
{
color = SpriteColor * texture(gSpriteTexture, TexCoords);
}
这是我附加属性的方式:
FloatBuffer vertexBuf = BufferUtils.createFloatBuffer(vertices.length * Vertex.numFields());
for (Vertex v : vertices) {
vertexBuf.put(v.toFloatArray());
}
vertexBuf.flip();
vao = glGenVertexArrays();
int vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexBuf, GL_STATIC_DRAW);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 2, GL_FLOAT, false, Vertex.stride(), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, false, Vertex.stride(), 8);
glVertexAttribPointer(2, 2, GL_FLOAT, false, Vertex.stride(), 24);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
Vertex.numFields 为 8,Vertex.stride() 为 32。
我的绘图功能:
@Override
public void draw(RenderTarget rt, RenderStates states) {
...
texture.bind(COLOR_TEXTURE_UNIT);
shader.enable();
shader.setTextureUnit(COLOR_TEXTURE_UNIT_INDEX);
shader.setProjection(getOrthoMatrix(rt.getView()));
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
...
}
我不认为错误在这里,因为我没有更改绘图函数从它工作时开始(当我使用统一变量作为精灵颜色时)
但是,这里没有任何内容。我在搞砸什么?
即使我在颜色输出中什至不使用 SpriteColor,它仍然不输出任何内容。
【问题讨论】:
-
请勿致电
glDisableVertexAttribArray。这是 VAO 状态的一部分。 -
解决了!哇,我真的不知道这是问题所在。你能进一步解释为什么会这样吗?我认为在使用完属性数组后禁用它们是个好主意。 @dari