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!
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
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”
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)”
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.
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.
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”
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
@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.
@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)
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
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.
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]
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.