#include #include typedef struct { float x; float y; float z; } point3; point3 vertices[4]={{0.0,0.0, 0.0},{250.0,500.0, 100.0},{500.0,0.0,0.0},{250.0, 250.0, 250.0}}; int j; point3 new_p, old={250,100,250}; void clear(void){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); } void display( void ){ j=rand()%4; /* pick a vertex at random */ new_p.x=(old.x+vertices[j].x)/2; new_p.y=(old.y+vertices[j].y)/2; new_p.z=(old.z+vertices[j].z)/2; glBegin(GL_POINTS); glColor3f(1.0-new_p.z/250.,new_p.z/250.0, 0.0); glVertex3f(new_p.x,new_p.y,new_p.z); glEnd(); old.x=new_p.x; old.y=new_p.y; old.z=new_p.z; glFlush(); } void init(){ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 500.0, 0.0, 500.0, -500.0, 500.0); glMatrixMode(GL_MODELVIEW); } void mouse(int btn, int state, int x, int y){/*마우스 왼/오른버튼, 누름상태, 좌표x, 좌표y*/ if((btn==GLUT_LEFT_BUTTON) & (state==GLUT_DOWN)) glutIdleFunc(display); if((btn==GLUT_RIGHT_BUTTON) & (state==GLUT_DOWN)) glutIdleFunc(NULL); } void main(int argc, char** argv){ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB |GLUT_DEPTH); glutInitWindowSize(500,500); /* 500 x 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindow("Sierpinski Gasket"); /* window title */ glutDisplayFunc(clear); init(); glutIdleFunc(display); /* display callback invoked when window opened */ glutMouseFunc(mouse);// glEnable(GL_DEPTH_TEST); /*뒤에 만들어지는것이 안 보이게 하기 위함*/ glutMainLoop(); }