View Full Version : Tiling texture on a sphere

05-03-2004, 10:14 PM
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.)


05-04-2004, 01:08 AM

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:

glEnable( GL_TEXTURE_GEN_S );

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.

05-05-2004, 03:17 PM
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