Slide 1

Slide 1 text

jgs SER332 Introduction to Graphics and Game Development Lecture 16: Mesh Data Structure Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Mesh Data Structure Programming

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 3 jgs Goal § Represent (a geometric object) as a set of finite elements (triangles) for computational modeling. § Mesh Data Structure

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4 jgs Mesh Data Structure typedef Vec3 Vec3f; typedef Vec2 Vec2f; struct Mesh { vector vertice_xyz; // array of vertices vector normal_xyz; // array of normals vector texture_xy; // array of texture coordinates (2D) vector vertex_index; // vertex indices vector normal_index; // normal indices vector texture_index; // texcoord indices };

Slide 5

Slide 5 text

jgs C++ Standard Template Library

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6 jgs STL § The Standard Template Library, or STL, is a C++ library of container classes, algorithms, and iterators. § It is a generic library, meaning that its components are heavily parameterized: almost every component in the STL is a template. References: § http://www.cplusplus.com/reference/stl/ § The C++ Standard Library: A Tutorial and Reference. Nicolai M. Josuttis. Addison-Wesley (2012) § Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library. Scott Meyers. Addison-Wesley (2005)

Slide 7

Slide 7 text

jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7 // STL vector: vector a; for(i = 0;i<10;i ++) { a.push_back(i); } // * clean itself when out of scope // * automatically resize itself. // Array int * a = new int[10]; for(i = 0;i<10;i ++){ a[i] = i; } delete[] a; Array vs STL Vector

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 8 jgs Vector Member Functions § vector::front Returns reference to first element of vector. § vector::back Returns reference to last element of vector. § vector::size Returns number of elements in the vector. § vector::empty Returns true if vector has no elements. § vector::capacity Returns current capacity (allocated memory) of vector § vector::insert Inserts an element in an specific position § vector::push_back Inserts an element to the end. § vector::erase Deletes elements in an specific position § vector::pop_back Erases the last element of the vector. § vector::clear Erases all of the elements.

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 9 jgs Mesh Data Structure typedef Vec3 Vec3f; typedef Vec2 Vec2f; struct Mesh { vector vertice_xyz; // array of vertices vector normal_xyz; // array of normals vector texture_xy; // array of texture coordinates (2D) vector vertex_index; // vertex indices vector normal_index; // normal indices vector texture_index; // texcoord indices };

Slide 10

Slide 10 text

jgs Vector Library Ilmbase

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 11 jgs Vector Library Ilmbase is a vector and matrix library for graphics programming. It allows: § Add vector § Subtract Vector § Multiply With Scalar § Length § Normalize § Cross product § Dot Product

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 12 jgs Example // https://github.com/javiergs/SER332/tree/master/Lecture16 #include "Imathvec.h" using namespace Imath; // this makes the following declaration easier to write typedef Vec3 Vec3f; Vec3f v0; // instead of Vec3 v0; Vec3f v1; Vec3f v2; v0 v1 v2

Slide 13

Slide 13 text

jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 13 // main void main() { v0.setValue(-1, 0, 0); //Ok v1.setValue(1.0, 0.0, 0.0); //Ok // v1.setValue(1.0, 0, 0); <-- ERROR: type mismatch v2.setValue(0, 1, 0); //Ok printf("V0 = (%f, %f, %f)\n", v0.x, v0.y, v0.z ); printf("v1 = (%f, %f, %f)\n", v1.x, v1.y, v1.z); printf("v2 = (%f, %f, %f)\n", v2.x, v2.y, v2.z); // dot product float dotproduct = v1.dot(v2); printf("v1 * v2 = %f\n", dotproduct); //cross product Vec3f v3 = v1.cross(v2); printf("v0 x v1 = (%.2f,%.2f,%.2f)\n", v3.x,v3.y,v3.z); // test equality v1.setValue(100.0,100.0,100.0); v2 = v1; v2+= Vec3f(1,0,0); }

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 14 jgs Example

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 15 jgs Test Yourselves How to calculate the area of a triangle given 3 vertex?

Slide 16

Slide 16 text

jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 16 // calculate the area of a triangle given three vertices template T triangle_area (const Vec3 &v1,const Vec3 &v2,const Vec3 &v3){ Vec3 AB = v2-v1, AC = v3-v1; return (AB.cross(AC)).length()/2; } // include these lines in main to call the method // float area = triangle_area(v0,v1,v2); // printf("the area of the triangle is %.4f\n", area);

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 17 jgs Example

Slide 18

Slide 18 text

jgs Library Set up Windows + Visual Studio

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 19 jgs Vector Library: IlmBase § Download from: http://www.openexr.com/downloads.html § Include many useful feature such as multi-threading. § Generic programming style (uses templates).

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 20 jgs Windows Setup In Visual Studio, in project property dialog, add the following § Include Directories .\ilmbase\inc\half, .\ilmbase\inc\ilmthread, .\ilmbase\inc\iex, .\ilmbase\inc\imath § Lib Directory § Include imathvec.h in your *.cpp files.

Slide 21

Slide 21 text

jgs Library Set up mac OSX + Xcode

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 22 jgs mac OSX Setup § Press Command+Space and type Terminal and press enter/return key. § Run in Terminal app: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null and press enter/return key. § Wait for the command to finish. § Run: brew install ilmbase Done! You can now use ilmbase.

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 23 jgs Using Xcode § In your project, go to "Build Settings" and search for "Header Search Paths." Add the folder for the *.h files

Slide 24

Slide 24 text

jgs Library Set up mac OSX + Eclipse Oxygen 2

Slide 25

Slide 25 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 25 jgs mac OSX Setup § Press Command+Space and type Terminal and press enter/return key. § Run in Terminal app: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null and press enter/return key. § Wait for the command to finish. § Run: brew install ilmbase Done! You can now use ilmbase.

Slide 26

Slide 26 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 26 jgs Using Eclipse § Create a C/C++ Project § In properties configure Path and Symbols à Include in GNU C++ /usr/local/Cellar/ilmbase/2.2.0/include/OpenEXR § And, Path and Symbols à Libraries Path /usr/local/Cellar/ilmbase/2.2.0/lib

Slide 27

Slide 27 text

jgs Load Data in a Mesh Data Structure Reading Files in C/C++

Slide 28

Slide 28 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 28 jgs OBJ File Format v 0.0 2.0 0.0 v 0.0 -2.0 0.0 v -1.0 0.0 -1.0 v 1.0 0.0 -1.0 v 1.0 0.0 1.0 v -1.0 0.0 1.0 f 1 4 3 f 1 3 6 f 1 6 5 f 1 5 4 f 2 3 4 f 2 6 3 f 2 5 6 f 2 4 5

Slide 29

Slide 29 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 29 jgs Mesh Data Structure Normal Coordinates n 0 x = … y = … z = … n 1 x = … y = … z = … n 2 x = … y = … z = … n 3 x = … y = … z = … n 4 x = … y = … z = … n 5 x = … y = … z = … Normal Index Indices ni 0 (f 0 ) ni 1 (f 1 ) ni 2 (f 2 ) ni 3 (f 3 ) ni 4 (f 4 ) ni 5 (f 5 ) Face Indices f 0 0, 4, 3 f 1 0, 3, 1 f 2 1, 3, 2 f 3 0, 5, 6 f 4 0, 6, 4 f 5 4, 6, 7 Vertex Coordinates V 0 x = … y = … z = … V 1 x = … y = … z = … V 2 x = … y = … z = … V 3 x = … y = … z = … V 4 x = … y = … z = … V 5 x = … y = … z = … V 6 x = … y = … z = … V 7 x = … y = … z = … Texture Indices ti 0 ti 1 ti 2 ti 3 ti 4 ti 5 Texture Coordinates t 0 s = … t = … t 1 s = … t = … t 2 s = … t = … t 3 s = … t = … t 4 s = … t = … t 5 s = … t = …

Slide 30

Slide 30 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 30 jgs Example // full example: // https://github.com/javiergs/SER332/tree/master/Lecture17 // mesh data structure typedef Vec3 Vec3f; typedef Vec2 Vec2f; struct Mesh { // vertex vector dot_vertex; vector dot_normal; vector dot_texture; // faces vector face_index_vertex; vector face_index_normal; vector face_index_texture; };

Slide 31

Slide 31 text

jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 31 // load a mesh from an OBJ file Mesh* loadFile(const char* file) { // ... switch (current_line[0]) { case'v': float x, y, z; switch (current_line[1]) { case 'n': sscanf_s(current_line, "vn %f %f %f", &x, &y, &z); break; case 't': sscanf_s(current_line, "vt %f %f", &x, &y); m->dot_texture.push_back(Vec2f(x, y)); break; default: sscanf_s(current_line, "v %f %f %f", &x, &y, &z); m->dot_vertex.push_back(Vec3f(x, y, z)); break; } break; // ... }

Slide 32

Slide 32 text

jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 32 // return a displayList id with the mesh elements GLuint meshToDisplayList(Mesh* m, int id) { GLuint listID = glGenLists(id); glNewList(listID, GL_COMPILE); glBegin(GL_TRIANGLES); for (unsigned int i = 0; i < m->face_index_vertex.size(); i++) { if (!m->dot_normal.empty() && !m->face_index_normal.empty()) glNormal3fv(&m->dot_normal[m->face_index_normal[i]].x); if (!m->dot_texture.empty() && !m->face_index_texture.empty()) glTexCoord2fv(&m->dot_texture[m->face_index_texture[i]].x); // color Vec3f offset = (m->dot_vertex[m->face_index_vertex[i]]); glColor3f(fabs(sin(offset.x)), fabs(cos(offset.y)), fabs(offset.z)); glVertex3fv(&m->dot_vertex[m->face_index_vertex[i]].x); } glEnd(); glEndList(); return listID; }

Slide 33

Slide 33 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 33 jgs Example

Slide 34

Slide 34 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 34 jgs Homework § Set Up the IlmBase Vector Library § Practice using C++ Standard Template Library § Test your set up using the following program https://github.com/javiergs/SER332/blob/master/Lecture16/test.cpp

Slide 35

Slide 35 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 35 jgs Homework: Review this OBJ files

Slide 36

Slide 36 text

jgs SER332 Introduction to Graphics Javier Gonzalez-Sanchez [email protected] Spring 2018 Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.