glTexImage2D 'target is not valid'

glTexImage2D 'target is not valid'

我正在编写游戏引擎,但在加载纹理时遇到了一个令人费解的问题。

我的代码如下:

//Load to GL
glGenTextures(1, &glTextureID);
glBindTexture(GL_TEXTURE_2D, glTextureID);
glTexImage2D(glTextureID, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);

在启用 GL 调试输出的情况下单步执行显示 glTexImage2D 行给出了错误:

Error has been generated. GL error GL_INVALID_ENUM in (null): (ID: 1938861751) 1 is not valid.

其中格式取决于正在加载的图像,并使用 FreeImage 确定。 (对于相应的图像类型,使用 GL_RGBAGL_RED 中断)。 width/height 也由 FreeImage 决定,在 GL_RGBA 的情况下,两个值都是 512。据我了解,这些值对两种格式参数均有效。

使用 FreeImage 的完整代码在这里包含起来有点冗长,并且使用了多个线程(我确信这段代码是 运行 在主线程上,我在之前断言这些行)。

以下也不起作用:

glTexImage2D(glTextureID, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

我没能找到任何其他遇到此错误的人。我已经研究了如何抛出 GL_INVALID_ENUM,但是 none 记录的方法似乎适用,当然不适用于我收到的消息。

glTextureID 的值在这两行之间没有修改,保持为“1”。

尝试加载其他纹理时也会抛出相同的错误,'target' 不同。

我的 GL 上下文已正确初始化,因为我能够使用其他 GL 函数并成功绘制无纹理的多边形。

可能是什么原因造成的?

glTexImage2D的第一个参数是target,必须是GL_TEXTURE_2DGL_PROXY_TEXTURE_2DGL_TEXTURE_1D_ARRAY ...

将您的代码更改为:

glTexImage2D(glTextureID, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);

注意glTexImage2D为绑定到当前Texture unit的纹理对象指定二维纹理。


OpenGL 4.6 core profile specification - 8.5. TEXTURE IMAGE SPECIFICATION, page 212:

The command

void TexImage2D( enum target, int level, int internalformat, sizei width,
    sizei height, int border, enum format, enum type, const void *data );

is used to specify a two-dimensional texture image. target must be one of TEXTURE_2D for a two-dimensional texture, TEXTURE_1D_ARRAY for a onedimensional array texture, TEXTURE_RECTANGLE for a rectangle texture, or one of the cube map face targets from table [...]