Textures containing transparent sections
Blitz3D Forums/Blitz3D Programming/Textures containing transparent sections
| ||
I am having some trouble creating a texture with transparency from a masked image in memory. Here's what I'm trying to do... my program generates an image in memory with certain parts having RGB values of 255,0,255 which I then set as masked. If I draw this to the screen as a normal image it will plot correctly with the 255,0,255 portions invisible. Now I need to create a texture out of this image, maintaining transparency where the color is 255,0,255. I can't seem to get this to work. I create the texture, set the buffer to texturebuffer() of my texture and DrawImage() the image to the texture. If I apply this texture to an object using the COLOR flag, it draws the texture showing the 255,0,255 color.. if I try to set the MASKED flag it draws the entire texture completely black. I've tried setting different texture flags but nothing is working as I imagined (they either turn the texture completely black on the screen or completely invisible). I tried searching the forums and wasn't able to find any threads on this (if they exist I sure couldn't find them!). Any help would be greatly appreciated :-) -Steve |
| ||
i'm not sure if you can set the masked colour of a texture. I'd be very glad if someone proved me wrong tho :) Oh the search feature of the forums kinda sucks, so don't give up if you've searched and not came across anything :o) Could you not just set use 0,0,0 as your masked colour? And set the black parts to 1,1,1? |
| ||
Not sure I understand what you mean by setting the black to 1,1,1 ... Here's a very stripped down example of what I'm trying to do.. I'd like the black part of the texture (the black circle) to be transparent or even semi-transparent: [CODE] Graphics3D 640,480,0,2 camera = CreateCamera() light = CreateLight() cube = CreateCube() sphere = CreateSphere() ;Create image gfx = CreateImage(64,64) SetBuffer ImageBuffer(gfx) ClsColor 200,0,0 Cls() Color 0,0,0 Oval 2,2,60,60,True Color 255,255,255 Text 10,25,"HELLO!" MaskImage gfx,0,0,0 ;Create texture from masked image tex = CreateTexture(64,64,1) SetBuffer TextureBuffer(tex) DrawImage gfx,0,0 SetBuffer BackBuffer() ;Apply texture to cube EntityTexture cube, tex ;Position Camera PositionEntity camera,0,-5,0 PointEntity camera,cube While Not KeyHit(1) TurnEntity cube,1,2,1 UpdateWorld() RenderWorld() Flip() Wend End [/CODE] -Steve |
| ||
First - there are two Transparency Modes for Textures: Mask and Alpha. You have to define this as the Textureflag, 4 for Mask or 2 for Alpha. tex=createtexture(64,64,2 or 1) so it's Alpha as well as fullright. With Mask you only have the choice of opaque(non-black) or fully transparent(black). With alpha the Brightness of the Pixel will define its tranparency, the darker, the more transparent, UNLESS you load a Texture with a predefined Alpha-Channel such as a 32 Bit TGA Picture. However, you can manipulate the Alpha-Channel of a Texture in Memory whenever you like this way: setbuffer texturebuffer(tex) lockbuffer for j=0 to textureheight(tex)-1 for i=0 to texturewidth(tex)-1 rgb=readpixelfast(i,j) and $FFFFFF ; example of semitransparence for black parts: if rgb=0 ; black argb=(127 shl 24) or rgb ; using 127 for alphabyte else argb=rgb ; alphabyte remains zero endif writepixelfast i,j,argb next next unlockbuffer setbuffer backbuffer() Please note, that unlike Maskmode the Alphamode sometimes makes Problems with the Z-Order of the Objects, especially if they have intersecting parts. |
| ||
JFK: Thanks for your reply, this is essentially what I was after, only I hoped there was an "easier" way to do this using drawimage() or copyrect(). Not long before you posted I found another thread that gave a very similar solution to what you gave here. I did notice a z-order flaw when using the alpha (and later read a reference to this in a different thread). Too bad as I was looking forward to using alpha rather than a complete mask. Is there any work-around for this? Thanks again for your help! -Steve |