krb
krb

Reputation: 16315

OpenGL texture generation

I'm currently trying to figure out how to use OpenGL. I want to create a texture from a byte buffer.

For some reason, when I do it with glTexImage2D, it does not work (the texture comes out blank):

glTexImage2D(GL_TEXTURE_2D,
             0,
             GL_RGBA,
             texwidth,
             texheight,
             0,
             GL_RGBA,
             GL_UNSIGNED_BYTE,
             bytes
             );

When I use gluBuild2DMipmaps it does work:

gluBuild2DMipmaps(GL_TEXTURE_2D,
                  3,
                  texwidth,
                  texheight,
                  GL_RGBA,
                  GL_UNSIGNED_BYTE,
                  bytes
                  );

What is the difference between the two, and what am I doing wrong?

Upvotes: 4

Views: 672

Answers (1)

Christian Rau
Christian Rau

Reputation: 45948

As it "works" with gluBuild2DMipmaps and not with glTexImage2D (texture is black, I guess), I would guess you have a mipmap based texture filter for the texture (which is also the default). Try calling

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  //or maybe GL_NEAREST

Always remember that glTexImage2D only sets an image for the selected mipmap level (in your case level 0) and if you use a mipmap-based minification filter (a constant with _MIPMAP_ in its name) the results of the texturing are only defined if you supply images to all mipmap levels (which gluBuild2DMipmaps does for you) or generate them automatically using either

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

before uplaoding the image or the newer

glGenerateMipmap(GL_TEXTURE_2D);

once it's uplaoded. The former requires OpenGL 1.4 and the latter requires FBO support, I think, and is also the non-deprectaed modern way to generate mipmaps. These two ways (especially the latter) are also prefered to the deprecated GLU functionality.

Upvotes: 3

Related Questions