Reputation: 6513
I want to draw many spheres (~100k) using OpenGL. So far, I'm doing something like
for (int i=0; i<pnum; i++){
glPushMatrix();
glTranslatef(bpos[i].x, bpos[i].y, bpos[i].z);
glCallList(DListSPHERE);
glPopMatrix();
}
Before using proper spheres, I used GL_POINTS
. That allowed me to call glDrawArrays
with an array containing all points which was very efficient. Is there a better way than the above code to draw many, identical objects?
Upvotes: 7
Views: 5508
Reputation: 70204
Have a look at this page on instancing: it contains many references:
Some test made that shows when to use instancing and when not: http://www.ozone3d.net/blogs/lab/?p=87
An OpenGL implementation of a pseduo-instancing (recommended for old hardware). glsl_pseudo_instancing.pdf
OpenGL instancing: http://www.opengl.org/registry/specs/EXT/draw_instanced.txt
See also Geometry instancing on Wikipedia.
Upvotes: 8
Reputation: 9759
You could use point sprites and a fragment shader to duplicate the effect of a rendered sphere without the actual sphere geometry. I would try instancing first, however.
Upvotes: 2
Reputation: 5637
If you draw ~100k spheres, you might want to consider raycasting them instead of using polygon meshes to approximate them. The papers GPU-Based Ray-Casting of Quadratic Surfaces by Sigg et al. (2006) and Splatting Illuminated Ellipsoids with Depth Correction by Gumhold (2003) show how to do this. If you do this, you can reuse much of your fast point sprite code.
Upvotes: 3