Hello everybody.
So I’m doing a iOS application where I have volume information stored in a kind of 3D texture. To render it, I’m using OpenGL ES, and as it does not support 3D textures, I’m forced to simulate 3D textures using multiple 2D textures (every 2D texture contains some slices of the 3D texture).
To do this I have 1x1 cube and a fragment shader that, for every fragment, throws a ray from the observer to it and looks up for its color to one of the 2D textures. If the color found is the color i’ve defined as transparent, it should keep looking for a color in the ray direction. With this a volume can be simulated.
The algorithm can be found here (the fragment shader at the end of the page).
The problem I have is that this not seem to work. I’ll paste some code from the fragment shader:
vec4 getColorFromTextures(in vec3 coordinates) {
//This function simulates that we have a 3D texture. To test the ray-casting part, I've replaced the actual code for this:
if((coordinates.z < 0.25) || (coordinates.z > 0.5 && coordinates.z < 0.75)) return vec4(1.0);
else return vec4(vec3(0.0), 1.0);
}
//So it's supposed to have the cube sepparated in 4 parts: black-white-black-white.
Then, the main:
/*
eye_position is an uniform vec3 that is supposed to have the camera position in it.
It's initialized with the first 3 values of the last column of the ModelView Matrix, changing their sign.
(I'm not sure this is ok, maybe this is is the root of all the problems).
frag_position is a varying initialized in the vertex shader with gl_Vertex.xyz.
*/
void main()
{
highp vec3 ray_dir = normalize(eye_position - frag_position); //ray direction = observer-fragment_position.
highp vec3 ray_pos = textureVarying.xyz;
highp vec4 frag_color = vec4(0.0);
highp vec4 color = getColorFromTextures(ray_pos);
highp vec4 transparent_color = vec4(0.0, 0.0, 0.0, 1.0);
bool continue = true;
if(color != transparent_color)
{
frag_color = color;
continue = false;
}
while (continue)
{
ray_pos += ray_dir * sample_step; //sample_step is, for example, 1.0/50.0
if (any(greaterThan(ray_pos,pos111))) //Out of the cube -> out of the "3D texture"
continue = false;
if (any(lessThan(ray_pos,pos000))) //Out of the cube -> out of the "3D texture"
continue = false;
if(continue)
{
color = getColorFromTextures(ray_pos);
if(color != transparent_color)
{
frag_color = color;
continue = false;
}
}
}
if (frag_color == vec4(0.0)) //Not color found => in the direction of the ray, all is transparent.
discard;
else
gl_FragColor = frag_color;
}
And that’s the result:
As you can see, something’s wrong. The “empty white slice” should be filled with white
Any idea of what’s wrong? I’d really appreciate any help you can give me, I’m really blocked with this… I hope I’ve explained well enough too.
Thanks on advance.