Advertisement Jump to content
Sign in to follow this  
Silvo

OpenGL Texture Loading with Corona and OpenGL

This topic is 4283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

G'day. I am having trouble getting my texture loader to work properly (properly = at all). The issue is that the texture is not loaded, but is just a white box. The relevant source code is:
		GLuint id = 0;

		corona::Image* image = corona::OpenImage(name.c_str(), corona::PF_R8G8B8A8);
		if (!image) {
			ReportMinorError("Unable to load image: " + name);
			glBindTexture(GL_TEXTURE_2D, 0);
			return;
		}

		int width = image->getWidth();
		int height = image->getHeight();
		unsigned char* pixels = (unsigned char*) image->getPixels();

		glGenTextures(1, &id);
		glBindTexture(GL_TEXTURE_2D, id);
		glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		::Manager::AddTexture(name, id);
		glBindTexture(GL_TEXTURE_2D, id);
		delete image;

I think there is something wrong with thsi line: glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); I don't really know what should be where the 4 is and the GL_RGBA is.

Share this post


Link to post
Share on other sites
Advertisement
The 4 refers to how many components the image contains (i.e. R, G, B, A) If you have an image which has an Alpha channel, for example a 32-bit TGA, then you have this as 4, if it's just a BMP then it'll be 3 (RGB).

The GL_RGBA tells OpenGL how to read the 'pixels' data. Typically this is stored in little-endian format so is usually GL_BGRext. Since Bitmaps store their pixel data backwards, as do TGA's.

Basically:
-If you're using 24-Bit BMP's or TGA's then use
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_BGRExt, GL_UNSIGNED_BYTE, pixels);
-If you're using 32-Bit TGA's then use GL_BGRAExt
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_BGRAExt, GL_UNSIGNED_BYTE, pixels);

Share this post


Link to post
Share on other sites
Thanks for that. I have it working now. It turns out that corona will convert the pixel format to the one specified (PF_R8G8B8A8 = 32 bit RGBA). What I had to do was change it to:
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA_EXT, GL_UNSIGNED_BYTE, pixels);
regardless of whether it was a bitmap or something else, because I specified when using corona::OpenImage() to use RGBA format. Otherwise it ends up as a mess of colours. Quite pretty.

The main issue (the reason for the white box) was that my image's width and height were not powers of two. Oops!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!