Grayscale Image Transitions Using SlimDX
#1 Members - Reputation: 100
Posted 31 May 2011 - 11:28 PM
Can any body please give me an idea how can i perform that or can post some code to give me an idea.
Thanks
#2 Senior Staff - Reputation: 4404
Posted 03 June 2011 - 05:23 AM
I've actually done this in SlimDX, but my code is a little elaborate to post here.
http://wpffx.codeplex.com/
Here's a list of transitions they have available:
BandedSwirl, Blings, Blood, CircleReveal, CircleStretch, CircularBlur, CloudReveral, Cloudy, Crumble, Dissolve, DropFade, Fade, LeastBright, LineReveal, MostBright, PixelateIn, PixelateOut, Pixelate, RadialBlur, RadialWiggle, RandomCircleReveal, Ripple, Rotate, Saturate, Shrink, SlideIn, SmoothSwirl, Swirl, Water, Wave.
#4 Members - Reputation: 199
Posted 07 June 2011 - 01:35 PM
Can you please post a little bit of the main working code. I will be grateful to you
Are you familiar with HLSL and how to set that up on the CPU side? If not, it's too much to go into in a thread post. If you do know how- then obtaining your greyscale would be easy:
Color.rgb = (Color.r+Color.g+Color.b)/3.0f;
#5 Members - Reputation: 100
Posted 08 June 2011 - 02:19 AM
#6 Members - Reputation: 593
Posted 08 June 2011 - 03:41 AM
Can you please post a little bit of the main working code. I will be grateful to you
Are you familiar with HLSL and how to set that up on the CPU side? If not, it's too much to go into in a thread post. If you do know how- then obtaining your greyscale would be easy:
Color.rgb = (Color.r+Color.g+Color.b)/3.0f;
A better formula to use would be:
Color.rgb = (Color.r * 0.299f + Color.g * 0.587f + Color.b * 0.114f);
The reason is that in terms of luminance (which is what the grayscale value is a representation of), the three colour primaries are not equally weighted. Use of those coefficients is fairly standard in image processing.
#8 Members - Reputation: 199
Posted 08 June 2011 - 09:34 AM
How can I use my HLSL .fx File in my slimDx code
Check out the "Loading, Compiling and Creating Shaders" section...
#9 Members - Reputation: 100
Posted 14 June 2011 - 04:29 AM
How can I use these shader files in my slimdx c# code. I have gone through the examples of the pixel shader and vertex shader but they are not clear enough to show the use of pixel shader and vertex shader. please help me out to show the use of pixel shader and the vertex shader in slimdx or directx using c#.
#10 Members - Reputation: 100
Posted 14 June 2011 - 07:31 AM
sampler inputImage;
sampler productImage;
float4 Timers;
struct PS_INPUT
{
float2 texCoord:TEXCOORD0;
};
float4 main(PS_INPUT In):COLOR
{
float xPosition=Timers.w*1.1;
float2 newCoords;
float4 c=.5;
xPosition=In.texCoord.x-xPosition;
if((xPosition<0)||(xPosition>1))
{
c=tex2D(productImage,In.texCoord);
}
else
{
newCoords.y=In.texCoord.y;
newCoords.x=xPosition;
c=tex2D(inputImage,newCoords);
}
return c;
} This is an hlsl code for transition between two images , can any body tell me that how can i use it in the real implementation in slimdx
#12 Members - Reputation: 199
Posted 15 June 2011 - 08:02 AM
Can anybody please help me out in doing this
Pixel Shader files end in ".fx". I did a google search for wpf pixel shaders and returned over 200,000 results. Here's a link to the first tutorial who is doing the exact same thing as you.
http://bursjootech.blogspot.com/2008/06/grayscale-effect-pixel-shader-effect-in.html
DirectX - SlimDx - XNA, thery're all based off the same platform pretty much so the only different is the language used to access them and who is resonsible for memory clean up. In other words, you can take C++ directx code and easily turn it into XNA or SlimDX assembly. Just keep in mind that the XNA platform uses the right handed cordinate system.
Good luck!
#14 Members - Reputation: 100
Posted 16 June 2011 - 08:40 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using SlimDX.Direct3D9;
using SlimDX;
using SlimDX.Windows;
using System.Drawing;
using System.Threading;
namespace WindowsFormsApplication1
{
// Vertex structure.
[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector3 Position;
public float Tu;
public float Tv;
public static int SizeBytes
{
get { return Marshal.SizeOf(typeof(Vertex)); }
}
public static VertexFormat Format
{
get { return VertexFormat.Position | VertexFormat.Texture1; }
}
}
static class Program
{
public static Device D3DDevice; // Direct3D device.
public static VertexBuffer Vertices; // Vertex buffer object used to hold vertices.
public static Texture Image; // Texture object to hold the image loaded from a file.
public static int time; // Used for rotation caculations.
public static float angle; // Angle of rottaion.
public static Form1 Window =new Form1();
public static string filepath;
static VertexShader vertexShader = null;
static ConstantTable constantTable = null;
static ImageInformation info;
[STAThread]
static void Main()
{
filepath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Garden.jpg";
info = new ImageInformation();
info = ImageInformation.FromFile(filepath);
PresentParameters presentParams = new PresentParameters();
// Below are the required bare mininum, needed to initialize the D3D device.
presentParams.BackBufferHeight = info.Height; // BackBufferHeight, set to the Window's height.
presentParams.BackBufferWidth = info.Width+200; // BackBufferWidth, set to the Window's width.
presentParams.Windowed =true;
presentParams.DeviceWindowHandle = Window.panel2 .Handle; // DeviceWindowHandle, set to the Window's handle.
// Create the device.
D3DDevice = new Device(new Direct3D (), 0, DeviceType.Hardware, Window.Handle, CreateFlags.HardwareVertexProcessing, presentParams);
// Create the vertex buffer and fill with the triangle vertices. (Non-indexed)
// Remember 3 vetices for a triangle, 2 tris per quad = 6.
Vertices = new VertexBuffer(D3DDevice, 6 * Vertex.SizeBytes, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
DataStream stream = Vertices.Lock(0, 0, LockFlags.None);
stream.WriteRange(BuildVertexData());
Vertices.Unlock();
// Create the texture.
Image = Texture.FromFile(D3DDevice,filepath );
// Turn off culling, so we see the front and back of the triangle
D3DDevice.SetRenderState(RenderState.CullMode, Cull.None);
// Turn off lighting
D3DDevice.SetRenderState(RenderState.Lighting, false);
ShaderBytecode sbcv = ShaderBytecode.CompileFromFile("C:\\Users\\yashwinder singh\\Desktop\\vertexShader.vs", "vs_main", "vs_1_1", ShaderFlags.None);
constantTable = sbcv.ConstantTable;
vertexShader = new VertexShader(D3DDevice, sbcv);
ShaderBytecode sbc = ShaderBytecode.CompileFromFile("C:\\Users\\yashwinder singh\\Desktop\\pixelShader.txt", "ps_main", "ps_3_0", ShaderFlags.None);
PixelShader ps = new PixelShader(D3DDevice, sbc);
VertexDeclaration vertexDecl = new VertexDeclaration(D3DDevice, new[] {
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
new VertexElement(0, 12, DeclarationType.Float2 , DeclarationMethod.Default, DeclarationUsage.TextureCoordinate , 0),
VertexElement.VertexDeclarationEnd
});
Application.EnableVisualStyles();
MessagePump.Run(Window, () =>
{
// Clear the backbuffer to a black color.
D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
// Begin the scene.
D3DDevice.BeginScene();
// Setup the world, view and projection matrices.
//D3DDevice.VertexShader = vertexShader;
//D3DDevice.PixelShader = ps;
// Render the vertex buffer.
D3DDevice.SetStreamSource(0, Vertices, 0, Vertex.SizeBytes);
D3DDevice.VertexFormat = Vertex.Format;
// Setup our texture. Using Textures introduces the texture stage states,
// which govern how Textures get blended together (in the case of multiple
// Textures) and lighting information.
D3DDevice.SetTexture(0, Image);
// Now drawing 2 triangles, for a quad.
D3DDevice.DrawPrimitives(PrimitiveType.TriangleList , 0, 2);
// End the scene.
D3DDevice.EndScene();
// Present the backbuffer contents to the screen.
D3DDevice.Present();
});
if (Image != null)
Image.Dispose();
if (Vertices != null)
Vertices.Dispose();
if (D3DDevice != null)
D3DDevice.Dispose();
}
private static Vertex[] BuildVertexData()
{
Vertex[] vertexData = new Vertex[6];
vertexData[0].Position = new Vector3(-1.0f, 1.0f, 0.0f);
vertexData[0].Tu = 0.0f;
vertexData[0].Tv = 0.0f;
vertexData[1].Position = new Vector3(-1.0f, -1.0f, 0.0f);
vertexData[1].Tu = 0.0f;
vertexData[1].Tv = 1.0f;
vertexData[2].Position = new Vector3(1.0f, 1.0f, 0.0f);
vertexData[2].Tu = 1.0f;
vertexData[2].Tv = 0.0f;
vertexData[3].Position = new Vector3(-1.0f, -1.0f, 0.0f);
vertexData[3].Tu = 0.0f;
vertexData[3].Tv = 1.0f;
vertexData[4].Position = new Vector3(1.0f, -1.0f, 0.0f);
vertexData[4].Tu = 1.0f;
vertexData[4].Tv = 1.0f;
vertexData[5].Position = new Vector3(1.0f, 1.0f, 0.0f);
vertexData[5].Tu = 1.0f;
vertexData[5].Tv = 0.0f;
return vertexData;
}
}
}
This is my code for the pixel shader use. The image is being displayed properly if I don't set the pixel shader but as and when I give the pixel shader to device then nothing is displayed.
Please help me out to resolve this problem.
#15 Members - Reputation: 199
Posted 16 June 2011 - 09:00 AM
I've never used SlimDX, sorry
#16 Members - Reputation: 100
Posted 17 June 2011 - 12:36 AM
// Pixel shader input structure
struct PS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};
// Pixel shader output structure
struct PS_OUTPUT
{
float4 Color : COLOR0;
};
// Global variables
sampler2D Tex0;
// Name: Simple Pixel Shader
// Type: Pixel shader
// Desc: Fetch texture and blend with constant color
//
PS_OUTPUT ps_main( in PS_INPUT In )
{
PS_OUTPUT Out; //create an output pixel
Out.Color = tex2D(Tex0, In.Texture); //do a texture lookup
Out.Color *= float4(0.9f, 0.8f, 0.0f, 1); //do a simple effect
return Out; //return output pixel
}
this is my pixel shader file that i am importing and there is no use of importing the effect file in this program. the vertex shader and pixel shader does required things. And below is my vertex shader code.
// Vertex shader input structure
struct VS_INPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};
// Vertex shader output structure
struct VS_OUTPUT
{
float4 Position : POSITION;
float2 Texture : TEXCOORD0;
};
// Global variables
float4x4 WorldViewProj;
// Name: Simple Vertex Shader
// Type: Vertex shader
// Desc: Vertex transformation and texture coord pass-through
//
VS_OUTPUT vs_main( in VS_INPUT In )
{
VS_OUTPUT Out; //create an output vertex
Out.Position = mul(In.Position,
WorldViewProj); //apply vertex transformation
Out.Texture = In.Texture; //copy original texcoords
return Out; //return output vertex
}
#18 Members - Reputation: 593
Posted 21 June 2011 - 07:11 AM
In my code I do something like this:
To load the shader file:
Effect shader = Effect.FromFile(device, "shader.fx", ShaderFlags.None);
EffectHandle paramWorldViewProj = new EffectHandle("matWorldViewProj");.. then at render time:...
// here is an example of setting a shader parameter... shader.SetValue(paramWorldViewProj , worldViewProjection); shader.Begin(); shader.BeginPass(0); // primitives render call goes here // model.Render(); shader.EndPass(); shader.End();
#19 Members - Reputation: 199
Posted 21 June 2011 - 09:17 AM
#20 Members - Reputation: 593
Posted 21 June 2011 - 07:56 PM
Postie -what you posted makes sense to me too but it looks like Yash14 is using pre-compiled shaders. Regardless, he'll still need to set vars and render using the same technique as your code. Currently he's using Device.SetTexture() which is a fixed function pipeline code. He needs to pass the texture into the shader as a varable. The code he posted is both a Progammable Pipeline and a Fixed Function Pipeleine.
Yeah, it seemed like he was mixing shaders and the fixed function pipeline. I haven't used pre-compiled shaders myself, so that didn't make sense to me. Though I noted that the link you mentioned earlier is a link to a SlimDX tutorial for DirectX11, and he's trying to use DX9 (from his inclusion of "using SlimDX.Direct3D9"), so I didn't know if the bytecode stuff was how you handle shaders under dx11 and wouldn't work correctly with dx9, but I haven't used dx11 yet, so I wasn't sure and decided to post what I knew works for me.






