我正在尝试创建一个由随机值网格组成的3d纹理,但每当我在片段着色器中进行采样时,我只得到一个非常小的值 - 即使我只是将其设置为0.5的网格或类似的东西那!

这就是我尝试这样做的方式:

static GLuint noise_tex = 0;

//create grid of random numbers:
int tex_width = 256;
int tex_height = 256;
int tex_depth = 256;
int tex_size = tex_width*tex_height*tex_depth;
GLfloat* noise_data = new GLfloat[tex_size];
for (int i = 0; i < tex_size; i++){
      noise_data[i] = static_cast <GLfloat> (rand()) / static_cast <GLfloat> (RAND_MAX);
}

//create and bind texture
glGenTextures(1, &noise_tex);
glBindTexture(GL_TEXTURE_3D,noise_tex);

//set filtering and wrapping
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);

glTexImage3D(GL_TEXTURE_3D, 0, GL_R16F, tex_width, tex_height, tex_depth, 0, GL_RED, GL_FLOAT, noise_data);

在片段着色器中(不是我最终想要做的,只是为了看看高度有什么 Value ):

in vec3 pos;

 uniform sampler3D noise_tex;

 out vec4 fragColor;

 void main()
 {

     float height = texture(noise_tex, pos).r;

     if(height<0.000001){
         fragColor = vec4(1.0, 1.0,1.0,1.0);
     }
     else if .... etc

 }

我没有创建任何纹理坐标,但我只是假设它在使用GL_repeat和GL_linear时会起作用 - 我知道pos位于间隔pos.x:200-300,pos.y:200-300,pos.z :0-10,所以我也考虑将这些值映射到0-1,但这似乎也不是问题 .