Slide 1

Slide 1 text

10. Model loading Tatsuya Yatagawa

Slide 2

Slide 2 text

Shape definitions so far ◼ Shape data is hard-coded in source code. → It’s not realistic to larger and more complicated shape. Computer Graphics Course @Waseda University It’s much better to load shape data from mesh files!

Slide 3

Slide 3 text

Type of mesh files ◼ OBJ (Wavefront OBJ format) ◼ Position, normal, texture coordinates can be described. ◼ Material data is separately stored in MTL file. ◼ It does not support binary format (data size can be larger...). ◼ PLY (Stanford PLY format) ◼ Vertex data can be flexibly defined in header part! ◼ It supports binary format!! ◼ glTF (OpenGL transmission format) ◼ Comprehensive scene graph is described by JSON format. ◼ It’s simple and powerful. It has a potential to be a mainstream! Computer Graphics Course @Waseda University

Slide 4

Slide 4 text

OBJ file format ◼ Simplest OBJ file ◼ Only a single triangle Computer Graphics Course @Waseda University # vertex data follows v 1.0 1.0 0.0 v 1.0 0.0 0.0 v 0.0 1.0 0.0 # face data follows f 1 2 3 Vertices are indexed as 1, 2, 3 from the beginning. (be careful! it’s 1-base!!) # is followed by comments Indices of vertices in a polygon follow “f”

Slide 5

Slide 5 text

OBJ file format ◼ With texture coordinates and normal vectors Computer Graphics Course @Waseda University # vertex data follows v 1.0 1.0 0.0 v 1.0 0.0 0.0 v 0.0 1.0 0.0 vt 1.0 1.0 vt 1.0 0.0 vt 0.0 1.0 vn 0.0 0.0 1.0 vn 0.0 0.0 1.0 vn 0.0 0.0 1.0 # face data follows f 1/1/1 2/2/2 3/3/3 Position, texture coordinates, normal vectors are identified by specifiers: v, vt, and vn, respectively. Polygon is defined by tuples of indices, “(pos. idx)/(texture idx)/(normal idx)”

Slide 6

Slide 6 text

OBJ file format ◼ With texture coordinates and normal vectors Computer Graphics Course @Waseda University # vertex data follows v 1.0 1.0 0.0 v 1.0 0.0 0.0 v 0.0 1.0 0.0 vt 1.0 1.0 vt 1.0 0.0 vt 0.0 1.0 vn 0.0 0.0 1.0 # face data follows f 1/1/1 2/2/1 3/3/1 Indices in tuple can be different. In this case, common normal is specified to three vertices.

Slide 7

Slide 7 text

OBJ file format and material ◼ MTL file format Computer Graphics Course @Waseda University # material data follows newmtl mat0 Kd 1.0 1.0 1.0 map_Kd diffuse.png “newmtl” is followed by material name Detail of the material follows after the name.

Slide 8

Slide 8 text

OBJ file format and material ◼ Specify material file in OBJ file Computer Graphics Course @Waseda University # Specify material file mtllib default.mtl # vertex data follows v 1.0 1.0 0.0 v 1.0 0.0 0.0 v 0.0 1.0 0.0 # polygon data follows usemtl mat0 f 1 1 1 Specify file name after “mtllib” Specify material name after “usemtl”

Slide 9

Slide 9 text

How to load OBJ file? ◼ Implement by yourself ◼ Beginners can implement a simple one. ◼ Support for textures and normals is rather complicated. ◼ Support for MTL file will need more time! ◼ tinyobjloader ◼ URL: https://github.com/syoyo/tinyobjloader ◼ It consists only of a single header! ◼ Developed by Syoyo Fujita (a.k.a. @syoyo) ◼ For other format? ◼ tinyply: https://github.com/ddiakopoulos/tinyply ◼ tinygltf: https://github.com/syoyo/tinygltf Computer Graphics Course @Waseda University

Slide 10

Slide 10 text

tinyobjloader ◼ Open file and handle errors Computer Graphics Course @Waseda University

Slide 11

Slide 11 text

tinyobjloader ◼ Open file and handle errors Computer Graphics Course @Waseda University attrib stores arrays of “position”, “texture coordinates”, and “normal vector” as its member.

Slide 12

Slide 12 text

tinyobjloader ◼ Open file and handle errors Computer Graphics Course @Waseda University shapes is array of shapes. Each shape corresponds to a mesh group(*1) *1) OBJ file has a feature to separate a large shape into groups of polygons.

Slide 13

Slide 13 text

tinyobjloader ◼ Open file and handle errors Computer Graphics Course @Waseda University materials stores material data (not used in this lecture)

Slide 14

Slide 14 text

tinyobjloader ◼ Open errors and handle errors Computer Graphics Course @Waseda University err stores error message (more similar to warning) (*1) *1) error and warning is separated in the latest one (not in official release)

Slide 15

Slide 15 text

tinyobjloader ◼ Open file and handle errors Computer Graphics Course @Waseda University LoadObj method loads mesh data to input parameters.

Slide 16

Slide 16 text

tinyobjloader ◼ Traverse vertex and polygon arrays ◼ struct shape_t includes struct mesh_t as a parameter ◼ mesh_t::indicdes stores indices of triangle vertices Computer Graphics Course @Waseda University // Process for each vertex in a triangle

Slide 17

Slide 17 text

tinyobjloader Computer Graphics Course @Waseda University ◼ Group attributes into class Vertex

Slide 18

Slide 18 text

tinyobjloader Computer Graphics Course @Waseda University ◼ Group attributes into class Vertex ◼ Vertices, texcoords, normals in attrib are described with float array. ◼ Their indices are stored in index_t::xxxx_index. ◼ -1 is stored if vertex does not have corresponding attributes.

Slide 19

Slide 19 text

Prepare VAO from attribute array ◼ Not changed a lot, but keep the length of index buffer. Computer Graphics Course @Waseda University Be careful! It’s the number of indices (not byte size).

Slide 20

Slide 20 text

Specify normal vector as output color ◼ OBJ does not have color data ◼ It’s possible to specify colors using MTL file. ◼ This time, use normal vectors to colorize vertices. Computer Graphics Course @Waseda University Fragment shader used in this example. XYZ of normal vector is in [-1, 1] → modify them to be in [0, 1]

Slide 21

Slide 21 text

Result ◼ Bunny object is successfully loaded and shown! Computer Graphics Course @Waseda University

Slide 22

Slide 22 text

Practice 10-1 Computer Graphics Course @Waseda University Compute surface normal in fragment shader. It’s approximate one but is quite useful when the model does not have normal data. ◼ Hint: Use GLSL’s build-in functions dFdx and dFdy to compute screen-based differentiation of varying variables (cannot be used for other variables). ◼ Hint: A normal is simply given by the cross product of two differentiations of vertex positions given by dFdx and dFdy.