Skip to content

Instantly share code, notes, and snippets.

@orymate
Created May 4, 2011 13:22
Show Gist options
  • Save orymate/955197 to your computer and use it in GitHub Desktop.
Save orymate/955197 to your computer and use it in GitHub Desktop.
Wavefront obj file of polygon model to C++ converter
# http://en.wikipedia.org/wiki/Wavefront_.obj_file
$1 == "o" {
o++;
base[o] = vnum;
vnum_ = 0;
name[o] = $2;
}
$1 == "v" {
v[vnum++] = "Vec(" $2 "," $3 "," $4 ")";
vnum_ ++;
vcount[o] = vnum_;
}
$1 == "vn" {
vn[vnnum++] = "Vec(" $2 "," $3 "," $4 ")";
}
$1 == "f" {
for (i=2; i<=5; i++) {
split($i,a,"//");
face[o] = face[o] a[1] ",";
}
fcount[o]++;
}
END {
base[o+1] = vnum;
printf "const Vec v[] = {\n";
for (i = 0; i < vnum; i++) {
printf "\t%s,\n", v[i]
}
printf "};\nconst Vec vn[] = {\n";
for (i = 0; i < vnnum; i++) {
printf "\t%s,\n", vn[i]
}
printf "};\n"
for (i=1; i <= o; i++) {
printf "{\n\tconst int f[] = {";
printf "%s", face[i];
printf "};\n"
printf "\t%s = new Polygon(%d, %d, v+%d, vn+%d, f, %d);\n}\n", name[i],
vcount[i], fcount[i], base[i], base[i], base[i]+1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment