Silverlight & XAML Playground
about XAML and other Amenities

Silverlight 5.0: Plotting beautiful 3D functions

2011-04-13T19:24:00+01:00 by codeblock

1Following the announcement made today on the scene on MIX 2011 I’ve prepared an example for the purpose of showing the new powerful 3D available in the beta bits. Remembering ancient days when I enjoyed myself writing small programs to plot 3D functions on the screen of my Amiga 500, I decided to follow again this path and put together a few lines of code to draw the sample functions in Silverlight (so this pairs with the fractals experiments made in occasion of MIX 2009).

Drawing 3D scenes in Silverlight is not so intuitive as it is in WPF due the fact that the API exposed by the plugin is on a lower level than the WPF one. The team choosed to implement an API that is very close to the XNA framework one. So to create objects in 3D space you have to deal with lot of triangles, vertex and pixel shaders and other exotic stuff. Nonetheless, once you understand the way the thing works the results don’t delude you.

Basically to create a 3D scene you have to put in the markup a special element named DrawingSurface. This element is able to render a 3D set. Every time it surface needs to be rendered it raises an OnDraw event passing to the handler and instance of a GraphicsDevice that is useful to actually draw the scene on the surface. In the example a class named Scene is used to setup the 3D environment (camera, viewing direction and elements in the scene) do inside the OnDraw event the Scene’s Draw method is called to perform the draw.

2Inside the Scene class every element draw itself. In my case the scene is populated only by FunctionPlot class that contains the function to draw and receives some parameters. During the rendering is is possible to apply some transformation matrices that apply rotation, translation and scaling.

The drawing of the function is probably the simplest thing. While the x and y coordinates run on the plane the vertexs of a square are build. The area of every square is divided in two triangles.

A detailed explanation of how the 3D pipeline works is out of the scope of this article and should take an huge number of posts, not few lines. For now I give you the code I wrote for this example and I hope you appreciate like me the beautiful images that it creates.

Download: SLPG.3D.zip (256kb)

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (4) | Comment RSSRSS comment feed

Comments (4) -

January 25. 2012 04:12

I wanted to create you a very little observation just to give thanks the moment again for your precious tricks you have shown here. This has been so pretty generous with you in giving extensively what a lot of people could have offered for sale as an ebook to get some profit for themselves, even more so considering that you might have done it if you ever desired. The creative ideas additionally worked as a easy way to recognize that other people have the same keenness similar to my own to know the truth more and more on the topic of this matter. I\\\'m sure there are several more pleasant instances up front for individuals that read through your site.

SEO

January 25. 2012 04:12

I wanted to put you this little remark to finally say thank you over again about the great secrets you have contributed above. It has been quite strangely generous with people like you to convey unreservedly precisely what a number of us would\\\'ve distributed as an ebook to generate some dough for their own end, chiefly considering that you could have done it in case you wanted. Those good ideas likewise acted to become fantastic way to be certain that someone else have the same fervor much like mine to figure out much more on the topic of this problem. I\\\'m sure there are some more pleasant opportunities up front for folks who view your blog post.

SEO

January 25. 2012 04:12

I wanted to create you a bit of word to be able to say thanks the moment again on the marvelous knowledge you\\\'ve provided in this article. It is   open-handed with you to give easily just what a number of people could have sold as an electronic book in making some money on their own, precisely since you might well have tried it in the event you decided. The principles additionally worked to become a fantastic way to comprehend most people have the identical eagerness just like my own to understand significantly more with regards to this condition. I\\\'m certain there are lots of more enjoyable times in the future for those who browse through your blog.

SEO

January 25. 2012 04:13

I intended to compose you the very little word in order to thank you so much again on the nice pointers you\\\'ve shown here. It was quite pretty generous with you to provide openly exactly what some people might have offered for an electronic book to help with making some money for their own end, specifically seeing that you could have tried it in case you considered necessary. These suggestions likewise acted as the great way to recognize that other people online have the identical dreams the same as my very own to see significantly more when it comes to this issue. Certainly there are several more pleasurable periods in the future for people who look over your blog.

SearchEngineOptimization