www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: ErDetEnAnd? on December 22, 2008, 04:18:33 pm

Title: Transparent blitting
Post by: ErDetEnAnd? on December 22, 2008, 04:18:33 pm
Hello!

How do I blit a dark picture (r/g/b<0x0F) using transparency?

When blitting images the image must fit into a texture, even though the extra space in unwanted. The image cannot be scaled, thus the extra space is made transparent using an alpha channel (see image).

(http://img131.imageshack.us/img131/2673/transblittingtt7.png)

Using a dark(er) image as destination, and a transparent border as source to fit into a texture, blitting by

public void blit(Texture src,
                 int srcX,
                 int srcY,
                 int destX,
                 int destY,
                 int width,
                 int height,
                 boolean transparent)

doesn't make use of the alpha values. So how do I fit a darker image into a texture, showing only the image?
Title: Re: Transparent blitting
Post by: EgonOlsen on December 22, 2008, 05:37:41 pm
How do I blit a dark picture (r/g/b<0x0F) using transparency?
When using the hardware renderer, the alpha channel will always work. When using the software renderer, the

Code: [Select]
public void blit(Texture src, int srcX, int srcY, int destX, int destY, int sourceWidth, int sourceHeight, int destWidth, int destHeight, int transValue, boolean additive, Color addColor)
-method should do the trick. Internally, it wraps the blitting call into an Overlay (which i consider to be a bit hacky... ;D), so you may also use an Overlay directly, if that is more appropriate in your case.
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 22, 2008, 06:35:55 pm
Thank you.

I had to update my jPCT version in order to get that method, but know my billboarding objects not facing the camera and not transformed. What is changed?
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 22, 2008, 07:59:51 pm
Another 2 issues:

Where do I find the package version of a downloaded jPCT?

The blit method you refered to makes colors less than 0x0f transparent using transparency. I guess this is correct. However setting the transvalue argument to -1 (disabled), the method is ignoring the alpha-channel. Again, how to I draw a dark image with the only transparent pixels having an alpha-value?
Title: Re: Transparent blitting
Post by: EgonOlsen on December 22, 2008, 08:09:11 pm
That version has regression bug with bill boarding...try the jar from http://www.jpct.net/download/beta/jpct.jar (http://www.jpct.net/download/beta/jpct.jar). It should fix the bill boarding problem again (http://www.jpct.net/forum2/index.php/topic,1253.0.html (http://www.jpct.net/forum2/index.php/topic,1253.0.html)). The auto-transparency thing with color values below 0x0f only applies to textures that weren't created with an alpha channel. If you create them with one of the appropriate useAlpha-constructors in Texture, the alpha channel will be taken instead.
However, of course alpha is limited to transparency...you may try to use a very high value for the transparency. That should make the dark areas almost opaque while the alpha channel is still taken into account for the transparent parts.
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 22, 2008, 08:28:01 pm
Exactly and impressive!

Respect for your work with this engine, but you really should elaborate the rather bland doc for this engine (eg. with references from the blit to the alpha-channel texture). It would take time but really pay off.

Btw. where do I find the version of my jPCT?
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 22, 2008, 08:35:57 pm
Using the jar you just posting is not scaling the bill boards, when scaling its parent.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 22, 2008, 08:38:45 pm
Btw. where do I find the version of my jPCT?
Config has a getVersion()-method, but that doesn't reflect sub-versions, so the official release and the jar from the beta-directory will return you the same version number.
Concerning the docs, i agree that they could be better regarding that blitting method and the alpha stuff. I must have forgotten to update them properly because i kinda kludged in both of them in very short time.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 22, 2008, 08:40:17 pm
Using the jar you just posting is not scaling the bill boards, when scaling its parent.
Did the old one do this? It's actually a merge of the new sources with the ones that contained the fix. I can't see a reason why it should have behaved different before... ???
Title: Re: Transparent blitting
Post by: EgonOlsen on December 22, 2008, 09:03:22 pm
Using the jar you just posting is not scaling the bill boards, when scaling its parent.
I can't verify this, albeit i have to admit that the behaviour that i'm getting is wrong too. What i get is, that the scaling of the parent is applied two times to the child, which is wrong. I'll try to fix this...

Edit: I've updated the jar in the beta-directory. The scaling of the parent should now be applied to the child correctly...i should have defined bill boarding as not being support for parent-child-relations much earlier... ;)
Title: Re: Transparent blitting
Post by: EgonOlsen on December 23, 2008, 01:08:02 pm
Did it work  ???
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 23, 2008, 03:33:42 pm
I'm affraid not. I'm using the billboards for text which I want to keep with a certain size regardless of parent scaling. This is (was) achived by scale inverted on the billboards. Now I cant scale the billboards seperately. The jPCT version I used before was about 1 year old.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 23, 2008, 08:23:29 pm
So the parent has 0.5f (for example) and the child gets a 2f to compensate for that and to get a child-scaling of actually 1f? If so, then that works fine in my test case. I can't verify your problem. Either the version you were using was working wrong (earlier versions didn't take the parent's scaling into account, which was actually wrong behavior) or you are doing something different. Can you provide a simple test case that shows the problem?

Edit: And make sure that you really have the latest jar-version. I've updated it twice yesterday. If you've got an in-between-version, you may got a buggy one.
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 25, 2008, 11:49:59 am
I've just updated the jar with the beta posted above, but with no effect.

Code: [Select]
// Scale on parent
mc.getOriginRoom().setScale(scale);

// Scale inverted on billboards (which are children)
for (RoomObject3D ro3d: mc.getRooms()) {
    ro3d.getBillBoardObject3D().setScale(bbscale);

You got my scaling right. Whatever I scale the parent, its children* is scaled with its reciprocal value (which worked fine before). This should leave the size of the billboards unchanged for the viewer.

Here are some numbers i use. scale is used on parent while bbscale on the billboards. What I get are bigger billboards (even on first setScale (for keeping the culumative out of the picture)).

scale=1.05, bbscale=0.952381, diff=1.0
scale=1.1024998, bbscale=0.90702957, diff=0.99999994
scale=1.1576248, bbscale=0.8638377, diff=1.0
scale=1.2155061, bbscale=0.82270265, diff=1.0000001
scale=1.2762814, bbscale=0.78352636, diff=1.0000001
scale=1.3400954, bbscale=0.74621564, diff=1.0000001
scale=1.4071001, bbscale=0.7106816, diff=1.0000001

*) Its childrens children actually.
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 25, 2008, 12:06:46 pm
Found a solution!

If the billboard is not a successor of the object you scale, but eg. a child to the successor, the billboard scaling is not affected. To do what I want, the billboard must be a successor to the scaled.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 25, 2008, 12:25:41 pm
I need a simple test case for this, please. I still can't verify it and i don't fully understand your description of the solution... ???
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 25, 2008, 12:48:22 pm
I'd paste the code if I could, but the project would give too much overhead, so I hope this drawing will do.

(http://img364.imageshack.us/img364/1025/scalingpk6.png)

Object3D B is a direct successor of Object3D A in both cases, and respectively same for C and B on the left. Object3D C is the billboard. Now my inverted scaling only works if this scaling is done on the direct successor of the previously normally scaled Object3D.

Object B and C are sharing matrices.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 25, 2008, 01:00:06 pm
So it seems like it doesn't work if the chain is longer than 2 elements...strange. However, why do B and C share one matrix? Maybe that causes the problem. A bill boarded object usually don't need to share the rotation matrix with another object. What should that be good for? It inherits the global rotation from above anyway and it's own, local rotation doesn't matter, because it's bill boarded.

If it doesn't help to decouple the matrices, i'll try to create a proper test case myself and find what the actual problem is, but that will take some days, because of all the family and christmas-stuff going on right now... ;)
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 25, 2008, 03:20:50 pm
Np.

I got it wrong. A, B and C each has their own matrices.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 28, 2008, 08:45:14 pm
Ok, here we go again: http://www.jpct.net/download/beta/jpct.jar (http://www.jpct.net/download/beta/jpct.jar)

I really hope i got it right this time. I have created myself a test case, i was able to verify the problem, i found a flaw in my calculations, corrected it and my test case works fine now...i hope that your application will do to...
Title: Re: Transparent blitting
Post by: EgonOlsen on December 29, 2008, 08:02:27 pm
Is it better now?
Title: Re: Transparent blitting
Post by: paulscode on December 30, 2008, 02:40:03 am
This may not be related, but when using the above beta version of jPCT, it appears that child billboarded objects have become invisible in a few of my applets.

Here is an example:
Using old jpct.jar (http://www.paulscode.com/source/BetaAlpha/)
Using beta jpct.jar (http://www.paulscode.com/source/BetaAlpha/BetaVersion.html)

Here is the Source Code (http://www.paulscode.com/source/BetaAlpha/AlphaSource.zip) for this applet.

I haven't pinpointed exactly where this problem is coming from (some billboarded child objects are fine).  I'll tinker around with this some more to see if I can provide more information.
Title: Re: Transparent blitting
Post by: ErDetEnAnd? on December 30, 2008, 10:32:03 am
Mine aren't invisible  ???

It seems to work.

Since children inherits the scale value, its much easier for me to make the child (object c) a child of object a, because it is never scaled. Thanks alot Egon.

I guess finding a solution to the "billboards rotation around its 0;0 problem" harder to solve without help ;). http://www.jpct.net/forum2/index.php/topic,1253.15.html (http://www.jpct.net/forum2/index.php/topic,1269.0.html)
Title: Re: Transparent blitting
Post by: paulscode on December 30, 2008, 01:04:38 pm
A couple quick questions on how this works now (as this will affect how I create complex parent/child relationships).

1) Children inherit scale from their parents now, correct (or am I misreading this topic)?

2) Is the scale inheritance recursive (i.e. a grandchild inherits scale from both the parent and grandparent <asuming parent was further scaled, not just what it inheritied from grandparent>)?

3) This update affects all Object3Ds not just billboarded ones, correct?

