Archived

This topic is now archived and is closed to further replies.

graveyard filla

problem with drawing pieces of transparent textures (OpenGL)

Recommended Posts

high, i have a tilemap image (its like a checkerboard image of my tiles) which i use as a texture, then i "slice" this texture to map my quads (individual tiles) with. basically, i have one big tilemap image and then i slice it into pieces and use that piece as the texture for a single tile. heres the problem: i cant get transparency to work while slicing the texture.... transparency works fine if i map the ENTIRE texture to the quad, but if i only map a portion of the quad (via glTextCoord()), the transparency stops working. heres what im talking about, firstly, heres what the tilemap looks like: if you look at the bottom row of tiles, you will see that they have transparent pieces to them (they are edges of buildings as you are about to see) now heres what this looks like in game do you see what the problem is? if you look at the bottom and top of the building, where i used the tiles which had transparent pieces, the transparent part is showing up!! even more weird, is its showing up BLACK! why is it black, and not transparent? i dont think its something weird im doing with my code, if you notice the sprite next to the building, his transparency is working! also, i wanted to make sure its not my image loading code, so i tried mapping the ENTIRE tilemap to my players quad... look what that looks like.. if you look at the bottom row of tiles, they ARE transparent! so why will transparency work if i map an ENTIRE texture to a quad, but it craps out on me and gives me black when i try to map a piece of a texture to a quad ??? anyone experiance this before? any help is greatly appreciated!!! (will post code if its needed) [edited by - graveyard filla on May 27, 2004 4:04:38 PM] [edited by - graveyard filla on May 27, 2004 5:55:30 PM] [edited by - graveyard filla on May 27, 2004 5:59:32 PM]

Share this post


Link to post
Share on other sites
Willbo    139
There could be some diffrent things making your quad not showing up transparent. You say it works when you map the texture to your player tile.
Are you rendering the player tile and the "map" tiles with diffrent settings? Blending, vertex alpha enabled/disabled etc.

- Patrik Willbo

The Lord says He can get me out of this mess, but He's pretty sure you're fucked.
- Stephen (Braveheart)


[edited by - Willbo on May 27, 2004 6:18:35 PM]

Share this post


Link to post
Share on other sites
the code i do for drawing a tile is the same for drawing a regular quad... although i DO blit the whole tilemap with Draw_Quad() to test to see if it will have transparency (the 3rd pic)... but there is no difference really... heres the 2 functions..


void System::Draw_Quad(GLuint &texture,float x, float y,float width,float height)
{
//bind the texture we sent in to this quad

glBindTexture(GL_TEXTURE_2D,texture);

//draw this quad white

glColor3f(1.0f,1.0f,1.0f);


glBegin(GL_QUADS); /* Draw A Quad */

glTexCoord2f(0.0f, 1.0f);
glVertex2f(x, y + height); /* Bottom Left */

glTexCoord2f(1.0f, 1.0f);
glVertex2f(x + width,y + height); /* Bottom Right */

glTexCoord2f(1.0f, 0.0f);
glVertex2f(x + width,y); /* Top Right */

glTexCoord2f(0.0f, 0.0f);
glVertex2f(x,y); /* Top Left */

glEnd();

}


void System::Draw_Tile(float x, float y, float x_loc, float y_loc)
{
//bind the texture we sent in to this quad

glBindTexture(GL_TEXTURE_2D,map_data.tilemap_texture);

//draw this quad white

glColor3f(1.0f,1.0f,1.0f);

//float corner_offset = 32.0f/64.0f; //(map.tmap_height*TILESIZE);


float t_perc_w = 32.0f/((float)map_data.tmap_width*(float)TILESIZE);
float t_perc_h = 32.0f/((float)map_data.tmap_height*(float)TILESIZE);

glBegin(GL_QUADS);

/* Bottom Left */
glTexCoord2f(x_loc, y_loc + t_perc_h); //was 0.0f,1.0f

glVertex2f(x, y + 32.0f);

/* Bottom Right */
glTexCoord2f(x_loc + t_perc_w , y_loc + t_perc_h); //was 1.0, 1.0

glVertex2f(x + 32.0f,y + 32.0f);

/* Top Right */
glTexCoord2f(x_loc + t_perc_w , y_loc); //was 1.0,0.0

glVertex2f(x + 32.0f,y);

/* Top Left */
glTexCoord2f(x_loc,y_loc); //was 0.0 0.0

glVertex2f(x,y);

glEnd();

}



