Jack3311
Jack3311

Reputation: 23

glTexImage2D 'target is not valid'

I am writing a game engine, but I'm having a puzzling issue when loading textures.

My code is as follows:

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

Stepping through with GL debug output enabled shows that the glTexImage2D line gives the error:

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

Where format is dependent on the image being loaded, and is determined using FreeImage. (Breaks with both GL_RGBA and GL_RED for respective image types). width/height is also determined with FreeImage, both values being 512 in the GL_RGBA case. It is my understanding that these values are valid for both format parameters.

The full code using FreeImage is a bit lengthy to include here, and uses multiple threads (I am sure that this code is run on the main thread, and I assert so prior to these lines).

The following also does not work:

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

I have not been able to find any other people who have had this error. I have researched how GL_INVALID_ENUM can be thrown, but none of the documented ways seem applicable, certainly not with the message I received.

The value of glTextureID is not modified between these lines, remaining at '1'.

The same error is also thrown when trying to load additional textures, with the 'target' being different.

My GL context is correctly initialized, since I am able to use other GL functions, and successfully draw untextured polygons.

What could be causing this?

Upvotes: 1

Views: 1218

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

The fist parameter of glTexImage2D is the target, which must be GL_TEXTURE_2D, GL_PROXY_TEXTURE_2D, GL_TEXTURE_1D_ARRAY ...

Change your code to:

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

Note glTexImage2D specifies the two-dimensional texture for the texture object that is bound to the current Texture unit.


See 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 [...]

Upvotes: 14

Related Questions