Alpha transparency with particle effects in OpenGL

I have a simple particle effect in OpenGL using GL_POINTS. The following is caused: particles are transferred in order from the particle farthest from the camera, first to the nearest camera:

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);
}

However, when rendering, there are some artifacts that can be seen in the following effect: artifact image http://img199.imageshack.us/img199/9574/particleeffect.png

Problems disappear if I turn off depth testing, but I need the effects to interact with other elements in the scene, appearing in front of and behind elements of the same GL_TRIANGLE_STRIP depending on the depth.

+3
source share
2 answers

, :

  • GL WRITE DEPTH, ( )
  • .

, .

+3

. , OpenGL . API. -, . , . , , -, .

glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SPRITE);
glEnable(GL_CULL_FACE);

while(1)
{
    ...
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDisable(GL_BLEND);
    glDepthMask(1);
    RenderOpaque();
    SortSprites();
    glEnable(GL_BLEND);
    glDepthMask(0);
    DrawSprites();
    ...
}
+2
source

Source: https://habr.com/ru/post/1708948/


All Articles