there really is no difference... i dunno why this is happening.. thanks for any help

Share this post


Link to post
Share on other sites
leiavoia    960
instead of colorkeying your textures, just add a regular alpha chanel (Assuming you have a decent image editor... and if you don''t get the GIMP for free). As long as blending is turned on, OpenGL doesn''t care if you color key your textures, don''t have any transparency, or load the alpha data right from the actual file, it''s going to treat it like it has alpha anyway, so might as well use it.

That, and color keys just bite.

Share this post


Link to post
Share on other sites
leai, i just tried that but no luck.. for some reason the alpha channel isnt coming through.. instead of being transparent its a white background... maybe i made the alpha channel wrong in the image editor? i dont think i did.... also, i use SDL_CreateRGBSurface and copy my image onto that surface before loading it, so its in RGBA order... i read somewhere that this stops alpha channels from working.. but if i try not copying my image to an RGB surface, i cant get ANYTHING to look right (screen turns all black)... im just trying to figure out how why its now being transparent in the first place just because im only texturing a piece of the image...

Share this post


Link to post
Share on other sites
hmm.. still no good, the alpha part of the texture is just coming out white... maybe i didnt do the image part right or something? could you email me an image which you know has an alpha channel if you dont mind? maybe its my code? heres my code to load a texture


GLuint System::Load_Texture(string image, bool is_transparent)
{

GLuint texture;

//Create The Texture

glGenTextures(1,&texture);

SDL_Surface *img = IMG_Load(image.c_str());//Load_Image(image);//IMG_Load(image.c_str());//Load_Image(image.c_str());


//if(is_transparent)

// SDL_SetColorKey(img, SDL_SRCCOLORKEY,SDL_MapRGB(img->format, 255, 0, 255));


SDL_Surface *img2 = SDL_CreateRGBSurface(SDL_HWSURFACE|SDL_SRCALPHA,img->w,img->h,32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

SDL_BlitSurface(img, NULL,img2, NULL);

//Typical Texture Generation Using Data From The Bitmap

glBindTexture(GL_TEXTURE_2D,texture);

//Linear Filtering

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

// Generate The Texture

glTexImage2D(GL_TEXTURE_2D, 0,4,img2->w,img2->h, 0 ,GL_RGBA,GL_UNSIGNED_BYTE, img2->pixels);

//free our image and return our texture

SDL_FreeSurface(img);
SDL_FreeSurface(img2);

return texture;
}


thanks alot for all your help!!!

Share this post


Link to post
Share on other sites
ok...

im starting to get really frustrated. i finnally figured out how to add an alpha channel in the GIMP... anyway, i add the alpha channel to the spots i want....

I GET THE SAME EXACT EFFECT!!!!!!!!!!!!!!!!!!!!!!!

if i map the ENTIRE tilemap image, onto the enemy's quad, THE TRANSPARENCY WORKS!!!!!

BUT, when i map only a PERCENTAGE of the texture to a 32x32 quad (my tile), THE TRANSPARENCY IS BLACK!!!

also, im 100% positive the alpha channel i did correctly in the image editor, because it IS transparent if i map the ENTIRE texture to a quad... but if i try mapping only a percentage of this texture, THE TRANSPARENT PART IS BLACK!!!!!

(read my first post, it is doing the SAME exact thing, only this time im not color-keying anything, im using alpha channels....)

i only call glEnable(GL_BLEND); ONCE, at the begining of the game... i make sure to fill the quad with a white opaque color before drawing.. in fact, read my second post on this thread. it contains my 2 functions i use to draw, Draw_Tile() and Draw_Quad(), they are EXACTLY the same except Draw_Tile() "slices" the tilemap into a single tile piece...

does ANYONE have any idea on why this is happening. i know there must be someone out there who maps portions of a texture with either color keys, alpha channels, or something. PLEASE respond to this thread, post your code, anything.. any help is greatly appreciated...

[edited by - graveyard filla on May 28, 2004 6:22:35 PM]

[edited by - graveyard filla on May 28, 2004 7:37:50 PM]

Share this post


Link to post
Share on other sites
after tearing out my hair, i think i figured out the problem.

i was trying all kinds of things, going crazy, when i thought - "hey self, lets try drawing a 32x32 quad, we will map a portion of the tilemap to this quad, just like i do when i draw my map, but instead, i will only draw a SINGLE tile instead of a scree full of them"

ok, so i draw only a single tile, mapped with a portion of the texture which *SHOULD* appear transparent..

IT DOES!! IT WORKS!! IT WORKS!!!

ok, so it IS possible to map a portion of a transparent texture!!

im thinking that the black im seeing, is actually the background - how do i change this! im thinking, since there is "nothing" "behind" the tile, it shows up black - but when i manually draw the SAME quad, "on top of" (after) i draw the map, its background DOES show up transparent...



the one labeled "no good", is an actual tile that is in my map[][] array. i render this whole map array before i render ANYTHING else, so this is "on the bottom".

the one labeled "good!" is a tile rendered in the EXACT manner the "no good" one is, EXCEPT, i render this one AFTER i draw my whole map, so it is "on top of" my map. notice that the transparent part IS transparent!!!

"the whole thing" is just my entire tilemap texture rendered to a 128x128 quad (the size of the texture). again, the transparency is working because its "on top of" my map.

this is why the tilemap texture would appear to be normal if i mapped it to my player/enemy quads, because my player/enemies are rendered AFTER/on top of the map!!

so HOW do i get rid of the background, so that instead of getting a black piece where it should be transparent, i get a REAL transparent piece??

thanks for any help...

edit: thinking about it, the reason its showing black is because theres literally nothing "behind" it. how would i go about doing this then... do i have to have 2 maps which represent 2 different layers? one with trasparent tiles, and another without transparent tiles? first i would draw the opaque map, then i would draw the transparent map right after? im REALLY hoping i wont have to do this... this will be complicated and a huge pain in the ass, effecting everything from collision, to my map editor, it will just be such a big deal to do for such a small problem...

[edited by - graveyard filla on May 28, 2004 8:17:51 PM]

[edited by - graveyard filla on May 28, 2004 8:18:46 PM]

Share this post


Link to post
Share on other sites
leiavoia    960
Reality Check: well, why do you want transparency if there is nothing behind it? See what i mean?

If there isn't, just make a regular bmp that joins to the other tiles visually. Otherwise, blit the ground first, then whatever is on top of the ground for layered effect. There isn't any other way to do it.

However, keep in mind that many of ye old classic RPGs just used regular non-transparent tiles to achieve their effects.

The other thing i'll point out is that you are creating what appears to be a 3/4 perspective with square tiles. This is bad from a design point of view (like using a hammer to set screws). Either make a true top-down view with square tiles (like Final Fantasy), or a 3/4 view with diamond tiles.

here is some wonderfull info on isometric vs topdown tiling systems and how to make them work.

EDIT: oh, and when you open a thread, say "hi" or "hello". "high" is the opposite of "low" ;-)


