#ifdef __APPLE__ #include #else #include #endif #include #include #define DG M_PI/180.0 #define SSTEP 10 int step, dtime; int mouse_x, mouse_y; float t = 0.1; GLfloat vv[3], nn[3]; // Define the material for the torus GLfloat plainShininess[] = {100.0}; GLfloat plainSpecular[] = {1.0, 1.0, 1.0}; GLfloat plainDiffuse[] = {0.4, 0.4, 0.4}; // Define the material for the sphere (orbit) GLfloat sphereShininess[]= {128.0}; GLfloat sphereAmbient[] = {0.0, 0.0, 0.0}; GLfloat sphereDiffuse[] = {0.5, 0.0, 0.0}; GLfloat sphereSpecular[] = {0.5, 0.5, 0.5}; GLfloat sphereEmission[] = {0.05, 0.0, 0.0}; GLfloat torusLightSpecular[]= { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat torusLightDiffuse[] = { 1.0f, 0.8f, 0.1f, 1.0f }; GLfloat torusLightPosition[]= { -1.0f, -1.0f, -1.0f, 1.0f }; GLfloat sphereLightAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f }; GLfloat sphereLightDiffuse[] = { 0.5f, 0.0f, 0.0f, 1.0f }; GLfloat sphereLightSpecular[]= { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat sphereLightPosition[]= { 0.0f, 0.0f, 0.3f, 1.0f }; void hair(int th, int ta) { float fth, fta; fth = DG * (float)th; fta = DG * (float)ta; vv[0] = cos(fth)*(1.0 + 0.5 * cos(fta)); vv[1] = sin(fth)*(0.7 + 0.5 * cos(fta)); vv[2] = 0.5 * sin(fta); nn[0] = cos(fth) * cos(fta); nn[1] = sin(fth) * cos(fta); nn[2] = sin(fta); } void drawit( ) { int th, ta; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_LIGHTING); glLoadIdentity( ); glTranslatef(0.0, 0.0, -1.0); glRotatef(mouse_x, 1.0, 0.0, 0.0); glRotatef(mouse_y, 0.0, 1.0, 1.0); glEnable(GL_LIGHT1); glMaterialfv(GL_FRONT, GL_SPECULAR, plainSpecular); glMaterialfv(GL_FRONT, GL_SHININESS, plainShininess); glMaterialfv(GL_FRONT, GL_DIFFUSE, plainDiffuse); glColor3f(0.0, 0.0, 0.0); for(th = 0; th <= 360; th += step) { glBegin(GL_TRIANGLE_STRIP); for(ta = 0; ta <= 360; ta += step) { hair(th, ta); glNormal3fv(nn); glVertex3fv(vv); hair(th+step, ta); glNormal3fv(nn); glVertex3fv(vv); } glEnd( ); } glDisable(GL_LIGHT1); glPushMatrix( ); glMaterialfv(GL_FRONT, GL_SHININESS, sphereShininess); glMaterialfv(GL_FRONT, GL_AMBIENT, sphereAmbient); glMaterialfv(GL_FRONT, GL_DIFFUSE, sphereDiffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, sphereSpecular); glMaterialfv(GL_FRONT, GL_EMISSION, sphereEmission); sphereLightPosition[0] = 0.0f; sphereLightPosition[1] = 1.0 + sin(DG * t * dtime); sphereLightPosition[2] = cos(DG * t * dtime); glTranslatef(sphereLightPosition[0], sphereLightPosition[1], sphereLightPosition[2]); glLightfv(GL_LIGHT2, GL_POSITION,sphereLightPosition); glColor3f(1.0, 0.0, 0.2); glutSolidSphere(0.1, 150, 80); glPopMatrix( ); glutSwapBuffers( ); } void idle( ) { if(t >= (int) 360.0/dtime) { t = 0.0; } else { t += 0.1; } glutPostRedisplay( ); } void init( ) { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity( ); glOrtho(-3.0, 3.0, -3.0, 3.0, -3.0, 3.0); glMatrixMode(GL_MODELVIEW); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glShadeModel(GL_SMOOTH); glLightfv(GL_LIGHT1, GL_SPECULAR, torusLightSpecular); glLightfv(GL_LIGHT1, GL_DIFFUSE, torusLightDiffuse); glLightfv(GL_LIGHT1, GL_POSITION,torusLightPosition); glLightfv(GL_LIGHT2, GL_AMBIENT, sphereLightAmbient); glLightfv(GL_LIGHT2, GL_DIFFUSE, sphereLightDiffuse); glLightfv(GL_LIGHT2, GL_POSITION,sphereLightPosition); glLightfv(GL_LIGHT1, GL_SPECULAR,sphereLightSpecular); glEnable(GL_LIGHT2); } void passiveMouse(int x, int y) { mouse_x = 0.8 * mouse_x + 0.2 * x; mouse_y = 0.8 * mouse_y + 0.2 * y; } int main(int argc, char** argv) { step = 10; dtime = 10; mouse_x = 0; mouse_y = 0; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(400, 400); glutCreateWindow("light"); glEnable(GL_DEPTH_TEST); init( ); glutPassiveMotionFunc(passiveMouse); glutMotionFunc(passiveMouse); glutDisplayFunc(drawit); glutIdleFunc(idle); glutMainLoop( ); return 0; }