Monday, January 25, 2010

Do you block Google Analytics tracking in your browser?

Recently I checked the server logs for my website and compared the statistics with the Google Analytics data for the timespan. I expected that the page views and visits won't be the same, but I haven't expected such a large difference I encountered.
Just to make it clear, I don't use Google Analytics for evil, I'm not Google and I'm not interested in advertising or web marketing, I'm just curious what my visitors (you) are looking for and what caught the interest, so I can provide more content in this field.
I know some people that block Analytics tracking in their browser because of the Google octopus, or disabled Java Script at all.

The difference between the Google Analytics statistics and the server logs raised the question in me how reliable the Google Analytics data is when several people block it in their browser. I started to search for some block statistics, but couldn't find any useful information and I also think the actual blocking-rate does crucially depend on the website's audience and used technology. That's why I created a little poll at twtpoll.com and asked my Twitter friends to vote. Big thanks to all that already voted, but not all of you are on Twitter, so I like to ask here too:

Do you block Google Analytics tracking in your browser?

I will publish the results here on my blog after the poll is over on Feb 28, 2010. Thanks for taking your time.

Update 03-10-2010
The results are now available.

Monday, January 18, 2010

Matrix3DEx 1.0 - When PlaneProjection is not enough

A new year has begun and I started a new open source project at CodePlex. It's called Matrix3DEx and is an extension library for the Silverlight Matrix3D struct. Most of the functionality I implemented in Matrix3DEx was originally required for another open source project I'm currently working on (hint, hint), and as you might know I also like to extend Silverlight's graphics functionality in a reusable manner. That's why I decided to extract the Matrix3D code into a separate open source project, add some more useful methods, document it and write a sample.


The Matrix3DEx project description from the CodePlex site:
The Matrix3DEx library is a collection of extension and factory methods for Silverlight's Matrix3D struct. The Matrix3D struct represents a 4x4 matrix that is used in combination with the Matrix3DProjection to apply more complex semi-3D scenarios to any UIElement than are possible with the simple PlaneProjection. This makes it possible to apply arbitrary model transformation matrices and perspective matrices to Silverlight elements.
The Matrix3D struct is very minimalistic and has only a few members. The Matrix3DEx library tries to compensate that with extension and factory methods for common transformation matrices that are easy to use like built in methods.

Features:
  • Factory methods

    • Translation, scaling and rotation around x, y, z or any defined axis
    • Perspective field of view and orthographic projection
    • Camera (look-at) with position, target and up vector
    • Support for left-handed and right-handed coordination systems

  • Extension methods

    • Calculation of the matrix' determinant
    • Matrix transpose
    • SwapHandedness to change from right-handed to left-handed coordination system and vice versa
    • Dump of the values row by row into a formatted string

  • Math helper methods

    • Angle conversion from degrees to radians and vice versa


Some use cases where Silverlight's semi-3D projection is needed can be implemented with the PlaneProjection, but there are also some scenarios where the PlaneProjection wouldn't work or only with a lot of effort. One example is the usage of a physics library that returns a matrix for an object. Traditional matrix transformations are an elegant alternative for such cases and the Matrix3DEx library has factory and extension methods for all the common matrices.

Live
Open the sample in a new page.



The application loads a bunch of photos asynchronously and randomizes the position vector of each. The sample uses most of the Matrix3DEx features and has some Sliders and CheckBoxes to change the parameters. Uncheck the "Animate" CheckBox to disable the camera movement and click on an Image to select it. The translation, scaling and the rotation matrices of the selected element can be changed with the corresponding Sliders. You can move the camera, change the target and the "Field Of View" with the other Sliders or fix the camera target at the selected element.
The basic functionality of the sample might also be done with the PlaneProjection, but a separate look-at matrix (camera) simplifies the code a lot and makes it easier to read. Other things like a custom field of view are not possible with the PlaneProjection.

How it works
The sample uses a DipatcherTimer to call the Update method every 100 milliseconds.
The core of the Update method:
// Create global transformations
var vw = Viewport.Width;
var vh = Viewport.Height;
var invertYAxis = Matrix3DFactory.CreateScale(1, -1, 1);
var translate   = Matrix3DFactory.CreateTranslation(TranslateX, 
                                                    TranslateY,
                                                    TranslateZ);         
var rotateX     = Matrix3DFactory.CreateRotationX(MathHelper.ToRadians(RotateX));
var rotateY     = Matrix3DFactory.CreateRotationY(MathHelper.ToRadians(RotateY));
var rotateZ     = Matrix3DFactory.CreateRotationZ(MathHelper.ToRadians(RotateZ));
var scale       = Matrix3DFactory.CreateScale(ScaleX, ScaleY, ScaleZ);
var lookAt      = Matrix3DFactory.CreateLookAtLH(CameraX, CameraY, CameraZ,
                                                 CameraLookAtX, 
                                                 CameraLookAtY, 
                                                 CameraLookAtZ);