[edited by - leiavoia on May 28, 2004 9:54:09 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by leiavoia
Reality Check: well, why do you want transparency if there is nothing behind it? See what i mean?

If there isn't, just make a regular bmp that joins to the other tiles visually. Otherwise, blit the ground first, then whatever is on top of the ground for layered effect. There isn't any other way to do it.

However, keep in mind that many of ye old classic RPGs just used regular non-transparent tiles to achieve their effects.

The other thing i'll point out is that you are creating what appears to be a 3/4 perspective with square tiles. This is bad from a design point of view (like using a hammer to set screws). Either make a true top-down view with square tiles (like Final Fantasy), or a 3/4 view with diamond tiles.

here is some wonderfull info on isometric vs topdown tiling systems and how to make them work.

EDIT: oh, and when you open a thread, say "hi" or "hello". "high" is the opposite of "low" ;-)



hey leai,

i was waiting for you to help me . you are right about the tiles. im using square tiles and i realize doing 45 degree angles with square tiles wasnt going to work neither visually or codily()... the truth is, i have no idea on how to begin with the artwork...good thing im working with an artist .. hes going to mess around trying to figure out exactlty how to make things look right visually (i think he said hell start with around 80 degree angle and work from there)...

something i DONT understand, is this:


take shadwrun for example (my fav! and inspired me to make this game)... anyway, you see how im "behind" the building... how do they do this? i mean, i could manage "this tile is non solid, this tile is solid..".. but how do they have certain tiles draw OVER the player and not under him? im thinking they used layers for this.. so im gunna wind up having to make layers anyway? does anyone have experiance with making a top down square tiled RPG have any advice? and how does it all work? do i have something like 3 map[][] arrays, one "lowest level", one "middle level", and one "highest level"... the lowest level stuff would be the floor, the middle level stuff would be buildings (which then could have transparent pieces)... the higest level stuff would be the tops of buildings (ala the pic so i could go behind the building) or even lights hanging from the roof of a building (another thing shadowrun has...) but i think the hardest part would be connecting ALL of the maps, and doing collision.. im guessing collision would be performed on all the maps? only the 'middle' one? or maybe all 3? BAH! i DONT want to have layers, unless i have to. but did shadowrun have them, or what technique do you tihnk they used to get that behind-the-building effect? gunna check out that link you showed... thanks a lot for your help man


[edited by - graveyard filla on May 28, 2004 10:58:42 PM]

Share this post


Link to post
Share on other sites
leiavoia    960
ah, well, yes you are going to have to make layers then. If you ever played any of the emulated SNES games (not that any of us have...), some of the emulators have options to turn off seperate graphics layers. Evidently the Super Nintendo built graphics into layers. If you take a game like Chrono Trigger or Final Fantasy 3/6j, you can turn off the different layers and see how the game was built. So you'd have the background layer, the ground tile layer, the sprite layer, the tops of the buildings (which lets you peek inside and see hidden passages, teehee!), and special effects like fog, mist, sunshine or underwater looking things on top, all blended of course.

So, as was my original original suggestion, organize your UpdateObjects into layers. for my system, i have a sprite layer, 4 background layers and 4 foreground layers. That gives me lots of room to create special effects. Then i blit each layer in order from bottom to top. My objects are not tied to a map, as such, so...

The other thing is that you don't necessarily need to stick to the tile-based mode of thinking for everything. You can mix tiles and free-floating objects if you organize it right. Your free-floaters can be pointless eye candy too. But don't get stuck in the "Everything is a tile" mode of thought or you will unnecessarily limit yourself. However, if you still want to do that, that link i posted previously has links to articles that discuss that very problem of "walking under" tiles.

You can also see what i'm talking about mixing tiles and free-floaters here in this thread of mine

[edited by - leiavoia on May 28, 2004 11:12:51 PM]

Share this post


Link to post
Share on other sites
you know.. you are right about not thinking in terms of tiles... i could easily add ''free floaters'' to the game, like in shadowrun where you can walk ''underneath'' a light... this could easily just be a button in the map editor and when you click it puts |= LIGHT to the flags in that tile (my tile class has a flags paremter), anyway, when drawing the map i would just check for LIGHT and if so draw a light there (i already do the same thing for the player and enemies) ... it shouldnt be hard to add some nice visual depth that way, but i wouldnt be able to add collideble objects this way, would i? i mean, if i did i would have to do bounding box collision on them no? also, i think i will add some more layers to the game.. i can see how much nicer the game will feel with layers, so i can have stuff like walking behind buildings... so im guessing i will have 2 map[][] arrays which each had seperate maps, and i would draw the background map before the foreground one (what about messing with the Z axis for this? worth it or no?)... thinking about it it wont be that hard to do this in the map editor (tha part that will prolly be the hardest).. i could just have a button for switching between which layer im currently working on.....

i read through that thread, but i dont know what you were talking about with the "make all the objects collision objects" or "not use arrays for the map"... how would you go about doing that? im not sure how quad trees work, but i think they just only check for collision on objects that are close to each other to cut back on checking... ok but how do you draw a screen full of tiles without an array? just seems weird... again thanks a lot for all your help

Share this post


Link to post
Share on other sites
leiavoia    960
you are not drawing a "scene full of tiles with an array". Your just drawing a bunch of objects. My system doesn't care if it's a tile, gunshot, character, exposion, etc. Let's disect:



