summaryrefslogtreecommitdiff
path: root/src/render/model.h
blob: 3683efa7ac6fff1fc14c2f95e18ba0a54caa1e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "../util/typedef.h"
#include "../util/string.h"
#include "../util/vector.h"
#include "../util/file.h"

#include <assert.h>

struct MODEL_DATA {
  VEC3 position;
  VEC2 uv;
  VEC3 normal;
};

struct MODEL {
  LIST<MODEL_DATA> vertices;
  LIST<U32> indices;
};

enum LINE_TYPES : I32 {
  comment      = ( '#' | ( ' ' << 8 ) ),
  transparency = ( 'd' | ( ' ' << 8 ) ),
  face         = ( 'f' | ( ' ' << 8 ) ),
  group        = ( 'g' | ( ' ' << 8 ) ),
  illum        = ( 'i' | ( 'l' << 8 ) ),
  ambient_clr  = ( 'K' | ( 'a' << 8 ) ),
  diffuse_clr  = ( 'K' | ( 'd' << 8 ) ),
  emissive_clr = ( 'K' | ( 'e' << 8 ) ),
  specular_clr = ( 'K' | ( 's' << 8 ) ),
  texname      = ( 'm' | ( 'a' << 8 ) ),
  mtlname      = ( 'm' | ( 't' << 8 ) ),
  newmtl       = ( 'n' | ( 'e' << 8 ) ),
  opticdensity = ( 'N' | ( 'i' << 8 ) ),
  specular_exp = ( 'N' | ( 's' << 8 ) ),
  object       = ( 'o' | ( ' ' << 8 ) ),
  shading      = ( 's' | ( ' ' << 8 ) ),
  vert         = ( 'v' | ( ' ' << 8 ) ),
  vertnorm     = ( 'v' | ( 'n' << 8 ) ),
  vertuv       = ( 'v' | ( 't' << 8 ) ),
  usemtl       = ( 'u' | ( 's' << 8 ) )
};

static inline char* skip_space( char* s ) {
  while( *s == ' ' || *s == '\t' ) ++s;
  return s;
}

static inline VEC3 read_vec3( char* s ) {
  VEC3 temp{};
  s = skip_space( s );
  temp.x = strtof( s, &s );
  s = skip_space( s );
  temp.y = strtof( s, &s );
  s = skip_space( s );
  temp.z = strtof( s, &s );
  return temp;
}

static inline LINE_TYPES get_first_two( char* s ) {
  I32 c0 = *s;
  U16 c1 = *(s + 1);
  return (LINE_TYPES)( c0 | c1 << 8 );
}

struct MTLDATA {
  STR name;
  F32 specular_exp;
  VEC3 ambient_clr;
  VEC3 diffuse_clr;
  VEC3 specular_clr;
  VEC3 emissive_clr;
  F32 opticdensity;
  F32 transparency;
  I32 illum;
  STR map_Kd;
};

struct MTLLIB {
  STR name;
  LIST<MTLDATA> data;
};

inline I32 line_type_offset( LINE_TYPES first_two ) {
  switch( first_two ) {
  case face:
  case vert:
  case group:
  case object:
  case shading:
  case transparency: return 2;
  case ambient_clr:
  case diffuse_clr:
  case emissive_clr:
  case specular_clr:
  case opticdensity:
  case specular_exp:
  case vertuv:
  case vertnorm: return 3;
  case illum: return 6;
  case mtlname:
  case newmtl:
  case usemtl:
  case texname: return 7;
  case comment: return 2;
  }
}

inline void mtl_parse_line( char* file, U64 idx, MTLLIB* mtl ) {
  LINE_TYPES first_two = get_first_two( file + idx );
  char* p = file + idx + line_type_offset( first_two );
  U64 mtl_idx = mtl->data.size - 1;

  switch( first_two ) {
  case newmtl: {
    char* end = p;
    while( *end && *end != '\n' ) ++end;
    U32 len = end - p;
    STR temp;
    temp.reserve( len + 1 );
    memcpy( temp.data, p, len );
    temp.data[len] = 0;
    mtl->data.resize( mtl->data.size + 1 );
    mtl->data[mtl->data.size - 1].name = temp.data;
  } break;
  case transparency: mtl->data[mtl_idx].transparency = strtof( p, &p ); break;
  case opticdensity: mtl->data[mtl_idx].opticdensity = strtof( p, &p ); break;
  case specular_exp: mtl->data[mtl_idx].specular_exp = strtof( p, &p ); break;
  case illum: mtl->data[mtl_idx].illum = (I32)strtol( p, &p, 10 ); break;
  case ambient_clr: mtl->data[mtl_idx].ambient_clr = read_vec3( p ); break;
  case diffuse_clr: mtl->data[mtl_idx].diffuse_clr = read_vec3( p ); break;
  case emissive_clr: mtl->data[mtl_idx].emissive_clr = read_vec3( p ); break;
  case specular_clr: mtl->data[mtl_idx].specular_clr = read_vec3( p ); break;
  case texname: {
    char* end = p;
    while( *end && *end != '\n' ) ++end;
    U32 len = end - p;
    STR temp;
    temp.reserve( len + 1 );
    memcpy( temp.data, p, len );
    temp.data[len] = 0;
    mtl->data[mtl_idx].map_Kd = temp.data;
  } break;
  case comment: break;
  default: {
    dlog(
      "mtl_from_file() : unhandled char combo %c%c\n",
      first_two & 0xFF, ( first_two >> 8 ) & 0xFF
    );
  } break;
  }
}

