# texture wrapping with openGL

**URL:** <https://psychtoolbox.discourse.group/t/texture-wrapping-with-opengl/1806>\
**Category:** Uncategorized\
**Created:** [November 4, 2008, 9:48pm UTC](https://psychtoolbox.discourse.group/t/texture-wrapping-with-opengl/1806 "2008-11-04T21:48:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [November 4, 2008, 9:48pm UTC](https://psychtoolbox.discourse.group/t/texture-wrapping-with-opengl/1806/1 "2008-11-04T21:48:32Z")

</div>

Hi everyone I'm trying to wrap a texture to a sphere using openGL. My aim is a gray and   
white checkers board texture. I'm getting odd textures, like a striped sphere. I've looked   
at the demos but in my case Im generated my own texture not loading an image. Here are   
the relevant parts of my code:  
  
The texture:  
  
texSize = 512;  
  
soccerBall = zeros(texSize,texSize,3);  
 for i = 1: texSize  
 for j = 1: texSize  
  
 a = uint8(bitand(i,8)==0);  
 b = uint8(bitand(j,8)==0);  
 c = (a^b)\*255;  
 soccerBall(i,j,1) = c;  
 soccerBall(i,j,2) = c;  
 soccerBall(i,j,3) = c;  
  
 end  
 end  
  
  
  
 InitializeMatlabOpenGL(1);  
 Screen('BeginOpenGL', displayParams.window);  
 glEnable(GL\_TEXTURE\_2D);  
 glMatrixMode(GL\_PROJECTION);  
 glPushMatrix();  
 glLoadIdentity(); %%% Reset The Modelview Matrix  
 gluPerspective(displayParams.windowRect(4),displayParams.aspectRatio, 1.0, 3\*2.0);  
 glMatrixMode(GL\_MODELVIEW);  
 glPushMatrix(); %% Push The Matrix Onto The Stack  
 glLoadIdentity(); %%/ Reset The Modelview Matrix  
 glClearColor(0.5, 0.5, 0.5, 1.0); %%// Background  
 glClearDepth(0.0);   
  
 mysphere = gluNewQuadric;  
 gluQuadricTexture(mysphere, GL\_TRUE);   
  
 glTexGeni(GL\_S, GL\_TEXTURE\_GEN\_MODE, GL\_SPHERE\_MAP);   
 glTexGeni(GL\_T, GL\_TEXTURE\_GEN\_MODE, GL\_SPHERE\_MAP);   
  
 glColor3f(0.8, 0.8, 0.8);  
 gluQuadricNormals(mysphere, GL\_SMOOTH);   
 glEnable(GL\_TEXTURE\_2D);  
  
 glShadeModel(GL\_SMOOTH);   
 glEnable(GL\_DEPTH\_TEST);   
 glDepthFunc(GL\_LEQUAL);   
 glHint(GL\_PERSPECTIVE\_CORRECTION\_HINT, GL\_NICEST);   
 glLightfv(GL\_LIGHT0, GL\_AMBIENT, [3 3 3 1]);   
 glLightfv(GL\_LIGHT0, GL\_DIFFUSE, [3 3 3 1]);   
 glLightfv(GL\_LIGHT0, GL\_POSITION, [0 -2 -1 1]);   
  
 tex = glGenTextures(1);  
 glBindTexture(GL\_TEXTURE\_2D, tex);   
  
 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\_MAG\_FILTER, GL\_LINEAR);  
 glTexParameteri(GL\_TEXTURE\_2D, GL\_TEXTURE\_MIN\_FILTER, GL\_LINEAR);  
  
  
glTexImage2D(GL\_TEXTURE\_2D,0,GL\_LUMINANCE,texSize,texSize,0,GL\_RGB,GL\_UNSIGNED  
\_BYTE,soccerBall);  
  
  
 glMatrixMode(GL\_PROJECTION);  
 glLoadIdentity;  
  
 glColor3f(0.8, 0.8, 0.8);  
 gluQuadricNormals(mysphere, GL\_SMOOTH);   
 glEnable(GL\_TEXTURE\_2D);  
 gluPerspective(25,1/aspectRatio,0.1,100);  
glMatrixMode(GL\_MODELVIEW);  
glLoadIdentity;  
gluLookAt(3,3,5,0,0,0,0,1,0);  
glClearColor(0.5,0.5,0.5,0);  
  
  
  
