Page 1 of 2

OpenGL Help

Posted: Wed Oct 20, 2010 9:32 pm
by RandomDever
How do you do do pixel manipulation with OpenGL textures.
All I want to do is have a color key function so it removes all of a certain color and sets it alpha or whatever to 0.
I've read it has something to do with glGetTexImage() but most people skip 2D stuff and jump to 3D so IDK.

Re: OpenGL Help

Posted: Wed Oct 20, 2010 9:38 pm
by epicasian
I *think* you have to color key it using SDL (if that's the library you're using) and then feed the pixel data into an OpenGL texture.

I'm not sure, but the code at the bottom could probably help:
http://www.gamedev.net/community/forums ... _id=517739

Re: OpenGL Help

Posted: Wed Oct 20, 2010 10:20 pm
by qpHalcy0n
OpenGL has no color keying.

You can either grab the image from the GL after it has been loaded. Or you can do it as a preprocessing step. Either way, if you're not using SDL or SFML or something like that, there is no "function" that does this for you.
You have to write it yourself.

glGetTexImage does indeed give you the raw pixmap. From there it is up to you to do the processing required to make this work. It will require you to allocate enough space for some alpha bits (depending on the pixel format), iterate through and write out the new pixmap, then re-upload it. Doing this as a pre-processing step (before you load it to begin with) cuts out some steps thus making it a bit more of a streamlined process.

Re: OpenGL Help

Posted: Wed Oct 20, 2010 11:59 pm
by MadPumpkin
One simple way that I used to use, before using transparent textures, so that I didn't NEED to color key is called discarding. Basically you tell OpenGL not to write a pixel to the screen.
By asking nicely what color the pixel is first, you can tell it to write it how you want it to.

WARNING: I'm not 100% sure this will work, but pretty damn. I've only ever used it for 3D objects

Code: Select all

sampler2D Tex;
vec2 TexCoords;

int main()
{
     vec4 ColorKey = texture2D(Tex, TexCoords); 
             
     if (ColorKey.rgb == vec3(*Your R, G, B goes here*))
          discard; 
   
     gl_FragColor = color;
}
Also note that if this works in may leave an undesired edge around your object, meaning if you are discarding cyan, then it will remove all cyan EXCEPT what is touching the pixels NOT to discard.
If this is the case, you should be able to fix it with these as your texture params:

Code: Select all

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

Re: OpenGL Help

Posted: Thu Oct 21, 2010 5:24 am
by N64vSNES
If your using SDL then I normaly just use SDL_SetColorkey()

Like so

Code: Select all


SDL_Surface *TMP = IMG_Load( TheImage ) // Load the image

SDL_SetColorKey ( TMP->format , SDL_SRCCOLORKEY, SDL_MapRGB ( TMP->format, 0xff, 0x00, 0xff ) ); // set the color key

SDL_Surface *Surface = SDL_DisplayAlphaFormat( TMP ); // Transfer the surface over with its current alpha format

SDL_FreeSurface ( TMP ); // We no longer need the old surface so free it


Re: OpenGL Help

Posted: Thu Oct 21, 2010 4:12 pm
by GroundUpEngine
OpenGL alpha blending ;)

Re: OpenGL Help

Posted: Thu Oct 21, 2010 4:52 pm
by Ginto8
GroundUpEngine wrote:OpenGL alpha blending ;)
^this.

However, if you want to get more into things like rendering to a texture, that's possible too, it's just more complicated and I can't really think of many applications offhand.

Re: OpenGL Help

Posted: Sun Oct 24, 2010 12:06 am
by RandomDever
epicasian wrote:I *think* you have to color key it using SDL (if that's the library you're using) and then feed the pixel data into an OpenGL texture.

I'm not sure, but the code at the bottom could probably help:
http://www.gamedev.net/community/forums ... _id=517739
Tried it didn't work.
Tried blend, didn't work.

All I want to do is load a .png with alpha and render it as a texture on a quad.

PS yes this is the second time I've tried to post this, that's why it was so brief.

Re: OpenGL Help

Posted: Sun Oct 24, 2010 11:13 am
by qpHalcy0n
Well then you need to post what you've got so far. Doing image preprocessing yourself isn't so easy if it's your first stab at it. There may be an issue with your loader, you may be passing mismatched pixel formats, you may have the blend mode set incorrectly for what you want to do...who knows.

Need more info. There isn't a one or two line solution for your problem, sorry :]

Re: OpenGL Help

Posted: Sun Oct 24, 2010 2:09 pm
by Ginto8
RandomDever wrote:
epicasian wrote:I *think* you have to color key it using SDL (if that's the library you're using) and then feed the pixel data into an OpenGL texture.

I'm not sure, but the code at the bottom could probably help:
http://www.gamedev.net/community/forums ... _id=517739
Tried it didn't work.
Tried blend, didn't work.

All I want to do is load a .png with alpha and render it as a texture on a quad.

PS yes this is the second time I've tried to post this, that's why it was so brief.
I have 5 bucks here saying you colorkeyed it then passed surface->pixels. Now, this won't work for 1 main reason: colorkeying does NOT alpha-blend pixels of that color. All that happens is, during blitting, if a pixel has that color, SDL ignores it and doesn't use it during blitting. What you need is a pre-alpha blended image, load it, and just pass surface->pixels straight on to glTexImage2D. This should give you the results you're looking for. If not, post some code.

Re: OpenGL Help

Posted: Sun Oct 24, 2010 9:17 pm
by RandomDever
This post was a glitch.

Re: OpenGL Help

Posted: Sun Oct 24, 2010 9:21 pm
by RandomDever
Load Texture:

