Tiling texture on a sphere

Hi All,
I want to tile a texture on a sphere. I am using glTexGeni. How should i do it… The texture should not keep moving with the camera… I mean, right now, i am not tiling the texture, but it is always facing the camera. (I am using SPHERE_MAP, not OBJECT_LINEAR or EYE_LINEAR as they dont give the required result.)

Thankx.

Hassan,

it all depends on what you mean by “tile”. I assume you’re using the sphere mapping not for reflection, but for general texturing. This technique will not get you there. There all many ways to map a texture onto a sphere. One way is to simply project the sphere onto a plane. You’ll get some distortion, but there’s no avoiding that, at least to some extent. Mapping a flat rectangular texture onto a sphere just isn’t possible without distortion.

Try projecting onto the plane y = 0:


glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
glEnable( GL_TEXTURE_GEN_S );

glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
glEnable( GL_TEXTURE_GEN_T );

float s[4] = { scaleS, 0, 0, 0 };
glTexGenfv( GL_S, GL_OBJECT_PLANE, s );

float t[4] = { 0, 0, scaleT, 0 };
glTexGenfv( GL_T, GL_OBJECT_PLANE, t );

// now draw your sphere…

You can experiment with different values for scaleS and scaleT. If your texture is 256 x 256, for example, then a scaleS of 1/256 and a scaleT of 1/256 would result in the texture repeating every 256 units.

You can also pick a different plane to project on to, it’s completely arbitrary.

You could also try a kind of horizontal mapping:
s = sin(azimuth) * scaleS
t = sin(elevation) * scaleT

where scaleS and scaleT have the same general meaning as before, azimuth is in [0,2pi], and elevation is in [0,pi/2]. There is no built in support for this kind of mapping, so you’re on your own with this one, but it has the virtue of being very flexible.

None of the available texgens will wrap around a sphere.

You should generate the coordinates parametrically using the sphere vertices.

Something like s_coord = vertex_azimuth_angle * wrapscale_s, t_coord = vertex_elevation_angle * wrapscale_t