Any pointers or suggestions are appreciated

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [November 5, 2008, 2:59am UTC](https://psychtoolbox.discourse.group/t/texture-wrapping-with-opengl/1806/2 "2008-11-05T02:59:50Z")

</div>

It's simpler than that, because the line  
  

> \> gluQuadricTexture(mysphere, GL\_TRUE);

already creates proper texture coordinates for  
the sphere. You don't need GL\_SPHERE\_MAP,  
and if i remember correctly, using it is a bit  
more complex than that 2 calls.  
  
Try this:  
  
edit MinimalisticOpenGLDemo.m  
  
then replace line 271 (the imread() command),  
by this to create a standard Matlab checkerboard  
image:  
  
myimg = double(checkerboard(16, 32, 32)\>0.5) \* 255;  
  
Save and run the demo and you'll see a nice  
checkerboard-textured rotating sphere.  
  
To reduce aliasing, you can replace the line  
  
glTexParameteri(gltextarget, GL.TEXTURE\_MIN\_FILTER, GL.LINEAR);  
  
by  
  
glTexParameteri(gltextarget, GL.TEXTURE\_MIN\_FILTER, GL.LINEAR\_MIPMAP\_LINEAR);  
  
and add this after the glBindTexture(gltextarget, gltex);  
command:  
  
glGenerateMipmapEXT(GL.TEXTURE\_2D);  
  
This will enable trilinear filtering for improved  
customer satisfaction - or throw an error if your  
hardware is too old to support glGenerateMipmapExt,  
in which case you'll need to learn about  
gluBuild2DMipmaps(), or simply look into the  
corresponding parts of ShepardZoomDemo.m,  
for the old-style "more hazzle, but runs everywhere"  
method of creating mipmaps.  
  
good luck,  
-mario  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "mniprog" \<nuha.jabakhanji@...\> wrote:  
> \>  
> \> Hi everyone I'm trying to wrap a texture to a sphere using openGL. My aim is a gray and   
> \> white checkers board texture. I'm getting odd textures, like a striped sphere. I've   
> looked   
> \> at the demos but in my case Im generated my own texture not loading an image. Here   
> are   
> \> the relevant parts of my code:  
> \>   
> \> The texture:  
> \>   
> \> texSize = 512;  
> \>   
> \> soccerBall = zeros(texSize,texSize,3);  
> \> for i = 1: texSize  
> \> for j = 1: texSize  
> \>   
> \> a = uint8(bitand(i,8)==0);  
> \> b = uint8(bitand(j,8)==0);  
> \> c = (a^b)\*255;  
> \> soccerBall(i,j,1) = c;  
> \> soccerBall(i,j,2) = c;  
> \> soccerBall(i,j,3) = c;  
> \>   
> \> end  
> \> end  
> \>   
> \>   
> \>   
> \> InitializeMatlabOpenGL(1);  
> \> Screen('BeginOpenGL', displayParams.window);  
> \> glEnable(GL\_TEXTURE\_2D);  
> \> glMatrixMode(GL\_PROJECTION);  
> \> glPushMatrix();  
> \> glLoadIdentity(); %%% Reset The Modelview Matrix  
> \> gluPerspective(displayParams.windowRect(4),displayParams.aspectRatio, 1.0, 3\*2.0);  
> \> glMatrixMode(GL\_MODELVIEW);  
> \> glPushMatrix(); %% Push The Matrix Onto The Stack  
> \> glLoadIdentity(); %%/ Reset The Modelview Matrix  
> \> glClearColor(0.5, 0.5, 0.5, 1.0); %%// Background  
> \> glClearDepth(0.0);   
> \>   
> \> mysphere = gluNewQuadric;  
> \> gluQuadricTexture(mysphere, GL\_TRUE);   
> \>   
> \> glTexGeni(GL\_S, GL\_TEXTURE\_GEN\_MODE, GL\_SPHERE\_MAP);   
> \> glTexGeni(GL\_T, GL\_TEXTURE\_GEN\_MODE, GL\_SPHERE\_MAP);   
> \>   
> \> glColor3f(0.8, 0.8, 0.8);  
> \> gluQuadricNormals(mysphere, GL\_SMOOTH);   
> \> glEnable(GL\_TEXTURE\_2D);  
> \>   
> \> glShadeModel(GL\_SMOOTH);   
> \> glEnable(GL\_DEPTH\_TEST);   
> \> glDepthFunc(GL\_LEQUAL);   
> \> glHint(GL\_PERSPECTIVE\_CORRECTION\_HINT, GL\_NICEST);   
> \> glLightfv(GL\_LIGHT0, GL\_AMBIENT, [3 3 3 1]);   
> \> glLightfv(GL\_LIGHT0, GL\_DIFFUSE, [3 3 3 1]);   
> \> glLightfv(GL\_LIGHT0, GL\_POSITION, [0 -2 -1 1]);   
> \>   
> \> tex = glGenTextures(1);  
> \> glBindTexture(GL\_TEXTURE\_2D, tex);   
> \>   
> \> 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\_MAG\_FILTER, GL\_LINEAR);  
> \> glTexParameteri(GL\_TEXTURE\_2D, GL\_TEXTURE\_MIN\_FILTER, GL\_LINEAR);  
> \>   
> \>   
> \>   
> glTexImage2D(GL\_TEXTURE\_2D,0,GL\_LUMINANCE,texSize,texSize,0,GL\_RGB,GL\_UNSIGNED  
> \> \_BYTE,soccerBall);  
> \>   
> \>   
> \> glMatrixMode(GL\_PROJECTION);  
> \> glLoadIdentity;  
> \>   
> \> glColor3f(0.8, 0.8, 0.8);  
> \> gluQuadricNormals(mysphere, GL\_SMOOTH);   
> \> glEnable(GL\_TEXTURE\_2D);  
> \> gluPerspective(25,1/aspectRatio,0.1,100);  
> \> glMatrixMode(GL\_MODELVIEW);  
> \> glLoadIdentity;  
> \> gluLookAt(3,3,5,0,0,0,0,1,0);  
> \> glClearColor(0.5,0.5,0.5,0);  
> \>   
> \>   
> \>   
> \> Any pointers or suggestions are appreciated  
> \>
