1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #include <windows.h> #include <iostream.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <gl/glut.h> #include <math.h> #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(); } |
'Study > OpenGL' 카테고리의 다른 글
파노라마 제작하기 (0) | 2011.10.27 |
---|---|
OpenGL 스피어 렌더링하기 (0) | 2011.10.27 |
OpenGL bunny코드 분석 (0) | 2011.10.16 |
OpenGL 중간고사 (0) | 2011.10.13 |
OpenGL txt File 이용해 그래픽 불러 들이기 - bunny (1) | 2011.10.06 |