Drawing many spheres in OpenGL

折月煮酒 提交于 2019-11-28 21:42:41

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1995262/drawing-many-spheres-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!