Paletted texture appears in greyscale.

Hi I need some help with my OpenGL texture loading code with SDL.
The image I'm currently loading is in a paletted format(Files below).
Maybe something is missing in the gl setup functions in context.cpp.

The code snippet shows the code for the paletted texture's gl pixel transfer/map commands in LoadGLTexture(...) in textures.cpp.
if (palette)	// If the image is paletted:
{
	type = (bipp == 8) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
	format = GL_COLOR_INDEX;
	internalFormat = bypp;

	GLsizei mapSize = palette->ncolors;

	GLfloat *Rmap = new GLfloat[mapSize];
	GLfloat *Gmap = new GLfloat[mapSize];
	GLfloat *Bmap = new GLfloat[mapSize];
	GLfloat *Amap = new GLfloat[mapSize];

	for (int i = 0; i < mapSize; i++)
	{
		SDL_Color *pColor = palette->colors + i;
		Rmap[i] = (GLfloat)pColor->r / mapSize;
		Gmap[i] = (GLfloat)pColor->g / mapSize;
		Bmap[i] = (GLfloat)pColor->b / mapSize;
		Amap[i] = (GLfloat)pColor->unused / mapSize;
	}

		glPixelMapfv(GL_PIXEL_MAP_I_TO_R,mapSize,Rmap);
		glPixelMapfv(GL_PIXEL_MAP_I_TO_G,mapSize,Gmap);
		glPixelMapfv(GL_PIXEL_MAP_I_TO_B,mapSize,Bmap);
		glPixelMapfv(GL_PIXEL_MAP_I_TO_A,mapSize,Amap);

		glPixelTransferi(GL_MAP_COLOR,GL_TRUE);

		delete [] Rmap;
		delete [] Gmap;
		delete [] Bmap;
		delete [] Amap;
	}

Open in new window

Grass.png
textures.cpp
context.cpp
test.cpp
AntonKarlssonAsked:
Who is Participating?
 
AntonKarlssonConnect With a Mentor Author Commented:
I had no patience waiting so I solved it myself now.

It seemed two faults:

1. I realized that internalFormat should be 4 instead of bypp(1).
And:
2. That it should be 256 instead of mapSize(128).
if (palette)	// If the image is paletted:
{
	type = (bipp == 8) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
	format = GL_COLOR_INDEX;
	internalFormat = 4;//bypp;

	GLsizei mapSize = palette->ncolors;
	
	GLfloat *Rmap = new GLfloat[mapSize];
	GLfloat *Gmap = new GLfloat[mapSize];
	GLfloat *Bmap = new GLfloat[mapSize];
	GLfloat *Amap = new GLfloat[mapSize];

	for (int i = 0; i < mapSize; i++)
	{
		SDL_Color *pColor = palette->colors + i;

		Rmap[i] = (GLfloat)pColor->r / 256;//mapSize;
		Gmap[i] = (GLfloat)pColor->g / 256;//mapSize;
		Bmap[i] = (GLfloat)pColor->b / 256;//mapSize;
		Amap[i] = (GLfloat)pColor->unused / 256;//mapSize;
	}

	glPixelMapfv(GL_PIXEL_MAP_I_TO_R,mapSize,Rmap);
	glPixelMapfv(GL_PIXEL_MAP_I_TO_G,mapSize,Gmap);
	glPixelMapfv(GL_PIXEL_MAP_I_TO_B,mapSize,Bmap);
	glPixelMapfv(GL_PIXEL_MAP_I_TO_A,mapSize,Amap);

	glPixelTransferi(GL_MAP_COLOR,GL_TRUE);

	delete [] Rmap;
	delete [] Gmap;
	delete [] Bmap;
	delete [] Amap;
}

Open in new window

Correct.jpg
0
 
AntonKarlssonAuthor Commented:
It looks like this:
Result.jpg
0
 
AntonKarlssonAuthor Commented:
It seems it only reads the red color map for black to white.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.