简体   繁体   中英

i want to wrap an image around a sphere in opengl

I am developing Billiard game for which i have several balls in the table. to make each ball unique i tried to give numbers to them using texture. Using texture i can also show rotation of balls.

So far i have mapped texture using SOIL in

INIT() function ---

glGenTextures(1,&texture_id);
texture_id = SOIL_load_OGL_texture("globe.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,  SOIL_FLAG_INVERT_Y);

glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

DISPLAY FUNCTION()

glPushMatrix();                                       // Push the current matrix stack
    glColor3f(1,1,1);
    glEnable( GL_TEXTURE_2D );
    glBindTexture( GL_TEXTURE_2D, texture_id);

    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);
    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_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTranslatef(world.qBall.pos.x,world.qBall.pos.y,world.qBall.pos.z);  //Multiply the current matrix by a translation matrix
    glRotatef(30,1,0,0);
    glutSolidSphere  (world.qBall.radius*5,50,50); 
    glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);
    glDisable(GL_TEXTURE_2D);
glPopMatrix();  // Pop the current matrix stack

I am getting from this is whole image mapped onto visible portion of sphere and not wrapping sphere so that only parts of image are visible . also when i move my camera texture also rotates with it.

So , how do i wrap my texture around sphere so that only partial image appears!.

Don't use texture coordinate generation mode. There's only a very small number of applications for it. Yours is not among them. GL_SPHERE_MAP is not wrapping a texture around a sphere; the name is a bit deceiving. Actually sphere maps are an early form of reflection maps, that look as if you'd photograph your environment through a spherical mirror.

从上面的MSDN链接获取的Sphere Map示例

In your case however you probably want to apply a cylindrical mapping, like the mercator projection .

圆柱图像投影

Fixed function OpenGL can not do it for you. However it's trivial to implement in a vertex shader phi = atan2(x/y); theta = atan2(x/z) phi = atan2(x/y); theta = atan2(x/z) use phi and theta for texture coordinates.

Anyway, the far better method is to draw the sphere geometry with texture coordinates ready for use. In https://stackoverflow.com/a/5989676/524368 I provided code for generating sphere geometry including mercator mapping texture coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM