PDA

View Full Version : Texture sphere many times



Kojot
06-02-2010, 02:44 PM
I want to texture my sphere many times using one loaded texture. The texture should be repeated. I set these parameters:


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

but the texture doesn't repeat, it's just stretched.
Spehere is drawn by: gluSphere function.

Dark Photon
06-02-2010, 03:18 PM
but the texture doesn't repeat, it's just stretched.
Spehere is drawn by: gluSphere function.

Most likely texcoords go from 0..1 once around the sphere. For repeating, you want them to go many times. With the fixed-function pipeline, IIRC you can use the TEXTURE matrix to accomplish this. Check this out:

http://glprogramming.com/red/chapter09.html#name8

mhagain
06-02-2010, 03:20 PM
gluSphere generates texcoords in the 0 to 1 range, so you'll need to use the texture matrix to scale the texcoords.

glMatrixMode (GL_TEXTURE);
glLoadIdentity ();
glScalef (4, 2, 1);
Will repeat s x 4 and t x 2, for example. Don't forget to switch back to modelview (and potentially do another glLoadIdentity before that) after drawing.

Kojot
06-03-2010, 05:09 AM
Thanks mhagain, you answered my question.
I have another problem with texturing sphere. I will introduce what I need to get. I have an application where you can move and rotate camera. I want to add textured sphere, which will present the sky. I have written some code and I have the texture placed into my sky, but the texture is rotating with the camera. I always see the same part of the sky.
Before drawing sky I use gluLookAt function.
x, y, z are camera position coordinates, angle is an angle of camera rotation around 0,1,0 vector and angleV rotates camera around 1,0,0 vector.

I want my texture to be placed in constant position, so when I am rotating camera I can see other part of texture.

Here is code of sky rendering function:


void sky() {
glPushMatrix();

glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

glBindTexture(GL_TEXTURE_2D, skyTexture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

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

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

GLUquadricObj* quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric,GLU_FILL);
gluQuadricNormals(quadric,GLU_SMOOTH);
gluQuadricOrientation(quadric, GLU_OUTSIDE);
gluQuadricTexture(quadric, GLU_TRUE);

glTranslatef(x, y, z);

glMatrixMode(GL_TEXTURE);
glPushMatrix();
//glRotatef(angle, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);

gluSphere(quadric, 1000.0, 10, 10);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glMatrixMode(GL_TEXTURE);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);


glPopMatrix();


}