0
\$\begingroup\$

I am trying to make a volumetric rendering of a cloud. I have been defining the cloud density functions on the glsl shaders and performing ray-marching methods successfully. But now I would like to render a 3D grid (100x100x100) representing the density of a cloud that I calculated using the cpu. The idea that I was trying was to make use of the storage buffer objects, but when I access the array to get the density value and render it, it doesn't work.

This is at the beginning of the glsl fragment:

#version 440 core
layout(std430, binding = 3) buffer layoutName
{
    float data_SSBO[100*100*100];
};

And the density function definition is:

float density(vec3 position, float t){
    const float dx = 1./100., dy = 1./100., dz = 1./100.;
    int i, j, k;
    if( (position.x >= 0.)&&(position.y >= 0.)&&(position.z >= 0.)&&(position.x <= 1.)&&(position.y <= 1.)&&(position.z <= 1.)){
        i = int(position.x/dx);
        j = int(position.y/dy);
        k = int(position.z/dz);

        return data_SSBO[i*100*100 + j*100 + k];
    }
    else
        return 0.;
}

And in the c code there are the buffer creation, bindings, etc:

    glGenBuffers(1, &ssbo);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
    glBufferData(GL_SHADER_STORAGE_BUFFER, 100*100*100*sizeof(float), grid, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, 100*100*100*sizeof(float), grid);

and in the rendering function there is:

    glClearColor(1.f, 1.f, 0.f, 1.0f);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    glBindBuffer(GL_ARRAY_BUFFER, VBO);


    glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);

    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo);

//  glBufferSubData(GL_ARRAY_BUFFER, 0, 3*2*2*sizeof(float), buffer);



    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2* sizeof(float), (void*)0);      // coordenadas

    glEnableVertexAttribArray(0); 



    glUseProgram(shaderProgram);

I believe the problem has to do with the binding, I have been trying different combinations, like binding after and before glUseProgram, etc. I literally have no idea what is wrong, I see this is really confusing.

\$\endgroup\$
1
\$\begingroup\$

A 3D texture will suit this use case much better, and it can be set up to automatically handle clamping for you rather than requiring conditions.

You're using GLSL version 440, which means OpenGL 4.4+, so you have support for everything that is required:

\$\endgroup\$
  • \$\begingroup\$ I am very s***, it was a problem in the cpu program instead, I wasn't filling the grid with values properly. \$\endgroup\$ – Agustín Nov 13 '19 at 17:24
  • \$\begingroup\$ @Agustin - glad you got it sorted. Even so, I'd still suggest you consider switching to a 3D texture, it's a more natural fit for this use case. \$\endgroup\$ – Maximus Minimus Nov 13 '19 at 18:45
  • \$\begingroup\$ Thank you for the suggestion and the links, I will research a little more about this. \$\endgroup\$ – Agustín Nov 13 '19 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.