void polygonint a int b int c int
void polygon(int a, int b, int c , int d){ gl. Begin(GL_POLYGON); gl. Color 3 fv(colors[a]); gl. Vertex 3 fv(vertices[a]); gl. Color 3 fv(colors[b]); gl. Vertex 3 fv(vertices[b]); gl. Color 3 fv(colors[c]); gl. Vertex 3 fv(vertices[c]); gl. Color 3 fv(colors[d]); gl. Vertex 3 fv(vertices[d]); gl. End(); }
void colorcube(void){ // map vertices to faces polygon(0, 3, 2, 1); polygon(2, 3, 7, 6); polygon(0, 4, 7, 3); polygon(1, 2, 6, 5); polygon(4, 5, 6, 7); polygon(0, 1, 5, 4); }
static GLfloat theta[] = {0. 0, 0. 0}; static GLint axis = 2; void display(void){ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); gl. Rotatef(theta[0], 1. 0, 0. 0); gl. Rotatef(theta[1], 0. 0, 1. 0, 0. 0); gl. Rotatef(theta[2], 0. 0, 1. 0); colorcube(); glut. Swap. Buffers(); } void spin. Cube(){ theta[axis] += 2. 0; if( theta[axis] > 360. 0 ) theta[axis] -= 360. 0; glut. Post. Redisplay(); } void mouse(int btn, int state, int x, int y){ if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; }
void my. Reshape(int w, int h){ gl. Viewport(0, 0, w, h); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if (w <= h) gl. Ortho(-2. 0, -2. 0 * (GLfloat) h / (GLfloat) w, -10. 0, 10. 0); else gl. Ortho(-2. 0 * (GLfloat) w / (GLfloat) h, -2. 0, -10. 0, 10. 0); } void main(int argc, char**argv){ glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window("colorcube"); glut. Reshape. Func(my. Reshape); glut. Display. Func(display); glut. Idle. Func(spin. Cube); glut. Mouse. Func(mouse); gl. Enable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glut. Main. Loop(); }
정점배열을 활성화 시킨다. 배열에 저장할 데이터 지정 void main(int argc, char**argv){ glut. Init. Display. Mode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window("colorcube"); glut. Reshape. Func(my. Reshape); glut. Display. Func(display); glut. Idle. Func(spin. Cube); glut. Mouse. Func(mouse); gl. Enable. Client. State(GL_COLOR_ARRAY); gl. Enable. Client. State(GL_VERTEX_ARRAY); gl. Vertex. Pointer(3, GL_FLOAT, 0, vertices); gl. Color. Pointer(3, GL_FLOAT, 0, colors); gl. Enable(GL_DEPTH_TEST); glut. Main. Loop(); }
GLfloat vertices[][3] = {{-1. 0, -1. 0}, {1. 0, -1. 0}, {-1. 0, 1. 0}, {1. 0, 1. 0}, {-1. 0, 1. 0}}; GLfloat colors[] [3]= {{0. 0, 0. 0}, {1. 0, 0. 0}, {0. 0, 1. 0}, {1. 0, 1. 0}, {0. 0, 1. 0}}; GLubyte cube. Indices[]={0, 3, 2, 1, 2, 3, 7, 6, 0, 4, 7, 3, 1, 2, 6, 5, 4, 5, 6, 7, 0, 1, 5, 4}; void display(void){ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); gl. Rotatef(theta[0], 1. 0, 0. 0); gl. Rotatef(theta[1], 0. 0, 1. 0, 0. 0); gl. Rotatef(theta[2], 0. 0, 1. 0); gl. Draw. Elements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cube. Indices); /* for(int i=0; i<6; i++) gl. Draw. Elements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &cube. Indices[4*i]); */ glut. Swap. Buffers(); }
정점배열의 의미 gl. Enable. Client. State(GL_COLOR_ARRAY); gl. Enable. Client. State(GL_VERTEX_ARRAY); gl. Vertex. Pointer(3, GL_FLOAT, 0, vertices); gl. Color. Pointer(3, GL_FLOAT, 0, colors); gl. Draw. Elements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cube. Indices); /* for(i=0; i<6; i++) gl. Draw. Elements(GL_QUADS, 4, GL_UNSIGNED_BYTE, &cube. Indices[4*i]); */ /* int i; gl. Begin(mode); for(i=0; i<count; i++) gl. Array. Element(cube. Indices[i]); gl. End(); */ Note: gl. Array. Element(i); 의 의미는 아래 코드와 같다. gl. Color 3 fv(colors+cube. Indiced[i]*3); // gl. Color 3 fv(colors[cube. Indices[i]]); gl. Vertex 3 fv(vertices+cube. Indices[i]*3); //gl. Vertex 3 fv(vertices[cube. Indices[i] ]);
#define X. 525731112119 #define Z. 850650808352 static GLfloat vdata[12][3]={{-X, 0. 0, Z}, {X, 0. 0, -Z}, {0. 0, Z, X}, {0. 0, Z, -X}, {0. 0, Z, X}, {0. 0, -Z, -X}, {Z, X, 0. 0}, {-Z, X, 0. 0}, {Z, -X, 0. 0}, {-Z, -X, 0. 0}}; static GLuint tindices [20][3]={ {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4}, {1, 10, 8}, {10, 3, 8}, {8, 3, 5}, {3, 2, 5}, {3, 7, 2}, {3, 10, 7}, {10, 6, 7}, {6, 11, 7}, {6, 0, 11}, {6, 1, 0}, {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};
- Slides: 16