summaryrefslogtreecommitdiff
path: root/gfx/models/obj.ll
blob: 84884fed8679b74030eed3962aca852bc378f88e (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
%option batch
%option c++
%option noyywrap
%option 8bit
%option stack
%option yyclass="ObjParser"
%option prefix="objbase"

%{
#include <gfx/models/obj.h>
#include <glm/glm.hpp>
#include <memory>
#include <string>
#include <vector>
class objbaseFlexLexer;
%}

comment #.*
float -?[0-9.]+
index -?[0-9]+
linestring [^\r\n]*
truthy (1|on)
falsey (0|off)

%x FACE
%x MTLLIB
%x OBJECT
%x SMOOTH
%x USEMTL
%x VERTEX
%x NORMAL
%x TEXCOORD

%%

<INITIAL>{comment} {
  // fprintf(stderr, "COMMENT %s\n", YYText());
}
<INITIAL>"f " {
  BEGIN(FACE);
  objects.back().second.emplace_back();
  objects.back().second.back().emplace_back();
  axis = 0;
}
<INITIAL>"mtllib " {
  BEGIN(MTLLIB);
}
<INITIAL>"o " {
  BEGIN(OBJECT);
}
<INITIAL>"s " {
  BEGIN(SMOOTH);
}
<INITIAL>"usemtl " {
  BEGIN(USEMTL);
}
<INITIAL>"v " {
  BEGIN(VERTEX);
  vertices.emplace_back(0, 0, 0, 1);
  axis = 0;
}
<INITIAL>"vn " {
  BEGIN(NORMAL);
  normals.emplace_back(0, 0, 0);
  axis = 0;
}
<INITIAL>"vt " {
  BEGIN(TEXCOORD);
  texCoords.emplace_back(0, 1, 1);
  axis = 0;
}
<USEMTL>{linestring} {
  // fprintf(stderr, "USEMTL <%s>\n", YYText());
}
<MTLLIB>{linestring} {
  // fprintf(stderr, "MTLLIB <%s>\n", YYText());
}
<OBJECT>{linestring} {
  objects.emplace_back(YYText(), Faces{});
}
<SMOOTH>{truthy} {
  // fprintf(stderr, "Set smooth\n");
}
<SMOOTH>{falsey} {
  // fprintf(stderr, "Set flat\n");
}
<VERTEX>{float} {
  vertices.back()[axis++] = std::stof(YYText());
}
<NORMAL>{float} {
  normals.back()[axis++] = std::stof(YYText());
}
<TEXCOORD>{float} {
  texCoords.back()[axis++] = std::stof(YYText());
}
<FACE>{index} {
  objects.back().second.back().back()[axis] = std::stoi(YYText());
}
<FACE>\/ {
  axis++;
}
<FACE>[ \t] {
  objects.back().second.back().emplace_back();
  axis = 0;
}

<*>[ \t] {
}
<*>[\r\n\f] {
  BEGIN(INITIAL);
}

%%

// Make iwyu think unistd.h is required
[[maybe_unused]]static auto x=getpid;