Alpha blending doesn't work correctly

The place for artists, modellers, level designers et al to discuss their approaches for creating content for OGRE.
Post Reply
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Alpha blending doesn't work correctly

Post by nsxeagle »

Hi! :)

I am developing a space strategy game with Ogre and I just started to make some 3D models of suns.
My first attempt was to use two spheres (one inside another) to display a sun. The inner sphere represents the surface of the sun and the outer sphere the corona. The suns are emissive, since there are no lights in my scene.
Now I encountered problems with alpha-blending. I read some topics about problems with alpha-blending in this forum, but none of the solutions worked for me. :-(

So, first a picture of how my suns look now:
Image

As you can see, the corona is displayed, but there seems to be a black surface which blots out the sun's surface completely. I don't think that the "black" surface is in fact transparent because you cannot see the orange sun through the red one.

In addition, I'll post my material script:

Code: Select all

material RedSun/Surface
{
  receive_shadows off
  technique
  {
    pass
    {
      ambient 0.0 0.0 0.0
      diffuse 0.0 0.0 0.0
      specular 0.0 0.0 0.0 0.0
      emissive 1.0 1.0 1.0

      texture_unit
      {
        scroll_anim -0.002 0.0
        texture RedSun.png
      }
    }
  }
}

material RedSun/Corona
{
  receive_shadows off
  technique
  {
    pass
    {
      ambient 0.0 0.0 0.0
      diffuse 0.0 0.0 0.0
      specular 0.0 0.0 0.0 0.0
      emissive 1.0 1.0 1.0
      scene_blend alpha_blend
      depth_write off

      texture_unit
      {
        scroll_anim 0.002 0.0
        wave_xform scale_y sine 0.0 0.005 0.0 0.4
        colour_op alpha_blend
        texture RedCorona.png
      }
    }
  }
}
Dou you have any suggestions about what is going wrong? :(
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 67
Contact:

Post by sinbad »

1. You don't need colour_op alpha_blend - that's for blending between the texture units and you only have one anyway
2. Perhaps your texture has no alpha channel
3. You should specify 'lighting off' if you don't need lighting, it will increase performance

Alpha blending works just fine - see the OGRE logo in the bottom right of the demos for proof of that. That's defined in OgreCore.zip, check the definition out.
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

Oh, I didn't mean that Ogre's alpha blending doesn't work in general. :oops:
I was only a little bit frustrated because it took me a whole month to see that alpha blending still does not want to work when I get involved in 3d modelling. :( Sorry for that.

1. Oops, I must have missed that. :D
2. Really? Then what exactly is my photoshop exporter doing? :wink: Hm, but I though about that point, too. I have Adobe Photoshop 6 and a plugin for importing and exporting DDS files. Photoshop says that my image contains transparency and I export it to DDS with an alpha channel. So there shouldn't be anything wrong, but in fact, I am not sure. I'll check this out again! :)
3. Thank you for this little tip!

At this moment, I am so frustrated to think about learning how to program vertex and pixel shaders. :shock:
But I'll try and try until I have this little bug! :D
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

Try this: Save your image as a PNG file, which also can store an alpha channel. Then, open an OGRE example material script, take one material that uses alpha blending, and change the texture filename the example material points to, and use yours. In the demo where that material is used, you should see transparency.

Anyways, a good way to see if the alpha channel was saved correctly is to close photoshop and open the image again. You should be able to see the image transparency there ;)
Image
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

Ha! The corona is alpha-blended. Notice that you can see a black sphere inside the corona (you can see it best where the yellow sun "shines through" the corona of the red one):
Image

Now my problem is that the inner sphere (that means the sun itself) is black.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

If you put up a skybox in a color other than black it would be way easier to see if it really is transparent. :wink:
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

Have you tried to use one of the example materials? Maybe it's just a problem of the unlit scene... :?
Last edited by Kencho on Wed Apr 27, 2005 11:36 pm, edited 1 time in total.
Image
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

@jacmoe: Now you can see it more easily.

Image

[EDIT]
I am coming near the solution. First, a second screenshot. Notice the red sun is shown correctly without the corona:

Image

These are the materials:

Code: Select all

material RedSun/Surface
{
  receive_shadows off
  lighting off
  technique
  {
    pass
    {
      emissive 1.0 1.0 1.0 0.0

      texture_unit
      {
        scroll_anim -0.002 0.0
        texture RedSun.png
      }
    }
  }
}

material RedSun/Corona
{
  receive_shadows off
  lighting off
  technique
  {
    pass
    {
      emissive 1.0 1.0 1.0
      //scene_blend alpha_blend

      texture_unit
      {
        scroll_anim 0.002 0.0
        wave_xform scale_y sine 0.0 0.005 0.0 0.4
        texture RedCorona.png
      }
    }
  }
}
When I remove the comment, the scene looks like the screenshot before.
[/EDIT]
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 67
Contact:

Post by sinbad »

'lighting off' should be in the pass. Please check your Ogre.log for errors.

You've removed 'depth_write off' from your transparent material. Put it back, you need it.
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8
Contact:

Post by haffax »

scene_blend alpha_blend is needed to, you need to uncomment it.
Are you sure your png file has an alpha channel?
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

@tanis:
Which PNG file do you mean? The corona surely has an alpha channel (see the upper screenshot of my last post). Does the sun's surface need an alpha channel? The object is fully opaque, so I don't need one, do I?
I know that I need scene_blend alpha_blend. I just turned it off to see what happens then. :wink:

But I think that something isn't quite correct with my textures. It can't be the material script anymore, can it? I'll check my textures by trying to use them in Ogre demos (like Kencho said) over the weekend. I'll tell you guys the result, then. :)
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8
Contact:

