OpenGL: Managing and editing large amounts of line strips

lwjgl, opengl, vbo

Solution

Chapter 12 of the OpenGL SuperBible has a section on "Drawing a lot of Geometry Efficiently", in which they have a demo of millions of blades of grass being animated. This is done by using a single vertex description, the glDrawElementsInstanced method, and a shader to modify each 'instance' stamped out in whatever manner you like (e.g. perturb vertices, scale & rotate, change texture lookup, etc.)

This is very similar to your 'go through all the vbos' loop, except that you would only upload vertices for a single blade of grass, and OpenGL will take care of passing a unique gl_InstanceID to your shader. You can then encode the changes each frame either procedurally, or in a 'texture' that you upload as often as needed. The book has sample code (and it may be available from the web site as well).

Edit: Confirmed that the sample code is in the downloads from the site - look at the src\grass\grass.cpp to see a sample using textures to control grass length, orientation, color, and bend.

Problem

I am currently working on a small 2D game with LWJGL. I use line strips to draw randomly generated grass. Each of the blades consists of 6 points and has a random green color. The problem I face: To fill the ground with a gapless layer of grass, I need approx. 400 line strips... In addition, every line strip has to be shifted, when the player moves around and should (optionally) wave in the wind. Therefore I need to change the data of 400 vbos every frame. Is there any way to accelerate these operations? My Code: ``` //upload the vertex data void uploadGrass(int offset){ FloatBuffer grassBuffer=BufferUtils.createFloatBuffer(5*5); for(int i=0;i<Storage.grasslist.size();i++){ if(grassvbo[i]==0){ grassvbo[i]=GL15.glGenBuffers(); } grassBuffer.clear(); for(int j=1;j<6;j++){ grassBuffer.put(Utils.GL_x((int) Storage.grasslist.get(i)[j][0]-offset)); grassBuffer.put(Utils.GL_y((int) Storage.grasslist.get(i)[j][1])); //index 0 of every blade contains RGB values for the color. grassBuffer.put((float) Storage.grasslist.get(i)[0][0]); grassBuffer.put((float) Storage.grasslist.get(i)[0][1]); grassBuffer.put((float) Storage.grasslist.get(i)[0][2]); } grassBuffer.flip(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,grassvbo[i]); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, grassBuffer, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } } //draw line strips void drawGrass(){ GL20.glUseProgram(pId2); //color shader for(int i=0;i<grassvbo.length;i++){ //go through all the vbos GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,grassvbo[i]); GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 5*4, 0); GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 5*4, 2*4); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL11.glDrawArrays(GL11.GL_LINE_STRIP, 0, 5); } GL20.glUseProgram(0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0); GL20.glDisableVertexAttribArray(0); } ``` until now it looks like that ;) (still needs antialiasing and alpha blending): https://i.stack.imgur.com/V9Glw.png

Original source