3d texture sampling as black

Started by sephjfox, October 10, 2023, 01:14:28

Previous topic - Next topic

sephjfox

hello friends  8)

i am attempting to display a simple 3d texture in my scene, but the 3d texture always samples black. any idea what i could be doing wrong?

please note the line in the glsl section that sets the output pixel to green, that is used to test color placement.

// Create array
int t3dim = 32;
int[] tex3d_r = new int[t3dim*t3dim*t3dim*4];
for(int i=0;i<(t3dim*t3dim*t3dim);i++) {
	tex3d_r[i*4+0] = 0;
	tex3d_r[i*4+1] = 255;
	tex3d_r[i*4+2] = 0;
	tex3d_r[i*4+3] = 255;
}

// Create texture
int id = glGenTextures();
glActiveTexture(GL_TEXTURE0);
ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE0_ARB);
glClientActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D,id);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexImage3D(GL_TEXTURE_3D, 0, GL30.GL_RGBA8, t3dim, t3dim, t3dim, 0, GL30.GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, tex3d_r);
glGenerateMipmap(GL_TEXTURE_3D);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_LOD_BIAS, 0.4f);

// Render
glUniform1i(vIndex("volumeMode"),1);
glUniform1i(vIndex("volumeMap"),6);
glActiveTexture(GL_TEXTURE0+6);
ARBMultitexture.glActiveTextureARB(ARBMultitexture.GL_TEXTURE0_ARB+6);
glClientActiveTexture(GL_TEXTURE0+6);
glBindTexture(GL_TEXTURE_3D,id);

// GLSL
uniform int volumeMode;
uniform sampler3D volumeMap;
if(volumeMode==1) {
	float xd = gl_FragCoord.x/resolution.x; // 0..1
	vec4 sc = texture(volumeMap, vec3(xd));
	//sc = vec4(0,255,0,255); // makes circle green
	sc /= 255;
	pixel = vec4(sc.xyz,1f);
}







sephjfox

Hello all  ;D  I have found the solution to my problem. Hopefully it will help someone else in their journey.

These are the changes I made to get it working. Essentially I declare the array slightly differently and specify a different datatype:

// Java
int t3dim = 64;
int[] tex3d_r = new int[t3dim*t3dim*t3dim*4];
int r,g,b,a,color;
for(int i=0;i<(t3dim*t3dim*t3dim);i++) {
    r = (int)(Mati.fRand_static()*255.0f);
    g = (int)(Mati.fRand_static()*255.0f);
    b = (int)(Mati.fRand_static()*255.0f);
    a = (int)(Mati.fRand_static()*255.0f);
    color = (a << 24) | (r << 16) | (g << 8) | b;
    tex3d_r[i]=color;
}

glTexImage3D(GL_TEXTURE_3D, 0, GL30.GL_RGBA, t3dim , t3dim , t3dim , 0, GL30.GL_RGBA, GL_UNSIGNED_BYTE, tex3d_r);

And the debug shader where I can browse the layers of the 3d texture by altering dbg_val1 between 0 and 1:

// GLSL
if(volumeMode>0) {
    pixel = texture(volumeMap, vec3(uvs_vs0.xy,dbg_val1));
    return;
}



Enjoy, and happy coding  :)