You forgot to enable GL_COLOR_MATERIAL, when using lighting. I
#define GLUT_DISABLE_ATEXIT_HACK
#include <stdlib.h>
#include <GL/glut.h>
// Globals
void display();
void myInit();
//Set up material
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_ambient_color[] = { 1,1,1,1 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = {1,1,1,1};
//Set up lighting
GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0};
GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0};
GLfloat ambient0[]={0.4, 1.0, 1.0, 1.0};
GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat local_view[] = { 0.0 };
GLfloat global_ambient[]={0,0,0,0};
void myReshape(int w, int h);
void drawSphere();
GLsizei wh=800, ww=800;
void myInit()
{
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); // Set background color to sky blue.
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
//NEW
glEnable(GL_COLOR_MATERIAL);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Draw sphere
glPushMatrix();
glTranslatef (-4.0, 5.0, 0.0);
glColor3f(1.0f,1.0f,0.0f);
glutSolidSphere(.5, 50, 50);
glPopMatrix();
//ground
glBegin(GL_POLYGON);
glColor3f(0.0,1.0,0.0);
glNormal3f(0.0f,0.0f,1.0f);
glVertex3d(-8,-8,0);
glVertex3d(8,-8,0);
glVertex3d(8,-2,0);
glVertex3d(-8,-2,0);
glEnd();
//dont use glFlush()
//glFlush();
//use these two instead
glutSwapBuffers();
glutPostRedisplay();
//just call this once in your main
glutReshapeFunc(myReshape);
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= (h * 2))
glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w,
3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2),
6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ww=w;
wh=h;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//use GLUT_DOUBLE not GLUT_SINGLE
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 800);
glutCreateWindow("Sphere");
myInit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}