Question

I'm an OpenGL newbie. I'm trying to create a system of particles and I have everything set up, but the particle trails.

The easiest way, that I see, for me to implement this is to clear the screen with a nearly transparent colour e.g alpha = 0.05. This will fade the previous positions drawn.

However, this doesn't work. I've also tried to draw a rectangle over the screen.

After setting alpha of my particles to 0.3, my transparency doesn't seem to be working.

This is my code:

do{
    glBindVertexArray(VertexArrayID);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    time = (float)glfwGetTime();

    //  ** Calculating new positions and placing into vertex array
    iter = 0;

    for(int i = 0; i < n; i++){
        bodies[i].F(bodies, i, n, 1);
        bodies[i].calcPosition(dt);
        bodies[i].getVertexArray(vertexArray, iter, scale, i);
    }

    for(int i = 0; i < n; i++){
        bodies[i].F(bodies, i, n, 2);
        bodies[i].calcVelocity(dt);
    }

    // **

    glBufferData(GL_ARRAY_BUFFER, 21 * 6 * n * sizeof(float), vertexArray, GL_STREAM_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 20 * 3 * n * sizeof(GLuint), elements, GL_STREAM_DRAW);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(
        0,                  
        2,                  
        GL_FLOAT,           
        GL_FALSE,           
        6*sizeof(float),    
        (void*)0            
    );

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(
        1, 
        4,
        GL_FLOAT,
        GL_FALSE, 
        6*sizeof(float), 
        (void*)(2*sizeof(float))
    );

    glDrawElements(GL_TRIANGLES, 20 * 3 * n, GL_UNSIGNED_INT, 0);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    glfwSwapBuffers();

    while((float)glfwGetTime() - time < dt){
    }
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
       glfwGetWindowParam( GLFW_OPENED ) );

My shaders:

#version 330 core

in vec4 Color;

out vec4 outColor;

void main()
{
    outColor = Color;
}

#version 330 core

layout(location = 0) in vec2 position;

layout(location = 1) in vec4 color;
out vec4 Color;

void main(){
    gl_Position = vec4(position, 0.0, 1.0);
    Color = color;
}

This outputs n circles (20 sided polygons) travelling round the screen in different colours. All previous drawings stay on the screen, I want them to fade

Thanks Andy

Was it helpful?

Solution

The easiest way, that I see, for me to implement this is to clear the screen with a nearly transparent colour e.g alpha = 0.05. This will fade the previous positions drawn.

That is not going to work in a double-buffered window (and you don't want a single buffered one). The contents of the back buffer are undefined after SwapBuffer. If you really, really lucky, you might get some of the older image contents (but not the last one, as this is the front buffer now).

To solve this issue, you have to render to a texture, so you can redraw the previous contents (with your fadeout), add the new particle positions (still rendering into a texture for the next frame), and finally render or blit that texture to the real framebuffer. So you need at least two additional textures in a ping-pong fashion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top