OpenGL Help

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

OpenGL Help

Post 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.
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

Re: OpenGL Help

Post 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
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: OpenGL Help

Post 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.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: OpenGL Help

Post 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);
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Help

Post 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

User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: OpenGL Help

Post by GroundUpEngine »

OpenGL alpha blending ;)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: OpenGL Help

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Help

Post 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.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: OpenGL Help

Post 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 :]
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: OpenGL Help

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Help

Post by RandomDever »

This post was a glitch.
Last edited by RandomDever on Sun Oct 24, 2010 9:21 pm, edited 1 time in total.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Help

Post 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?
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Help

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: OpenGL Help

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: OpenGL Help

Post 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?
Post Reply