inline MTLLIB mtl_from_file( const char* path, const char* name ) {
  U64 mtl_size;
  STR full_path( path );
  full_path += name;
  char* f_mtl = (char*)file_read( full_path.data, &mtl_size );
  if( !f_mtl ) {
    dlog( "mtl_from_file() : failed to read file %s\n", full_path.data );
    return {};
  }

  MTLLIB mtl;
  mtl.name = name;
  for( U64 i = 0; i < mtl_size - 1 && f_mtl[i]; ++i ) {
    if( f_mtl[i] == '\n' )
      continue;
     mtl_parse_line( f_mtl, i, &mtl );
     while( f_mtl[i] && f_mtl[i] != '\n' ) ++i;
  }

   free( (void*)f_mtl );
  return mtl;
}

enum FACE_TYPES : U8 {
  V     = 0,
  VVT   = 1,
  VVN   = 2,
  VVTVN = 3
};

constexpr U32 STR_LEN = 0x40;

struct OBJFACE {
  FACE_TYPES type;
  LIST<U32> indices;
};

struct OBJDATA {
  LIST<VEC2> uvs;
  LIST<VEC3> normals;
  LIST<VEC3> vertices;
  LIST<MTLLIB> mtllib;
  struct UseMtl {
    LIST<U32> face_start;
    LIST<STR> mtl_name;
  } usemtl;
  LIST<OBJFACE> faces;
};

static inline U32 face_stride( FACE_TYPES t ) {
  switch( t ) {
  case V:     return 1;
  case VVT:   return 2;
  case VVN:   return 2;
  case VVTVN: return 3;
  }
  return 0;
}

inline OBJFACE triangulate_face( OBJFACE ref, U32 idx, U32 stride, FACE_TYPES type ) {
  OBJFACE tri{};
  tri.type = type;
  for( U32 ind = 0; ind < stride; ++ind )
    tri.indices.push( ref.indices.data[ind] );
  for( U32 ind = 0; ind < stride; ++ind )
    tri.indices.push( ref.indices.data[idx * stride + ind] );
  for( U32 ind = 0; ind < stride; ++ind )
    tri.indices.push( ref.indices.data[( idx + 1 ) * stride + ind] );
  return tri;
}

inline void obj_parse_f_line( OBJDATA* obj, char* p ) {
  FACE_TYPES type = (FACE_TYPES)0;
  bool type_set = 0;
  OBJFACE temp{};
  while( *p && *p != '\n' ) {
    bool has_vt = 0;
    bool has_vn = 0;
    U32 v = strtoul( p, &p, 10 );
    temp.indices.push( --v );
    if( *p == '/' ) {
      ++p;
      if( *p != '/' ) {
        U32 vt = strtoul( p, &p, 10 );
        temp.indices.push( --vt );
        has_vt = 1;
      }
      if( *p == '/' ) {
        ++p;
        U32 vn = strtoul( p, &p, 10 );
        temp.indices.push( --vn );
        has_vn = 1;
      }
    }
    if( !type_set ) {
      type_set = 1;
      if( has_vt && has_vn ) type = VVTVN;
      else if( has_vt )      type = VVT;
      else if( has_vn )      type = VVN;
      else                   type = V;
    }
    while( *p && *p != ' ' && *p != '\n' ) ++p;
    p = skip_space( p );
  }
  temp.type = type;
  U32 stride = face_stride( type );
  U32 count = temp.indices.size / stride;
  if ( count <= 3 )
    obj->faces.push( temp );
  else {
    for( U32 idx = 1; idx < count - 1; ++idx )
      obj->faces.push( triangulate_face( temp, idx, stride, type ) );
  }
}

inline void obj_parse_line( const char* path, char* file, U64 idx, OBJDATA* obj ) {
  LINE_TYPES first_two = get_first_two( file + idx );
  char* p = file + idx + line_type_offset( first_two );

  switch( first_two ) {
  case face: obj_parse_f_line( obj, p ); break;
  case vert:
  case vertnorm: {
    if( first_two == vert ) obj->vertices.push( read_vec3( p ) );
    else obj->normals.push( read_vec3( p ) );
  } break;
  case vertuv: {
    VEC2 temp{};
    p = skip_space( p );
    temp.x = strtof( p, &p );
    p = skip_space( p );
    temp.y = strtof( p, &p );
    obj->uvs.push( temp );
  } break;
  case mtlname:
  case usemtl: {
    bool is_mtllib = first_two == mtlname;
    char* end = p;
    while( *end && *end != '\n' ) ++end;
    U32 len = end - p;
    STR temp;
    temp.reserve( len + 1 );
    memcpy( temp.data, p, len );
    temp.data[len] = 0;
    if( !is_mtllib ) {
      if( obj->usemtl.mtl_name.size )
        obj->usemtl.face_start.push( obj->faces.size );
      obj->usemtl.mtl_name.push( temp.data );
    } else
      obj->mtllib.push( mtl_from_file( path, temp.data ) );
  } break;
  case comment:
  case group:
  case object:
  case shading: break;
  default: {
    dlog(
      "obj_from_file() : unhandled char combo %c%c\n",
      first_two & 0xFF, ( first_two >> 8 ) & 0xFF
    );
  } break;
  }
}

inline OBJDATA obj_from_file( const char* path, const char* name ) {
  char* f_obj = nullptr;
  STR obj_path( path );
  U64 obj_size = 0;
  obj_path += name;
  obj_path += ".obj";
  f_obj = (char*)file_read( obj_path.data, &obj_size );
  if( !f_obj ) {
    dlog( "obj_from_file() : failed to read obj file %s\n", obj_path.data );
    return {};
  }
  OBJDATA obj;
  for( U64 i = 0; i < obj_size - 1 && f_obj[i]; ++i ) {
    if( f_obj[i] == '\n' )
      continue;
    obj_parse_line( path, f_obj, i, &obj );
    while( f_obj[i] && f_obj[i] != '\n' ) ++i;
  }
  obj.usemtl.face_start.push( obj.faces.size );
  if ( !obj.usemtl.mtl_name.size )
    obj.usemtl.mtl_name.push( "default" );
  free( (void*)f_obj );
  return obj;
}

inline MODEL obj_to_model( OBJDATA obj ) {
  struct KEY { U32 v, vt, vn; };
  struct ENTRY { KEY key; U32 idx; };
  MODEL model;
  LIST<ENTRY> table;
  table.reserve( obj.faces.size * 3 );
  for( U32 f = 0; f < obj.faces.size; ++f ) {
    OBJFACE& face = obj.faces.data[f];
    U32 stride = face_stride( face.type );
    U32 count = face.indices.size / stride;
    for( U32 v = 0; v < count; ++v ) {
      KEY key = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
      U32 base = v * stride;
      key.v = face.indices.data[base + 0];
      if( face.type == VVT || face.type == VVTVN )
        key.vt = face.indices.data[base + 1];
      if( face.type == VVN )
        key.vn = face.indices.data[base + 1];
      if( face.type == VVTVN )
        key.vn = face.indices.data[base + 2];
      U32 idx = 0;
      bool found = 0;
      // this should be fine until ~1m triangles, right?
      for( U32 i = 0; i < table.size; ++i ) {
        ENTRY& e = table.data[i];
        if( e.key.vn != key.vn
         || e.key.vt != key.vt
         || e.key.v != key.v
        ) continue;
        idx = e.idx;
        found = 1;
        break;
      }
      if( !found ) {
        MODEL_DATA data{};
        data.position = obj.vertices.data[key.v];
        if( face.type == VVTVN || face.type == VVT )
          data.uv = obj.uvs.data[key.vt];
        if( face.type == VVTVN || face.type == VVN )
          data.normal = obj.normals.data[key.vn];
        idx = model.vertices.size;
        model.vertices.push( data );
        table.push({ key, idx });
      }
      model.indices.push( idx );
    }
  }
  return model;
}

inline MODEL model_from_obj( const I8* path, const I8* name ) {
  OBJDATA obj = obj_from_file( path, name );
  return obj_to_model( obj );
}