Opengl / glsl:通过纹理将浮点数组传递给着色器

我试图将包含128 * 128浮点数的float *传递给着色器。由于此数组太大,因此我试图通过纹理以使我的float *进入着色器。问题是我不知道如何在着色器中使用此数组。 我先打印浮点数*,然后再将其发送到着色器,并且其中包含-1和1之间的数字。

我的目标是用Perlin噪声模拟水上的波浪,所以我的漂浮物是Perlin噪声。

所以我像这样实例化我的噪声纹理:

_perlin_noise = new float[_dimension * _dimension];

//... I put float inside my _perlin_noise variable

//Then I instantiate my texture : 

glGenTextures(1,&_perlin_noise_text);
glBindTexture(GL_TEXTURE_2D,_perlin_noise_text);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D,GL_R32F,_dimension,GL_RED,GL_FLOAT,_perlin_noise);


glactiveTexture(0);
glBindTexture(GL_TEXTURE_2D,0);

我的水使用两种纹理:一种纹理是全蓝色,另一种是泡沫纹理。 所以现在我有了第三种纹理,那就是我的佩林噪声。而且我想通过Perlin噪声来调整顶点的高度。

因此,在进行任何操作之前,我都将统一变量分配给着色器,如下所示:

main_water_shader->use();
glUniform1i(glGetUniformLocation(main_water_shader->getProgram(),"main_water_texture"),0);
glUniform1i(glGetUniformLocation(main_water_shader->getProgram(),"foam_texture"),1);
glUniform1i(glGetUniformLocation(main_water_shader->getProgram(),"perlin_noise"),2);

然后我想在我的顶点着色器中调整顶点的高度,但是我不知道该怎么做。我试图在我的顶点着色器中做到这一点:

uniform sampler2D perlin_noise;
//some other uniform...

void main()
{
    //calculating vertex height
    float height = float( texture2D(perlin_noise,vec2(x,y)) ); 
    vec3 new_vertex_position = vertex_position;
    new_vertex_position.y = height;

    //Standard stuff
    vs_position = vec4(ModelMatrix * vec4(new_vertex_position,1.f)).xyz;
    vs_texcoord = vec2(vertex_texcoord.x,vertex_texcoord.y );
    vs_normal = mat3(ModelMatrix) * vertex_normal;

    gl_Position = Projectionmatrix  * ViewMatrix * ModelMatrix * vec4(new_vertex_position,1.f);
}

没有任何变化,我的顶点高度仍然相同。而且,如果我尝试在片段着色器中将我的perlin_noise纹理显示在平面上,则会得到以下提示:

Opengl / glsl:通过纹理将浮点数组传递给着色器

您可以看到我的飞机(非常大),上面有黑白纹理,重复此操作。所以我想我的perlin_noise纹理确实包含一些东西(即使有点怪异),但是我不知道如何在我的顶点着色器中使用它。

编辑:在我的屏幕快照中,我还使用了顶点着色器,其高度已根据Perlin噪声进行了调整,但是如您所见,我的平面是平面,所以它似乎不起作用。

EDIT2:如果我不够清楚,请告诉我

bmliuxuan142524 回答:Opengl / glsl:通过纹理将浮点数组传递给着色器

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3136320.html

大家都在问