【问题标题】:ERROR: 0:10: Invalid call of undeclared identifier 'texture2D'错误:0:10:未声明的标识符“texture2D”的无效调用
【发布时间】:2018-10-05 12:13:50
【问题描述】:

我正在使用 Mac 和 OpenGL,我正在做关于纹理的作业。

当我尝试执行该文件时,我的终端中出现一个空白(黑色)窗口,并显示以下错误消息:

Compile failure in the fragment shader:
ERROR: 0:10: Invalid call of undeclared identifier 'texture2D'

这是我的片段着色器文件06_fshader.glsl中的代码:

#version 330


out vec4 frag_color;

uniform sampler2D texture;
in vec2 tex;
void main () 
{
      frag_color = texture2D(texture, tex);     
}

我知道这里有类似的问题:GLSL: "Invalid call of undeclared identifier 'texture2D'",但它对我不起作用。

【问题讨论】:

  • 你改成texture(...)是什么意思?
  • 它说:片段着色器中的编译失败:错误:0:10:“纹理”调用无效(不是函数或子程序统一)
  • 是的,那是因为您将制服称为 texture,现在发生了名称冲突。重命名制服,它应该可以工作。
  • 不管你选择哪个名字。只要不是质感。例如使用uniform texture2D mytexture;... = texture(mytexture, tex)。简而言之:永远不要用 glsl 方法名命名变量。
  • 对不起。 uniform sampler2D mytexture.

标签: macos opengl shader fragment-shader


【解决方案1】:

代码有两个问题。第一个是,如链接问题中所述,texture2D 已替换为texture

第二个问题是已经有一个名为texture 的制服,这会在尝试调用texture(方法)时导致命名冲突。这可以通过重命名制服来解决。

最终的着色器应该如下所示:

#version 330

out vec4 frag_color;

uniform sampler2D mytexture;
in vec2 tex;
void main () 
{
      frag_color = texture(mytexture, tex);     
}

【讨论】:

    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2013-08-30
    • 2013-01-22
    • 2011-10-25
    • 2011-02-15
    相关资源
    最近更新 更多