Jump to content

  • Log In with Google      Sign In   
  • Create Account


Losing my alpha channel somewhere !


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Gavin Williams   Members   -  Reputation: 658

Like
0Likes
Like

Posted 23 January 2012 - 09:25 PM

I'm trying to render a quad (with character sprite from a spritesheet) over the top of my terrain. But I seem to be losing my alpha data, I'm saving my 32bit bitmap out of photoshop, and I've checked that the alpha channel is still there using Pixelformer (which shows alpha clearly)

Posted Image

Here's the shader code that should demonstrate the alpha channel :

float4 finalColor = gSpriteSheet.Sample(PointSampler, input.texcoord);
// ------------------------------------------------------------------------
// final composition
// ------------------------------------------------------------------------
return float4(finalColor.a,0,0,1);

But all I get is red !

What could I be doing wrong ?

Here's my rendertarget construction :

		    // create DirectX11 device
		    CreateDevice11(form, out swapChain, out device11);
		    // create depth buffer
		    CreateDepthBuffer(form.ClientSize.Width, form.ClientSize.Height);
		    // create a view of our render target, which is the backbuffer of the swap chain we just created
		    RenderTargetView renderTarget;
		    using (Resource resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
			    renderTarget = new RenderTargetView(device11, resource);
		    // setting a viewport is required if you want to actually see anything
		    var context = device11.ImmediateContext;
		    //var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
		    context.OutputMerger.SetTargets(renderTarget);

and here's my device/swapchain construction :

	    private static Result CreateDevice11(RenderForm form, out SwapChain swapChain, out Device device11)
	    {
		    var description = new SwapChainDescription()
		    {
			    BufferCount = 2,
			    Usage = Usage.RenderTargetOutput,
			    OutputHandle = form.Handle,
			    IsWindowed = true,
			    ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
			    SampleDescription = new SampleDescription(1, 0),
			    Flags = SwapChainFlags.AllowModeSwitch,
			    SwapEffect = SwapEffect.Discard
		    };
		    return SlimDX.Direct3D11.Device.CreateWithSwapChain(adapter1, DeviceCreationFlags.Debug, description, out device11, out swapChain);
	    }


Sponsor:

#2 iedoc   Members   -  Reputation: 934

Like
1Likes
Like

Posted 23 January 2012 - 11:19 PM

return float4(finalColor.a, 0, 0, 1);

this will return red. you should do it like this:

return float4(Red, Green, Blue, Alpha);

if your just trying to return the color of the texel in the sprite sheet with the alpha value, you can just do this:

return finalColor;

if you just want to completely not draw the transparent parts, you can clip, or stop pixels with a certain alpha value from even getting processed like this:

float4 finalColor = gSpriteSheet.Sample(PointSampler, input.texcoord);
clip(finalColor.a - .25);
return finalColor;

so anything with an alpha value of .25 or less will not be rendered.
Braynzar Soft - DirectX Lessons & Game Programming Resources!

#3 Gavin Williams   Members   -  Reputation: 658

Like
0Likes
Like

Posted 24 January 2012 - 12:13 AM

return float4(finalColor.a, 0, 0, 1);
this code is just to demonstrate that all alpha values are coming through as 1.0, rather than the mix of 0 & 1 (which the texure contains). In other words, when rendering float4(finalColor.a,0,0,1); I would expect the areas of the texture with alpha=1.0 to be rendered red and the areas with alpha = 0 to be rendered black. But this is not happening, by the time the texture gets to the shader, the whole alpha plane is 1.0.

So when i use return finalColor; I don't get the alpha values that are present in the texture at design time.

#4 iedoc   Members   -  Reputation: 934

Like
0Likes
Like

Posted 24 January 2012 - 01:19 AM

ooh, i get what you were trying to do, sorry for not seeing that, haha

have you tried using a different format? like jpeg? I've come across other people having problems with directx, bmp, and alpha channels before. maybe the alpha channel in the bmp is not being recognized, my only suggestion at the moment

also, i don't think you would want to use a format like bmp anyway since it's a larger file than better compressed formats like jpeg or png
Braynzar Soft - DirectX Lessons & Game Programming Resources!

#5 Gavin Williams   Members   -  Reputation: 658

Like
0Likes
Like

Posted 24 January 2012 - 02:56 AM

Well it looks to me like photoshop can't save png with alpha, there is a warning sign next to alpha channels in the save box. And when i look at the image in Pixelformer, the background is black. I'm so bored with all these alpha channel issues, I don't understand why these issues still exist.

#6 Gavin Williams   Members   -  Reputation: 658

Like
0Likes
Like

Posted 24 January 2012 - 02:57 AM

Jpeg doesn't support alpha either out of photoshop.

#7 Gavin Williams   Members   -  Reputation: 658

Like
0Likes
Like

Posted 24 January 2012 - 03:27 AM

I just can't believe that Adobe think like this .... to save transparency in png format I had to remove the transparency channel and only save it as an rgb. That is so so counter-intuitive, that i would say it is completely disfunctional reasoning in software design. So very frustrating that some things are like this in software. And why haven't MS fixed the issue with bitmaps not loading alpha ? Do I not understand alpha and it's usage or do these giant companies who make this stuff not understand it ?

Anyway, I have transparency in my quads now, and thanks for introducing me to the clip function iedoc, that's a handly little thing aint it.

#8 iedoc   Members   -  Reputation: 934

Like
0Likes
Like

Posted 24 January 2012 - 03:48 AM

yeah that clip function really is nice.

I'm glad to see you got it working. That really is strange your having problems with photoshop exporting the alpha channel. I always use jpg and alpha seems to be fine. Another format you might be interested in is the tiff format, which seems to be a common format for a lot of games. I'm not real experienced though, so i couldn't tell you which one is best ;)
Braynzar Soft - DirectX Lessons & Game Programming Resources!

#9 mhagain   Crossbones+   -  Reputation: 7866

Like
0Likes
Like

Posted 24 January 2012 - 04:06 AM

Bitmaps not having alpha is not an issue, it's part of the bitmap specification. If MS were to change that, imagine all the legacy programs that would need to be re-tested. It's not going to happen.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#10 kauna   Crossbones+   -  Reputation: 2363

Like
1Likes
Like

Posted 24 January 2012 - 04:46 AM

Hi,

you should use DDS format (there is an export plugin from NVidia for Photoshop).
It allows you to control exactly the format of the loaded image. Exactly in the sense that dds format contains the data in the format which is used by the hardware and there is no need to do any (unless the format isn't supported of course) conversion at the load time.

Cheers!

#11 Gavin Williams   Members   -  Reputation: 658

Like
0Likes
Like

Posted 24 January 2012 - 05:48 AM

Thank you kauna, It took me no time to test alpha blending with a texture in dds format. I never knew about that exporter. So very helpful.

#12 Postie   Members   -  Reputation: 932

Like
0Likes
Like

Posted 24 January 2012 - 08:06 AM

Sounds like you've solved your problem already by switching to the dds format, but thought I'd mention I had exactly the same problem with png's output from Photoshop losing their Alpha Channel. It looks as though Photoshop converts the png to use pre-multiplied alpha when loading the file. The thing is, the png I was editing was originally exported from Illustrator, another Adobe product, and guess what... It writes the alpha channel in the png properly. Bizarre huh? Not sure why those two products are inconsistent.

I discovered a plugin for Photoshop called SuperPNG which fixes the problem. To load the png, you select "Open As" and then pick the super png format, and the alpha channel is properly imported as a separate channel.
Currently working on an open world survival RPG - For info check out my Development blog: ByteWrangler




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS