Page 1 of 1

Alpha blending doesn't work correctly

Posted: Tue Apr 26, 2005 3:57 pm
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? :(

Posted: Wed Apr 27, 2005 5:51 pm
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.

Posted: Wed Apr 27, 2005 10:55 pm
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

Posted: Wed Apr 27, 2005 11:00 pm
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 ;)

Posted: Wed Apr 27, 2005 11:10 pm
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.

Posted: Wed Apr 27, 2005 11:13 pm
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:

Posted: Wed Apr 27, 2005 11:15 pm
by Kencho
Have you tried to use one of the example materials? Maybe it's just a problem of the unlit scene... :?

Posted: Wed Apr 27, 2005 11:26 pm
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]

Posted: Thu Apr 28, 2005 12:29 am
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.

Posted: Thu Apr 28, 2005 7:20 am
by haffax
scene_blend alpha_blend is needed to, you need to uncomment it.
Are you sure your png file has an alpha channel?

Posted: Thu Apr 28, 2005 9:01 am
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. :)

Posted: Thu Apr 28, 2005 10:04 am
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.

Posted: Thu Apr 28, 2005 1:25 pm
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. :)

Posted: Thu Apr 28, 2005 5:51 pm
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. :)

Posted: Thu Apr 28, 2005 6:26 pm
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.

Posted: Thu Apr 28, 2005 8:50 pm
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.

Posted: Sun May 01, 2005 10:38 pm
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?

Posted: Sun May 01, 2005 11:45 pm
by sinbad
Do you have any lines above that first definition? Any funny characters?

Posted: Mon May 02, 2005 10:54 am
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 :?

Posted: Mon May 02, 2005 12:24 pm
by sinbad
No, empty lines aren't a problem. But 'funny' characters might be. I think we need to see the real file.

Posted: Mon May 02, 2005 1:36 pm
by :wumpus:
I don't know what a '' is but I see one before the material, at least in the log.

Posted: Mon May 02, 2005 4:41 pm
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). :(

Posted: Mon May 02, 2005 4:56 pm
by :wumpus:
Nothing wrong with UTF8, but ogre requires real (as in codepoint 0x20) spaces.

Posted: Mon May 02, 2005 5:04 pm
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: