#include #include #include #include #define K 0.1 int n=300; GLfloat alpha=0.f; GLfloat rad=10; GLfloat pos[] = {0, 0, 10, 1.f}; GLfloat dir[] = {0, 0, -1}; GLfloat cutOff=3.f; void Idle(void) { static int i; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glLoadIdentity(); // glTranslatef(0,0,-10); //glutSolidTeapot(0.6); i++; if (i>360) i=0; glRotatef(i,1,1,1); glutSolidSphere(0.8,n,n); glPopMatrix(); glutSwapBuffers(); } void Display(void) { } void MyInit(void) { GLfloat mat_specular[] = {1, 1, 1, 1.0}; GLfloat mat_diffuse[] = {1, 1, 1, 1.0}; GLfloat mat_shininess[] = {50.0}; GLfloat specular[] = {0.5, 0.5, 0.5, 1}; GLfloat diffuse[] = {0.5, 0.5, 0.5, 1}; glClearColor(1,1,1,1); /* Set up material */ glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); /* Set up light0 */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, pos); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,cutOff); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); printf("Usage:\n+,-: # of triangles\n"); printf("a,z: size of the spot\n"); printf("up,down: light position\n"); printf("(the light always shines into the center)\n"); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1,1,-1,1,-10,10); glMatrixMode(GL_MODELVIEW); } void Key(unsigned char key, int x, int y) { switch (key){ case '+': n++;break; case '-': n--;break; case 'a': cutOff+=K;break; case 'z': cutOff-=K;break; case 27: case 'Q': case 'q': exit(0);break; } glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,cutOff); printf("n=%i\n",n); } void SpecKey(GLint key, int x, int y) { switch (key){ case GLUT_KEY_UP: alpha+=K;break; case GLUT_KEY_DOWN: alpha-=K;break; } pos[0]=0; pos[1]=rad*sin(alpha); pos[2]=rad*cos(alpha); glLightfv(GL_LIGHT0, GL_POSITION, pos); dir[0]=-pos[0]; dir[1]=-pos[1]; dir[2]=-pos[2]; glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir); printf("pos=%f %f %f\n",pos[0],pos[1],pos[2]); } int main(int argc, char **argv) { glutInitWindowSize(400,400); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutCreateWindow("fuBar"); MyInit(); glutDisplayFunc(Display); glutIdleFunc(Idle); glutKeyboardFunc(Key); glutSpecialFunc(SpecKey); glutReshapeFunc(myReshape); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }