www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: raft on March 15, 2008, 01:41:38 am

Title: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 01:41:38 am
hello,

first my aim: i want to port karga speech bubbles to GL renderer. they are currently blitted over awt.Graphics by using methods of graphics class. the bubbles has a popup effect, that is when they first appear they have an alpha value of 0 and alpha value increases in time giving a feeling of they appear from nothing.

how can i do that with GL renderer ? can i change alpha value of a texture when blitting it ? FrameBuffer.blit(..) method doesnt support it. Texture class itself also doesnt have any methods to change alpha.

Texture has an add method to mix two or more textures with some weight. adding bubble texure to an empty texture with an increasing weight may be an option, but that way colors will also change in time.

what else can i do ?

thanks,
r a f t
Title: Re: changing alpha when blitting a texture
Post by: Jonas on March 15, 2008, 10:15:08 am
hi

I think you could use the new overlay class to do that.

Or create a billboarded plane, add the texture to it and raise the objects transparency instead of the textures transparency..

Or just use graphics to manipulate the image the texture is made of.
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 15, 2008, 10:31:17 am
Manipulating the texture is most likely too slow, because it require a texture upload to the graphics card each time. But as Jonas said, playing around with setTransparency may work. There are some config settings in Config to tweak transparency for a wider transparency range than the default range.
Title: Re: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 03:02:36 pm
thanks for the anwers :)

well, i dont want to use planes since it will create other problems for overlapping bubbles:

* rendering very close objects with transparency has some sorting problems
* more importantly, bubble text will be blitted after all bubble images has been rendered and it will look very strange. (placing text on planes proved to be a very bad idea)

does LWJGL offer methods to blit with transparency ? maybe jPCT may expose them (this can also be done in sw renderer) ? or else maybe i can use lwjgl methods directly

ps: karga bubbles try to not overlap but they do overlap for some time. for example during the process of going off the way
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 15, 2008, 03:50:31 pm
I don't understand the problem exactly. Maybe you can illustrate it somehow? The Overlay-class can change the depth of the bubbles if needed (without changing the size), so sorting shouldn`t be an issue. I don't get the section about the text. First, you render the bubbles, then the text on top of that? If so, what's the problem with this approach?

OpenGL doesn't really support bitmap blits in a usable way. You always have to fall back to triangles, which is what jPCT does too. So i don't see how using LWJGL directly can improve the situation. Then again, i don't understand the situation right now... ;D
Title: Re: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 04:42:45 pm
ok, so sorting wont be issue. for text blitting here is an illustration:

this is how it's now. bubbles are rendered one after another depending on some z order
(http://img84.imageshack.us/img84/3526/currenttm2.gif)

this is how it will look if we first render all bubble images and blit text on them later
(http://img388.imageshack.us/img388/4962/problemxx9.gif)
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 15, 2008, 04:51:40 pm
I see...rendering the text into a texture isn't an option? You could use a pool of textures for this to avoid that texture memory fills up after time.
Title: Re: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 05:24:21 pm
wont rendering text to texture kill performance ? other than that, text is very sensitive for scaling. even a very slight scaling makes it look very ugly.

i planned to use feng gui's text renderer for text. it's very handy, can create textures from awt fonts on the fly. the result is very similar to awt.Graphics.drawString(..) method. that's also one of the reasons that i want to use blit someway, it will require a minimal code change.

you said lwjgl doesnt allow directly blitting images but requires usage of triangles. i guess that means FrameBuffer.blit(..) also uses a plane behind scenes. if so, the main difference between overlay and blit is, overlay uses an Object3D which is effected from perspective but blit isnt, right ? if this is the case, isnt it possible to use alpha and scaling in blitting ?
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 15, 2008, 06:51:37 pm
About rendering the text into a texture: Drawing the text once into the texture shouldn't be that much of an issue. I requires a new upload of course, but as long as you don't do this every frame, it should be ok.

Yes, blit() uses a plane consisting of two triangles behind the scenes. It's very similar to Overlay (Overlay isn't affected by perspective, because it auto-adepts to the camera similar to billboarding) with the excpetion that it doesn't include the overhead of an Object3D.
Alpha in blitting is already possible, but it's currently limited to the alpha channel of the texture, i.e. it's not very dynamic which means that fading will still be an issue.
Adding dynamic transparency to blitting (limited to the OpenGL renderer at first) seems to be quite easy. Adding a kind of scaling should be doable too. I'll look into it.

Title: Re: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 07:04:59 pm
thanks, i will be glad indeed ;D

why is it limited to GL renderer ? it should be trivial in sw renderer too. Graphics2D.setComposite(AlphaComposite.getInstance(...)) provided you hold texture in image format. i dont know about java 1.1 compatibility..

i was looking into feng gui docs, it does allow drawing scaled images but with no alpha adjustment. if you add scaled/alpha blitting i will be more than happy and use it, otherwise i will try to modify feng gui blitting to support alpha. i certainly prefer jPCT in this case, as bubbles arent part of gui and i dont want to depend another library at that level

btw, i may post or mail relevant part of feng gui source
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 15, 2008, 07:07:03 pm
jPCT does everything on the int/byte-level when using the software renderer. I don't use any image-related stuff except for loading data. After that, it's all bit-fiddling...
Title: Re: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 07:33:45 pm
i see  ::)
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 15, 2008, 07:54:21 pm
I have the modified blitting stuff working, i think.

