Sampling a cubemap with unsigned_bytes

Hi,
I created a cubemap with stored stencil data in all directions. I then sampled the cubemap (using spherical coordinates) to render the information into a quad in order to generate a flattened version of the information in the cubemap.

Previously, I used this technique successfully to generate a flattened version of the cubemap storing depth from camera location to its surroundings.

I was hoping to use the same code but instead of binding to the depth cubemap before rendering, binding to the stencil cubemap instead.

Unfortunately I am not getting any result when I render the second cubemap. I am wondering if the cubesampler does not work because it is not sampling a cubemap that is made out of floats but instead out of unsigned_bytes?

Here is the sampling part of my code,

#  SAMPLING SCENE

# create a framebuffer to store scene
fbo = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, fbo)

# create color texture to be populated from sampling scene depth cubemap
scene_tex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, scene_tex)
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, scene_width, scene_height, 0, GL_RED, GL_FLOAT, None)      
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

# create an index texture to be populated from sampling scene stencil cubemap
stencil_tex = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, stencil_tex)
glTexImage2D(GL_TEXTURE_2D, 0, GL_STENCIL_INDEX8, scene_width, scene_height, 0, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, None)      
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

# attach textures to scene framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, scene_tex, 0)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, stencil_tex, 0)

# check for errors
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE):
    raise RuntimeError("ERROR.FRAMEBUFFER. Framebuffer is not complete!")

# Rendering to quad

# set viewport to scene resolution
glViewport(0, 0, scene_width, scene_height)

# activate sampling shader
shader_sampling.use()

glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS)

# pass parameters to sampling shader
shader_sampling.set_uniform_vec2('size_display', glm.vec2(scene_width, scene_height))
shader_sampling.set_uniform_int('cube', 0)

# clear screen
glClearColor(0.0, 0.0, 0.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT)

# render stencil to quad
glBindTexture(GL_TEXTURE_CUBE_MAP, cube_stencil_tex)
quad.draw(shader_sampling)
glBindTexture(GL_TEXTURE_CUBE_MAP, 0)

# render depth to quad
glBindTexture(GL_TEXTURE_CUBE_MAP, cube_depth_tex)
quad.draw(shader_sampling)

glfw.swap_buffers(window)

# depth output
tmp = glReadPixelsf(0, 0, scene_width, scene_height, GL_RED, GL_FLOAT)
scene= tmp.reshape((scene_height, scene_width))
scene[scene == 0] = np.nan 
np.save('scene_depth', scene)

# stencil output
tmp = glReadPixelsf(0, 0, scene_width, scene_height, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE)
mask= np.frombuffer(tmp, dtype=np.uint8).reshape((scene_height, scene_width))
#mask[mask == 0] = np.nan 
np.save('scene_sel', mask)

# unbind scene framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0)

Here is the sampling shader:

#version 440 core
//sampling_shader.vs

layout (location = 0) in vec3 position; // vec2
void main(){
    gl_Position = vec4(position.x, position.y, 0.0f, 1.0f);
}
#version 440 core
//sampling_shader.fs

#version 440 core

uniform vec2 size_display;
uniform vec2 offset;
uniform float hrange;
uniform float vrange;
uniform samplerCube cube;

out float color;

void main()
{
    // scale coordinates (0,1)
    vec2 scaleCoord = gl_FragCoord.xy / size_display;

    // convert offset to radians
    vec2 off = vec2(radians(offset.x), radians(offset.y));

    // center the view
    vec2 angles = ((scaleCoord * 2.0) - vec2(1.0)) * vec2(radians(hrange/2), radians(vrange/2)) + off;

    // sampling vector
    vec3 samplingVec = vec3(-cos(angles.y) * cos(angles.x), sin(angles.y), cos(angles.y) * sin(angles.x));

    // output
    color = texture(cube, samplingVec).r;
} 

The type of a sampler variable should match the texture’s data type: sampler* for floating-point or normalised textures, isampler* for signed integer textures, usampler* for unsigned integer textures (which includes stencil textures and the stencil component of depth+stencil textures).

Thank You @GClements ! I will incorporate this and see how I fair. Thanks again!!

So I changed the fragment shader I was using to the following,

#version 440 core

uniform vec2 size_display;
uniform vec2 offset; // used this to center the view
uniform float hrange; // horizontal angular range
uniform float vrange; // vertical angular range
uniform usamplerCube cube;

out uint color;

void main()
{
    // scale coordinates (0,1)
    vec2 scaleCoord = gl_FragCoord.xy / size_display;

    // convert offset to radians
    vec2 off = vec2(radians(offset.x), radians(offset.y));

    // center the view
    vec2 angles = ((scaleCoord * 2.0) - vec2(1.0)) * vec2(radians(hrange/2), radians(vrange/2)) + off;

    // sampling vector
    vec3 samplingVec = vec3(-cos(angles.y) * cos(angles.x), sin(angles.y), cos(angles.y) * sin(angles.x));

    // output
    color = texture(cube, samplingVec).r;
} 

Everything else is exactly the same but still no success :frowning: I am including additional information in my shader (in trying to simplify my post last time I deleted important information from my fragment shader, edited my original version as well)