Donuts! (Procedural Torus in C++)

Recently I got a mail through the contact form of my website about texture mapping on torus (a donut) who said that D3DXCreateTorus didn’t not provide texture coordinates. The reason why DirectX and OpenGL (and GLUT) don’t provide texture coordinates with these function is best seen when you generate a cube. A cube has 8 vertices, however when you add texture coordinates it will increase because otherwise they will share the same texture coordinate. With normals it is no problem (in fact you would prefer that) but if you have a dice (from 1 to 6) than each vertex would use a single texture coordinate for three sides. Just draw an unfolded dice on paper where each edge is connected, but does not cross another edge. It is simply not possible. These complexities are the reason why those procedural mesh functions are so simple. Adding these functionalities would increase complexity of the function and requires you to explain it. On top of that the developers have no idea how you are going to use that function. Maybe you don’t want to use the legal texture range [0...1] but [0...3].

Anyway I have decided to take it on and here is the result. You can download the source code here: TorusDX.zip.I have not commented it, but it is pretty straight forward. The texture that I used is from Dilbert.

Leave a comment