【问题标题】:texture2D cause Gl_INVALID_OPERATION when glDrawArrays is called调用 glDrawArrays 时,texture2D 导致 Gl_INVALID_OPERATION
【发布时间】:2016-04-12 19:40:03
【问题描述】:

我正在尝试在片段着色器中使用纹理,如下行所示

   gl_FragColor =vec4(diffuseReflection+ambientLighting+texture2D(texSampler2D, uvCoordinates).xyz*specularReflection,material0.diffuse.w);

但是在调用 glDrawArrays 时会发生 Gl_INVALID_OPERATION 错误。

如果我省略了texture2D,比如

   gl_FragColor =vec4(diffuseReflection+ambientLighting+specularReflection,material0.diffuse.w);

然后不会发生错误。

这是我创建纹理的线条

    Texture* texture=new Texture();
    cv::Mat image = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
    if(image.data!=0)
    {
        texture->init(image.data,GL_RGB,GL_RGB,GL_UNSIGNED_BYTE,image.cols,image.rows);
}
//Texture class is below the post

这是我上传纹理的行

   CHECK_GL(glActiveTexture(GL_TEXTURE0));
    texture->bind();
    CHECK_GL(glUniform1i(texture->texSampler2DHandler,GL_TEXTURE0));

这是我的 Texture 类的相关部分:

    void Texture::bind() {
  CHECK_GL(glBindTexture(GL_TEXTURE_2D, textureID_));
    }

    void Texture::unbind() {
        CHECK_GL(glBindTexture(GL_TEXTURE_2D, 0));
    }

    void Texture::init(void* data_,GLenum format, GLenum internal_format, GLenum type, GLsizei width,
        GLsizei height, GLint minfilter, GLint magfilter) {

        CHECK_GL(glGenTextures(1, &textureID_));
      bind();
      format_ = format;
      type_ = type;
      width_ = width;
      height_ = height;
      internal_format_ = internal_format;
      CHECK_GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter));
      CHECK_GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter));
      CHECK_GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
      CHECK_GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
      data(data_);
      unbind();
    }

    void Texture::data(void* data)
    {
        CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, internal_format_, width_, height_, 0, format_, type_, data));
    }

【问题讨论】:

  • 如果您在调用 glDrawArrays 时遇到错误,请查看本页末尾的可能原因:opengl.org/sdk/docs/man/html/glDrawArrays.xhtml
  • 这是 OpenGL 还是 OpenGL ES?
  • 最有可能的是,您的着色器只是在一种情况下没有编译。当 GLSL (ES) 版本未知时,不清楚您的代码是否有效。
  • 您好,我的代码在 opengl es 中并在桌面上运行,这要归功于 Qt。当我的 glsl 编译中出现语法错误时,它会打印出错误消息,我可以使用 'glGetProgramInfoLog(program, maxLength, &infologLength, infoLog); if (infologLength > 0) { Error(std::string("Shader 链接程序失败:") + infoLog); }'

标签: opengl opengl-es textures


【解决方案1】:

这一行

CHECK_GL(glUniform1i(texture->texSampler2DHandler,GL_TEXTURE0));

必须是

CHECK_GL(glUniform1i(texture->texSampler2DHandler,0));

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多