PDA

View Full Version : 2D-texture mapping a sphere. glTexGen*()



9ine
08-13-2004, 07:57 PM
In Red Book 1.1 there is a sample 'texgen.c' for mapping a 1D texture 'Automatic Texture-Coordinate Generation' and it works fine. I want to wrap a 2D texture around a sphere with glTexGen*() functions. I have 32x32 blue texture with red square in the center. The init() code looks like:
Init()
....
glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP)
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP)
glEnable(GL_TEXTURE_GEN_S)
glEnable(GL_TEXTURE_GEN_T)
....
But when a shpere is renered and I begin to rotate it, assigned texture doesnt rotate with the sphere but behaves like with GL_EYE_LINEAR and GL_EYE_PLANE enabled with 1D Red Book sample.
How make it to stick to sphere surface?

Rob The Bloke
08-14-2004, 02:01 AM
GL_OBJECT_LINEAR ?

Bob
08-14-2004, 02:02 AM
Sounds to me that you misunderstood the purpose of sphere mapping. Sphere mapping is a kind of reflection mapping. The texture is an image of the surrounding envoronment (as seen from the object), and GL_SPHERE_MAP generates texture coordinates for that image so you can use it to get a reflection of the environment in the object being textured.

If you want to wrap a texture around a sphere, like a texture of the earth to get a nice picture of our planet, you have to generate the texture coordinates yourself. The automatic texture coordinate generation won't help you in that case.

9ine
08-14-2004, 06:47 PM
Ive done with it. Yes with GL_TEXTURE_GEN_S and GL_TEXTURE_GEN_T texture will always facing you in GL_SPHERE_MAP mode. There are a good samples on how to calculate texture coordinates to get texture wrap a sphere and yet another way to do it is Quadrics. Just a simple one with few lines of code:
q = gluNewQuadric[];
gluQuadricNormals[q, GL_SMOOTH];
gluQuadricTexture[q, GL_TRUE]; <-- Thats it!
glTexGeni[GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP]
glTexGeni[GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP]
.......
glBindTexture[...];
gluSphere[q, ....];