Lecture #02. 3D model consuming

Computer graphics in Game development

Ivan Belyavtsev

09.09.2022

Rendering pipeline

Artists prepare 3D model for us

Artists prepare 3D models for rendering using:

  • Autodesk 3ds Max
  • Autodesk Maya
  • Blender

Blender example

From: https://www.3dtoday.ru/blogs/accurate-random/blender-3d-has-some-interesting-bygen-addon-generative-design

Export formats

  • glTF
  • FBX
  • Wavefront OBJ

Where get OBJ files of famous reference scenes

McGuire Computer Graphics Archive

http://casual-effects.com/data/

Famous references

Stanford bunny

From: https://casual-effects.com/data

[1]

Famous references

Utah teapot

From: https://casual-effects.com/data

[2]

Famous references

Sponza palace

From: https://casual-effects.com/data

[2]

Famous references

Cornel box

From: https://casual-effects.com/data

[3]

Our OBJ models

For our labs we have OBJ files in models folder:

  • Cube
  • Z-test
  • CornelBox
  • Sponza

Wavefront OBJ format

  • # - comment
  • v - geometric vertex
  • vn - vertex normals
  • vt - texture coordinates
  • f - face (polygon) - list of vertices, normals, and tex coordinates
  • g - group name
  • usemtl - material name (from .mlt file) [4]

Cornell box example

## Object floor
v  -1.01  0.00   0.99 
v   1.00  0.00   0.99
v   1.00  0.00  -1.04
v  -0.99  0.00  -1.04

g floor
usemtl floor
f -4 -3 -2 -1

[2]

Material template library

  • Ka - ambient color
  • Kd - diffuse color
  • Ks - specular color
  • Ns - specular exponent

Cornell box example

newmtl floor
  Ns 10.0000
  Ni 1.0000
  illum 2
  Ka 0.725 0.71 0.68 # White
  Kd 0.725 0.71 0.68
  Ks 0 0 0
  Ke 0 0 0

[2]

Obj parsing

Let’s skip reinventing the wheel

There are a lot of libraries to parse OBJ files

We will use tinyobjloader [5]

Tinyobjloader

How to init reader

// define this in only *one* .cc
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"

std::string inputfile = "cornell_box.obj";
tinyobj::ObjReaderConfig reader_config;
// Path to material files
reader_config.mtl_search_path = "./"; 

tinyobj::ObjReader reader;

[5]

Tinyobjloader

How to parse OBJ file

if (!reader.ParseFromFile(inputfile, reader_config)) {
  if (!reader.Error().empty()) {
      std::cerr << "TinyObjReader: " << reader.Error();
  }
  exit(1);
}
if (!reader.Warning().empty()) {
  std::cout << "TinyObjReader: " << reader.Warning();
}

auto& attrib = reader.GetAttrib();
auto& shapes = reader.GetShapes();
auto& materials = reader.GetMaterials();

[5]

Tinyobjloader

How to access vertex data

// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
  // Loop over faces(polygon)
  size_t index_offset = 0;
  for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
    int fv = shapes[s].mesh.num_face_vertices[f];
    // Loop over vertices in the face.
    for (size_t v = 0; v < fv; v++) {
      // access to vertex
      tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
      tinyobj::real_t vx = attrib.vertices[3*idx.vertex_index+0];
      tinyobj::real_t vy = attrib.vertices[3*idx.vertex_index+1];
      tinyobj::real_t vz = attrib.vertices[3*idx.vertex_index+2];
      tinyobj::real_t nx = attrib.normals[3*idx.normal_index+0];
      tinyobj::real_t ny = attrib.normals[3*idx.normal_index+1];
      tinyobj::real_t nz = attrib.normals[3*idx.normal_index+2];
      tinyobj::real_t tx = attrib.texcoords[2*idx.texcoord_index+0];
      tinyobj::real_t ty = attrib.texcoords[2*idx.texcoord_index+1];
      // Optional: vertex colors
      // tinyobj::real_t red = attrib.colors[3*idx.vertex_index+0];
      // tinyobj::real_t green = attrib.colors[3*idx.vertex_index+1];
      // tinyobj::real_t blue = attrib.colors[3*idx.vertex_index+2];
    }
    index_offset += fv;
    // per-face material
    shapes[s].mesh.material_ids[f];
  }
}

[5]

How to pass the vertex data to a renderer

  1. Pack each vertex to a vertex struct
  2. Place all vertex struct to a resource - vertex buffer
  3. Pass the vertex buffer to the render
Vertex buffer

Indexing

The index buffer is a resource that stores the sequent indices of vertices in drawing order [6]

Vertex buffer vs Vertex+Index buffer

Lab: 1.03 Vertex and index buffer

  1. Implement cg::vertex struct
  2. Using tinyobjloader implement load_obj, allocate_buffers, compute_normal, fill_vertex_data, fill_buffers, get_vertex_buffers, get_index_buffers methods of cg::world::model class. Note: each shape should be placed into separated vertex and index buffer
  3. Adjust cg::renderer::rasterization_renderer class to consume cg::world::model

References

1.
Turk G. The stanford bunny [Electronic resource]. 2000. URL: https://www.cc.gatech.edu/~turk/bunny/bunny.html.
2.
McGuire M. Computer graphics archive. 2017.
3.
Computer Graphics C.U.P. of. Cornell box data [Electronic resource]. 2005. URL: http://www.graphics.cornell.edu/online/box/data.html.
4.
5.
Fujita S. Tinyobjloader [Electronic resource]. 2020. URL: https://github.com/tinyobjloader/tinyobjloader.
6.
Overvoorde A. Index buffer - vulkan tutorial [Electronic resource]. 2019. URL: https://vulkan-tutorial.com/Vertex_buffers/Index_buffer.