Computer Graphics 7 Lee ByungGook 12122021 Computer Graphics
Computer Graphics 7 Lee Byung-Gook 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 1
3 D Modeling Data • polygonal mesh - a collection of polygons each polygon is defined by its vertices and its front (vs. back) face defined by its normal vector. • Uses: a polygonal mesh can be used to define any type of object, but it is required when an object cannot be defined as a collection of standard solid primitives, such as cubes, spheres and cylinders. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 2
Programming polygonal meshes • A polygonal mesh is usually interconnected. That is, the polygons share vertices. An efficient representation of a polygonal mesh should not store duplicate data. • Three basic things need to be stored: – the vertices (geometric information) – the face normal vectors (orientation information) – which vertices define a specific face (topological information). • Assuming that all of the polygons have the same number of vertices, the data can efficiently be stored in arrays and processed using simple loops. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 3
Procedure to create a polygonal mesh 1. Draw a good sketch of the object. 2. Label each vertex with an index, starting with 0. 3. Store the vertices in an array consistent with the vertex labels. (That is, store vertex 3 in position 3 of the array. ) 4. Label each face with an index, starting with 0. 5. Calculate the normal vectors to each face and store them in an array consistent with the face labels. (That is, store the normal vector face 3 in position 3 of the array. ) 6. Store the indexes of the vertices that make up each face in a "face array" and make them consistent with the face labels. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 4
Normal vector • A normal vector to a plane is a vector that is at right angles to every vector that lies in the plane. (There are two such vectors for any given plane. ) 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 5
Normal vector • A normal vector to a face is a vector that is at right angles to every vector that lies in the plane and it points away from the "front side". Looking at the back side 12/12/2021 Looking at the front side Computer Graphics, Lee Byung-Gook, Dongseo Univ. 6
Normal vector • To calculate the face normal, take the cross product of two consecutive edges of the face. To get the correct normal vector, always specify the vertices of the face in a counter-clockwise direction when looking at the front of the face. v 1 v 0 v 2 v 3 counter-clockwise from front 12/12/2021 v 3 v 0 v 2 v 1 counter-clockwise from front Computer Graphics, Lee Byung-Gook, Dongseo Univ. 7
Example Y 4 3 (left face) 5 1 6 7 2 0 (origin) 3 Z 12/12/2021 4 (back face) Open Side 1 X 2 0 (bottom face) Computer Graphics, Lee Byung-Gook, Dongseo Univ. 8
Vertices const GLint Number. Vertices = 8; GLfloat Vertices[Number. Vertices][3] = { {0. 0, 0. 0}, // vertex 0 {1. 0, 0. 0}, // vertex 1 {1. 0, 0. 0, 1. 0}, // vertex 2 {0. 0, 1. 0}, // vertex 3 {0. 0, 1. 0, 0. 0}, // vertex 4 {1. 0, 0. 0}, // vertex 5 {1. 0, 1. 0}, // vertex 6 {0. 0, 1. 0} // vertex 7 }; 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 9
Faces // A face is defined as indexes into the vertices array. const GLint Number. Faces = 5; GLint Faces[Number. Faces][4] = { {0, 1, 2, 3}, // face 0 {4, 7, 6, 5}, // face 1 {2, 6, 7, 3}, // face 2 {3, 7, 4, 0}, // face 3 {4, 5, 1, 0} // face 4 }; 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 10
Normals GLfloat Normals[Number. Faces][3] = { { 0. 0, -1. 0, 0. 0}, // face 0 { 0. 0, 1. 0, 0. 0}, // face 1 { 0. 0, 1. 0}, // face 2 {-1. 0, 0. 0}, // face 3 { 0. 0, -1. 0} // face 4 }; 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 11
Draw. Mesh void Draw. Mesh(GLfloat vertices[][3], Glfloat normal. Vectors[][3], Glint faces[][4], Glint n. Faces) { for (j=0; j<n. Faces; j++) { gl. Begin(GL_QUADS); gl. Normal 3 fv(normal. Vectors[j]); gl. Vertex 3 fv(vertices[ faces[j][0]]); gl. Vertex 3 fv(vertices[ faces[j][1]]); gl. Vertex 3 fv(vertices[ faces[j][2]]); gl. Vertex 3 fv(vertices[ faces[j][3]]); gl. End(); } } 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 12
lab 13. cpp #include <math. h> #include <stdio. h> #include <windows. h> #include <gl/glut. h> typedef float vec 3_t[3]; float size=3. ; float theta=. 0; float theta. Delta=. 125; float eye. Delta=. 125; float scale=1. 0; float scale. Delta=1. 125; int int int mouse. X = 0; mouse. Y = 0; mouse. State = 0; mouse. Button = 0; projection = 0; ani. On = 0; depth. On = 1; open. On = 0; fill. On = 1; frontface = 0; 12/12/2021 int int cullface = 0; window. Width; window. Height; vec 3_t rot = {0. , 0. }; vec 3_t eye = {0. , -5. }; vec 3_t center = {0. , 0. }; vec 3_t color[] = { {1. 0 f, 0. 0 f}, //red {0. 0 f, 1. 0 f, 0. 0 f}, //green {0. 0 f, 1. 0 f}, //blue {1. 0 f, 0. 0 f}, //yellow {1. 0 f, 0. 0 f, 1. 0 f}, //magenta {0. 0 f, 1. 0 f}, //cyan {1. 0 f, 1. 0 f}, //white {. 25 f, . 25 f}, //dark gray {. 60 f, . 40 f, . 70 f}, //barney purple {. 98 f, . 625 f, . 12 f}, //pumpkin orange {. 98 f, . 04 f, . 70 f}, //pastel pink {. 75 f, . 75 f}, //light gray {. 60 f, . 40 f, . 12 f} //brown }; Computer Graphics, Lee Byung-Gook, Dongseo Univ. 13
lab 13. cpp const GLint Number. Vertices = 8; GLfloat Vertices[Number. Vertices][3] = { {0. 0, 0. 0}, // vertex 0 {1. 0, 0. 0}, // vertex 1 {1. 0, 0. 0, 1. 0}, // vertex 2 {0. 0, 1. 0}, // vertex 3 {0. 0, 1. 0, 0. 0}, // vertex 4 {1. 0, 0. 0}, // vertex 5 {1. 0, 1. 0}, // vertex 6 {0. 0, 1. 0} }; // vertex 7 const GLint Number. Faces = 5; GLfloat Normals[Number. Faces][3] = { { 0. 0, -1. 0, 0. 0}, // face 0 { 0. 0, 1. 0, 0. 0}, // face 1 { 0. 0, 1. 0}, // face 2 {-1. 0, 0. 0}, // face 3 { 0. 0, -1. 0} }; // face 4 12/12/2021 GLint Faces[Number. Faces][4] = { {0, 1, 2, 3}, // face 0 {4, 7, 6, 5}, // face 1 {2, 6, 7, 3}, // face 2 {3, 7, 4, 0}, // face 3 {4, 5, 1, 0} }; // face 4 void draw. Mesh(GLfloat vertices[][3], GLfloat normal. Vectors[][3], GLint faces[][4], GLint n. Faces) { int j; for(j=0; j<n. Faces; j++) { gl. Color 3 fv(color[j%12]); gl. Begin(GL_QUADS); gl. Normal 3 fv(normal. Vectors[j]); gl. Vertex 3 fv(vertices[faces[j][0]]); gl. Vertex 3 fv(vertices[faces[j][1]]); gl. Vertex 3 fv(vertices[faces[j][2]]); gl. Vertex 3 fv(vertices[faces[j][3]]); gl. End(); } } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 14
lab 13. cpp void my. Display (void) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); if(!projection) glu. Look. At(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); gl. Rotatef(rot[0], 1. 0 f, 0. 0 f); gl. Rotatef(rot[1], 0. 0 f, 1. 0 f, 0. 0 f); gl. Rotatef(rot[2], 0. 0 f, 1. 0 f); gl. Scalef(scale, scale); draw. Mesh(Vertices, Normals, Faces, Number. Faces); gl. Flush(); glut. Swap. Buffers(); } 12/12/2021 void my. Look. At(int key) { if(key == GLUT_KEY_UP) { eye[2] = eye[2]-cos(theta)*eye. Delta; eye[0] = eye[0]+sin(theta)*eye. Delta; } else if(key == GLUT_KEY_DOWN) { eye[2] = eye[2]+cos(theta)*eye. Delta; eye[0] = eye[0]-sin(theta)*eye. Delta; } center[2] = eye[2]-cos(theta); center[0] = eye[0]+sin(theta); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 15
lab 13. cpp void my. Resize (int width, int height){ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Viewport(0, 0, width, height); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); if(projection) { gl. Ortho(-size, -size, size); eye[2] = 0. ; } else { glu. Perspective(60. , (float)width/height, . 1, 100. ); eye[2] = 5. ; my. Look. At(0); } gl. Enable(GL_DEPTH_TEST); gl. Cull. Face(GL_BACK); gl. Polygon. Mode(GL_BACK, GL_LINE); gl. Polygon. Mode(GL_FRONT, GL_FILL); window. Width=width; window. Height=height; } 12/12/2021 void my. SKeyboard (int key, int x, int y) { switch (key) { case GLUT_KEY_UP : break; case GLUT_KEY_DOWN : break; case GLUT_KEY_LEFT : theta-=theta. Delta; break; case GLUT_KEY_RIGHT : theta+=theta. Delta; break; default : return; } my. Look. At(key); glut. Post. Redisplay(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 16
lab 13. cpp void my. Keyboard (unsigned char key, int x, int y) { switch (key) { case 'p': projection = !projection; my. Resize(window. Width, window. Height); break; case 'd': depth. On = !depth. On; if(depth. On) gl. Enable(GL_DEPTH_TEST); else gl. Disable(GL_DEPTH_TEST); break; case 'f': frontface = !frontface; if(frontface) gl. Front. Face(GL_CW); else gl. Front. Face(GL_CCW); break; case 'c': cullface = !cullface; if(cullface) gl. Enable(GL_CULL_FACE); else gl. Disable(GL_CULL_FACE); break; 12/12/2021 case 'z': scale*=scale. Delta; break; case 'x': scale/=scale. Delta; break; } glut. Post. Redisplay(); } void my. Mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { glut. Idle. Func(NULL); mouse. State=state; mouse. Button=btn; mouse. X=x; mouse. Y=y; } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 17
lab 13. cpp else if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP) mouse. State=-1; else return; glut. Post. Redisplay(); } void my. Motion(int x, int y) { if(mouse. Button == GLUT_LEFT_BUTTON && mouse. State == GLUT_DOWN) { rot[1] -= (mouse. X - x)/5. ; rot[0] -= (mouse. Y - y)/5. ; glut. Post. Redisplay(); mouse. X=x; mouse. Y=y; } } 12/12/2021 void main (void) { glut. Init. Display. Mode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glut. Init. Window. Size(500, 500); glut. Create. Window("lab 13 by lbg@dongseo. ac. kr"); glut. Display. Func(my. Display); glut. Reshape. Func(my. Resize); glut. Keyboard. Func(my. Keyboard); glut. Special. Func(my. SKeyboard); glut. Mouse. Func(my. Mouse); glut. Motion. Func(my. Motion); glut. Main. Loop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 18
gl. Cull. Face • gl. Cull. Face - specify whether front- or back-facing facets can be culled • void gl. Cull. Face( GLenum mode ) • PARAMETERS mode Specifies whether front- or back-facing facets are candidates for culling. Symbolic constants GL_FRONT, GL_BACK, and GL_FRONT_AND_BACK are accepted. The initial value is GL_BACK. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 19
gl. Front. Face • gl. Front. Face - define front- and back-facing polygons • void gl. Front. Face( GLenum mode ) • PARAMETERS mode Specifies the orientation of front-facing polygons. GL_CW and GL_CCW are accepted. The initial value is GL_CCW. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 20
gl. Polygon. Mode • gl. Polygon. Mode - select a polygon rasterization mode • void gl. Polygon. Mode( GLenum face, GLenum mode ) • PARAMETERS face Specifies the polygons that mode applies to. Must be GL_FRONT for front-facing polygons, GL_BACK for backfacing polygons, or GL_FRONT_AND_BACK for front- and back-facing polygons. mode Specifies how polygons will be rasterized. Accepted values are GL_POINT, GL_LINE, and GL_FILL. The initial value is GL_FILL for both front- and back- facing polygons. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 21
3 D Modeling Software 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 22
Wavefront obj file format • • http: //www. cica. indiana. edu/graphics/object_specs/OBJ. format. txt http: //www. okino. com/conv/exp_wave. htm http: //www-sfb 288. math. tu-berlin. de/egmodels/formats/Format_Obj. html http: //www. zenstar. com/dxfobj. html 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 23
Glm. c Nate Robins, 1997, 2000 nate@pobox. com, http: //www. pobox. com/~nate Wavefront OBJ model file format reader/writer/manipulator. Includes routines for generating smooth normals with preservation of edges, welding redundant vertices & texture coordinate generation (spheremap and planar projections) + more. 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 24
Lab 14 • Downloading GLMmodel *pmodel = NULL; if(pmodel) glm. Draw(pmodel, GLM_WIREFRAME); name = "data/mydata. obj"; pmodel = glm. Read. OBJ(name); glm. Unitize(pmodel); glm. Translate(hmodel[i], trans); glm. Scale(hmodel[i], . 1); 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 25
Exercise • Make wavefront obj model file 12/12/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 26
- Slides: 26