Transparency help [Solved, I've been stupid]

Started by
27 comments, last by horizon981 17 years, 9 months ago
Yes, I use IMG_Load() for my textures. First of all, I wrote the following function to reverse the pixels (that are stored differently than how opengl requires for textures):
SDL_Surface *FlipSurfaceV(SDL_Surface *bitmap){	if(!bitmap)return NULL;	SDL_Surface *temp=SDL_CreateRGBSurface(0,bitmap->w,bitmap->h,		bitmap->format->BitsPerPixel,bitmap->format->Rmask,bitmap->format->Gmask,		bitmap->format->Bmask,bitmap->format->Amask);	if(!temp)return NULL;	unsigned int rowsize = bitmap->w*(bitmap->format->BitsPerPixel/8);	char* src = (char*)bitmap->pixels;	char* dest = ((char*)temp->pixels)+(temp->h-1)*rowsize;	for(int i=0;i<bitmap->h;i++)	{	    memcpy(dest, src, rowsize);	    src += rowsize;	    dest -= rowsize;	}	return temp;}

Then the following code loads the image and creates the texture:
                string file = map.name;                SDL_Surface* mm = IMG_Load(file.c_str());                SDL_Surface* m = FlipSurfaceV(mm);                unsigned int t;                glGenTextures(1, &t);                glBindTexture(GL_TEXTURE_2D, t);                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m->w, m->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, m->pixels);                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
Advertisement
[crying] [crying] [crying][crying] [crying] [crying][crying] [crying] [crying]
There seems to be no end to my problems!!!

If I use the code provided by you above, my compiler tells me GL_BGRA is undeclared.

So, I used this Class to load the TGA texture in RGBA mode. To my surprise, the brown background of the image has returned!!! However, when I view that image in GIMP, it has no background. In IrfanView however, I see a brown background!!! In Nero PhotoSnap Viewer, it has a white background!!
What's going on?????

Will somebody please help me out!!

[Edited by - horizon981 on July 4, 2006 3:21:21 AM]
I think that GL_BGRA is defined in glext.h, the header for the opengl extensions.
Well, BGRA simply leads to a color format change. The damn image doesn't seem to be losing its background.
Are you sure that you are correctly setting opengl parameters for blending? Try with this nehe lesson about blending and check that you're doing the right operations.
so you are gonna use opengl. then may i suggest using allegrogl for your sprites? it has masked blitting already implemented. it even defines what the mask color is already -- bright pink (255, 0, 255).

you can check here : http://allegrogl.sourceforge.net/docs/main.html
that is the allegrogl page.

and then here : http://alleg.sourceforge.net/onlinedocs/en/allegro.html
is the allegro page.

but, if you like manually forcing everything as you are currently doing it, have fun!
Ok, first, telling us there is no end to your problems and that the "damn thing" won't lose it's background won't help us help you.

First, start a clean project. Write only the most basic of stuff, then make your .tga. Load it, and display it. Work with that only until it works out. THEN you take what worked from the small project and put it carefully in your big project.

And DO NOT flip anything or do anything. Once you get it to display with a transparent background, be it flipped, toyed with, fubar or not you know you loaded it right. Then play with it until it displays correctly and THEN put it into your project.

Good luck.

BTW, please no "urgent help required"... We are not "required" to give you any kind of help. We just feel like helping others who are in trouble.

Not to be rude, but...
you could just add the alpha channel for a certain color like magneta, you can do this by modifying your Texture loading code heres what you would have to add
this is done in java, but you can easily port it.
        for(int s=0, d=0; s < dat.length; s+=3, d+=4) // marches through all the bytes of the image        {            scratch.put(d, dat);     <br>            scratch.put(d+<span class="cpp-number">1</span>, dat[s+<span class="cpp-number">1</span>]);     <br>            scratch.put(d+<span class="cpp-number">2</span>, dat[s+<span class="cpp-number">2</span>]);<br>            <span class="cpp-keyword">if</span>(dat == -<span class="cpp-number">1</span> &amp;&amp; dat[s+<span class="cpp-number">1</span>] == <span class="cpp-number">0</span> &amp;&amp; dat[s+<span class="cpp-number">2</span>] == -<span class="cpp-number">1</span>) <span class="cpp-comment">// checks if the color is (255, 0, 255) also known as magneta</span><br>                scratch.put(d+<span class="cpp-number">3</span>, (byte)<span class="cpp-number">0</span>); <span class="cpp-comment">// if it is give it a value of 0 in the alpha channel </span><br>            <span class="cpp-keyword">else</span><br>                scratch.put(d+<span class="cpp-number">3</span>, (byte)-<span class="cpp-number">1</span>); <span class="cpp-comment">// if not give it a value of 255 in the alpha channel</span><br>        }<br><br></pre></div><!–ENDSCRIPT–><br><br>thats to prepare the alpha channel for craeting the texture then you create it like this.<br><br>just notice how a few things changed when trying to create the texture like the number 4,  which means 4 bytes hence RGBA instead of 3 bytes (RGB)<br>and at the third to last parameter its GL_RGBA telling it that this has a alpha channel<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br>        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, <span class="cpp-number">0</span>, <span class="cpp-number">4</span>, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), <br>                IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), <span class="cpp-number">0</span>, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);    <br><br></pre></div><!–ENDSCRIPT–><br><br>and when you want to apply a texture with transparency just do this.<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br>        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);<br>        GL11.glEnable(GL11.GL_ALPHA_TEST);<br>        GL11.glAlphaFunc(GL11.GL_GREATER, <span class="cpp-number">0</span>);            <br>        Texture.Apply(Texture);<br><br></pre></div><!–ENDSCRIPT–><br><br>youll have to port this code though if your not using LWJGL + Java of course but its real easy all you would have to do it is take away the GL11. from the start of everything and its perfect OpenGL code.  Hope this helps
Quote:Original post by The_Neverending_Loop
just notice how a few things changed when trying to create the texture like the number 4, which means 4 bytes hence RGBA instead of 3 bytes (RGB)
and at the third to last parameter its GL_RGBA telling it that this has a alpha channel and when you want to apply a texture with transparency just do this.


Bingo!!
I am an IDIOT!! Yes, I repeat, I am an Idiot!!
And you should change your name, because because of you, my LOOP has ended!!

Thanks to ALL of you who helped, I learnt a lot in the process.

[smile][smile][smile][smile][smile][smile][smile][smile][smile][smile][smile][smile]

This topic is closed to new replies.

Advertisement