当调用glDrawArrays时,texture2D导致Gl_INVALID_OPERATION

时间:2016-04-12 19:40:03

标签: opengl opengl-es textures

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

   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));
    }

1 个答案:

答案 0 :(得分:0)

这一行

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

必须是

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