Intro, so I’ve spent a couple days trying to figure out the lwjgl 3 way of doing things with vbo’s. This being the first time I’m hearing a lot of the terminology isn’t making the resources/tutorials easy to understand. Nor do I want to begin at the very beginning, since I have a solid understanding of 3d rendering (though not of opengl or gpu’s). Also, though I would like to take the time to learn opengl thoroughly eventually, at the moment, I just want to get my little experiment working.
Previously, I had been using Java graphics2D to draw polygons; e.g., brush.drawPolygon(xy[0], xy[1], xy[0].length);
However, I was not happy with the 15fps it was dipping too when polygon count would reach 15-20k. So I decided to try out lwjgl. Here’s some code snippet I copied from a previous project before lwjgl 3 had come about, and was very happy with performance,
glColor3fv(color);
GL11.glBegin(GL11.GL_POLYGON);
for (byte i = 0; i < vertices.length; i += 2)
GL11.glVertex2d(vertices[i], vertices[i + 1]);
GL11.glEnd();
But for learning’s sake, I wanted to get a little familiar with the newer way of drawing in opengl. So I tried figuring out from tutorials and the online reference and eventually arrived at this working snippet (not included in the snippet is a glclear and glfwSwapBuffers that each happens once per frame):
glColor3fv(color);
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
verticesBuffer.put(vertices).flip();
int vboID = glGenBuffers();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
glDrawArrays(GL_QUADS, 0, vertices.length);
Unfortunately, this last snippet, was dropping painfully slow even at 1000 polygon count, which was disappointing, but also indicated that must not be the correct way of doing things.
I’m not sure if I can group up all my draw’s into 1 giant array and only call glDrawArrays once, since I think only the last glColor would then be applied to all the quads. Nor do I want to include the color per vertex, since doing so seems to require writing a shader or something, and that seems like overkill for my requirement.
Additionally, if it matters, vertexes are in 2D coordinates. I do the 3d -> 2d transformation and lighting -> color calculations with java+math rather than opengl. Hence my reluctance to write shaders or anything too complicated.
Thanks,