stb_image

May 5, 2020 at 6:01pm
I am using the famous stb_image to load some textures but I get this https://imgur.com/AtMQ6Gn instead of this https://imgur.com/hs8BUnq, any idea? I tried loading the image with the flag STBI_rgb as well as STBI_rgb_alpha and I have even tried to calculate the stride based on the width but that only made things even worse
Last edited on May 5, 2020 at 6:02pm
May 5, 2020 at 6:12pm
tried flag STBI_rgb as well as STBI_rgb_alpha
Can you show the actual line of code where you call it (and the variables you declare for the call).

And also show where you create the texture, assuming you're using some graphics API (DX? OpenGL?)
Last edited on May 5, 2020 at 6:13pm
May 5, 2020 at 6:19pm
yeah sure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void LoadTexture(const char* filename)
	{

		int width, height, nrChannels;
		unsigned char* data = stbi_load(filename, &width, &height, &nrChannels, STBI_rgb);
		if (data)
		{
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		}
		else
		{
			std::cout << "Failed to load texture" << std::endl;
		}
		stbi_image_free(data);
	}


EDIT: I feel like there is a problem with that width, maybe it has to be recalculated somehow
Last edited on May 5, 2020 at 6:25pm
May 5, 2020 at 7:35pm
To be honest, that looks OK to me. Just for fun, what happens if you use STBI_rgb_alpha and GL_RGBA?

If it's an issue with width alignment, maybe try calling glPixelStorei(GL_UNPACK_ALIGNMENT, 1); first thing within the if (data).

Also, what is the file format of filename?
Last edited on May 5, 2020 at 7:41pm
May 5, 2020 at 8:13pm
that worked! thanks
May 5, 2020 at 8:23pm
an additional question, do you know how to apply such skybox textures to a cube, will it work if I use these texcoords { x: 0.0, 0.25, 0.5, 0.75, 1.0 } { y: 0.0, 0.33, 0.66, 1.0 }
Topic archived. No new replies allowed.