Code: Select all

GLtexture2d Video::LoadTexture( const char *filename )
{
	//Create the nessasary variables
	SDL_Surface *surface;
    GLuint textureid;
    int mode;
	GLtexture2d texture;

	//Load the specified image into the SDL_Surface *surface variable
    surface = IMG_Load( filename );
	//texture.filename = filename;

	//Color key the texture
	/*for( int x = 0; x <= surface->w; x++ )
	{
		for( int y = 0; y <= surface->h; y++ )
		{
			SDL_Color pixel = TranslateColor( GetPixel( surface, x, y ) );
			if( pixel.r == keyR && pixel.g == keyG && pixel.b == keyB )
			{
				Uint32 pixel32 = SDL_MapRGBA( surface->format, 0, 0, 255, 0 );
				SetPixel( surface, x, y, pixel32 );
			}
		}
	}*/

	//If there are only 3 Bytes ( 24 bits ) per pixel
    if( surface->format->BytesPerPixel == 3 )
	{
		//Set the OpenGL color schema to Red Green Blue 24bit
        mode = GL_RGB;
    }
	//If there are 4 Bytes ( 32 bits ) per pixel
	else if( surface->format->BytesPerPixel == 4 )
	{
		//Set the OpenGL color schema to Red Green Blue Alpha 32bit
		mode = GL_RGBA;

    }
	else
	{
		//Free unnessasary memory
        SDL_FreeSurface( surface );
    }
	
	//Set the textures width and height variables
	texture.w = (float)surface->w;
	texture.h = (float)surface->h;
    
	//Generate a texture ID number
    glGenTextures( 1, &textureid );

	//If this texture has been loaded before delete the curent texture before making a new one
	glDeleteTextures( 1, &texture.ID );

	//Bind that ID number to a type ( I guess )
    glBindTexture( GL_TEXTURE_2D, textureid );

	//No clue
	//TODO:Lookup glTexImage2d()
    glTexImage2D( GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels );

	//Set the parameters fot the texture
    glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR );

	//Clean up
    SDL_FreeSurface( surface );

	//Set the texture's ID number to the correct texture ID
	texture.ID = textureid;

	//Set the textures properties to default
	texture.scale = 1;
	texture.zAngle = 0;

	////Add a pointer to the current texture to thhe textures list
	//textures.push_back( &texture );

	//Return the texture
    return texture;
}
Draw Texture:

Code: Select all

void Video::DrawTexture( float x, float y, GLtexture2d texture )
{
	//Get ready for the drawing
	//glBindTexture( GL_TEXTURE_2D, texture.ID );
    glEnable( GL_TEXTURE_2D );
	glTranslatef( x + ( texture.w / 2 ), y  + ( texture.h / 2 ), 0 );
	
	//Make ABSOLUTELY SURE THAT THERE ARE NO COLOR SCREW UPS
	glColor3f( 1, 1, 1 );

	//Add the special properties
	glScalef( texture.scale, texture.scale, texture.scale );
	glRotatef( texture.zAngle, 0, 0, 1.0 );

    //Make a rectangle
    glBegin( GL_QUADS );

    //Top Left
    glTexCoord2i( 0, 0 );
    glVertex3f( -texture.w / 2, -texture.h / 2, 0 );

    //Top Right
    glTexCoord2i( 1, 0 );
    glVertex3f( texture.w / 2, -texture.h / 2, 0 );

    //Bottom Right
    glTexCoord2i( 1, 1 );
    glVertex3f( texture.w / 2, texture.h / 2, 0 );

    //Bottom Left
    glTexCoord2i( 0, 1 );
    glVertex3f( -texture.w / 2, texture.h / 2, 0 );

	//Finish the drawing
    glEnd();
    glDisable( GL_TEXTURE_2D );
}
EDIT: "I have 5 bucks here saying you colorkeyed it then passed surface->pixels." so where's my 5 bucks Ginto?

Re: OpenGL Help

Posted: Sun Oct 24, 2010 9:31 pm
by RandomDever
Ginto8 wrote:
GroundUpEngine wrote:OpenGL alpha blending ;)
^this.

However, if you want to get more into things like rendering to a texture, that's possible too, it's just more complicated and I can't really think of many applications offhand.
Well I'm not entirely sure how blend works but if what I read around the internet was true, it doesn't work.

Re: OpenGL Help

Posted: Sun Oct 24, 2010 10:24 pm
by Ginto8
RandomDever wrote:
Ginto8 wrote:
GroundUpEngine wrote:OpenGL alpha blending ;)
^this.

However, if you want to get more into things like rendering to a texture, that's possible too, it's just more complicated and I can't really think of many applications offhand.
Well I'm not entirely sure how blend works but if what I read around the internet was true, it doesn't work.
You must be in the wrong internet.
RandomDever wrote:

Code: Select all

[/quote]
... and what exactly is the issue? is it turning purple? Is it simply not showing up? Is it following you home in a white van at night? I need info.
Though why you need to colorkey it in the first place (instead of just having alpha in the surphace already) is a bit beyond me.
[quote="RandomDever"]EDIT: "I have 5 bucks here saying you colorkeyed it then passed surface->pixels." so where's my 5 bucks Ginto?[/quote]
Hey, just cuz I have a five dollar bill that can talk doesn't mean it becomes yours.

Re: OpenGL Help

Posted: Mon Oct 25, 2010 12:44 am
by RandomDever
It has a black background instead of a transparent one.
Well what I did is in Paint .NET I have a background color of a fully transparent black.
And I'm not color keying I have a .png that has transparency but it won't render correctly.

EDIT: Is it a problem with Paint .NET?