The goal of the project was to render a tiger bunny similar to the paper Lapped Textures . The process of Lapped Texturing involves pasting parts of a sample texture onto the bunny. The result can then be saved to another texture called a texture atlas. As a simpler solution to this problem, I decided to to map the texture once onto the whole mesh and while still using a texture atlas to store the results.
I did the texture atlas renderings to an internal datastructure instead of relying on OpenGL calls. I could have saved myself a lot of time and had better results by drawing to a framebuffer object (FBO). The algorithm I made linearly interpolates across an output triangle in 2D. From the generated X,Y coordinates I calculate the input texture position using barycentric coordinates.
The basic idea was to plot UV coordinates (positions on the 2D texture) based on a mapping that works for a generic mesh. I chose to use spherical texture mapping seen in the figure below [3,7]. The UV coordinates are calculated using the formula:
tu = asin(Nx)/PI + 0.5 tv = asin(Ny)/PI + 0.5
The normal (Nx, Ny, Nz) could be estimated as the average of the normal of each polygonal face connected to that vertex. As a shortcut, I approximated the normal at a vertex as the normalized form of the vertex position. This works because the bunny is centered around the origin.
Texture baking is the the process of drawing material properties of a mesh onto a texture. These material properties can include normals, shadows, lighting conditions, displacements, textures, specularity, or color (normal mapping, shadow mapping, etc). These can then be used to speed up the calculation later by mapping into the texture atlas to retrieve the properties at that given position on a polygon.
The results texture baking from the Lapped Textures paper can be seen below. Image (a) shows the output of the tiger stripes without texture baking. Image (b) shows the output of the bunny with texture baking. Note that it has some aliasing effects that cause distortions. The texture atlas that had the patches baked onto it can be seen in (c).
Previous bugs:Flat shaded bug
Bunny renderings from my texture atlas (first) versus texture mapped by coordinates (second). There are many problems with my solution which are listed later.
This section describes problems with my solution that are results of the algorithm choice and not bugs.
White pixels show up without extending the atlased triangles one pixel. Even extending one pixel does not remove the problem completely but removes most noticable ones. A workaround would be to push the UV coordinates towards the center of the triangle when rasterizing to the screen (not the atlas).
256x256 versus 2048x2048 atlases close up of a bunny. There is aliasing of texture coordinates when the texture atlases becomes too small.
This section describes unintended results from bugs.
There's a random white triangle on the bunny. All triangles are at least drawing 1 pixel, so I don't even know which one this should be.
The borders of many triangles have the wrong color.
Some atlased triangles stretch to the right due to incorrect interpolation, causing them to be drawn over other triangles.