PDA

View Full Version : Palettes?



Drenn
07-01-2011, 11:43 AM
Hi! I'm writing a gameboy emulator. So far it's going smoothly, and I decided to translate my SDL video functions to openGL for more speed and flexibility. I have a problem I couldn't solve in SDL or opengl; images need to be palettized. The graphics look fine, except for the fact that colors are fixed, so they are sometimes wrong. I read a bit about palettes in opengl, but they seem to be deprecated and not supported at all with ATI cards (what I have).
Speed is key, since this is an emulator, but any working solution would be helpful for now.

aqnuep
07-01-2011, 12:08 PM
Paletted textures are no longer supported on ATI cards (I think the're not anymore supported by newer NVIDIA cards either). You should rather use standard 8 bit per component RGBA textures. On modern cards this additional overhead shouldn't be an issue for a gameboy emulator, actually everything else would be suboptimal.

Drenn
07-01-2011, 12:28 PM
Pardon my... newbiness, but I don't understand. I think I am using 32-bit RGBA textures, but how do I select different colours every time it's mapped? Or do you mean I should regenerate the image when needed? I just started with opengl yesterday :p

aqnuep
07-01-2011, 02:07 PM
Ah, I see, so you want somehow to be able to use the exact same 32 bit RGBA texture with different palettes. Well, the easiest (but obviously not the most memory optimal) solution would be simply to regenerate the image.

There are actually many alternatives, but it greatly depends on what is your target hardware (GPU generation) and how much run-time performance and/or memory footprint is an issue.

V-man
07-01-2011, 02:50 PM
The modern approach to graphics programming is to use shaders.
For doing pallette textures, you need 2 textures. Your pallette (index) texture and another texture which is your table of real RGBA values.
http://www.opengl.org/wiki/Common_Mistakes#Paletted_textures

I'm assuming the "But there are still a lot of video cards around that don't support shaders," refers to Intel integrated graphics. In fact, the latest versions of Intel don't support it either. You have to go back to the days of Intel 810.

Drenn
07-01-2011, 07:50 PM
Do I need any #includes other than "GL/GL.h" to use shaders? I simply get "sampler2D was not declared in this scope" and the like.

BionicBytes
07-02-2011, 02:33 AM
sampler2D was not declared in this scope

Sampler2D is a GLSL command which has nothing to do with GL/GL.h C code.

GLSL commands/tokens are compiled by the GLSL compiler which is included as part of your video card drivers. Your text file, which has all the shader code, is submitted to OpenGL for compilation by feeding the contents of the shader text file to the appropriate OpenGL function call. The OpenGL APIs expect the shader text file to be read into memory as an array of text, which is then passed to glShaderSource and then compiled with glCompileShader.

Drenn
07-02-2011, 09:35 AM
I'm starting to get somewhere - I had to #include "GL/glext.h" and add -DGL_GLEXT_PROTOTYPES to my makefile to use shader functions. I don't know where to go from here, though. I loosely followed a tutorial online and ended up with this:

GLenum my_program;
GLenum my_fragment_shader;

// Create Shader And Program Objects
my_program = glCreateProgram();
my_fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);

// Load Shader Sources
glShaderSource(my_fragment_shader, 1, &shadersrc, NULL);

// Compile The Shaders
glCompileShader(my_fragment_shader);

// Attach The Shader Objects To The Program Object
glAttachShader(my_program, my_fragment_shader);

// Link The Program Object
glLinkProgram(my_program);

// Use The Program Object Instead Of Fixed Function OpenGL
glUseProgram(my_program);

I used the GLSL code from the wiki's common mistakes page.

//Fragment shader
uniform sampler2D ColorTable; //256 x 1 pixels
uniform sampler2D MyIndexTexture;
varying vec2 TexCoord0;
void main()
{
//What color do we want to index?
vec4 myindex = texture2D(MyIndexTexture, TexCoord0);
//Do a dependency texture read
vec4 texel = texture2D(ColorTable, myindex.xy);
gl_FragColor = texel; //Output the color
}
So, how do I get myIndexTexture and ColorTable to correspond to textures in my program?
...I'm probably overdoing this, the gameboy only has 4 colours :P

V-man
07-02-2011, 07:44 PM
Did you look at tutorials such as
http://www.lighthouse3d.com/tutorials/
and
http://www.lighthouse3d.com/tutorials/glsl-tutorial/simple-texture/

They are old but do the job.
That's all GLSL 1.10.

There is this which is modern and very extensive explainations
http://www.arcsynthesis.org/gltut/