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