sampler2D ourImage : register(s0);
float4 main(float2 locationInSource : TEXCOORD) : COLOR
{
float4 color = tex2D( ourImage , locationInSource.xy);
color.a = 0.2;
return color;
}
using System;using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using SlimDX;
using SlimDX.Direct3D9;
using Point = System.Windows.Point;
namespace AlphaBlendTesting
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Private Variables
private Device _device;
private VertexBuffer _vertexBuffer;
private static VertexDeclaration _vertexDeclaration;
private Texture _texture;
private Surface _renderTarget;
#endregion
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
InitializeDevice();
InitializeVertices();
ThreadPool.QueueUserWorkItem(delegate
{
RenderEnvironment();
});
}
private void InitializeDevice()
{
var direct3D = new Direct3D();
var windowHandle = new WindowInteropHelper(this).Handle;
var presentParams = new PresentParameters
{
Windowed = true,
BackBufferWidth = (int)SystemParameters.PrimaryScreenWidth,
BackBufferHeight = (int)SystemParameters.PrimaryScreenHeight,
// Enable Z-Buffer
// This is not really needed in this sample but real applications generaly use it
EnableAutoDepthStencil = true,
AutoDepthStencilFormat = Format.D16,
// How to swap backbuffer in front and how many per screen refresh
BackBufferCount = 1,
SwapEffect = SwapEffect.Copy,
BackBufferFormat = direct3D.Adapters[0].CurrentDisplayMode.Format,
PresentationInterval = PresentInterval.Default,
DeviceWindowHandle = windowHandle
};
_device = new Device(direct3D, 0, DeviceType.Hardware, windowHandle, CreateFlags.SoftwareVertexProcessing | CreateFlags.Multithreaded, presentParams);
var shaderByteCode = ShaderBytecode.Compile(File.ReadAllBytes(@"EdgeBlenDing.fx"), "main", "ps_2_0", ShaderFlags.None);
var pixelShader = new PixelShader(_device, shaderByteCode);
_device.PixelShader = pixelShader;
}
private void InitializeVertices()
{
_renderTarget = _device.GetRenderTarget(0);
var vertexBuffer = new VertexBuffer(_device, 6 * Vertex.SizeBytes, Usage.WriteOnly, VertexFormat.Normal, Pool.Managed);
using (DataStream stream = vertexBuffer.Lock(0, 0, LockFlags.None))
{
stream.WriteRange(BuildVertexData());
vertexBuffer.Unlock();
}
_vertexBuffer = vertexBuffer;
//Setting the vertex elements
var vertexElems = new[]
{
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position , 0),
new VertexElement(0, 12 , DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
VertexElement.VertexDeclarationEnd
};
//Declaring the vertex
_vertexDeclaration = new VertexDeclaration(_device, vertexElems);
SetRenderState();
_texture = Texture.FromFile(_device, @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
}
private void SetRenderState()
{
// Turn off culling, so we see the front and back of the triangle
_device.SetRenderState(RenderState.CullMode, Cull.None);
// Turn off lighting
_device.SetRenderState(RenderState.Lighting, false);
}
private void RenderEnvironment()
{
while (true)
{
try
{
_device.BeginScene();
_device.Clear(ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
_device.SetTexture(0, _texture);
_device.SetRenderTarget(0, _renderTarget);
_device.VertexDeclaration = _vertexDeclaration;
_device.SetStreamSource(0, _vertexBuffer, 0, Vertex.SizeBytes);
_device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
_device.EndScene();
//Show what we draw
_device.Present();
}
catch (Exception e)
{
}
}
}
private Vertex[] BuildVertexData()
{
var bottomLeftVertex = new Point(0, 0);
var topLeftVertex = new Point(0, 1);
var bottomRightVertex = new Point(1, 0);
var topRightVertex = new Point(1, 1);
var vertexData = new Vertex[6];
vertexData[0].Position = new Vector3(-1.0f, 1.0f, 0.0f);
vertexData[0].TextureCoordinate = new Vector2((float)bottomLeftVertex.X, (float)bottomLeftVertex.Y);
vertexData[1].Position = new Vector3(-1.0f, -1.0f, 0.0f);
vertexData[1].TextureCoordinate = new Vector2((float)topLeftVertex.X, (float)topLeftVertex.Y);
vertexData[2].Position = new Vector3(1.0f, 1.0f, 0.0f);
vertexData[2].TextureCoordinate = new Vector2((float)bottomRightVertex.X, (float)bottomRightVertex.Y);
vertexData[3].Position = new Vector3(-1.0f, -1.0f, 0.0f);
vertexData[3].TextureCoordinate = new Vector2((float)topLeftVertex.X, (float)topLeftVertex.Y);
vertexData[4].Position = new Vector3(1.0f, -1.0f, 0.0f);
vertexData[4].TextureCoordinate = new Vector2((float)topRightVertex.X, (float)topRightVertex.Y);
vertexData[5].Position = new Vector3(1.0f, 1.0f, 0.0f);
vertexData[5].TextureCoordinate = new Vector2((float)bottomRightVertex.X, (float)bottomRightVertex.Y);
return vertexData;
}
[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector3 Position;
public Vector2 TextureCoordinate;
public static int SizeBytes
{
get { return Marshal.SizeOf(typeof(Vertex)); }
}
}
}
I am unable to figure out why alpha value is not getting set. Any kind of help is appreciated.