250

# SER332 Lecture 16

Introduction to Graphics and Game Development
Mesh Data Structure
(201804)

March 20, 2018

## Transcript

1. ### jgs SER332 Introduction to Graphics and Game Development Lecture 16:

Mesh Data Structure Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment

3. ### 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
4. ### Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4 jgs

Mesh Data Structure typedef Vec3<float> Vec3f; typedef Vec2<float> Vec2f; struct Mesh { vector<Vec3f> vertice_xyz; // array of vertices vector<Vec3f> normal_xyz; // array of normals vector<Vec2f> texture_xy; // array of texture coordinates (2D) vector<int> vertex_index; // vertex indices vector<int> normal_index; // normal indices vector<int> texture_index; // texcoord indices };

6. ### 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)
7. ### jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7

// STL vector: vector<int> 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
8. ### 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.
9. ### Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 9 jgs

Mesh Data Structure typedef Vec3<float> Vec3f; typedef Vec2<float> Vec2f; struct Mesh { vector<Vec3f> vertice_xyz; // array of vertices vector<Vec3f> normal_xyz; // array of normals vector<Vec2f> texture_xy; // array of texture coordinates (2D) vector<int> vertex_index; // vertex indices vector<int> normal_index; // normal indices vector<int> texture_index; // texcoord indices };

11. ### 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
12. ### 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<float> Vec3f; Vec3f v0; // instead of Vec3<float> v0; Vec3f v1; Vec3f v2; v0 v1 v2
13. ### 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); }

Example
15. ### Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 15 jgs

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

// calculate the area of a triangle given three vertices template<class T> T triangle_area (const Vec3<T> &v1,const Vec3<T> &v2,const Vec3<T> &v3){ Vec3<T> 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);

Example

20. ### 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.

22. ### 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.
23. ### 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

25. ### 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.
26. ### 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

in C/C++
28. ### 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
29. ### 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 = …
30. ### Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 30 jgs

Example // full example: // https://github.com/javiergs/SER332/tree/master/Lecture17 // mesh data structure typedef Vec3<float> Vec3f; typedef Vec2<float> Vec2f; struct Mesh { // vertex vector<Vec3f> dot_vertex; vector<Vec3f> dot_normal; vector<Vec2f> dot_texture; // faces vector<int> face_index_vertex; vector<int> face_index_normal; vector<int> face_index_texture; };
31. ### 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; // ... }
32. ### 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; }

Example
34. ### 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
35. ### Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 35 jgs

Homework: Review this OBJ files
36. ### 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.