Confused regarding the sampling of a cubemap

So I have a cubemap that I generated with depth values of a scene.

My understanding was that I could sample a cubemap to retrieve its values using a vec3 (see below fragment shader) but I have seen examples where a vec2 vector is being used instead??? Also I am assuming that to sample my cubemap all I need are vectors between (-1,1), is this correct???

here is the fragment shader I am using to sample my depth cubemap

#version 330 core

uniform vec3 hangle; // horizontal angular range and increment
uniform vec3 vangle; // vertical angular range and increment
uniform samplerCube cubetex;
out float depth;


void main(){
    for (float theta = vangle.x; theta < float(vangle.y); theta+= vangle.z){
        for (float psi = hangle.x;  psi <  float(hangle.y); psi+= hangle.z){
            float x = sin(radians(theta)) * sin(radians(psi));
            float z = cos(radians(theta)) * sin(radians(psi));
            float y = cos(radians(theta));
            depth = texture(cubetex, vec3(x,y,z)).r;
        }
    } 
}

Citation needed. We can’t comment on anything we don’t know about. Odds are good that such examples are not operating on cubemaps.

What is the purpose of the nested loops? You are overwriting depth on each iteration, so only the value calculated by the final iteration is used as the result of the fragment shader…

The purpose of these loops is to generate a vector that will be used to sample the values stored in a cubemap within certain horizontal and vertical angular range and at a certain angular step. In this case the cubemap holds the depth information for a scene surrounding a certain location. Hope this helps to clarify the intent.

Apologies @Alfonse_Reinheart. You are right! The routines I was looking treated the cubemap one side at a time. However, I am still uncertain whether the reason I am not getting a 2D ‘panorama’ of depths (obtained from using sampling using spherical coordinates a depth cubemap) is because of something wrong with my shader

Take a look at this bit of C++ code:

int value = 0;
for (int i = 0; i < 10; ++i) {
    value = i;
}
std::cout << "out: " << value << std::endl;

What is the output?
What is the point of executing the loop iterations when all but the final iteration’s value are overwritten?
Your fragment shader does the same thing, it runs a bunch of loops overwriting the depth variable in each iteration, so only the value from the final iteration becomes the output of your fragment shader.

The fragment shader is executed for each fragment produced by rasterization. As a first approximation “each fragment produced by rasterization” corresponds to each texel in your target image that are overlapped by what you are drawing - in your case probably a full size quad. The result of each execution of the fragment shader is written to the output texel it was executed for.
Assigning repeatedly to an out qualified variable in a fragment shader just overwrites the value and only the final value assigned is the output of the fragment shader.

Thanks @carsten_neumannT this makes sense but I am then lost as to how to generate a 2d image with that results from the sampling of the cubemap?

I think I know where I have been confused.

I was hoping to be able to use the fragment shader to do the sampling of all the cubemap!!

You can. You just can’t do it all in one fragment shader invocation.

When doing rendering, it may help to think backwards. A fragment shader invocation sets the value of a single pixel on the screen. Where does that pixel’s value come from is the question the fragment shader answers.

The “loop” is something that happens to the fragment shader, not in the fragment shader.

Thanks @Alfonse_Reinheart ! I am trying to get my head around what you mentioned (I think that I was very confused about how a fragment shader operated :frowning: )

I am wondering if the something like following would make sense,…

I would generate my display with the following dimensions

  • width = int(horizontal angular span / angular step)
  • height = int(vertical angular span / angular step)

my fragment shader would then be something like the following???

#version 330 core

uniform float angular_step;
uniform samplerCube depth_cube; 
uniform vec2 display_size; // contains (width, height) of display
out float depth;

void main(){
// psi is the horizontal deviation angle
float psi = (gl_FragCoord.x - display_size.x / 2 ) * angular_step;
// theta is the vertical deviation angle
float theta = (gl_FragCoord.y - display_size.y / 2 ) * angular_step;
float x = sin(theta) * sin(psi);
float z = cos(theta) * sin(psi);
float y = cos(theta);
depth = texture(depth_cube, vec3(x,y,z)).r;
}