Texture wrapping

About time I put some DirectX content on here isn't it!

Those of you who bother to read the Ask The ZMan column on Coding4Fun may remember I left the spherical texturing article with a bit of a teaser. Why does the texture get messed up in the middle of the pacific ocean. Check out the last picture on the spherical texturing article.

Necroscope had the correct answer over on the channel 9 forums. The texture coordinates are distributed around the globe, the 1st being 0.0 the last being less than 1.0 because 1.0 would be the same vertex that we set to 0.0. Since the vertices in the Mesh.Sphere are shared you can't actually have a start and an end. So if our globe has 10 segments our texture coordinates are 0.0, 0.1, ....0.8, 0.9, 0.0. When the texture is interpolated for the last section you get the entire texture from 0.9 back to 0.0 in a single segment.

My solution was going to be to create a new mesh that didn't have to overlapping vertices so that there was an 'end' that could be set to 1.0. That wouldn't have been the whole article but it was to be part of one which showed the innards of a mesh.

Well today whilst browsing the newsgroups I came across a post called 'Looping Textures' by the very frequent poster Robert Dunlop where he magically solves the problem with a single line of code. There is a renderstate that I had not come across before which controls how DirectX does its texture interpolation. If you add this line before you render

device.RenderState.Wrap0 = WrapCoordinates.Zero;

then the texture anomaly goes away.

Basically this renderstate says "when you interpolate assume that 0.0 and 1.0 are the same thing and then interpolate in the direction that gives you the shortest distance". So in the example above 0.9 -> 0.0 is a bigger interpolation than 0.9 -> 1.0 so it interpolates from 0.9 -> 1.0 which is exactly what you want. The SDK has a help topic called Texture Wrapping that also explains it.

Published 07 December 2005 12:18 AM by zman


No Comments



This Blog