<?php
// << the DtTvB >>
// Here is the source file we use.
$sourcefile = 'link.obj';
// Get the file data and open a file for writing.
$fd = file($sourcefile);
// Prepare the array.
$vdata = array();
$fdata = array();
$gdata = array();
$cgroup = 'Ungrouped';
// We need this variable to make sure it counts vertice id correctly.
$j = 0;
// Loop through each line of file.
foreach ($fd as $i => $fl) {
$fl_d = explode(' ', $fl_l = trim($fl));
// Vertice?
if ($fl_d[0] == 'v') {
$vdata[++$j] = array('x' => $fl_d[1], 'y' => $fl_d[2], 'z' => $fl_d[3]);
// Group
} elseif ($fl_d[0] == 'g') {
$cgroup = $fl_d[1];
// Face?
} elseif ($fl_d[0] == 'f') {
// Split it out, we need only vertice id.
preg_match ('~^f (.+?)/(.+?)/(.+?) (.+?)/(.+?)/(.+?) (.+?)/(.+?)/(.+?)$~', $fl_l, $mats);
// Search for group and add vertice
if (!isset($gdata[$cgroup]))
$gdata[$cgroup] = array();
if (!isset($k[$cgroup]))
$k[$cgroup] = 0;
if (!isset($fdata[$cgroup]))
$fdata[$cgroup] = array();
$gdata[$cgroup][$k1 = $k[$cgroup]++] = $mats[1];
$gdata[$cgroup][$k2 = $k[$cgroup]++] = $mats[4];
$gdata[$cgroup][$k3 = $k[$cgroup]++] = $mats[7];
$fdata[$cgroup][] = array('f1' => $k1, 'f2' => $k2, 'f3' => $k3);
}
}
// Define sizes and other parameters.
$size = 3000;
$scale = 160;
$xoffset = $size / 2;
$yoffset = $size * 0.9;
// Create an image stream!
$im = imagecreatetruecolor($size, $size);
imagefill ($im, 0, 0, imagecolorexact($im, 240, 240, 240));
$linecolor = imagecolorexact($im, 0, 113, 188);
$textcolor = imagecolorexact($im, 0, 0, 0);
imageantialias ($im, false);
$l = 0;
$m = 0;
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$end = isset($_GET['end']) ? $_GET['end'] : count($gdata);
foreach ($gdata as $i => $cgroup) {
if ($m++ < $start)
continue;
if ($m > $end)
break;
$ccolor = imagecolorexact($im, rand(50, 200), rand(50, 200), rand(50, 200));
imagestring ($im, 3, 2, 2 + ($l++ * 15), $i, $ccolor);
foreach ($fdata[$i] as $cf) {
imageline ($im, $xoffset + ($vdata[$cgroup[$cf['f1']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f1']]]['z'] * $scale), $xoffset + ($vdata[$cgroup[$cf['f2']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f2']]]['z'] * $scale), $ccolor);
imageline ($im, $xoffset + ($vdata[$cgroup[$cf['f2']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f2']]]['z'] * $scale), $xoffset + ($vdata[$cgroup[$cf['f3']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f3']]]['z'] * $scale), $ccolor);
imageline ($im, $xoffset + ($vdata[$cgroup[$cf['f3']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f3']]]['z'] * $scale), $xoffset + ($vdata[$cgroup[$cf['f1']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f1']]]['z'] * $scale), $ccolor);
imagestring ($im, 2, $xoffset + ($vdata[$cgroup[$cf['f1']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f1']]]['z'] * $scale), $cgroup[$cf['f1']], $textcolor);
imagestring ($im, 2, $xoffset + ($vdata[$cgroup[$cf['f2']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f2']]]['z'] * $scale), $cgroup[$cf['f2']], $textcolor);
imagestring ($im, 2, $xoffset + ($vdata[$cgroup[$cf['f3']]]['x'] * $scale), $yoffset - ($vdata[$cgroup[$cf['f3']]]['z'] * $scale), $cgroup[$cf['f3']], $textcolor);
}
}
//foreach ($vdata as $i => $cv) {
//imagestring ($im, 2, $xoffset + ($cv['x'] * $scale), $yoffset - (($cv['z'] * $scale)), $i, $textcolor);
//}
header ('Content-Type: image/png');
imagepng ($im);
// << the DtTvB >>
?>