Jeff Storey
Jeff Storey

Reputation: 57192

Java OpenGL rendering many triangles

I have an openGL (JOGL actually, but same concepts apply here) app where I have a scene that renders approximately 10k triangles on each rendering pass. The scale and angle of the triangles must be computed on each rendering since the triangles need to stay at a constant size and rotation relative to the camera location.

I've tried using a display list and then scaling/rotating before each call to glCallList. That works, but it brings the system to a crawl. I've looking into using vertex buffers for this, but I'm not sure if this is the appropriate way to do it since I need to rotate/scale on each rendering.

Can someone help point me in the right direction for rendering this many triangles in each scene?

EDIT - This provides some additional context to the problem.

The application is a map application showing a 3d view of the world. There are certain shapes that need to be drawn on the surface pointing in a direction relative to a compass, i.e. at 30 degrees. The user can rotate the 3d view but the shapes must stay flat on the surface of the earth and also pointing the specified direction.

thanks, Jeff

Upvotes: 2

Views: 996

Answers (2)

Kos
Kos

Reputation: 72241

10.000 triangles is really not a big amount even for your average notebook GPU of today.

Display lists are kind of obsolete, that's not the most useful approach.

The first thing to do is draw your trianges in a single draw call - that should easily be possible.

So:

1) Keep all your triangle data (vertex coordinates) in a single Buffer (that's how JOGL handles vertex arrays it, IIRC). Then either:

  • Update this buffer each frame on CPU and render all of it using glVertexPointer and glDrawArrays,
  • Create a VBO and (GL_DYNAMIC_DRAW type) and update it each frame with this buffer. A more modern approach, but not really a big difference from the above in terms of speed in this case.

And after that, you'd render all of that 10.000 triangles via a single glDrawArrays call.

This would still leave the calculation of triangles on the CPU, but would save you a lot of CPU time spent on driver calls - all of the triangles would be sent via your PCI-E at once and rendered in a blink.

If that's still too slow:

2) Think if you can/want to move some of the operations done on the triangles to the GPU and have the computations in the vertex shader. I cannot say too much here until I know what calculations you're doing, but I really hope that 1) would be enough.

If you had like 10.000 calls to CallList each frame, that's definitely the culprit of your low performance.

Upvotes: 1

Ned Bingham
Ned Bingham

Reputation: 2769

Vertex Buffers with GL_DYNAMIC_DRAW and Indice Buffers with GL_STATIC_DRAW would be the best you could do in terms of simplicity.

If you have access to shaders (GLSL or HSL), use Vertex Buffers with GL_STATIC_DRAW and Indice Buffers with GL_STATIC_DRAW, and calculate the scale and rotation in the vertex shader. That way you could dump some of the work onto the GPU. (If the work was CPU heavy to begin with)

Maybe do some frustum culling before hand? Occlusion culling? The best way to speed up an application is to reduce the amount of triangles you are rendering.

Maybe even use some threading. One thread to render, the other to calculate the scale and angle.

Upvotes: 5

Related Questions