4) When loading a model with Loader using a scale value, the resulting Object3D(s) all have an initial scale of 1, correct?

Thanks in advance!
Title: Re: Transparent blitting
Post by: EgonOlsen on December 30, 2008, 01:36:59 pm
This change only affect bill boarded object. All other should behave as before. To answer your points:

1) Yes. They should have always done this, but is was wrong for bill boards. It hasn't changed for normal objects

2) Yes

3) No, only the bill boarded behaviour has changed. The rest should stay as before, because normal objects inherit the scaling anyway

4) Yes, that's correct. The loader applies a permanent scaling, it has nothing to do with the setScale-scaling.
Title: Re: Transparent blitting
Post by: EgonOlsen on December 30, 2008, 02:22:18 pm
This may not be related, but when using the above beta version of jPCT, it appears that child billboarded objects have become invisible in a few of my applets.

The old bill boarding code was wrong. Maybe you relied somehow on this wrong behavior. The beta version should behave correctly....i think...bill boarding of scaled childs or bill boarded childs of scaled parents really sucks... ;)
Title: Re: Transparent blitting
Post by: paulscode on December 30, 2008, 10:11:16 pm
The old bill boarding code was wrong. Maybe you relied somehow on this wrong behavior. The beta version should behave correctly....i think...bill boarding of scaled childs or bill boarded childs of scaled parents really sucks... ;)

