我使用 GL_POINTS 在 OpenGL 中有一个简单的粒子效果。调用以下方法,按照从最远的粒子到最接近相机的最后一个粒子的顺序传递粒子:
void draw_particle(particle* part) {
/* The following is how the distance is calculated when ordering.
* GLfloat distance = sqrt(pow(get_camera_pos_x() - part->pos_x, 2) + pow(get_camera_pos_y() - part->pos_y, 2) + pow(get_camera_pos_z() - part->pos_z, 2));
*/
static GLfloat quadratic[] = {0.005, 0.01, 1/600.0};
glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, quadratic);
glPointSize(part->size);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SPRITE_ARB);
glEnable(GL_TEXTURE_2D);
glBegin(GL_POINTS);
glColor4f(part->r, part->g, part->b, part->a);
glVertex3f(part->pos_x, part->pos_y, part->pos_z);
glEnd();
glDisable(GL_BLEND);
glDisable(GL_POINT_SPRITE_ARB);
}
但是,在渲染时会出现一些伪影,如以下效果所示:artifacted image http://img199.imageshack.us/img199/9574/particleeffect.png
如果我禁用深度测试,问题就会消失,但我需要效果能够与场景的其他元素交互,根据深度出现在相同 GL_TRIANGLE_STRIP 元素的前面和后面。