Search code examples
openglgeometrylight

OpenGL lighting and shading on a GL_QUAD_STRIP


New to OpenGL, trying to implement a sphere with shading and lighting. I am aware that there is a sphere function I could call on, but trying to create my own. However, while I can get lighting and shading on other objects in the scene (such as glcubes, etc.), I am unable to get any shading or lighting on the sphere. Any suggestions would be greatly appreciated!

Bellow is my function to draw the sphere:

void cg_SolidSphere( GLint slices, GLint stacks )
{
    float x, y, z ;
    for( float lat = 0 ; lat < PI ; lat = lat + PI/stacks )
    {
        glBegin( GL_QUAD_STRIP ) ;
        for( float lon = 0 ; lon < 2*PI ; lon = lon + 2*PI/slices )
        {
            x = cosf( lat ) * sinf( lon ) ;
            y = sinf( lat ) * sinf( lon ) ;
            z = cosf( lon ) ;
            glVertex3f( x, y, z ) ;
            x = cosf( lat + PI/stacks ) * sinf( lon ) ;
            y = sinf( lat + PI/stacks ) * sinf( lon ) ;
            z = cosf( lon ) ;
            glVertex3f( x, y, z ) ;
        }
        glEnd() ;
    }
}

Solution

  • You need glNormal3f calls, too. In this case, the normal is the same as the vertex, so we can use the same points:

    x = cosf( lat ) * sinf( lon ) ;
    y = sinf( lat ) * sinf( lon ) ;
    z = cosf( lon ) ;
    glNormal3f( x, y, z ) ;
    glVertex3f( x, y, z ) ;
    x = cosf( lat + PI/stacks ) * sinf( lon ) ;
    y = sinf( lat + PI/stacks ) * sinf( lon ) ;
    z = cosf( lon ) ;
    glNormal3f( x, y, z ) ;
    glVertex3f( x, y, z ) ;