3

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!.

akash300
  • 49
  • 1
  • 8
  • You only enabled 2 axes `glEnable(GL_TEXTURE_GEN_S) glEnable(GL_TEXTURE_GEN_T);` try to add Q and R (it's in 3d) – j-p Apr 10 '14 at 08:23
  • Environment mapping is normaly used to mimic reflective surface. try to use Object linear mapping instead – j-p Apr 10 '14 at 08:38
  • ` gl.glEnable(GL.GL_TEXTURE_GEN_R); gl.glTexGeni(GL.GL_R, GL.GL_TEXTURE_GEN_MODE, GL.GL_OBJECT_LINEAR);`seems to be sufficient to apply texture on random object. – j-p Apr 10 '14 at 08:41
  • [this link](http://jerome.jouvie.free.fr/opengl-tutorials/Tutorial11.php) explains well the different techniques – j-p Apr 10 '14 at 08:42
  • thanks @j-p for the quick reply. I tried to use Q and R axis as you mentioned but it didn't worked , also it removed what ever texture it was showing earlier also. Now it is showing plain white ball. – akash300 Apr 10 '14 at 09:29
  • In [this link](http://jerome.jouvie.free.fr/opengl-tutorials/Tutorial11.php) they only use R axis with object_linear. – j-p Apr 10 '14 at 09:35
  • Even then it is not working. – akash300 Apr 10 '14 at 10:05
  • could you update the question with your actual code plz? – j-p Apr 10 '14 at 10:33

1 Answers1

5

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.

Sphere Map example taken from the MSDN link above

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

Cylindrical Image 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) 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.

Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Yes i meant mercator projection only. I tried the solution which you provided and it worked wonderfully. However i need to put some texture wrapping the ball for example world map. How do i add my .bmp or .png file over there. Thanks for the reply. – akash300 Apr 10 '14 at 16:11
  • @akash300: Use one of the existing OpenGL texture loader libraries. SOIL, DevIL (not very actively maintained though), etc. If you type "OpenGL SOIL" into the little box of Google you should get plenty of results. – datenwolf Apr 10 '14 at 18:38