hanno
hanno

Reputation: 6513

Drawing many spheres in OpenGL

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

Answers (3)

Gregory Pakosz
Gregory Pakosz

Reputation: 70204

Have a look at this page on instancing: it contains many references:

See also Geometry instancing on Wikipedia.

Upvotes: 8

tkerwin
tkerwin

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

Malte Clasen
Malte Clasen

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

Related Questions