什么样的向量应该用来采样立方体贴图?

huangapple go评论56阅读模式
英文:

What kind of vector should I use to sample a cubemap?

问题

我在想是否有人可以提供一些澄清。

我有一个边长为8192像素的立方体贴图,宽度和高度都是8192像素。

我正在使用以下向量(见下面的dir_vector)来采样我的立方体贴图,

#version 330 core

uniform vec3 hangle; // 水平角度范围和增量
uniform vec3 vangle; // 垂直角度范围和增量
out vec3 dir_vector;

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));
            dir_vector = vec3(x, y, z);
        }
    } 
}

很明显,dir_vector 的长度将为1,x、y、z 的值将在 (-1.0, 1.0) 范围内。

鉴于我的立方体贴图的尺寸,这个向量的长度是否需要更大?

我还假设,OpenGL 已经根据dir_vector的坐标知道要采样立方体贴图的哪一侧。这个假设是否正确?

英文:

I am wondering if someone can provide some clarification.

I have a cubemap with a side of 8192 pixels for both width and height.

I am using the following vector (see below dir_vector)to sample my cubemap,

#version 330 core

uniform vec3 hangle; // horizontal angular range and increment
uniform vec3 vangle; // vertical angular range and increment
out vec3 dir_vector;


void main(){
   for (float theta = vangle.x; theta &lt; float(vangle.y); theta+= vangle.z){
       for (float psi = hangle.x;  psi &lt;  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));
           dir_vector = vec3(x,y,z);
       }
   } 
}

It is clear that dir_vector is going to have a length of 1 and x,y,z are going to be within (-1.0,1.0).

Would the length of the vector need to be greater given the size of my cubemap?

I am also assuming that given the coordinates of the dir_vector opengl already knows which side of the cubemap to sample. is this assumption correct?

答案1

得分: 2

It is clear that dir_vector is going to have a length of 1 ...
"很明显,dir_vector的长度将为1..."

If you're looking to convert from spherical coordinates, the formula for y should then be:
如果您想从球坐标转换,那么y的公式应该是:

float y = cos(radians(psi));  // 不是theta

Would the length of the vector need to be greater given the size of my cubemap?
基于我的cubemap的大小,矢量的长度需要更大吗?

No. OpenGL samples from a cubemap based on the direction of the vector, ignoring the length.
不需要。OpenGL根据矢量的方向从cubemap中采样,忽略长度。

I am also assuming that given the coordinates of the dir_vector opengl already knows which side of the cubemap to sample. is this assumption correct?
我还假设,根据dir_vector的坐标,OpenGL已经知道要采样cubemap的哪一面。这个假设是否正确?

Yes. OpenGL determines the face of the cubemap based on the direction of the vector.
是的。OpenGL根据矢量的方向确定cubemap的面。

Cubemap sampling
Cubemap采样

The details of cubemap sampling are described in section "8.13 Cube Map Texture Selection" of the OpenGL spec.

Cubemap采样的详细信息在OpenGL规范的"8.13 Cube Map Texture Selection"部分中有描述。

In short: The major axis is determined based on the coordinate of the largest magnitude. This is going to determine the cube-map face that's going to be sampled. The other two coordinates are divided by the major one, yielding values in the [-1, 1] range, then remapped to [0, 1] texture coordinates, which are then used to sample that face's texture like a regular 2d-texture.

简而言之:主轴是根据具有最大幅度的坐标确定的。这将确定将要采样的立方体贴图面。其他两个坐标被主坐标除以,产生在[-1, 1]范围内的值,然后重新映射到[0, 1]的纹理坐标,然后用于像普通2D纹理一样采样该面的纹理。

For example, given the input vector (10, 20, 100), the major axis is "positive z". OpenGL will calculate
例如,给定输入矢量(10, 20, 100),主轴是"正z"。OpenGL将计算

s = (10/100 + 1)/2 = 0.55
t = (-20/100 + 1)/2 = 0.40

and sample the GL_TEXTURE_CUBE_MAP_POSITIVE_Z face at coordinates (s, t). The conversion to an actual texel index will happen at this stage, taking the correct mipmap level into account.
并在坐标(s, t)处采样GL_TEXTURE_CUBE_MAP_POSITIVE_Z面。将在此阶段进行实际的像素索引转换,考虑正确的mipmap级别。

英文:

> It is clear that dir_vector is going to have a length of 1 ...

You have a mistake in your code. If you're looking to convert from spherical coordinates, the formula for y should then be:

float y = cos(radians(psi));  // NOT theta

> Would the length of the vector need to be greater given the size of my cubemap?

No. OpenGL samples from a cubemap based on the direction of the vector, ignoring the length.

> I am also assuming that given the coordinates of the dir_vector opengl already knows which side of the cubemap to sample. is this assumption correct?

Yes. OpenGL determines the face of the cubemap based on the direction of the vector.

Cubemap sampling

The details of cubemap sampling are described in section "8.13 Cube Map Texture Selection" of the OpenGL spec.

In short: The major axis is determined based on the coordinate of the largest magnitude. This is going to determine the cube-map face that's going to be sampled. The other two coordinates are divided by the major one, yielding values in the [-1, 1] range, then remapped to [0, 1] texture coordinates, which are then used to sample that face's texture like a regular 2d-texture.

For example, given the input vector (10, 20, 100), the major axis is "positive z". OpenGL will calculate

s = (10/100 + 1)/2 = 0.55
t = (-20/100 + 1)/2 = 0.40

and sample the GL_TEXTURE_CUBE_MAP_POSITIVE_Z face at coordinates (s, t). The conversion to an actual texel index will happen at this stage, taking the correct mipmap level into account.

huangapple
  • 本文由 发表于 2023年7月14日 00:58:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定