Study/OpenGL OpenGL 구에 조명주기 코딩하는 야구쟁이 2011. 10. 24. 10:49 #include #include #include #include #include #include #include #define PI 3.1415926 #define No_theta 36 #define No_phi 18 float theta, phi; float delta_theta, delta_phi; float start_theta, start_phi; float R_Position[] = { 3.0f, 0.0f, 5.0f, 1.0f}; float R_Diffuse[] = { 1.0f, 0.2f, 0.5f, 1.0f}; float R_Specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; float Position[] = { 3.0f, 3.0f, 3.0f, 1.0f }; float Diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f }; float Specular[] = { 1.0f, 0.0f, 0.0f, 1.0f }; float light0_position[] = {5.0, -2.0, 2.0, 1.0}; float light0_ambient[] = {1.0, 1.0, 1.0, 1.0}; float light0_diffuse[] = {1.0, 1.0, 1.0, 1.0}; float light0_specular[] = {1.0, 1.0, 1.0, 1.0}; float material0_specular[] = {1.0, 1.0, 1.0, 1.0}; float material0_diffuse[] = {1.0, 0.0, 0.0, 1.0}; float Radius; struct vector3d{ float x; float y; float z; }; struct point3d{ float x; float y; float z; vector3d normal; }; point3d ver[No_theta][No_phi]; void Vertex_Generation(void) { start_theta = 0.0; delta_theta = 2* PI/ No_theta; start_phi = -PI/2.0; delta_phi = PI / (No_phi-1); for ( int j = 0; j < No_phi; j++ ) { for ( int i = 0; i < No_theta; i++) { theta = start_theta + i * delta_theta; phi = start_phi + j * delta_phi; ver[i][j].x = Radius * cos(phi) * cos(theta); ver[i][j].y = Radius * cos(phi) * sin(theta); ver[i][j].z = Radius * sin(phi); } } } void init (void) { glClearColor (0.0, 0.0, 0.0, 0.0) ; glShadeModel(GL_SMOOTH); // glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); glMaterialfv(GL_FRONT, GL_SPECULAR, material0_specular); glMaterialfv(GL_FRONT, GL_DIFFUSE, material0_diffuse); glMaterialf(GL_FRONT, GL_SHININESS, 100); Radius = 1.0; Vertex_Generation(); } void Draw_Sphere(void) { for ( int j = 0; j < No_phi -1; j++ ) { for (int i = 0; i < No_theta; i++) { glBegin(GL_POLYGON); glNormal3f(ver[i][j].x, ver[i][j].y, ver[i][j].z); glVertex3f(ver[i][j].x, ver[i][j].y, ver[i][j].z); glNormal3f(ver[(i+1)%No_theta][j].x, ver[(i+1)%No_theta][j].y, ver[(i+1)%No_theta][j].z); glVertex3f(ver[(i+1)%No_theta][j].x, ver[(i+1)%No_theta][j].y, ver[(i+1)%No_theta][j].z); glNormal3f(ver[(i+1)%No_theta][j+1].x, ver[(i+1)%No_theta][j+1].y, ver[(i+1)%No_theta][j+1].z); glVertex3f(ver[(i+1)%No_theta][j+1].x, ver[(i+1)%No_theta][j+1].y, ver[(i+1)%No_theta][j+1].z); glNormal3f(ver[i][j+1].x, ver[i][j+1].y, ver[i][j+1].z); glVertex3f(ver[i][j+1].x, ver[i][j+1].y, ver[i][j+1].z); glEnd(); } } } void reshape (int w, int h) { glViewport(0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, 1.0, 1.0, 1000); gluLookAt(2.0 * Radius, 2.0 * Radius, 2.0 * Radius, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); } void display (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw_Sphere(); glFlush (); glutSwapBuffers(); } void main (int argc, char** argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowPosition (100, 100); glutInitWindowSize (500, 500); glutCreateWindow ("Generated Sphere"); glutDisplayFunc (display); glutReshapeFunc (reshape); glutIdleFunc(display); init(); glutMainLoop(); }