A: Is an explosion consisting of 1) an explosion animation, 2) a light flare (textured quad), and a shower of particles. It all happens real fast. The animation, flare, and particles are free-floating. They have no tile coords and are not tiles. They simply exist in world space at about 500x,500y. None of these entities are collision objects. They just look pretty.

B: Happy face! He blows up if he hits too many walls. He exists in world space and he's also a collision object. All collision objects get sorted into a quadtree which files objects into a specific region of space you only check objects against other objects in the local space, instead of everybody against everybody.

C: Fading Explosion. The light flare has gone (it fizzles out in 95ms) and was removed as an UpdateObject from the list. The animation is still going, as are the particles.

D: World block. No it's not a tile array, it's a quad with a repeating texture. The same is true for the side bars. In total there are 5 world blocks in this scene. They are also collision objects and get sorted into the quadtree as well.

E: Particles. They are not collisionable and exist in world space by themselves. The update engine cycles over each particle and updates and draws each one every frame.

F: The background! It's just a textured quad with an offset repeating texture. The offset produces a scrolling effect and parallax too (pretty schnazy).

There are no tiles on this scene. There is no array. All objects are independent. That doesn't mean that i couldn't ALSO use a tile array. Which i have also played with: (Screenshot shows offset tiles, except all tiles are still independent quadtree-sorted objects)



[edited by - leiavoia on May 29, 2004 1:58:43 PM]

Share this post


Link to post
Share on other sites
hey leia,

i appreciate you helping me. i have a question : if you dont use tiles to draw your background/world block, how do you track it all? i mean, if its not a tile, how do you judge how big to make the quad? your map editor must be really sophisticated to say "make a quad here x by x units"... also, you must keep track of all these things positions then, huh? so even the background tiles have a recorded x,y position for collision detection and such im assuming? also, how do you go about doing collision detection? is it just standard bounding box, or something different? lastly, is this system better then using tiles? i mean obviously you can get more diverse objects on your scene, but what about speed and such? i know that thread you mentioned it wasnt as fast....

thanks again for your help!

Share this post


Link to post
Share on other sites
leiavoia    960
quote:
if you dont use tiles to draw your background/world block, how do you track it all? i mean, if its not a tile, how do you judge how big to make the quad?


Everything gets stuffed into the UpdateObject list. Quadtrees keep track of collision object space partitioning.

quote:
your map editor must be really sophisticated to say "make a quad here x by x units"...


Actually it''s pretty simple... i don''t have a map editor, as such. (yet). I plan to keep my world data in XML though.

quote:
also, you must keep track of all these thing''s positions then, huh? so even the background tiles have a recorded x,y position for collision detection and such im assuming?


True. each object has a collision rectangle.

quote:
also, how do you go about doing collision detection? is it just standard bounding box, or something different?


Something different. It''s a rather compicated system now. It checks for abject type and object geometries. All objects have a collision hitbox, but some objects have other more sophisticated geometries like a sphere or polygon or point. If it passes the basic hitbiox test, i can stop or move on to a more accurate test. There is a lot more to it than that, but if you ware looking for a headswim, check this other thread of mine

quote:
lastly, is this system better then using tiles? i mean obviously you can get more diverse objects on your scene, but what about speed and such? i know that thread you mentioned it wasnt as fast....


No, not necessarily. This model works for free-floating objects very nicely. For tiles, i think i''d design something different and custom tailored for that. Not much can beat a raw array lookup for speed. My system is designed to handle objects of different types (world, player, enemy, weapon...) and different geometries (sphere, poly, point, box...) quickly. It wasn''t meant for a tile engine, but that doesn''t mean i couldn''t add a new kind of object to the list that acts like a tile array.

Share this post


Link to post
Share on other sites
leiavoia    960
I don''t know anything. Man, i work in a photolab!

I''ve been C++''ing for about ~2 years. I picked up perl and PHP (mostly forgotten now) a year or two before that. Perl still comes in handy. I started learning C++ by creating a TBS game which i worked on constantly for a year. and dropped it around March to work on more action/graphics oriented games (i wanted to program explosions!).

Most of what i know i pick up from other people or learn from trying different things. I''m sure i''m a terrible programmer, but i sound smart :-)

Share this post


Link to post
Share on other sites