Color swapping in textures [SDL + OpenGL]

Started by
2 comments, last by jjramone13 15 years, 2 months ago
Pretty simple question with what I hope will be a simple answer: How would I go about replacing a color, say (255, 255, 255), with another color (100, 100, 255) through the engine?
Advertisement
You're not very specific about the question; however, if you're looking to replace every instance of a color in a texture with another color, it's a fairly simple problem.

First, you need to get all the texture data from opengl.
Second, iterate through the copied texture data replacing intances of color A with color B.
Finally, replace the texture data with your data.

If you need help getting started on texture manipulation, take a look at PhoenixGL's texture class:

http://code.google.com/p/phoenixgl/source/browse/branches/0.3/libPhoenixGL/PhTexture.cpp

It shows how to get texture data (lockTexture), read it (getPixel), write it (setPixel), and write it back to opengl (unlockTexture).

Example for you would be:

texture->lockTexture();
for( i = 0; i < texture->width(); i++ ){
for( j = 0; j < texture->height(); j++){
if( texture->getPixel(i,j) == PhColor( 255,255,255) ) texture->setPixel( 100,100,255 );
}
}
texture->unlockTexture();

PhoenixGL is an OpenGL 2d engine that started as an SDL wrapper. You might find lots of useful code there.
That looks like it'll pretty much do it. Thanks a million.

EDIT: That method is acceptable for what I'm looking for, but is there any way to change the color on say "entity" level rather than the "texture" level? Meaning, modifying the color of the on-screen sprite itself rather than the texture behind it. I have multiple entities that use the same texture as a pointer, and it would be neat if I didn't have to create a new texture object each time.
Replacing one single color with another on the vertex level isn't really possible. However you can "colorize" on the vertex level just by calling glColor before each vertex (or using a color array, depending on your method of rendering).

This topic is closed to new replies.

Advertisement