Textures not loading to shader, for color lookup


I’m trying to implement a color lookup on a greyscale 3D volume rendering, using a 1x256 RGBA texture. The lookup texture I’m using is ofTexture, and I want to pass it to the shader, along with the ofx3dTexture of the volume itself (a stack of greyscale images). The 3dTexture is being passed to the shader with no problem, but the 1x256 lookup texture seems to be just black (0,0,0,1). This is the code I’m using to pass the textures to the shader:

//pass variables to the shader  
 volumeShader.setUniform1i("volume_tex", 1); // volume texture reference  
volumeShader.setUniformTexture("lutTexture",lutTexture, 2);	  

I’ve also tried setUniform1i for the lutTexture, but same with same result.

Here’s also an exerpt from the fragment shader, I’m trying this method for lookup:

uniform sampler3D volume_tex;  
uniform sampler1D lutTexture;  
void main()  
float color_sample;  
        for(int i = 0; i < int(steps); i++)  
        color_sample = texture3D(volume_tex, vec + zoffset).r;  
	gl_FragColor = texture1D(lutTexture, color_sample);    

But then I simple get a pitch black volume. The color_sample gets varying values, so setting glFragColor to the color_sample gives me a volume in greyscale. But this gets turned to all black when I try to use the color lookup, so I’m assuming that it’s the lutTexture that isn’t loading correctly.

The texture itself doesn’t seem to be the problem, as drawing it in the main program works fine.

If you need any other parts of the code, just let me know, didn’t want to paste things irrelevant to the problem.

Thanks in advance

Hey, are you modifying ofxVolumetrics by any chance?

I think that you need to use sampler2D and texture2D for your look up texture because ofTexture is a 2d texture format, even if you set one of the dimensions to 1.

Hi Tim,

I’m working together with Rune and yes we are modifying your awesome ofxVolumetrics. It has been very good to learn about raycasting for volume rendering with your example. We are still learning how to deal with shaders. We have implemented some clipping so far and we are trying to get the CLUT working now. We would also like to implement dithering for the banding problem…
We dont know if you would like to get our code once we have something working properly. In that case just let us know. We could just upload it on GitHub for everyone to see.
Thank you so much for your tips! Let see if we get it working soon.

Hi Jordi,

Sounds like some cool features. What banding are you referring to, do you mean the slicing that happens with a low Z resolution? Please, by all means, fork it on github and submit your changes when they’re complete!

Yes by banding I meant the aliasing, this visible waters on the “surface” of a volume. I guess it is not much related to the FBO quality or the Z step size, it seems to be related to the resolution of the source dataset. The linear filter helps A LOT, but still, it doesn’t solve this problem.

Here there is a nice explanation about that (section 4):

thanks for the link, very good resource! The aliasing/banding you’re talking about is something I struggled to get around, but I’ve never heard of the dithering solution. It looks very simple to implement, I may add a it in as a feature in the future.

I’d also like to do some simple lighting as described in the article. I was playing around with realistic shadows by tracing through to the light source using the volume’s density to determine the overall light intensity per voxel, but it is incredibly slow, as you can imagine. Computing a normal for diffuse lighting should be way more efficient. I was also thinking of using a low res mip map for shadowing.

Anyway, keep me posted on your progress with the look up table. Did using texture2d and sampler 2d help at all? If I get a chance to implement dithering I’ll post back here and update the git repo, it seems like a great feature.

Hey, Tim, sorry for the late response. Yes, working on your ofxVolumetrics, as Jordi said. It’s my first time working with shaders at all, have had some basic course in OpenGL before. So it’s great fun, and a great learning experience.

Also, thanks for the tip on using sampler2D instead, you were of course absolutely right. It worked like a charm. Still not sure if I want to use the texture method, or a different approach, where the user can control the color channels individually, more like a transfer function. That’s what I’m trying now, but I would like a good interface for it, where I can see the histogram of the grey values, and being able to see and control the color channel mappings on there.

I just implemented the ray dithering, it is super simple, and the difference in visual quality is amazing! Thanks for the tip again. I’m going to add diffuse lighting soon too.