Unfortunately, i only have an older version of jPCT's sources at hand at the moment, so can't update the 1.16rc right now with it. I'll try to add it on monday to the correct branch.
Title: Re: changing alpha when blitting a texture
Post by: raft on March 15, 2008, 08:09:43 pm
great, thank you ;D
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 17, 2008, 06:25:50 pm
I've added the requested stuff...it's not fully tested, so you may still encounter bugs in it. Get it here: http://www.jpct.net/download/beta/jpctapi116rc3.zip (http://www.jpct.net/download/beta/jpctapi116rc3.zip) (this time without CRC-errors...hopefully...).
The new blitting works in hardware as well as in software mode. In software mode, it wraps the blitting-call in an Overlay...quite tricky but it seems to work fine... ;D
Title: Re: changing alpha when blitting a texture
Post by: raft on March 17, 2008, 08:39:22 pm
no CRC error this time ;)

thank you ;) i guess the transparency argument works as it's for Object3D.setTransparency(int) and for GL renderer uses Config.glTransparencyMul and Config.glTransparencyOffset, right ?

it seemed to work ok, except i cannot blit a texture with no transparency but using alpha information: i give -1 as trans value and except transparent parts of my texture remains transparent, as the previous blit method does. but it blits transparent parts as black. i created the texture to use alpha

btw, what is the exact meaning of texture constructor paramater useAlpha ? if i create a texture with no alpha and transparent blit with old method, it correctly blits transparently. where does that alpha info comes from ?
Title: Re: changing alpha when blitting a texture
Post by: raft on March 17, 2008, 09:26:01 pm
a few more minor things:

* 0 is the highest transparency as it's in Object3D right ? docs say the opposite ;)
* old blit method creates much sharper edges, but i guess that's because of that transparency thing, i had to give a transparency to make transparent parts of my texture invisible
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 17, 2008, 11:52:20 pm
i guess the transparency argument works as it's for Object3D.setTransparency(int) and for GL renderer uses Config.glTransparencyMul and Config.glTransparencyOffset, right ?
Yes, i uses the same formula.

it seemed to work ok, except i cannot blit a texture with no transparency but using alpha information: i give -1 as trans value and except transparent parts of my texture remains transparent, as the previous blit method does. but it blits transparent parts as black. i created the texture to use alpha
You can set a high value for transparency which basically is opaque for everything with an alpha of 255. If you load a texture, jPCT automatically sets all black (or almost black) parts to alpha 0, all the others to alpha 255. To disable this behaviour, you've to create the texture with alpha (that's what the constructor is for). The alpha channel is part of the texture. PNG for example is a suitable format, because it supports an alpha channel.
If you don't have an alpha channel in your texture, you can add your own by using an ITextureEffect.
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 17, 2008, 11:55:46 pm
* 0 is the highest transparency as it's in Object3D right ? docs say the opposite ;)
* old blit method creates much sharper edges, but i guess that's because of that transparency thing, i had to give a transparency to make transparent parts of my texture invisible
* I'll correct this. I meant that 0 is lowest value for anything none opaque, which is true but pointless information... ;D
* Edges should be sharp as long as you don't scale. If you do, normal bilinear filtering applies. It's a normal textured polygon afterall. If you want sharp (but blocky) edges, do a setGLFiltering(false) on the texture.
Title: Re: changing alpha when blitting a texture
Post by: raft on March 18, 2008, 10:37:12 am
thanks for the explanations :)

i'm a bit confused about transparency. here is a shot:
(http://img249.imageshack.us/img249/5632/blit2wk9.png)

for both text and bubbles, upper lines are blitted by old method and lower lines are with new one. source texture is same for both lines. for bubbles transparency increases from left to right: -200, 0, 200, 400.. for text similar, -100, 0, 100, 200..

text transparency is alright, it cannot be seen in the shot but lower line of letters are completely opaque starting from 'c' as expected. the right most bubble has a transparency value of 800 but it's still transparent ?

i guess, the term 'sharp' was incorrect but you may see lower line of text is jagged. those letters are rendered to texture by using antialiasing. i thought antialiasing wont help much and even will look worse in this case, as we dont know the background color when we are creating the texture, but surprisingly it works quite fine for upper line. it may be because they're rendered to a transparent background (BufferedImage.TYPE_INT_ARGB) but not sure ?
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 18, 2008, 10:43:21 am
Maybe rendering with antialiasing creates alpha information...i'm not sure about this, but it looks like it. It would make sense anyway to do this, so maybe SUN got this right.

About the bubbles: You are obviously using additive blending. Try to set this parameter to false to see if it helps. Additive blending will never be opaque unless you add white, which is what you do for the letters.
Title: Re: changing alpha when blitting a texture
Post by: raft on March 18, 2008, 10:51:09 am
yes, it did help, it also helped letters, both methods blit the same now :) thank you ;D
Title: Re: changing alpha when blitting a texture
Post by: EgonOlsen on March 18, 2008, 10:57:36 am
yes, it did help, it also helped letters
Yes, because doing a additive blending with high transparency value makes almost everything white. It's adding the color multiple times until it's all white (or red or green or blue...).