I don't think that scaling is causing my problem, unless some code that was added for it changed something else (I only posted it here since the problem seems to have appeared after the scaling bug was fixed).  The Object3Ds in the above applet are created like this:

Code: [Select]
        parent = Object3D.createDummyObj();
        glowObject = createGlow( 8.0f, 3.0f, "Red Glow" );
        world.addObject( glowObject );
       
        parent.addChild( glowObject );

(the createGlow method for reference)
Code: [Select]
    // creates a billboarded glowing object:
    public Object3D createGlow( float offset, float zoffset, String texture )
    {
        Object3D obj = new Object3D( 4 );
       
        obj.addTriangle( new SimpleVector( -offset, -offset, -zoffset ),
                         0, 0,
                         new SimpleVector( -offset, offset, -zoffset ), 0, 1,
                         new SimpleVector( offset, offset, -zoffset ), 1, 1,
                         TextureManager.getInstance().getTextureID( texture ) );
        obj.addTriangle( new SimpleVector( offset, offset, -zoffset ),
                         1, 1,
                         new SimpleVector( offset, -offset, -zoffset ), 1, 0,
                         new SimpleVector( -offset, -offset, -zoffset ), 0, 0,
                         TextureManager.getInstance().getTextureID( texture ) );
        obj.addTriangle( new SimpleVector( offset, offset, zoffset ),
                         1, 1,
                         new SimpleVector( -offset, offset, zoffset ), 0, 1,
                         new SimpleVector( -offset, -offset, zoffset ), 0, 0,
                         TextureManager.getInstance().getTextureID(
                                                              "Transparent" ) );
        obj.addTriangle( new SimpleVector( -offset, -offset, zoffset ),
                         0, 0,
                         new SimpleVector( offset, -offset, zoffset ), 1, 0,
                         new SimpleVector( offset, offset, zoffset ), 1, 1,
                         TextureManager.getInstance().getTextureID(
                                                              "Transparent" ) );
        // Make it billboard:
        obj.setBillboarding( Object3D.BILLBOARDING_ENABLED );
        // Set up the transparency:
        obj.setTransparency( 0 );
        obj.setTransparencyMode( Object3D.TRANSPARENCY_MODE_ADD );
        obj.setLighting( Object3D.LIGHTING_NO_LIGHTS );
        obj.build();
        return obj;
    }

That demo applet is the simplest example I have that experienced the change.  I'll spend a little time on this today to try and create a more basic test case, and I'll post a new thread in the Bugs section, since the problem is not really related to this thread.