Post by haffax »

Code: Select all

material alpha_test
{
  technique
  {
    pass
    {
      scene_blend alpha_blend 
      depth_write off

      texture_unit
      {
        // texture with included alpha channel
        texture mytexture.png
      }
    }
  }
}
This is a minimal material script for alpha blending. I took it from our team's internal wiki and I know it works. Try it for your material and see what happens. If you get no alpha blending, then it is very probably because of your texture, yes.
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

Ok, guys! You were right! Mea culpa! :)

The whole material script issue is solved because I know now that my limited skills with Photoshop are responsible for my problem.
The main problem was that I got confused with the transparent background color and the existence of an alpha channel. I thought these two were the same. In addition, Photoshop seems not to save alpha channels in PNG files. :( But it does in TGA format, so I'll use this for my experiments.

I am not at home this weekend, so don't expect any results before monday. :)
User avatar
Bren
Goblin
Posts: 215
Joined: Tue Jul 08, 2003 4:41 pm
Location: 0,0,0
Contact:

Post by Bren »

nsxeagle wrote:In addition, Photoshop seems not to save alpha channels in PNG files. :( But it does in TGA format, so I'll use this for my experiments.
Yeah that's true. TGA would be fine for your tests, but eventually you'll almost certainly want to get the benefits of compressed PNGs. I would suggest you get GIMP and use it to create your PNGs, and/or convert the TGAs you make in PS to PNGs.

Looking forward to more screenshots. :)
qsilver
OGRE Community Helper
OGRE Community Helper
Posts: 198
Joined: Sat Oct 02, 2004 9:11 am
Location: San Francisco, California, USA

Post by qsilver »

It's been a while since I used Photoshop, but I think that if you use "Save As" and not "Export", transparent PNGs come out OK.
User avatar
IoN_PuLse
Goblin
Posts: 220
Joined: Mon May 31, 2004 5:54 am
Location: Canada
Contact:

Post by IoN_PuLse »

Yes, I also recommend using the Gimp to at least save your PNG textures. It has much better file format support than PS.
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

Hah! I am just back ten minutes ago and I have found something very important.

@sinbad: Thank you for opening my eyes. I read a post from you in another topic where you said: "Look into the Ogre logfile!!". I did it and I found something:

Code: Select all

23:20:56: Parsing script RedSun.material
23:20:56: Error at line 1 of RedSun.material: Unrecognised command: material
23:20:56: Error at line 2 of RedSun.material: Unrecognised command: {
23:20:56: Error at line 3 of RedSun.material: Unrecognised command: receive_shadows
23:20:56: Error at line 4 of RedSun.material: Unrecognised command: technique
23:20:56: Error at line 5 of RedSun.material: Unrecognised command: {
23:20:56: Error at line 6 of RedSun.material: Unrecognised command: pass
23:20:56: Error at line 7 of RedSun.material: Unrecognised command: {
23:20:56: Error at line 8 of RedSun.material: Unrecognised command: lighting
23:20:56: Error at line 10 of RedSun.material: Unrecognised command: texture_unit
23:20:56: Error at line 11 of RedSun.material: Unrecognised command: {
23:20:56: Error at line 13 of RedSun.material: Unrecognised command: texture
23:20:56: Error at line 14 of RedSun.material: Unexpected terminating brace.
23:20:56: Error at line 15 of RedSun.material: Unexpected terminating brace.
23:20:56: Error at line 16 of RedSun.material: Unexpected terminating brace.
23:20:56: Error at line 17 of RedSun.material: Unexpected terminating brace.
This is the material file RedSun.material, line 1 to 17:

Code: Select all

material RedSun/Surface
{
  receive_shadows off
  technique
  {
    pass
    {
      lighting off

      texture_unit
      {
        //scroll_anim -0.002 0.0
        texture RedSun.png
      }
    }
  }
}
So that's the reason why my sun's surfaces are all black! These errors occur in every material script I have. After the definition of my sun's surface material, there is the corona material which is parsed without errors!? :shock:

Has anybody any suggestions?
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 67
Contact:

Post by sinbad »

Do you have any lines above that first definition? Any funny characters?
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2
Contact:

Post by Kencho »

Unless there is a line 0, I doubt there is anything before that. But the copy/paste shows an empty line at the beginning. Maybe it's that :?
Image
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 67
Contact:

Post by sinbad »

No, empty lines aren't a problem. But 'funny' characters might be. I think we need to see the real file.
User avatar
:wumpus:
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3067
Joined: Tue Feb 10, 2004 12:53 pm
Location: The Netherlands
x 1

Post by :wumpus: »

I don't know what a '' is but I see one before the material, at least in the log.
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

Here is my material file: Red Sun Material.

I added an empty line to the beginning of the material file (as you can see) and now the Ogre says:

Code: Select all

...
17:27:46: Parsing script RedSun.material
17:27:46: Error at line 1 of RedSun.material: Unrecognised command: 
17:27:46: Parsing script WhiteSun.material
...
:D :D :D Now this looks sweet. At least both materials are recognized. Ah, now that's weird: The editbox of the Ogre forum tells me that there is a character behind the "Unrecognised command: ". Its charcode is #65279, which is: "65279 - FEFF -  - ZERO WIDTH NO-BREAK SPACE". Hmm, I think I know why this "error" occured. I will save the file again in ASCII mode (though I love Unicode and hate ASCII). :(
User avatar
:wumpus:
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 3067
Joined: Tue Feb 10, 2004 12:53 pm
Location: The Netherlands
x 1

Post by :wumpus: »

Nothing wrong with UTF8, but ogre requires real (as in codepoint 0x20) spaces.
User avatar
nsxeagle
Gnoblar
Posts: 16
Joined: Tue Oct 26, 2004 6:17 pm
Location: Germany
Contact:

Post by nsxeagle »

:D :D I solved all my problems!!! :D :D

It was not a wrong material script (but thank you very much for your help and tips regarding alpha blending :) ), no wrong texture formats, but a simple mistake when saving my material files.... It seems that my "editor" (Windows Notepad) has his own interpretation of what UTF-8 is... :(

But now everything is fine and as a little reward, you get the screenshot of the scene how it should REALLY look like a week before:
Image

This should have been only a first attempt, so I think the coronas are too small for suns. But it should work great for planets. :)

@All: Thank you for your patience! :D

EDIT:
When I implement a little bit more, I will show you new screenshots. :)
/EDIT:
Post Reply