Reputation: 67
After some effort, I managed to draw dynamic aircraft trail using OpenGL. You may see a part of my code below:
private void getTrailVertices(){
verticeBuffer = Buffers.newDirectFloatBuffer(6 * positionList.size());
for (int i = 0; i < positionList.size(); i++) {
List<Vec4> pointEdges = computeVec4(positionList.get(i));
verticeBuffer.put((float) pointEdges.get(0).x).put((float) pointEdges.get(0).y).put((float) pointEdges.get(0).z);
verticeBuffer.put((float) pointEdges.get(1).x).put((float) pointEdges.get(1).y).put((float) pointEdges.get(1).z);
}
}
private void drawTrail(){
verticeBuffer = getTrailVertices();
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
float[] colors = new float[]{trailColor.getRed() / 255.f, trailColor.getGreen() / 255.f, trailColor.getBlue() / 255.f};
gl.glColor4f(colors[0], colors[1], colors[2], 0.6f);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, verticeBuffer.rewind());
gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, verticeBuffer.limit() / 3);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
}
Using the above code, I can draw a dynamic 3D ribbon-like trail. But I need to improve the performance of code.
My first question: How can I dynamically change the values of FloatBuffer without creating a new one. I am asking this, because, in every frame, a new Vec4 (x,y,z) is added to position list and I want to use my existing FloatBuffer. Because as the positions added, the length of the trail gets longer, so the loop in the getTrailVertices method becomes larger and larger. For example, when 10 minutes pass while program running, positionList becomes a 15,000-position ArrayList. And most probably this decreases the performance. Instead of creating a new FloatBuffer, can I remove the last 2 vertices from the end and add another 2 vertices to the beginning of the FloatBuffer without creating a new one?
Second Question: Is it possible to use VBO for this kind of dynamic drawing? If so how can I use VBO?
Third Question: How can I change the capacity of FLoatBuffer dynamically. I am asking this because I want to increase or decrease the length of the trail on the fly. This means, if I increase the length of the trail I will need more vertices and higher capacity FloatBuffer and if I decrease the length of the trail, this means I will need fewer vertices and lower capacity FloatBuffer.
Last Question: How can I put texture on the trail. Or let me ask this way; how can I draw a trail with colour fading through the end of the trail. I think I need to use alpha fading?
Upvotes: 2
Views: 270
Reputation: 211220
The size of a Vertex Buffer Object cannot be changed. glBufferData
creates a new buffer object (it just reuses the name id).
The content of an existing buffer can be changed by glBufferSubData
or by Buffer Mapping.
Create a Vertex Buffer Object with the maximum size and update the content of the buffer.
Upvotes: 1