/* litsphere.c */

#include <GLUT/glut.h>

void display( void );
void resize( int width, int height );
void init( void );
int main( int argc, char** argv );

float angle = 0;

GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 0.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };

GLfloat mat_shininess = 20;
//GLfloat mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
//GLfloat mat_diffuse[] = { 0.5, 0.5, 0.5, 1.0 };

//GLfloat mat_specular[] = { 0.8, 0.6, 0.8, 1.0 };
GLfloat mat_specular[] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat mat_diffuse[] = { 0.3, 0.5, 0.0, 1.0 };

GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 }; // Positional!
//GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 }; // Directional!
//GLfloat light_position[] = { -1.0, 1.0, 1.0, 0.0 }; // Directional!

void display( void )
{
	glClear( GL_COLOR_BUFFER_BIT );
	glClear( GL_DEPTH_BUFFER_BIT );
	
	glLoadIdentity();
	
// Light source in camera (origin of view coord)
//	glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Positional, model coordinates in the proper place.
	
// world-to-view transform
	gluLookAt(0, 0, 4,  0, 0, 0,  0, 1, 0); // camera, look-at-point, up-vector
	
// Light source independent of camera (e.g. world coordinates)
// which means inside the big sphere in our case if at 0,0
// 1.1 right, 0.5 z places it besides
//	glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Positional, model coordinates in the proper place.
	
// Set light position before drawing!
	glPushMatrix();
	glRotatef(angle, 0, 1, 0); // Transf to small sphere's coordinate system
	glTranslatef(2, 0, 0);
// Light source that follows an object (e.g. model coordinates)
	glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Positional, model coordinates in the proper place.
	glPopMatrix();
	
// Light and material attributes
	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SHININESS, &mat_shininess);
//	glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Directional, world coordinates!
	glRotatef(angle, 0, 1, 0);
	glutSolidSphere(1.0, 64, 32);
	glTranslatef(2, 0, 0);
//	glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Positional, model coordinates in the proper place.
// Should not be here - see above.
	glutSolidSphere(0.3, 32, 16);
	
	glFlush();
}

void timer(int i)
{
	angle += 2;
	glutTimerFunc(20, timer, i);
	glutPostRedisplay();
}

void init( void )
{
	glClearColor( 0.7, 0.7, 0.9, 1.0 );
	
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-0.1, 0.1, -0.1, 0.1, 0.15, 100); // left, right, bottom, top, near, far
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 0, 5,  0, 0, 0,  0, 1, 0); // camera, look-at-point, up-vector
}

int main( int argc, char** argv )
{
	glutInit(&argc, argv);
	
	glutInitDisplayMode( GLUT_RGB|GLUT_SINGLE|GLUT_DEPTH );
	glutCreateWindow( argv[0] );
	init();
	glutDisplayFunc( display );
//	glutReshapeFunc( resize );
	glutTimerFunc(20, timer, 0);
	glutMainLoop();
	return 0;
}

