texture mapped sphere

Post: #1
hey everyone, it's been a while since I've been on these forums (school).

Anyways, I'm trying to draw a texture-mapped sphere. I've gotten past the initial problem of having nothing displayed, and now there's an issue with black polygons all over the sphere. Below is my code, a screenshot of the offending sphere, and the texture i want to apply to it. Also, a plea for help.

The texture map seems to be properly aligned, the only problem is the gaps.

Note that in the drawing algorithm, I haven't connected the last index to the first, and i havent connected the north and south poles of the sphere to the top and bottom coordinates in the array. I dont think this would cause such a pattern of black spaces though, i would only expect to see holes in the top and bottom of the sphere, and perhaps an empty gap going from top to bottom at some point.

Any commentary is appreciated

initialization code:
void SphereMap_Init (void)
    int            map_x, map_y;
    GLfloat        phi, theta, r;
    GLfloat        phi_rad, theta_rad;
    r = 1.0f;
    pm.south_Pt.x =
    pm.south_Pt.y = 0;
    pm.south_Pt.z = -1.0f;
    pm.north_Pt.x =
    pm.north_Pt.y = 0;
    pm.north_Pt.z = 1.0f;
    for (map_y = 0; map_y < kSphereMap_Height; ++ map_y) {
        // -80 deg <= phi <= 80 deg
        phi = kZAngleConstant + kZAngleIncrementPerIndex * map_y;
        for (map_x = 0; map_x < kSphereMap_Width; ++ map_x) {
            // 0 deg <= theta <= 350 deg
            theta = kXAngleConstant + kXAngleIncrementPerIndex * map_x;
            theta_rad    = theta * kConvertToRadians;
            phi_rad        = phi * kConvertToRadians;
            pm.texMap_Pt [map_x][map_y].x = r * sin(phi_rad) * cos(theta_rad);
            pm.texMap_Pt [map_x][map_y].y = r * sin(phi_rad) * sin(theta_rad);
            pm.texMap_Pt [map_x][map_y].z = r * cos(phi_rad);
            printf("mapx = %d, mapy = %d ||| ", map_x, map_y);
            printf("theta = %f, phi = %f:\n", theta, phi);
            printf("x=%f\t\ty=%f\t\tz=%f\n\n", pm.texMap_Pt [map_x][map_y].x,
                                    pm.texMap_Pt [map_x][map_y].y,
                                    pm.texMap_Pt [map_x][map_y].z);
        }    // set appropriate vertices

void    SphereMap_OGLDraw (    GLuint    sphere_texture,
                            GLfloat    sphere_radius,
                            OGLPoint3d sphere_coord    )
    int            map_x, map_y;
    GLfloat        texCoord_x, texCoord_y;    
    glBindTexture( GL_TEXTURE_2D, sphere_texture );
        for (map_y=0; map_y<kSphereMap_Height-1; map_y++) {
            texCoord_y = map_y * kSphereMapTexConvert_v;
        for (map_x=0; map_x<kSphereMap_Width-1; map_x++) {
            texCoord_x = map_x * kSphereMapTexConvert_h;
        // NOW THAT X and Y are set...
        // choose the 4 points on the mesh {(x,y); (x+1,y); (x,y+1); (x+1,y+1)}
                glTexCoord2f(    texCoord_x,
                                texCoord_y );
                glVertex3f    (    sphere_radius * pm.texMap_Pt [map_x  ][map_y  ].x,
                                sphere_radius * pm.texMap_Pt [map_x  ][map_y  ].y,
                                sphere_radius * pm.texMap_Pt [map_x  ][map_y  ].z );
                glTexCoord2f(    texCoord_x + kSphereMapTexConvert_h,
                                texCoord_y );
                glVertex3f    (    sphere_radius * pm.texMap_Pt [map_x+1][map_y  ].x,
                                sphere_radius * pm.texMap_Pt [map_x+1][map_y  ].y,
                                sphere_radius * pm.texMap_Pt [map_x+1][map_y  ].z );
                glTexCoord2f(    texCoord_x,
                                texCoord_y + kSphereMapTexConvert_v );
                glVertex3f    (    sphere_radius * pm.texMap_Pt [map_x  ][map_y+1].x,
                                sphere_radius * pm.texMap_Pt [map_x  ][map_y+1].y,
                                sphere_radius * pm.texMap_Pt [map_x  ][map_y+1].z );
                glTexCoord2f(    texCoord_x + kSphereMapTexConvert_h,
                                texCoord_y + kSphereMapTexConvert_v );
                glVertex3f    (    sphere_radius * pm.texMap_Pt [map_x+1][map_y+1].x,
                                sphere_radius * pm.texMap_Pt [map_x+1][map_y+1].y,
                                sphere_radius * pm.texMap_Pt [map_x+1][map_y+1].z );

the texture map (rectangular) and the improperly rendered sphere:
[Image: badsphere0zh.th.png]

okay... Adding some rotation, I now know that the sphere is only a half-sphere. There must be something wrong with my initialization code ... AND the drawing code. curse these polar coordinates. curse them!!!!

[Image: picture66iv.th.png]
Posts: 1,487
Joined: 2002.09
Post: #2
It looks to me like your vertex coordinates are out of order. When using GL_QUADS you have to put your vertexes in clockwise or counter-clockwise order.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Post: #3
yeah, skorche, that was the exact problem.

(its great to be back - this truly is the greatest forum in the universe)

I knew this. And now I know it again.

Anyways now the sphere renders perfectly, except there seems to be some strange thing going on with DepthTest.

at initialization i set:

// enable depth test

note: The sphere is constantly rotating when I run the app.

It seems once per rotation, the depth test gets INVERTED somehow, and the back of the sphere gets rendered. during this inversion, the sphere seems to divide into strips, and like blinds being opened, the texture beneath is revealed. After the inversion, the sphere appears to be rotating in the opposite direction. This happens once every 360 degrees... Any ideas?

(Update: I checked more carefully, and the depth test glitch occurs every 180 degrees, not 360.)


Might be because i set zNear to -256.... hmmmmm.....

opengl.org - "A mistake many programmers make is to specify a zNear clipping plane value of 0.0 or a negative value which isn't allowed"
Posts: 1,487
Joined: 2002.09
Post: #4
Don't know about that one. Offhand I would suspect that you have back face culling enabled and not all of your quads are wound in the same direction, but that doesn't quite sound like it fits the symptoms.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Posts: 5,143
Joined: 2002.04
Post: #5
Also, check that you have actually requested a depth buffer...

but yes, in gluPerspective, it makes no sense for near to be <= 0, and it makes no sense for far to be <= near. In general, you want to make the ratio far/near as small as possible.
Post: #6
yah, i'm set to 10 : 1000, which seems to work great. I've got a tiny seam between the [35] and [0] array indices on the sphere for some reason, but otherwise the problem's solved!
Thread Closed 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Need ray-sphere intersection code MattDiamond 23 21,313 Aug 31, 2009 02:28 PM
Last Post: Gengar003
  Lighting a sphere with OpenGL lights (normals wrong?) cecilkorik 3 9,994 Dec 27, 2007 02:40 PM
Last Post: _ibd_
  seeing the inside of a sphere Najdorf 10 6,389 Jul 17, 2006 09:19 PM
Last Post: OneSadCookie
  Space Box/Sphere misteranderson 1 3,397 May 12, 2006 10:48 AM
Last Post: TomorrowPlusX
  Texture mapped text woes... spaceb 2 3,159 Apr 9, 2005 09:32 PM
Last Post: spaceb