var viewport    = Matrix3DFactory.CreateViewportTransformation(vw, vh);
var fieldOfView = MathHelper.ToRadians(FieldOfView);
var projection  = Matrix3DFactory.CreatePerspectiveFieldOfViewLH(fieldOfView, 
                                                                 vw / vh,
                                                                 NearPlane,
                                                                 FarPlane);

// Transform all elements
var selectedMatrix = Matrix3D.Identity;
foreach (var elem in this.Elements)
{
   // The UIElement
   var e = elem.Element;

   // Create transformation matrices for UIElement
   var centerAtOrigin = Matrix3DFactory.CreateTranslation(-e.ActualWidth * 0.5,
                                                          -e.ActualHeight * 0.5, 
                                                          0);
   var baseTranslate = Matrix3DFactory.CreateTranslation(elem.PositionX, 
                                                         elem.PositionY,
                                                         elem.PositionZ);

   // Combine the transformation matrices
   var m = Matrix3D.Identity;
   m = m * centerAtOrigin;
   m = m * invertYAxis;

   // Apply addtional world transformations to the seleced element
   if (elem == SelectedElement)
   {
      m = m * scale;
      m = m * rotateX * rotateY * rotateZ;
      m = m * translate;

      // Should the camera target be fixed at the selected element?
      if (ChkLookAtSelected.IsChecked.Value)
      {
         lookAt = Matrix3DFactory.CreateLookAtLH(CameraX, CameraY, CameraZ, 
                                                 elem.PositionX, 
                                                 elem.PositionY, 
                                                 elem.PositionZ);
      }
   }

   // Calculate the final view projection matrix
   m = m * baseTranslate;
   m = Matrix3DFactory.CreateViewportProjection(m, lookAt, projection, viewport);

   // Apply the matrix to the UIElement
   e.Projection = new Matrix3DProjection { ProjectionMatrix = m };
}

First the global transformation matrices like camera projection are created using the left-handed Matrix3DEx factory methods. After that the element local transformations are calculated and the final matrix is applied to the UIElement's Projection property.
See the project site for another simplified code listing.

Go and grab it

The open source Matrix3DEx library is hosted at CodePlex and released under the Microsoft Public License (Ms-PL) license. If you have any comments, questions or suggestions don't hesitate and write a comment, use the Issue Tracker on the CodePlex site or contact me via any other media.
Have fun with the library and let me know if it was useful for you.

Sunday, January 10, 2010

Goodbye GPL

In the past I've released the source code that mostly comes along with my blog posts under the GPLv3 license to ensure that changes on the code always stay open and never get closed. But the GPL has a major drawback, it's too viral, which means if someone uses GPL licensed code the new code has to be released under the GPL too. And if someone uses this new code, the new code 2 has to be put under the GPL again and so on. And as the FSF states this even applies when the code is just linked dynamically with a GPL'ed library only except if it uses the GPL linking exception. That's why I wanted to move away from the GPL and searched for some alternatives. Finally I decided to use the Ms-PL license from now on if my work doesn't depend on GPL infected code. I've chosen the Ms-PL because it's approved by the OSI, it's short and easy understandable, the weak copyleft guarantees that changes on the original code have to be committed back and that it stays open and it could be used for commercial products. Jon Galloway (@jongalloway) summarized it nicely in his Twitter reply to me: "Ms-PL is essentially BSD + patent protection." So the Ms-PL is suitable, short, simple and keeps the trolls away.

I have changed all the source code headers and License.txt files and updated the Zip files with the source code you find linked on most blog posts. If you still find a GPL reference, just ignore it and if the Ms-PL is a problem for you, just contact me and I might put the relevant code under a second license.

Wednesday, December 30, 2009

Ye Olde Kamera - Silverlight 4 Webcam & Old Movie Shader

Another great and successful year comes to an end and I hope that the next year will get even better. We all don't get younger and we change over the years, but the last Silverlight demo I made for this year allows you take some fake aged memories with your webcam. The Silverlight 4 webcam and microphone support is always fun and even more when it's combined with a pixel shader. This demo uses my Old Movie Pixel Shader to create a neat real time webcam effect that simulates scratches, noise and other effects you might have seen in old movies. I also brushed things up a bit by framing the video output with a filmstrip and using some nice new icons from eggheadcafe.

Live
The Silverlight 4 runtime must be installed to run the demo. It's available for Windows and Mac.



The Webcam capturing could be started and stopped with the camera Button. If you press it for the first time you need to give your permission for the capturing. This application uses the default Silverlight capture device. You can specify the video and audio devices that are used by default with the Silverlight Configuration. Just press the right mouse button over the application, click "Silverlight" in the context menu and select the new "Webcam / Mic" tab to set them.
Press the disk Button to take a framed snapshot and save it to a JPEG file on your harddisk.
The amount of noise can be changed using the Slider and the movie ToggleButton allows you to disable the Old Movie Shader.

How it works
It's basically the Silverlight 4 CaptureSource class and my Old Movie Pixel Shader attached to the video output. I covered the Silverlight webcam and shader usage in this blog post and the JPEG encoding in this one. The Old Movie Shader was presented here.

Source code
The Visual Studio 2010 solution including the refactored Old Movie Shader is available here.

Final words
Thanks to my kids and especially to my wife for all her patience in 2009. I love her!
I also like to thank all the people and new friends I got to know this year and especially the Silverlight community. I hope you liked my blog posts and enjoyed the Silverlight demos I've made. I have more to come next year.
I wish you all a great new year, all the best in 2010 and peace!

Update 03-20-2010
Updated to the Silverlight 4 release candidate.

Update 04-15-2010
Updated to the final Silverlight 4 RTW build.

Thursday, December 17, 2009

World# - Real Time 3D Augmented Reality with Silverlight

Please read the Update at the end of this post.

In the Silverlight 4 Augmented Reality Proof Of Concept blog post I proved that it's possible to implement Augmented Reality applications with Silverlight 4 and the built-in webcam API. The proof of concept used an image showing a Silverlight logo that was attached to the tracked marker. With this blog post I'm back in this field, but this time with real 3D Augmented Reality in Silverlight!
I use the open source Augmented Reality library NyARToolkitCS again and implemented the necessary interfaces to make it work with Silverlight. The NyARToolkitCS library is a completely managed version of the well known ARToolkit. I also use the open source managed game engine Balder for the rendering of 3D models that augment the reality. Balder is mainly developed by the Silverlight MVP Einar Ingebrigtsen. He is working hard on his 3D game engine and provided quick fixes for some issues I encountered while using Balder and I also had a nice chat with him, so big shout out to Einar.

Live
A webcam and at least the Silverlight 4 runtime must be installed to run the sample. To view the application you need to install the Silverlight 4 runtime. It's available for Windows and Mac. You can also watch a video below.
If you want to try it yourself you need do download the SLAR and / or L marker, print them and hold them in front of the camera. The marker(s) should be printed non-scaled at the original size (80 x 80 mm) and centered for a small white border. Also make sure the camera is set up properly and the scene is illuminated well without hard shadows. See the SLARToolkit Markers documentation for more details.

Try it out here.

Simply press the "Start Fun" Button, hold the printed marker in front of the camera and move it around. Use the ComboBox to select a different 3D model. The "Flip x-axis" Checkbox could be used to flip the video (the webcam output is mirror-reversed by default).
If you click the "Start Fun" Button for the first time you need to give your permission for the capturing. This application uses the default Silverlight capture device. You can specify the video and audio devices that are used by default with the Silverlight Configuration. Just press the right mouse button over the application, click "Silverlight" in the context menu and select the "Webcam / Mic" tab to set them.
The Car model was made by Psionic, the Tank model by Ken Beyer. The Teapot is from the Balder samples and I guess it was made by Einar Ingebrigtsen. The rest was made by myself.

Video
This time I used Expression Encoder's Screen Capture feature to record a short video of the demo in action. Please keep in mind that the screen recording software eats up a lot of resources while recording and that the actual frame rate is way better.



How it works
Every time the CompositionTarget.Rendering event is fired, a webcam snapshot is taken asynchronously with the CaptureSource.AsyncCaptureImage method. This snapshot is converted in the right format and passed to the NyARToolkitCS library that outputs a transformation matrix. This matrix is used to transform the 3D model. The Balder viewport has a transparent background and the scene with the 3D objects is laid over the webcam video output. Another layer is an image that uses a WriteableBitmap and the WriteableBitmapEx library to highlight the tracked marker region with a red quad.
Sounds pretty easy, but actually it was not that easy and straight forward. One of the hardest parts was the initialization and to find the right parameters.

Results
I'm quite satisfied with the results and if you consider that all the heavy computation is done by the CPU, the performance is pretty good as well. The demo runs smoothly at 50-60 fps on a dual core machine utilizing its whole power and it should also scale up on more cores.

To be continued
If you expected source code this time I have to disappoint you. The code is not at that level where it should be, but I wanted to push this demo out. I'm not sure if I find enough time this year to work on the code and I don't want that someone tries to use it in the current state. But trust me, I will provide open source functionality to make Augmented Reality in Silverlight in the near future and I normally keep my promises, so stay tuned for more happy days...

Update 02-24-2010
This demo is now part of the open source SLARToolkit library which was announced in this blog post.

Update 03-15-2010
Updated to the Silverlight 4 Release Candidate.

Update 04-15-2010
Updated to the final Silverlight 4 RTW build.