简体   繁体   English

OpenGL和GLSL:灰度纹理未显示

[英]OpenGL & GLSL: Greyscale texture not showing

I'm trying to show a greyscale texture on the screen. 我正在尝试在屏幕上显示灰度纹理。 I create my texture via 我通过创建我的纹理

glGenTextures(1, &heightMap);
glBindTexture(GL_TEXTURE_2D, heightMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, 512, 512, 0, GL_RED, GL_FLOAT, colorData);

colorData is a float[512*512] with values between 0.0 and 1.0. colorData是一个float [512 * 512],其值介于0.0到1.0之间。

When rendering, I use: 渲染时,我使用:

glBindTexture(GL_TEXTURE_2D, heightMap);
glUniform1i(shader.GetUniformLocation("textureSampler"), 0);

shader.GetUniformLocation is a function of a library we use at university. shader.GetUniformLocation是我们在大学使用的库的功能。 It is essentially the same as glGetUniformLocation(shader, "textureSampler"), so don't be confused by it. 它本质上与glGetUniformLocation(shader,“ textureSampler”)相同,因此请不要对此感到困惑。

I render two triangles via triangle strip. 我通过三角带渲染两个三角形。 My fragment shader is: 我的片段着色器是:

#version 330
layout(location = 0) out vec4 frag_color;
in vec2 texCoords;
uniform sampler2D textureSampler;

void main()
{
    frag_color = vec4(texture(textureSampler, texCoords).r, 0, 0, 1);
}

I know the triangles are rendered correctly (eg if I use vec4(1.0, 0, 0, 1) for frag_color, I get a completely red screen). 我知道三角形可以正确渲染(例如,如果我对vrag4_color使用vec4(1.0,0,0,1),则会得到一个完全红色的屏幕)。 However with the line above, I only get a completely black screen. 但是,在上面的行中,我只能看到一个全黑的屏幕。 Every texture value seems to be 0.0. 每个纹理值似乎都是0.0。

Does anyone have an idea, what I have done wrong? 有人知道我做错了什么吗? Are there mistakes in that few lines of code or are these completely correct and the error is somewhere else? 几行代码中是否存在错误,或者这些错误是否完全正确并且错误在其他地方?

As one of the comments below says, setting glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 如下面的评论之一所述,设置glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); and glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 和glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); solves the problem. 解决了问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM