03.30
Finally fond some time to publish a new article.I recently have been working on a commercial project which involves some areas of computational geometry.In fact ,I can say that this is one of most complex areas of CS but is also one of most fascinating.The most important thing when you first get to it is not to panic.Many of the algorithms are extremely math intensive and make it hard to progress through the actual work.But the good news are that we live in the work of WWW and pretty every possible algorithm has already been implemented in some language.Therefore , in many scenarios one can  port some chunks from code based found in the net. I take this approach occasionally but my favorite way is -books. I read a lot of tech books primarily on 3D graphics programming.Most of them not only supply the source code but also deep explanations of the techniques and algorithms on which the end apps are based.
So one day I  decided to implement a  surface subdivision in Flash 3D .Typing the term in Google gives a great multitude of results .Most of those present the Catmull-Clark subdivision algorithm.And there are tons of  C and C++ open source practical implementation of it in the net.But beware ,there is a catch. Catmull-Clark operates on  quadrilateral polygons,that is polygons consisting of 4 edges .3D libraries like OpenGL or Direct3D can handle this type of geometry.In OpenGL you can define GL_QUAD or GL_POLYGON drawing mode and the library would render quadrilateral meshed (though much slower than using GL_TRIANGLES . Alas , in Stage3D we can draw only triangles for the good reasons -performance.While it is technically possible  subdividing using Catmull-Clark for Stage3D that may add a lot of overhead as in such a case we would need to join each 2 triangles into quads before the operation and then  split each resulting quad  back to 2 triangles before handing the subdivided model back to the rendering .
One of my recent readings has been “Advanced 3D game programming with DirectX 10″ . I must say that is one of the best 3D graphics books I have ever read.Not only it introduces the reader in very convenient way with the Direct3D programming but the book is stuffed with cool 3d techniques of all kinds  including elaborated examples .The author has a chapter on subdivision and he presents an algorithm called “Butterfly” because of the way the subdivided polygons look like.The implementation is really straightforward and if you don’t want to strain your brains to much you can just port the ready program from .cpp source supplied with the book.One important detail here must be mentioned.If the indices of your model are defined in a simple progressive way ( like:  first triangle  - 0 ,1 ,2  second triangle- 3 , 4 , 5 third triangle - 6 , 7 , 8 ) the algorithm fails.That is because it has no way to calculate all the adjacent edges for each vertex  which are essential to split the polygons. The format of the indices must be defined so that each index ,that defines a vertex that shares its position with another vertex  like one, inherits its value from the index of that vertex. Obj data format is a good example for such indices.
Here you can see my experiment program with “Butterfly ” in action .You can see some UV bugs on the first subdivision.That is something I still have to take care about .But generally it works really nice.The current implementation is powered by Flare3D engine.

