Reputation: 2921
So I have an array of Vertex objects, which look like this:
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]}
Where it's organized like this:
{[X, Y], [R, G, B, A], [U, V]}
And I have a shader that accepts these as attributes;
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);
}
And here's how I'm attaching the attributes:
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 is 8, and Vertex.stride() is 32.
My draw function:
@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);
...
}
I don't think the error is in here, though, as I didn't change the draw function from when it worked (when I was using a uniform variable for the sprite color)
However, nothing is drawing here. What am I messing up?
Even when I don't even use SpriteColor in my color output, it still outputs nothing.
Upvotes: 0
Views: 549
Reputation: 2455
By calling glDisableVertexAttribArray
while the VAO was still bound you can't access these attributes while rendering, because the vertex attributes are part of the VAO state.
I won't go into that much detail, because there a alot of other ressources explaining VAOs. For example:
What are Vertex Array Objects?
glVertexAttribPointer clarification
So here is a small explanation with my words:
Think of VAOs like normal objects from classes. Each VAO has a state (the value of their member variables). When binding a VAO and then calling functions like glEnableVertexAttribArray
or glVertexAttribPointer
the state of the currently bound VAO is changed (like a setter function for this object).
Later while rendering the state of the currenntly bound VAO is read and reacted appropriatly.
In your case:
You was disabling the vertex attributes while the VAO was bound. That means you changed the state of the VAO and when rendering OpenGL thinks you don't want to have any attributes.
Upvotes: 2