Texture Loading (on Mac)

Started by
6 comments, last by MARS_999 15 years, 11 months ago
I am trying to convert an SDL/OpenGL application of mine (that I've been developing on Windows) to Mac (using XCode). I'm new to C++ development on the Mac. The code works fine on Windows, but on the Mac, all of my textures are rendering as white. The images are loading fine. I am using SDL to load the images, and then converting them to OpenGL textures. Below I will post my image loading function. The original code is based on a post from the SDL mailing list, by the lead author of SDL. I THINK this is where the problem is, as I can't think of another reason why I would be getting white textures :P
void Renderer::loadImage(const std::string &fileName, Image *img)
{
	SDL_Surface *SDLsurface = IMG_Load(std::string("data/" + fileName).c_str());

	if (SDLsurface == NULL)
		EXCEPTION(Exception::ERR_FILE_NOT_FOUND, "loadImage()", "Error loading image: " + fileName);

	SDL_Surface *OGLsurface;

	OGLsurface = SDL_CreateRGBSurface(
		SDL_SWSURFACE,
		SDLsurface->w, SDLsurface->h,
		32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
		0x000000FF,
		0x0000FF00,
		0x00FF0000,
		0xFF000000
#else
		0xFF000000,
		0x00FF0000,
		0x0000FF00,
		0x000000FF
#endif
		);

	if (OGLsurface == NULL)
		EXCEPTION(Exception::ERR_INTERNAL_ERROR, "loadImage()", "RGB surface creation error");

	// Copy the surface into the GL texture image.
	SDL_Rect area;
	area.x = 0;
	area.y = 0;
	area.w = SDLsurface->w;
	area.h = SDLsurface->h;
	SDL_BlitSurface(SDLsurface, &area, OGLsurface, &area);

	uint numPixels = OGLsurface->w * OGLsurface->h;
	for (uint i = 0; i < numPixels; ++i)
	{
		uint index = i * 4;

		Uint8 r = static_cast<Uint8*>(OGLsurface->pixels)[index];
		Uint8 g = static_cast<Uint8*>(OGLsurface->pixels)[index + 1];
		Uint8 b = static_cast<Uint8*>(OGLsurface->pixels)[index + 2];
		Uint8 &a = static_cast<Uint8*>(OGLsurface->pixels)[index + 3];

		// Cyan goes to full transparency.
		if (r == 0 && g == 255 && b == 255)
			a = 0;
	}
	
	// Create an OpenGL texture for the image.
	glGenTextures(1, &static_cast<GLuint>(img->id));
	glBindTexture(GL_TEXTURE_2D, img->id);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D,
		0,
		GL_RGBA,
		OGLsurface->w, OGLsurface->h,
		0,
		GL_RGBA,
		GL_UNSIGNED_BYTE,
		OGLsurface->pixels);

	// Write out.
	img->w = OGLsurface->w;
	img->h = OGLsurface->h;

	// Cleanup.
	SDL_FreeSurface(OGLsurface);
	SDL_FreeSurface(SDLsurface);
}

Any other ideas as to why I am getting white textures on the Mac? btw, I am using XCode 2.5, and this is on OS X 10.4, PowerPC, NOT intel, so it should be big endian). Thanks!
Advertisement
Did you turn on texture mapping? or are you using shaders?
Yep, textures are enabled. I think the initialization should be correct, because it works flawless on Windows. I think the problem is related to endian-ness or some SDL conversion file format issue. Hmmmm...
Well if you are on Mac PPC then yes there is an endianess issue. SDL has support for this in the lib some place. I tried to get it to work back in my Mac days and never could find a correct way to do it. I ended up just using .tga files and doing a byte swap inline function or macro on the data coming in. I would recommend you do that also, just check for PPC CPU with a pre-processor #if and if PPC just do a byte swap on the imageData and should be ok.
I'm using .png. I thought my code should account for the endian issue. I switch the byte order appropriately. I will continue to play around with it.
Just figured it out: non-power of 2 textures (this Mac has an older version of OpenGL). Woops.

Thanks for your help :)
I know this is somewhat off-topic, but do newer OpenGLs allow non power of 2 images? Mine doesn't (I've run into that snag) Just curious.
Holy crap, you can read!
Quote:Original post by PCN
I know this is somewhat off-topic, but do newer OpenGLs allow non power of 2 images? Mine doesn't (I've run into that snag) Just curious.


GL2.0 is needed to use NPOT

This topic is closed to new replies.

Advertisement