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.