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