I have tried to load an image into an OpenGL texture use stb_image
, and here is the code:
unsigned int texture;
glGenTextures(1, &texture);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
I think this code could work, but it give me a black texture, which should be the image.
After spending a lot of time on solving this problem, I finally found two ways to solve it:
- add
glGenerateMipmap(GL_TEXTURE_2D)
afterglTexImage2D()
- add
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
afterglBindTexture()
Either of these two methods will solve the problem, but I don't know why.
Can anyone explain how it works?