Jump to content
  • Advertisement
Sign in to follow this  
Falken42

OpenGL palettes and opengl [Solved]

This topic is 4818 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

Hello all, I've been trying to get paletted textures to work under OpenGL for the past 4 hours and haven't had any luck so far. Googling turns up a lot of man pages for glColorTable and glTexture2D, but I haven't been able to find a single piece of sample code. My current hardware is an nVidia GeForce 2, which supports the GL_EXT_paletted_texture extention. Since newer GeForces don't support palettes directly, I plan to write a pixel shader to support them -- but I was trying to get older hardware to work right first. :) Anyway, my code looks something like this right now:
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_COLOR_TABLE);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glPixelTransferi(GL_INDEX_SHIFT, 8);
	glBindTexture(GL_TEXTURE_2D, texID);
	glColorTable(GL_COLOR_TABLE, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, texture);


The texture is a simple 128x128 8-bit texture with a 256 color RGBA palette. Unfortunately, all I see is a white polygon. I did notice that if I change internalFormat to 1 and format to GL_LUMINANCE, the texture actually shows up, but of course it's just black and white... so I know I'm close. Can anyone give me a tip on what I might be doing wrong here? Or perhaps, even a link to some sample code? Thanks! [ Edit: Added source tags :) ] [Edited by - bpoint on September 5, 2005 11:23:32 PM]

Share this post


Link to post
Share on other sites
Advertisement
Still searching, still poking around and no luck yet.

I have been able to determine that GL_COLOR_TABLE is not a valid glEnable flag, nor is it even necessary -- so I have removed that. I have also changed the target of glColorTable to be GL_TEXTURE_2D, but still no go.

Any thoughts? Anyone? Please? :)

Share this post


Link to post
Share on other sites
Okay, I've been able to determine that the initial code actually works (somewhat) after applying the small changes I described above.

If I force set the first entry in the palette to be RGBA (255,0,0,255), I get a solid red polygon. Just to make sure that my texture data isn't messed up (even though I used GL_LUMINANCE to test it), I force-wrote color indices 0-255 repeatedly in my texture. Still, the only color that is visible is red.

From what I can tell, OpenGL isn't using 256 entries, but using only one... or something like that. I've looked at glPixelMap and notice that there are settings to map indicies to individual R, G, B, and A components, and the fact that the initial size is only 1 leads me to believe that this might be part of the issue. But if I specify individual palette components through glPixelMap, glColorTable then becomes unnecessary... the logic doesn't quite make sense.

I also grabbed the source to PrBoom (OpenGL DOOM clone which uses paletted textures) and looked through their code to see if I could locate anything odd, but unfortunately not. They also don't use glPixelMap at all.

BTW, the glColorTable I use is actually glColorTableEXT. I notice that the function address for glColorTable and the EXT version are different -- and that the non-EXT version doesn't seem to work at all (no red polygon either).

Can anyone shed some light on what could be wrong in that little small code there is? I'm starting to run out of ideas... :(

Share this post


Link to post
Share on other sites
I think you want either:
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_TABLE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelTransferi(GL_INDEX_SHIFT, 8);
glBindTexture(GL_TEXTURE_2D, texID);
glColorTable(GL_COLOR_TABLE, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, texture);

or
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_TABLE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelTransferi(GL_INDEX_SHIFT, 8);
glBindTexture(GL_TEXTURE_2D, texID);
glColorTableEXT(GL_TEXTURE_2D, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, texture);

But I haven't actually tried either, so they might still be wrong. Regardless, you'll almost certainly get better performance by converting your indexed images to RGBA format yourself.

Enigma

Share this post


Link to post
Share on other sites
Enigma, thanks for the reply. Unfortunately, neither of the two suggestions worked... :(

glEnable(GL_COLOR_TABLE) doesn't appear to be valid. I get a GL_INVALID_ENUM error after calling it.

Quote:
Original post by Enigma
I think you want either:

glColorTable(GL_COLOR_TABLE, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, texture);



This made my test polygon black, even though I have the default color set to white... :/ I can't seem to get glColorTable (non-EXT) to do anything. If this did work, I believe what would happen here is OpenGL would probably unpack the color indexed texture into RGBA format when uploaded into video memory...

Quote:
or
glColorTableEXT(GL_TEXTURE_2D, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, texture);



This is basically what I've been able to get to work so far -- but the entire texture is mapped to a single color, which is the value of palette[0].

Quote:
Regardless, you'll almost certainly get better performance by converting your indexed images to RGBA format yourself.


The GeForce2 I have only has (heh) 64Mb of video RAM and I'm going over the limit already for a single frame when I use 24-bit RGB textures. I don't know how much a performance impact paletted textures have, but it's gotta be faster than having to page in/out 24-bit textures every frame. That's really the reason why I'm trying to get palettes to work.

Any other ideas? :) I'm gonna see if I can find some sample code somewhere on sourceforge...

Share this post


Link to post
Share on other sites
One more small thing:

As a test, I added the following code in my render loop, after calling glBindTexture():

static u8 val = 0;

palette[0].r = val++;
palette[0].g = 0;
palette[0].b = 0;
palette[0].a = 255;
glColorTableEXT(GL_TEXTURE_2D, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);


This causes the entire polygon to cycle through red color range, so it's obvious palettes are at least _working_. Now why the entire texture map is reallocated to only use palette entry zero is the only remaining mystery... :/

Share this post


Link to post
Share on other sites
FIXED IT! Finally...!

The culprit was the call to glPixelTransfer to set GL_INDEX_SHIFT. After commenting out that line, everything worked perfectly. Incidentally, GL_INDEX_SHIFT defaults to zero.

In the end, all that was necessary was:

glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, texID);
glColorTableEXT(GL_TEXTURE_2D, GL_RGBA8, 256, GL_RGBA, GL_UNSIGNED_BYTE, palette);
glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, width, height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, texture);


That's it!

I don't think too many people are using palettes nowadays, but even still, this might not be a bad thing to add to the FAQ... ;)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Unless you're working with 256 colors (why on Gods green earth are you recreating Wolf3D) you really don't need to do this. Plus from my understanding glPixelTransfer is SLOW. I could be wrong it's been 2 years and I don't remember using that function, just something about staying away from it... *shrug*.

Share this post


Link to post
Share on other sites
If you had even bothered to read the thread in its entirety before commenting, you would have already noticed that I'm running out of video memory, and textures are being paged in and out.

I don't know what kind of bad experience you had with glPixelTransfer, but I was able to bring my frame rate back from ~6fps (paging 24-bit RGB textures) to around 80fps (using 8-bit palettes). I'm quite pleased with the result.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!