Upgrade to Pro — share decks privately, control downloads, hide ads and more …

10. Model loading

10. Model loading

94870c02836167043f37f05ae1032690?s=128

Tatsuya Yatagawa

May 13, 2021
Tweet

More Decks by Tatsuya Yatagawa

Other Decks in Technology

Transcript

  1. 10. Model loading Tatsuya Yatagawa

  2. 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!
  3. 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
  4. 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”
  5. 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)”
  6. 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.
  7. 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.
  8. 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”
  9. 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
  10. tinyobjloader ◼ Open file and handle errors Computer Graphics Course

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

    @Waseda University attrib stores arrays of “position”, “texture coordinates”, and “normal vector” as its member.
  12. 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.
  13. tinyobjloader ◼ Open file and handle errors Computer Graphics Course

    @Waseda University materials stores material data (not used in this lecture)
  14. 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)
  15. tinyobjloader ◼ Open file and handle errors Computer Graphics Course

    @Waseda University LoadObj method loads mesh data to input parameters.
  16. 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
  17. tinyobjloader Computer Graphics Course @Waseda University ◼ Group attributes into

    class Vertex
  18. 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.
  19. 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).
  20. 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]
  21. Result ◼ Bunny object is successfully loaded and shown! Computer

    Graphics Course @Waseda University
  22. 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.