A feature sadly rarely used, that can sometimes fasten coding are user-defined type conversion operators that are invoked with a cast. In the following example we define a struct Sentence that has an explicit conversion operator from string to Sentence.
In the Main method we are creating an instance of Sentence by casting a string to it.
The output of the program looks like this:
There is little difference in syntax for using an implicit conversion. It is done by declaring the conversion this way:
And using it without the cast:
Implicit conversion can improve code readability, however if you are using implicit conversion, you MUST MAKE SURE that the conversion will NEVER loose information or cause exceptions. The reason for this is the compiler, it will use the conversion and if that can cause a problem you will wreck havoc in your code.
Monday, April 11, 2011
Explicit and implicit conversion (C#)
Sunday, April 10, 2011
HLSL (DirectX/XNA) effect for stereo side by side anaglyph
The following code takes the input texture and paints over it the stereo effect.
The technique used (actually the colro transformation matrix) is called Dubois by it's original author.
You will need to set the texture and the width/height from your DX / XNA code:
You will have to render to texture and then show only the half of the image that you need (if the first image in the source is left show the left side, else the right). If the source is half SBS (means shrinked to half) then you need to stretch it back (double the width).
The technique used (actually the colro transformation matrix) is called Dubois by it's original author.
You will need to set the texture and the width/height from your DX / XNA code:
You will have to render to texture and then show only the half of the image that you need (if the first image in the source is left show the left side, else the right). If the source is half SBS (means shrinked to half) then you need to stretch it back (double the width).
To follow in the next few posts
In the next few posts I will post some HLSL source code I happened to code for a purpose of being able to view stereoscopic side by side video using anaglyph glasses (cyan/magenta in my case).
Stereoscopy is a technique used to produce 3D images or video from 2D source material.
The source consists of two images/video streams captured at the same time with an offset of a few centimeters from the objectives. Very similar to how our eyes do it.
I may post other HLSL pixel shader examples if I found them to have some general value to 3D folks.
In the meantime I will post a link to a prealpha version of a video player I'm working on. You can download and try to open some stereo material. If you dare then you will need to open the anaglyph shader that suits you glasses, which of course you need to see something.
Here is a link to the source material:
Stereo samples
The player currently supports only SideBySide material (not the TopDown).
Here is the link to the alpha version of the player:
3DPEE / Tridipy - player and HLSL compiler
You will need the latest DirectX and .NET Framework 3.5.
Do not expect absence of bugs :)
Stereoscopy is a technique used to produce 3D images or video from 2D source material.
The source consists of two images/video streams captured at the same time with an offset of a few centimeters from the objectives. Very similar to how our eyes do it.
I may post other HLSL pixel shader examples if I found them to have some general value to 3D folks.
In the meantime I will post a link to a prealpha version of a video player I'm working on. You can download and try to open some stereo material. If you dare then you will need to open the anaglyph shader that suits you glasses, which of course you need to see something.
Here is a link to the source material:
Stereo samples
The player currently supports only SideBySide material (not the TopDown).
Here is the link to the alpha version of the player:
3DPEE / Tridipy - player and HLSL compiler
You will need the latest DirectX and .NET Framework 3.5.
Do not expect absence of bugs :)
C++ reference operator standard behaviour exemption
If you are reading this, you probably already know that:
means take the address of value and put it in valueAdress.
So the & operator gives back the address of something?!
Well, that is wrong. I't not always like that. There is one case when this is untrue.
In this case we are using a pointer to member and it does not make much sense to get the address of something that does not have an instance. In this case the & operator gives back an offset to the member in the class we are pointing to.
The offset (in the last line) called the member pointer is dereferenced to a member and then assigned normally.
means take the address of value and put it in valueAdress.
So the & operator gives back the address of something?!
Well, that is wrong. I't not always like that. There is one case when this is untrue.
In this case we are using a pointer to member and it does not make much sense to get the address of something that does not have an instance. In this case the & operator gives back an offset to the member in the class we are pointing to.
The offset (in the last line) called the member pointer is dereferenced to a member and then assigned normally.
Quick fire&forget method to delay execution of some code in WPF in C# 4.0
This code will trigger aproximately after 5 seconds and will execute on the UI thread through the dispatcher:
This example uses the Task class introduced with .Net 4.
If you need a more precise way to do it the you should look at the Stopwatch class.
This example uses the Task class introduced with .Net 4.
If you need a more precise way to do it the you should look at the Stopwatch class.
JS / JQuery: Sync two lists / containers with drag and drop
Given the following HTML when you want to sync the drag and drop reordering of the UL to the child divs of div_container:
This code will do it:
You can attach the same metodology to div_container to make it fll duplex :)
This code will do it:
You can attach the same metodology to div_container to make it fll duplex :)
DirectX / XNA shader test compiler in C#
You must have the fxc.exe directx compiler in the bin folder, it can be found in the direct x sdk.
This class provides an observable collection that can be used in WPF to show the list of shader profiles.
public static bool TryCompile(string code, ShaderModel model, string entrypoint, out string error)
Where code is the shader source code, model is an element prom the profile list, entry point is the main procedure for shaders that require it like the pixelshader, error is the error string if there is an error compiling.
This class provides an observable collection that can be used in WPF to show the list of shader profiles.
public static bool TryCompile(string code, ShaderModel model, string entrypoint, out string error)
Where code is the shader source code, model is an element prom the profile list, entry point is the main procedure for shaders that require it like the pixelshader, error is the error string if there is an error compiling.
How to apply a pixel shader to the video surface of DirectShow in C#
We must use Managed DirectX or the SlimDX libraries in out project and we must implement the IVMRImageCompositor9 interface provided by DirectShow.Net.
We then use our class by setting the image compositor on the VMR9 FilterConfig interface.
Only important and relevant parts of the class are shown.
The namespaces and definition:
Init the DirectX device through the DirectShow native pointer to the device:
Init the vertex buffer with a Fullscreen QUAD: two triangles that cover the whole suurface:
We implement the method CompositeImage, that is responsible for applying the shader.
Set the class on the VMR:
Implementing the device resetting and other DX needed tweaks is up to you.
We then use our class by setting the image compositor on the VMR9 FilterConfig interface.
Only important and relevant parts of the class are shown.
The namespaces and definition:
Init the DirectX device through the DirectShow native pointer to the device:
Init the vertex buffer with a Fullscreen QUAD: two triangles that cover the whole suurface:
We implement the method CompositeImage, that is responsible for applying the shader.
Set the class on the VMR:
Implementing the device resetting and other DX needed tweaks is up to you.
How to scramble/garble/obfuscate a bitmap image and get it back in c#
A simple way to do it is the follwing method. You pass the last parameter true to obfuscate, false to revert.
The method sadly uses 'unsafe' but you could convert all the pointer operations to Marshal methods.
This method is not very good for images that contain only a few colors, but the "switch" part in the code tries to handle that, and noise the image.
Sample:
The method sadly uses 'unsafe' but you could convert all the pointer operations to Marshal methods.
This method is not very good for images that contain only a few colors, but the "switch" part in the code tries to handle that, and noise the image.
Sample:
Subscribe to:
Posts (Atom)
