texture wrapping with openGL

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
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, "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
>