PDA

View Full Version : Textures and gluSphere



WalterPebbles
01-09-2003, 06:30 AM
Hi...

I'm trying to learn about applying textures to quadrics. I've almost got them working correctly, but I've run into a problem I just can't get past.

I'm trying to create a rotating sphere with a texture -- for example, a spinning planet.

When I apply a texture to a sphere drawn by gluSphere and then rotate the sphere, the texture doesn't rotate. I know the sphere is rotating -- if I draw it with the drawing style set to GLU_LINE, the lines move. The resulting object shows the same portion of the texture no matter what rotation angle is.

This is the code that gets called each time the image needs to be updated, after the appropriate glRotatef calls.

GLuint texName[1];
GLUquadricObj *gobj=gluNewQuadric();

gluQuadricDrawStyle(gobj,GLU_FILL);
gluQuadricNormals(gobj,GLU_SMOOTH);
gluQuadricTexture(gobj,GL_TRUE);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(MATERIALCOUNT,texName);

glBindTexture(GL_TEXTURE_2D,texName[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,TEXTURE_WIDE,T EXTURE_HIGH,
0,GL_RGB,GL_UNSIGNED_BYTE,TextureData);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MO DULATE);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

glBindTexture(GL_TEXTURE_2D,texName[0]);

glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);

gluSphere(gobj,0.75f,32,32);

glFinish();

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);

glDisable(GL_TEXTURE_2D);
glDisable(GL_CULL_FACE);

gluDeleteQuadric(gobj);

glDeleteTextures(1,texName);

I'm sure I'm missing something pretty obvious, but I can't see it. I'll be eternally grateful 'til next Tuesday if someone can point out what I'm doing wrong.

Thanks.

-Walt

Bob
01-09-2003, 06:44 AM
You're enabling texture cooridnates for the quadric object, which is good. But then you override them with OpenGL's automatic texture coordinate generation, and generating reflection coordinates instead. GL_SPHERE_MAP is not used to map a texture onto a sphere, it's used to create spheremap reflections.

Another thing that confuse me, do you perform all that every time your display needs to be updated? If so, I strongly suggest not to recreate the texture and the quadric object each frame. Craete the texture and quadric object once on startup instead.

WalterPebbles
01-09-2003, 07:20 AM
Thanks... yes! That works... almost.

Upon changing the glTextGen argument to GL_OBJECT_LINEAR, the textures rotate correctly. However (and this is probably something else obvious I'm missing) the texture image only appears in the upper right quadrant of the sphere. The other three quadrants are filled with the edge pixels of the texture copied repeatedly across the quadrant.

Any further suggestions?

Thanks again.

-Walt

Bob
01-09-2003, 08:44 AM
You should not have OpenGL's automatic texture coordinate generation enabled at all.

[This message has been edited by Bob (edited 01-09-2003).]

nexusone
01-09-2003, 08:44 AM
You can check out how I did my textured map glusphere.

Look under project's it is called Glball
http://www.angelfire.com/linux/nexusone/