www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: AGP on May 28, 2009, 04:07:29 am

Title: Texture.setAlpha(int)
Post by: AGP on May 28, 2009, 04:07:29 am
I thought when I saw this method in the documentation that I would be able to select a texture then say texture.setAlpha(Color.blue.getRGB()), and that would make every blue area completely transparent. Since there's nothing in the docs to explain what it's supposed to do, what exactly is it supposed to do? And more importantly to me, how do I achieve the same result as the one I expected? Thanks in advance.
Title: Re: Texture.setAlpha(int)
Post by: EgonOlsen on May 28, 2009, 07:19:33 am
...then say texture.setAlpha(Color.blue.getRGB()), and that would make every blue area completely transparent....
Pretty similar...just do texture.setAlpha(color.getAlpha());. However, with Color.blue, your alpha will be all opaque. The reasonable range range for alpha is (like in Color) 0..255.
Title: Re: Texture.setAlpha(int)
Post by: AGP on May 28, 2009, 08:10:16 am
I'm confused. I want to tell the renderer the color blue should be invisible. Is that doable? And I tried Color.blue.getAlpha() as well to the same result, but I guess you just explained that its alpha would be opaque. But isn't the purpose of the setAlpha(int) method to make a given area transparent?
Title: Re: Texture.setAlpha(int)
Post by: EgonOlsen on May 28, 2009, 12:36:50 pm
No, that's not what it does. What it does, is to set the alpha value of the whole texture to ONE value. If you want to have specific colors transparent, you can create your own image with an alpha channel and use that to create a texture from.
Title: Re: Texture.setAlpha(int)
Post by: AGP on May 28, 2009, 07:51:15 pm
I'll post two screenshots later today of what I'm writing about, but right now suffice to write that the alpha areas in my png texture are showing up as white. Is it possible to make them invisible?
Title: Re: Texture.setAlpha(int)
Post by: paulscode on May 28, 2009, 11:13:19 pm
If your alpha areas are all the same color and opacity, here is one way to make the white areas transparent if you have Photoshop (I don't know about other graphics editors):

Create a new image the same size as the texture image, making sure that "transparent" is selected.  The new image should look like a checkerboard patern indicating that it is transparent.  Select-all on your original texture image and copy.  Then paste onto the new transparent checkerboard image.  Finally, using the magic wand tool, select the white areas and press delete.  This will turn them from white to transparent (which will look "checkerboardy").  Save in the .png format to retain alpha information.

If that isn't your problem (i.e. if your alpha areas are already transparent but still not showing up correctly in jPCT), check a couple of things in your code:

Make sure you use true for the useAlpha parameter when creating your Texture instance:
Code: [Select]
TextureManager.getInstance().addTexture( "GLOWBALL.PNG",
                            new Texture( getClass().getClassLoader().
                                  getResourceAsStream( "Textures/GlowBall.png" ),
                                  true ) );

Play around with the int parameter for setTransparency:
Code: [Select]
glowBall.setTransparency( 0 );
Depending on what you are using the texture for, you may want to change the transparency mode or eliminate external lighting:
Code: [Select]
glowBall.setTransparencyMode( Object3D.TRANSPARENCY_MODE_ADD );   ...for example...
Code: [Select]
glowBall.setLighting( Object3D.LIGHTING_NO_LIGHTS );
glowBall.setAdditionalColor( ...whatever... );

--EDIT--
Another way if there is only one single transparent color in your texture, then change that color to black (color 0,0,0), and use a higher number int parameter for the setTransparency method (say 100 or so).  In this case, you would want to use false for the useAlpha parameter when creating your Texture instance.  If there are other black places in your texture, make sure they are a "different" black (say color 5, 5, 5 or something).
Title: Re: Texture.setAlpha(int)
Post by: AGP on May 29, 2009, 12:24:13 am
Thanks for your suggestions, although none of them worked and my images have no background. I even tried the black thing, and setting useAlpha to false. It is supposed to work on the software OpenGL, right? And another question: the docs for setAdditionalColor(java.awt.Color) say it's possible for you to set a color above 255, 255, 255 for lightsources and such, except that the JRE throws an IllegalArgumentException when your values are brighter than white. So what gives?

EDIT: I should specify that this object has a million textures, not just the one. Does that matter?
Title: Re: Texture.setAlpha(int)
Post by: paulscode on May 29, 2009, 02:03:44 am
I even tried the black thing, and setting useAlpha to false. It is supposed to work on the software OpenGL, right?
That's really odd.  The "black transparent color" method works fine for me in software rendering mode.  Here is an example:

Tree Viewer (http://www.paulscode.com/demos/jpct/TreeViewer/)  (Source Code (http://www.paulscode.com/demos/jpct/TreeViewer/TreeViewerSource.zip))

The leaves of the tree have black in the transparent areas.  I made the background of this applet grey so you can see it show through where the texture is transparent.  I am not sure what I did differently in this applet to make it work, but perhaps you can notice something from the source code.

EDIT: I should specify that this object has a million textures, not just the one. Does that matter?
Hmm, I don't really know.  It shouldn't matter, but I think I'll put together a simple test case of an object with multiple textures and transparency.  In your case, I assume the object a single Object3D instance, correct? (I want my test case to match what you are doing)
Title: Re: Texture.setAlpha(int)
Post by: AGP on May 29, 2009, 04:16:11 am
It's a single 3ds file with multiple parts to it, but are merged into one via Object3D.mergeObjects(Object3D,Object3D). Let me have a look at your code and get right back to you. And thanks again.
Title: Re: Texture.setAlpha(int)
Post by: AGP on May 29, 2009, 07:06:46 pm
Yeah, I did almost exactly the same thing you did, except that my model is far bigger, and each segment of it is made up of many parts, each of which with different textures. Plus, I loaded my textures before the model (as per jPCT norm) and I never called model.setTexture(Texture). Other than that, I did the same thing, so that leads me to believe that it's the multi-texture thing. Egon?
Title: Re: Texture.setAlpha(int)
Post by: EgonOlsen on May 29, 2009, 07:55:51 pm
You can't merge transparent and none-transparent objects into one. Well, actually you can, but you'll lose transparency with that, because it's per object in jPCT. Try to merge transparent and opaque parts of the model into separate objects. Maybe that helps.
Title: Re: Texture.setAlpha(int)
Post by: AGP on May 29, 2009, 08:29:26 pm
OK, good, so that answers it. I'm not sure how I can change this object, which is REALLY complex, but I'll try later and at least I know why it wasn't working. Thanks a lot.